summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/rbot/registry.rb10
-rw-r--r--lib/rbot/registry/daybreak.rb5
-rw-r--r--lib/rbot/registry/dbm.rb11
-rw-r--r--lib/rbot/registry/sqlite.rb5
-rw-r--r--lib/rbot/registry/tc.rb5
-rw-r--r--test/test_registry.rb24
6 files changed, 39 insertions, 21 deletions
diff --git a/lib/rbot/registry.rb b/lib/rbot/registry.rb
index 4fd5f77c..70b450ac 100644
--- a/lib/rbot/registry.rb
+++ b/lib/rbot/registry.rb
@@ -98,6 +98,8 @@ class Registry
# Abstract database accessor (a hash-like interface).
class AbstractAccessor
+ attr_reader :filename
+
# lets the user define a recovery procedure in case the Marshal
# deserialization fails, it might be manually recover data.
# NOTE: weird legacy stuff, used by markov plugin (WTH?)
@@ -187,13 +189,13 @@ class Registry
# Forces flush/sync the database on disk.
def flush
return unless @registry
- @registry.flush
+ # if not supported by the database, close/reopen:
+ close
+ registry
end
- # Should optimize/vacuum the database.
+ # Should optimize/vacuum the database. (if supported)
def optimize
- return unless @registry
- @registry.optimize
end
# Closes the database.
diff --git a/lib/rbot/registry/daybreak.rb b/lib/rbot/registry/daybreak.rb
index 6058b408..233c3a0d 100644
--- a/lib/rbot/registry/daybreak.rb
+++ b/lib/rbot/registry/daybreak.rb
@@ -24,6 +24,11 @@ class Registry
@registry ||= Daybreak::DB.new(@filename)
end
+ def flush
+ return unless @registry
+ @registry.flush
+ end
+
def optimize
return unless @registry
@registry.compact
diff --git a/lib/rbot/registry/dbm.rb b/lib/rbot/registry/dbm.rb
index a13cb8ce..9c0304a9 100644
--- a/lib/rbot/registry/dbm.rb
+++ b/lib/rbot/registry/dbm.rb
@@ -23,21 +23,10 @@ class Registry
@registry ||= DBM.open(@filename, 0666, DBM::WRCREAT)
end
- def flush
- return if !@registry
- # ruby dbm has no flush, so we close/reopen :(
- close
- registry
- end
-
def dbexists?
not Dir.glob(@filename + '.*').empty?
end
- def optimize
- # unsupported!
- end
-
end
end # Registry
diff --git a/lib/rbot/registry/sqlite.rb b/lib/rbot/registry/sqlite.rb
index 89e7d78c..1af5d242 100644
--- a/lib/rbot/registry/sqlite.rb
+++ b/lib/rbot/registry/sqlite.rb
@@ -29,10 +29,9 @@ class Registry
@registry
end
- def flush
- end
-
def optimize
+ return unless @registry
+ @registry.execute('VACUUM')
end
def [](key)
diff --git a/lib/rbot/registry/tc.rb b/lib/rbot/registry/tc.rb
index 63bfdf78..4e248f42 100644
--- a/lib/rbot/registry/tc.rb
+++ b/lib/rbot/registry/tc.rb
@@ -36,6 +36,11 @@ class Registry
@registry.sync
end
+ def optimize
+ return unless @registry
+ @registry.optimize
+ end
+
end
end # Registry
diff --git a/test/test_registry.rb b/test/test_registry.rb
index 76ee2b22..a7d45457 100644
--- a/test/test_registry.rb
+++ b/test/test_registry.rb
@@ -12,6 +12,26 @@ module RegistryHashInterfaceTests
end
def test_flush
+ # I don't know if there really is a good way to test this:
+ big_string = 'A' * (1024 * 512)
+ @reg['foo'] = big_string+'a'
+
+ dbfile = @reg.filename
+ assert_not_nil(dbfile)
+ if not File.exists? dbfile
+ # dbm ext. are arbitary
+ dbfile = Dir.glob(dbfile+'.*').first
+ end
+ assert_not_nil(dbfile)
+
+ assert(File.exists?(dbfile), 'expected database to exist')
+
+ size_before = File.size(dbfile)
+ @reg['bar'] = big_string
+ @reg.flush
+ size_after = File.size(dbfile)
+
+ assert(size_before < size_after, 'expected big string to be flushed on disk!')
end
def test_optimize
@@ -210,11 +230,9 @@ end
module TempRegistryTest
def setup_temp
@tempdir = Dir.mktmpdir
- puts ' setup registry test, using %s' % @tempdir
end
def teardown_temp
- puts ' teardown registry test, delete %s' % @tempdir
FileUtils.remove_entry @tempdir
end
@@ -262,7 +280,7 @@ class RegistryDaybreakTest < Test::Unit::TestCase
def setup
setup_temp
- @format = 'sqlite'
+ @format = 'daybreak'
@reg = open(@tempdir)
end