From d30f0f3f805292d752a9b973dd8181a3ede554ed Mon Sep 17 00:00:00 2001 From: mattbauer Date: Tue, 30 Dec 2008 14:15:04 -0600 Subject: [PATCH] Environment test and additional getters --- ext/bdb.c | 198 ++++++++++++++++++++++++++++++++++++++++++++++- test/env_test.rb | 132 ++++++++++++++----------------- 2 files changed, 252 insertions(+), 78 deletions(-) diff --git a/ext/bdb.c b/ext/bdb.c index 4449f98..5b0a596 100644 --- a/ext/bdb.c +++ b/ext/bdb.c @@ -1647,16 +1647,15 @@ VALUE env_set_cachesize(VALUE obj, VALUE size) if ( TYPE(size) == T_BIGNUM ) { ln = rb_big2ull(size); - bytes = (u_int32_t) ln; - gbytes = (u_int32_t) (ln >> sizeof(u_int32_t)); + gbytes = ln / (1024*1024*1024); + bytes = ln - (gbytes*1024*1024*1024); } else if (FIXNUM_P(size) ) { - bytes=NUM2INT(size); + bytes=NUM2UINT(size); } else { raise_error(0,"set_cachesize requires number"); return Qnil; } - rb_warning("setting cache size %d %d %d",gbytes,bytes,1); rv=eh->env->set_cachesize(eh->env,gbytes,bytes,1); if ( rv != 0 ) { raise_error(rv, "set_cachesize failure: %s",db_strerror(rv)); @@ -1666,6 +1665,39 @@ VALUE env_set_cachesize(VALUE obj, VALUE size) return Qtrue; } +/* + * call-seq: + * env.cachesize -> Fixnum|Bignum + * + * return the environment cache size. If it is a Bignum then it + * will populate the bytes and gbytes part of the DB struct. + * Fixnums will only populate bytes (which is still pretty big). + */ +VALUE env_get_cachesize(VALUE obj) +{ + t_envh *eh; + unsigned long long ln; + u_int32_t bytes=0,gbytes=0; + int ncache; + int rv; + Data_Get_Struct(obj,t_envh,eh); + if (!eh->env) + raise(0, "env is closed"); + + rv=eh->env->get_cachesize(eh->env,&gbytes,&bytes,&ncache); + if ( rv != 0 ) { + raise_error(rv, "get_cachesize failure: %s",db_strerror(rv)); + return Qnil; + } + + if (gbytes != 0) + return ULL2NUM(gbytes*1024*1024*1024+bytes); + else + return UINT2NUM(bytes); + + return Qtrue; +} + VALUE env_set_flags(VALUE obj, VALUE vflags, int onoff) { t_envh *eh; @@ -1688,6 +1720,32 @@ VALUE env_set_flags(VALUE obj, VALUE vflags, int onoff) return Qtrue; } +/* + * call-seq: + * env.flags -> flags + * + * get what flags are on. + */ +VALUE env_get_flags(VALUE obj) +{ + t_envh *eh; + int rv; + u_int32_t flags; + + Data_Get_Struct(obj,t_envh,eh); + if (!eh->env) + raise(0, "env is closed"); + + rv=eh->env->get_flags(eh->env,&flags); + + if ( rv != 0 ) { + raise_error(rv, "set_flags failure: %s",db_strerror(rv)); + return Qnil; + } + + return UINT2NUM(flags); +} + /* * call-seq: * env.flags_on=flags @@ -1701,6 +1759,7 @@ VALUE env_set_flags_on(VALUE obj, VALUE vflags) { return env_set_flags(obj,vflags,1); } + /* * call-seq: * env.flags_off=flags @@ -1714,6 +1773,7 @@ VALUE env_set_flags_off(VALUE obj, VALUE vflags) { return env_set_flags(obj,vflags,0); } + /* * call-seq: * env.list_dbs -> [Bdb::Db array] @@ -2286,6 +2346,29 @@ VALUE env_set_lk_max_locks(VALUE obj, VALUE vmax) return vmax; } +/* + * call-seq: + * env.get_lk_max_locks -> max + * + * Get the maximum number of locks in the environment + */ +VALUE env_get_lk_max_locks(VALUE obj) +{ + t_envh *eh; + u_int32_t max; + int rv; + + Data_Get_Struct(obj,t_envh,eh); + if (!eh->env) + raise(0, "env is closed"); + rv=eh->env->get_lk_max_locks(eh->env,&max); + if ( rv != 0 ) { + raise_error(rv, "env_get_lk_max_locks: %s",db_strerror(rv)); + } + + return UINT2NUM(max); +} + /* * call-seq: * env.set_lk_max_objects(max) -> max @@ -2311,6 +2394,29 @@ VALUE env_set_lk_max_objects(VALUE obj, VALUE vmax) return vmax; } +/* + * call-seq: + * env.get_lk_max_objects -> max + * + * Get the maximum number of locks in the environment + */ +VALUE env_get_lk_max_objects(VALUE obj) +{ + t_envh *eh; + u_int32_t max; + int rv; + + Data_Get_Struct(obj,t_envh,eh); + if (!eh->env) + raise(0, "env is closed"); + rv=eh->env->get_lk_max_objects(eh->env,&max); + if ( rv != 0 ) { + raise_error(rv, "env_get_lk_max_objects: %s",db_strerror(rv)); + } + + return UINT2NUM(max); +} + VALUE env_report_stderr(VALUE obj) { t_envh *eh; @@ -2350,6 +2456,37 @@ VALUE env_set_data_dir(VALUE obj, VALUE vdata_dir) return vdata_dir; } +/* + * call-seq: + * env.get_data_dir -> [data_dir_1, data_dir_2, ...] + * + * get data_dir + */ +VALUE env_get_data_dirs(VALUE obj) +{ + t_envh *eh; + const char **data_dirs; + int rv; + int ln; + + Data_Get_Struct(obj,t_envh,eh); + if (!eh->env) + raise(0, "env is closed"); + rv=eh->env->get_data_dirs(eh->env,&data_dirs); + if ( rv != 0 ) { + raise_error(rv, "env_get_data_dir: %s",db_strerror(rv)); + } + + ln = (sizeof (data_dirs))/sizeof(data_dirs[0]); + VALUE rb_data_dirs = rb_ary_new2(ln); + int i; + for (i=0; i lg_dir @@ -2375,6 +2512,29 @@ VALUE env_set_lg_dir(VALUE obj, VALUE vlg_dir) return vlg_dir; } +/* + * call-seq: + * env.get_lg_dir -> lg_dir + * + * get lg_dir + */ +VALUE env_get_lg_dir(VALUE obj) +{ + t_envh *eh; + const char *lg_dir; + int rv; + + Data_Get_Struct(obj,t_envh,eh); + if (!eh->env) + raise(0, "env is closed"); + rv=eh->env->get_lg_dir(eh->env,&lg_dir); + if ( rv != 0 ) { + raise_error(rv, "env_get_lg_dir: %s",db_strerror(rv)); + } + + return rb_str_new2(lg_dir); +} + /* * call-seq: * env.set_tmp_dir(tmp_dir) -> tmp_dir @@ -2400,6 +2560,29 @@ VALUE env_set_tmp_dir(VALUE obj, VALUE vtmp_dir) return vtmp_dir; } +/* + * call-seq: + * env.get_tmp_dir -> tmp_dir + * + * get tmp_dir + */ +VALUE env_get_tmp_dir(VALUE obj) +{ + t_envh *eh; + const char *tmp_dir; + int rv; + + Data_Get_Struct(obj,t_envh,eh); + if (!eh->env) + raise(0, "env is closed"); + rv=eh->env->get_tmp_dir(eh->env,&tmp_dir); + if ( rv != 0 ) { + raise_error(rv, "env_get_tmp_dir: %s",db_strerror(rv)); + } + + return rb_str_new2(tmp_dir); +} + static void txn_finish(t_txnh *txn) { if ( RTEST(ruby_debug) ) @@ -2626,6 +2809,8 @@ void Init_bdb() { rb_define_method(cEnv,"close",env_close,0); rb_define_method(cEnv,"db",env_db,0); rb_define_method(cEnv,"cachesize=",env_set_cachesize,1); + rb_define_method(cEnv,"cachesize",env_get_cachesize,0); + rb_define_method(cEnv,"flags",env_get_flags,0); rb_define_method(cEnv,"flags_on=",env_set_flags_on,1); rb_define_method(cEnv,"flags_off=",env_set_flags_off,1); rb_define_method(cEnv,"list_dbs",env_list_dbs,0); @@ -2640,13 +2825,18 @@ void Init_bdb() { rb_define_method(cEnv,"set_lk_detect",env_set_lk_detect,1); rb_define_method(cEnv,"get_lk_detect",env_get_lk_detect,0); rb_define_method(cEnv,"set_lk_max_locks",env_set_lk_max_locks,1); + rb_define_method(cEnv,"get_lk_max_locks",env_get_lk_max_locks,0); rb_define_method(cEnv,"set_lk_max_objects",env_set_lk_max_objects,1); + rb_define_method(cEnv,"get_lk_max_objects",env_get_lk_max_objects,0); rb_define_method(cEnv,"set_shm_key",env_set_shm_key,1); rb_define_method(cEnv,"get_shm_key",env_get_shm_key,0); rb_define_method(cEnv,"set_data_dir",env_set_data_dir,1); + rb_define_method(cEnv,"get_data_dirs",env_get_data_dirs,0); rb_define_method(cEnv,"set_lg_dir",env_set_lg_dir,1); + rb_define_method(cEnv,"get_lg_dir",env_get_lg_dir,0); rb_define_method(cEnv,"set_tmp_dir",env_set_tmp_dir,1); + rb_define_method(cEnv,"get_tmp_dir",env_get_tmp_dir,0); cTxnStat = rb_define_class_under(mBdb,"TxnStat",rb_cObject); rb_define_method(cTxnStat,"[]",stat_aref,1); diff --git a/test/env_test.rb b/test/env_test.rb index 856d609..92e9fe9 100644 --- a/test/env_test.rb +++ b/test/env_test.rb @@ -1,105 +1,89 @@ require 'test_helper' class EnvTest < Test::Unit::TestCase - - def test_new - end - - def test_open - - end - - def test_close - - end - - def test_db - - end - def test_cachesize - + env = Bdb::Env.new(0) + env.cachesize = 1024*1024*500 + assert_equal 1024*1024*500, env.cachesize + + env.cachesize = 1024*1024*1024*3 + assert_equal 1024*1024*1024*3, env.cachesize + + env.cachesize = 1024*1024*1024*3+1 + assert_equal 1024*1024*1024*3+1, env.cachesize end - def test_flags_on - + def test_flags + env = Bdb::Env.new(0) + + env.flags_on = Bdb::DB_AUTO_COMMIT | Bdb::DB_DSYNC_DB + assert_equal Bdb::DB_AUTO_COMMIT | Bdb::DB_DSYNC_DB, env.flags + + env.flags_off = Bdb::DB_AUTO_COMMIT | Bdb::DB_DSYNC_DB + assert_equal 0, env.flags end - - def test_flags_off - end - def test_list_dbs + env = Bdb::Env.new(0) + assert env.list_dbs.empty? + db = env.db + assert_equal db, env.list_dbs.first + end + + def test_set_and_get_timeout + env = Bdb::Env.new(0) + env.set_timeout(10, Bdb::DB_SET_LOCK_TIMEOUT) + assert_equal 10, env.get_timeout(Bdb::DB_SET_LOCK_TIMEOUT) end - def test_txn_begin - + def test_set_and_get_tx_max + env = Bdb::Env.new(0) + env.set_tx_max(100) + assert_equal 100, env.get_tx_max end - def test_txn_checkpoint + def test_set_and_get_lk_detect + env = Bdb::Env.new(0) + env.set_lk_detect(Bdb::DB_LOCK_MAXWRITE) + assert_equal Bdb::DB_LOCK_MAXWRITE, env.get_lk_detect + end + def test_set_and_get_lk_max_locks + env = Bdb::Env.new(0) + env.set_lk_max_locks(10_000) + assert_equal 10_000, env.get_lk_max_locks end - def test_txn_stat - + def test_set_and_get_lk_max_objects + env = Bdb::Env.new(0) + env.set_lk_max_objects(10_000) + assert_equal 10_000, env.get_lk_max_objects end - def test_set_timeout - + def test_set_and_get_shm_key + env = Bdb::Env.new(0) + env.set_shm_key(2506400) + assert_equal 2506400, env.get_shm_key end - - def test_get_timeout - - end - - def test_set_tx_max - - end - - def test_get_tx_max - - end - - def test_report_stderr - - end - - def test_set_lk_detect - - end - - def test_get_lk_detect - - end - - def test_set_lk_max_locks - - end - - def test_set_lk_max_objects - - end - - def test_set_shm_key - - end - - def test_get_shm_key - - end - - def test_set_data_dir + def test_set_and_get_data_dir + env = Bdb::Env.new(0) + env.set_data_dir('/tmp') + assert_equal ['/tmp'], env.get_data_dirs end def test_set_lg_dir - + env = Bdb::Env.new(0) + env.set_lg_dir('/tmp') + assert_equal '/tmp', env.get_lg_dir end def test_set_tmp_dir - + env = Bdb::Env.new(0) + env.set_tmp_dir('/tmp') + assert_equal '/tmp', env.get_tmp_dir end end