diff options
-rwxr-xr-x | bin/rbotdb | 2 | ||||
-rw-r--r-- | lib/rbot/registry.rb | 5 | ||||
-rw-r--r-- | lib/rbot/registry/sqlite.rb | 19 | ||||
-rw-r--r-- | lib/rbot/registry/tc.rb | 7 | ||||
-rw-r--r-- | test/test_registry.rb | 92 |
5 files changed, 78 insertions, 47 deletions
@@ -280,7 +280,7 @@ class RestoreRegistry def write_sqlite(file, data) db = SQLite3::Database.new(file + '.db') - db.execute('CREATE TABLE data (key string, value blob)') + db.execute('CREATE TABLE data (key PRIMARY_KEY, value)') data.each_pair do |key, value| db.execute('INSERT INTO data VALUES (?, ?)', key, value) diff --git a/lib/rbot/registry.rb b/lib/rbot/registry.rb index 70b450ac..c4e50faf 100644 --- a/lib/rbot/registry.rb +++ b/lib/rbot/registry.rb @@ -222,8 +222,8 @@ class Registry # like Hash#each def each(&block) return nil unless dbexists? - registry.each_key do |key| - block.call(key, self[key]) + registry.each do |key, value| + block.call(key, restore(value)) end end @@ -268,6 +268,7 @@ class Registry end # delete a key from the registry + # returns the value in success, nil otherwise def delete(key) return default unless dbexists? value = registry.delete(key.to_s) diff --git a/lib/rbot/registry/sqlite.rb b/lib/rbot/registry/sqlite.rb index 1af5d242..6a4a2312 100644 --- a/lib/rbot/registry/sqlite.rb +++ b/lib/rbot/registry/sqlite.rb @@ -23,7 +23,7 @@ class Registry begin @registry.execute('SELECT COUNT(*) FROM data') rescue - @registry.execute('CREATE TABLE data (key string, value blob)') + @registry.execute('CREATE TABLE data (key PRIMARY KEY, value)') end end @registry @@ -37,7 +37,7 @@ class Registry def [](key) if dbexists? begin - value = @registry.get_first_row('SELECT value FROM data WHERE key = ?', key.to_s) + value = registry.get_first_row('SELECT value FROM data WHERE key = ?', key.to_s) return restore(value.first) rescue return default @@ -65,12 +65,18 @@ class Registry end end + alias each_pair each + def has_key?(key) return nil unless dbexists? res = registry.get_first_row('SELECT COUNT(*) FROM data WHERE key = ?', key.to_s) return res.first > 0 end + alias include? has_key? + alias member? has_key? + alias key? has_key? + def has_value?(value) return nil unless dbexists? value = SQLite3::Blob.new(store(value)) @@ -81,10 +87,11 @@ class Registry def delete(key) return default unless dbexists? begin + value = self[key] registry.execute('DELETE FROM data WHERE key = ?', key.to_s) - registry.changes > 0 + value if registry.changes > 0 rescue - false + nil end end @@ -100,6 +107,8 @@ class Registry registry.execute('DELETE FROM data') end + alias truncate clear + # returns the number of keys in your registry namespace def length return 0 unless dbexists? @@ -107,6 +116,8 @@ class Registry res.first end + alias size length + end end # Registry diff --git a/lib/rbot/registry/tc.rb b/lib/rbot/registry/tc.rb index 4e248f42..f1835c8a 100644 --- a/lib/rbot/registry/tc.rb +++ b/lib/rbot/registry/tc.rb @@ -41,6 +41,13 @@ class Registry @registry.optimize end + def delete(key) + return default unless dbexists? + value = self[key] + registry.delete(key.to_s) + value # return deleted value if success + end + end end # Registry diff --git a/test/test_registry.rb b/test/test_registry.rb index 85383269..c6d68902 100644 --- a/test/test_registry.rb +++ b/test/test_registry.rb @@ -7,7 +7,27 @@ require 'rbot/registry' require 'pp' require 'tmpdir' +class FooObj + attr_reader :bar + def initialize(bar) + @bar = bar + end +end + module RegistryHashInterfaceTests + def test_object + @reg['store'] = { + :my_obj => FooObj.new(42) + } + + assert_equal(42, @reg['store'][:my_obj].bar) + + @reg.close + @reg = open(@tempdir) + + assert_equal(42, @reg['store'][:my_obj].bar) + end + def test_default @reg.set_default(42) assert_equal(42, @reg['not-here']) @@ -232,83 +252,75 @@ module RegistryHashInterfaceTests end end -module TempRegistryTest - def setup_temp +module RegistryTestModule + def setup @tempdir = Dir.mktmpdir + @reg = open(@tempdir) end - def teardown_temp + def teardown + @reg.close FileUtils.remove_entry @tempdir end def open(path, filename='testcase') - @@factory ||= Irc::Bot::Registry.new(@format) - @reg = @@factory.create(path, filename) + puts 'open type: ' + @format + @registry_class.new(File.join(path, filename)) end end class RegistryDBMTest < Test::Unit::TestCase - include TempRegistryTest + include RegistryTestModule include RegistryHashInterfaceTests - def setup - setup_temp + def initialize(o) + super o @format = 'dbm' - @reg = open(@tempdir) - end - - def teardown - @reg.close - teardown_temp + Irc::Bot::Registry.new(@format) + @registry_class = Irc::Bot::Registry::DBMAccessor end end class RegistryTCTest < Test::Unit::TestCase - include TempRegistryTest + include RegistryTestModule include RegistryHashInterfaceTests - def setup - setup_temp + def initialize(o) + super o @format = 'tc' - @reg = open(@tempdir) - end - - def teardown - @reg.close - teardown_temp + Irc::Bot::Registry.new(@format) + @registry_class = Irc::Bot::Registry::TokyoCabinetAccessor end end class RegistryDaybreakTest < Test::Unit::TestCase - include TempRegistryTest + include RegistryTestModule include RegistryHashInterfaceTests - def setup - setup_temp + def initialize(o) + super o @format = 'daybreak' - @reg = open(@tempdir) - end - - def teardown - @reg.close - teardown_temp + Irc::Bot::Registry.new(@format) + @registry_class = Irc::Bot::Registry::DaybreakAccessor end end - class RegistrySqliteTest < Test::Unit::TestCase - include TempRegistryTest + include RegistryTestModule include RegistryHashInterfaceTests - def setup - setup_temp + def initialize(o) + super o @format = 'sqlite' - @reg = open(@tempdir) + Irc::Bot::Registry.new(@format) + @registry_class = Irc::Bot::Registry::SqliteAccessor end - def teardown - @reg.close - teardown_temp + def test_duplicate_keys + @reg['foo'] = 1 + @reg['foo'] = 2 + res = @reg.registry.execute('select key from data') + assert res.length == 1 end end |