From 76a68577ce61c02689ad450c054b69bf1a8d6f60 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 4 Dec 2010 15:56:11 -0800 Subject: add support for "clear" and "repack" methods TDB#clear maps to tdb_wipe_all and TDB#repack maps to TDB#repack. --- ext/tdb/extconf.rb | 1 + ext/tdb/tdb.c | 26 ++++++++++++++++++++++++++ lib/tdb/mt.rb | 7 +++++-- test/test_tdb.rb | 14 ++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/ext/tdb/extconf.rb b/ext/tdb/extconf.rb index 32adafe..5359f7d 100644 --- a/ext/tdb/extconf.rb +++ b/ext/tdb/extconf.rb @@ -7,6 +7,7 @@ dir_config('tdb') have_header('tdb.h') or abort 'tdb.h missing' have_library('tdb') or abort 'libtdb missing' have_func('tdb_jenkins_hash') +have_func('tdb_repack') have_const('TDB_ERR_NESTING', 'tdb.h') create_makefile('tdb_ext') diff --git a/ext/tdb/tdb.c b/ext/tdb/tdb.c index 373da17..d1b573a 100644 --- a/ext/tdb/tdb.c +++ b/ext/tdb/tdb.c @@ -619,6 +619,28 @@ static VALUE lockall_unmark(VALUE self) return Qtrue; } +/* + * clears out the database + */ +static VALUE clear(VALUE self) +{ + struct tdb_context *tdb = db(self, 1); + if ((int)my_tbr((rb_blocking_function_t *)tdb_wipe_all, tdb)) + my_raise(tdb); + return self; +} + +#ifdef HAVE_TDB_REPACK +/* repacks a database to reduce fragmentation, available with tdb 1.2.x+ */ +static VALUE repack(VALUE self) +{ + struct tdb_context *tdb = db(self, 1); + if ((int)my_tbr((rb_blocking_function_t *)tdb_repack, tdb)) + my_raise(tdb); + return self; +} +#endif /* HAVE_TDB_REPACK */ + void Init_tdb_ext(void) { cTDB = rb_define_class("TDB", rb_cObject); @@ -663,6 +685,10 @@ void Init_tdb_ext(void) rb_define_method(cTDB, "unlockall_read", unlockall_read, 0); rb_define_method(cTDB, "lockall_mark", lockall_mark, 0); rb_define_method(cTDB, "lockall_unmark", lockall_unmark, 0); + rb_define_method(cTDB, "clear", clear, 0); +#ifdef HAVE_TDB_REPACK + rb_define_method(cTDB, "repack", repack, 0); +#endif /* HAVE_TDB_REPACK */ init_errors(); init_hashes(); diff --git a/lib/tdb/mt.rb b/lib/tdb/mt.rb index 6742cc4..7e7a09d 100644 --- a/lib/tdb/mt.rb +++ b/lib/tdb/mt.rb @@ -5,14 +5,17 @@ module TDB::MT @lock = Mutex.new end - %w( + wrap_methods = %w( close closed? fetch [] store []= insert! modify! insert modify key? has_key? include? member? nuke! delete lockall trylockall unlockall lockall_read trylockall_read unlockall_read lockall_mark lockall_unmark - ).each do |meth| + clear + ) + wrap_methods << :repack if TDB.method_defined?(:repack) + wrap_methods.each do |meth| eval "def #{meth}(*args); @lock.synchronize { super }; end" end diff --git a/test/test_tdb.rb b/test/test_tdb.rb index bd25203..64f34c0 100644 --- a/test/test_tdb.rb +++ b/test/test_tdb.rb @@ -267,4 +267,18 @@ class TestTdb < Test::Unit::TestCase TDB.constants.each { |const| values[TDB.const_get(const)] = const } assert_equal TDB.constants.size, values.size end + + def test_clear + @tdb = TDB.new(nil) + @tdb["hello"] = "world" + assert_equal @tdb, @tdb.clear + assert ! @tdb.include?("hello") + end + + def test_repack + @tdb = TDB.new(nil) + @tdb["hello"] = "world" + assert_equal @tdb, @tdb.repack + assert_equal "world", @tdb["hello"] + end if TDB.method_defined?(:repack) end -- cgit v1.2.3-24-ge0c7