Test structure change and db test updates

This commit is contained in:
mattbauer 2008-12-28 23:14:29 -06:00
parent e505f13f17
commit 93716e1ead
11 changed files with 321 additions and 178 deletions

View file

@ -14,11 +14,9 @@ task :default => "test"
desc "Clean"
task :clean do
include FileUtils
rm_rf File.join('ext', 'bdb_aux._c')
rm_rf File.join('ext', 'Makefile')
rm_rf File.join('ext', 'mkmf.log')
rm_rf File.join('ext', 'conftest.c')
rm_rf File.join('ext', '*.o')
Dir.chdir('ext') do
rm(Dir.glob('*') - ['bdb.c', 'bdb.h', 'extconf.rb'])
end
rm_rf 'pkg'
end

View file

@ -96,6 +96,7 @@ static void db_free(t_dbh *dbh)
if (dbh) {
if (dbh->db) {
if (dbh->db_opened == 1)
dbh->db->close(dbh->db,NOFLAGS);
if ( RTEST(ruby_debug) && dbh->filename[0] != '\0')
fprintf(stderr,"%s/%d %s %p %s\n",__FILE__,__LINE__,
@ -272,7 +273,7 @@ VALUE db_open(VALUE obj, VALUE vtxn, VALUE vdisk_file,
}
filename_copy(dbh->filename,vdisk_file)
dbh->adbc=rb_ary_new();
dbh->db_opened = 1;
return obj;
}
@ -302,6 +303,31 @@ VALUE db_flags_set(VALUE obj, VALUE vflags)
return vflags;
}
/*
* call-seq:
* db.flags -> value
*
* get database flags.
* see http://www.sleepycat.com/docs/api_c/db_get_flags.html
*
*/
VALUE db_flags_get(VALUE obj)
{
t_dbh *dbh;
int rv;
u_int32_t flags;
Data_Get_Struct(obj,t_dbh,dbh);
if (!dbh->db)
raise_error(0,"db is closed");
rv = dbh->db->get_flags(dbh->db,&flags);
if ( rv != 0 ) {
raise_error(rv, "db_flag_get failure: %s",db_strerror(rv));
}
return INT2NUM(flags);
}
/*
* call-seq:
* db.pagesize=value
@ -493,7 +519,7 @@ VALUE db_close(VALUE obj, VALUE vflags)
if ( rv != 0 ) {
raise_error(rv, "db_close failure: %s",db_strerror(rv));
}
dbh->db_opened = 0;
return obj;
}
@ -577,7 +603,6 @@ VALUE db_get(VALUE obj, VALUE vtxn, VALUE vkey, VALUE vdata, VALUE vflags)
}
if ( ! NIL_P(vflags) ) {
rb_warning("flags nil");
flags=NUM2UINT(vflags);
}
@ -637,7 +662,6 @@ VALUE db_pget(VALUE obj, VALUE vtxn, VALUE vkey, VALUE vdata, VALUE vflags)
}
if ( ! NIL_P(vflags) ) {
rb_warning("flags nil");
flags=NUM2UINT(vflags);
}
@ -909,8 +933,9 @@ VALUE db_rename(VALUE obj, VALUE vdisk_file,
StringValueCStr(newname),
flags);
if (rv)
if (rv) {
raise_error(rv,"db_rename failed: %s",db_strerror(rv));
}
return Qtrue;
}
@ -2559,6 +2584,7 @@ void Init_bdb() {
rb_define_method(cDb,"cursor",db_cursor,2);
rb_define_method(cDb,"associate",db_associate,4);
rb_define_method(cDb,"flags=",db_flags_set,1);
rb_define_method(cDb,"flags",db_flags_get,0);
rb_define_method(cDb,"open",db_open,6);
rb_define_method(cDb,"close",db_close,1);
rb_define_method(cDb,"[]",db_aget,1);
@ -2636,6 +2662,3 @@ void Init_bdb() {
rb_define_method(cTxn,"tid",txn_id,0);
rb_define_method(cTxn,"set_timeout",txn_set_timeout,2);
}
void Init_bdb2a() {
Init_bdb2();
}

View file

@ -50,6 +50,7 @@ typedef struct s_envh {
typedef struct s_dbh {
VALUE self;
DB *db;
int db_opened;
VALUE aproc;
t_envh *env; /* Parent environment, NULL if not opened from one */
VALUE adbc; /* Ruby array holding opened cursor */

View file

@ -1,161 +0,0 @@
#!/usr/bin/env ruby
require 'test_helper'
50.times {|n|
db=Bdb::Db.new
db.open(nil,"dbtest.db",nil,Bdb::Db::BTREE,Bdb::DB_CREATE,0)
db.put(nil,n.to_s,"ploppy #{n} #{Time.now}",0)
db.close(0)
}
db=Bdb::Db.new
db.open(nil,"dbtest.db",nil,nil,nil,0)
500.times {|n|
db.put(nil,n.to_s,"ploppy #{n}",0)
}
db.close(0)
db=Bdb::Db.new
db.open(nil,"dbtest.db",nil,nil,nil,0)
dbc=db.cursor(nil,0)
puts("cursor is: "+dbc.inspect.to_s)
kv=dbc.get(nil,nil,Bdb::DB_FIRST);
puts("first data is: " + kv.inspect.to_s)
kv=dbc.get(nil,nil,Bdb::DB_LAST);
puts("last data is: " + kv.inspect.to_s)
kv=dbc.get(nil,nil,Bdb::DB_PREV);
puts("prior data is: " + kv.inspect.to_s)
dbc.del;
begin
kv=dbc.get(nil,nil,Bdb::DB_CURRENT);
rescue Bdb::DbError => m
puts("deleted record is gone from current position:" + m.to_s)
puts("code is #{m.code}")
end
puts("current data is: " +kv.inspect.to_s)
dbc.put("elephant","gorilla",Bdb::DB_KEYFIRST)
kv=dbc.get(nil,nil,Bdb::DB_CURRENT);
puts("current data after put: " +kv.inspect.to_s)
kv=dbc.get(nil,nil,Bdb::DB_LAST);
puts("last data is: " + kv.inspect.to_s)
kv=dbc.get(nil,nil,Bdb::DB_PREV);
puts("prior data is: " + kv.inspect.to_s)
puts("duplicates here is: " + dbc.count.to_s)
dbc.close
db.close(0)
db=Bdb::Db.new
db.open(nil,"dbtest.db",nil,nil,nil,0)
5.times {|n|
$stdout.puts(db.get(nil,n.to_s,nil,0))
begin
db.del(n.to_s,0)
rescue
end
}
5.times {|n|
v=db.get(nil,n.to_s,nil,0)
if v
$stdout.puts("For #{n}:" + v)
else
$stdout.puts("-- not in database #{n}")
end
}
db.close(0)
$stderr.puts("All OK!")
File.delete('db1.db') if File.exist?('db1.db')
File.delete('db2.db') if File.exist?('db2.db')
if File.exist?('skus')
db=Bdb::Db.new
db.open(nil,"db1.db",nil,Bdb::Db::HASH,Bdb::DB_CREATE,0)
db2=Bdb::Db.new
db2.flags=Bdb::DB_DUPSORT
db2.open(nil,"db2.db",nil,Bdb::Db::HASH,Bdb::DB_CREATE,0)
db.associate(nil,db2,0,
proc {|sdb,key,data|
key.split('-')[0]
})
c=0
File.open("skus") {|fd|
tlen=fd.stat.size
pf=tlen/10
pl=0
fd.each do |line|
c+=1
if c%1000==0
$stderr.print('.')
cp=fd.pos
if ( cp/pf > pl )
pl=cp/pf
$stderr.print(" #{pl*10}% ")
end
end
line.chomp!
n=line*50
isbn,item=line.split('|')[0..1]
sku="%s-%03d"%[isbn,item]
db.put(sku,line,0)
end
}
$stderr.print("\ntotal count: #{c}\n")
db2.close(0)
db.close(0)
end
$stderr.puts("test environment")
if File.exist?('skus')
env=Bdb::Env.new(0)
env.cachesize=25*1024*1024;
env.open(".",Bdb::DB_INIT_CDB|Bdb::DB_INIT_MPOOL|Bdb::DB_CREATE,0)
db=env.db
db.open(nil,"db1.db",nil,Bdb::Db::HASH,Bdb::DB_CREATE,0)
db2=env.db
db2.flags=Bdb::DB_DUPSORT
db2.open(nil,"db2.db",nil,Bdb::Db::HASH,Bdb::DB_CREATE,0)
db.associate(nil,db2,0,
proc {|sdb,key,data|
key.split('-')[0]
})
c=0
File.open("skus") {|fd|
tlen=fd.stat.size
pf=tlen/10
pl=0
fd.each do |line|
c+=1
if c%1000==0
$stderr.print('.')
cp=fd.pos
if ( cp/pf > pl )
pl=cp/pf
$stderr.print(" #{pl*10}% ")
end
end
line.chomp!
n=line*50
isbn,item=line.split('|')[0..1]
sku="%s-%03d"%[isbn,item]
db.put(sku,line,0)
end
}
$stderr.print("\ntotal count: #{c}\n")
db2.close(0)
db.close(0)
env.close
end
exit
$stderr.puts(Rusage.get.inspect)
$stderr.puts(`ps -up #{$$}`)

23
test/cursor_test.rb Normal file
View file

@ -0,0 +1,23 @@
require 'test_helper'
class CursorTest < Test::Unit::TestCase
def test_get
end
def test_pget
end
def test_put
end
def test_close
end
def test_del
end
def test_count
end
end

117
test/db_test.rb Normal file
View file

@ -0,0 +1,117 @@
require 'fileutils'
require 'test_helper'
class DbTest < Test::Unit::TestCase
def setup
FileUtils::mkdir File.join(File.dirname(__FILE__), 'tmp')
@db = Bdb::Db.new
@db.open(nil, File.join(File.dirname(__FILE__), 'tmp', 'test.db'), nil, Bdb::Db::BTREE, Bdb::DB_CREATE, 0)
end
def teardown
assert(@db.close(0)) if @db
FileUtils::rm_rf File.join('test', 'tmp')
end
def test_put_and_get
@db.put(nil, 'key', 'data', 0)
result = @db.get(nil, 'key', nil, 0)
assert_equal 'data', result
end
def test_pget
end
def test_del
@db.put(nil, 'key', 'data', 0)
result = @db.get(nil, 'key', nil, 0)
assert_equal 'data', result
@db.del(nil, 'key', 0)
result = @db.get(nil, 'key', nil, 0)
assert_nil result
end
def test_associate
end
def test_flags_set_and_get
@db1 = Bdb::Db.new
@db1.flags = Bdb::DB_DUPSORT
assert Bdb::DB_DUPSORT, @db1.flags
end
def test_aget
end
def test_aset
end
def test_join
end
def test_get_byteswapped
@db.get_byteswapped
end
def test_get_type
assert_equal Bdb::Db::BTREE, @db.get_type
end
def test_remove
@db1 = Bdb::Db.new
@db1.open(nil, File.join(File.dirname(__FILE__), 'tmp', 'other_test.db'), nil, Bdb::Db::BTREE, Bdb::DB_CREATE, 0)
@db1.close(0)
Bdb::Db.new.remove(File.join(File.dirname(__FILE__), 'tmp', 'other_test.db'), nil, 0)
assert !File.exists?(File.join(File.dirname(__FILE__), 'tmp', 'other_test.db'))
end
def test_key_range
10.times { |i| @db.put(nil, i.to_s, 'data', 0) }
@db.key_range(nil, '2', 0)
end
def test_rename
@db1 = Bdb::Db.new
@db1.open(nil, File.join(File.dirname(__FILE__), 'tmp', 'other_test.db'), nil, Bdb::Db::BTREE, Bdb::DB_CREATE, 0)
@db1.close(0)
assert Bdb::Db.new.rename(File.join(File.dirname(__FILE__), 'tmp', 'other_test.db'), nil, File.join(File.dirname(__FILE__), 'tmp', 'other2_test.db'), 0)
assert File.exists?(File.join(File.dirname(__FILE__), 'tmp', 'other2_test.db'))
end
def test_pagesize_get_and_set
@db1 = Bdb::Db.new
@db1.pagesize = 1024
assert_equal 1024, @db1.pagesize
end
def test_h_ffactor_get_and_set
@db1 = Bdb::Db.new
@db1.h_ffactor = 5
assert_equal 5, @db1.h_ffactor
end
def test_h_nelem_get_and_set
@db1 = Bdb::Db.new
@db1.h_nelem = 10_000
assert_equal 10_000, @db1.h_nelem
end
def test_sync
assert @db.sync
end
def test_truncate
@db.put(nil, 'key', 'data', 0)
result = @db.get(nil, 'key', nil, 0)
assert_equal 'data', result
@db.truncate(nil)
result = @db.get(nil, 'key', nil, 0)
assert_nil result
end
def test_compact
assert @db.compact(nil, nil, nil, nil, 0)
end
end

105
test/env_test.rb Normal file
View file

@ -0,0 +1,105 @@
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
end
def test_flags_on
end
def test_flags_off
end
def test_list_dbs
end
def test_txn_begin
end
def test_txn_checkpoint
end
def test_txn_stat
end
def test_set_timeout
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
end
def test_set_lg_dir
end
def test_set_tmp_dir
end
end

Binary file not shown.

10
test/stat_test.rb Normal file
View file

@ -0,0 +1,10 @@
require 'test_helper'
class DbTest < Test::Unit::TestCase
# rb_define_method(cDbStat,"[]",stat_aref,1);
def test_stat
end
end

View file

@ -1 +1,6 @@
require "test/unit"
require "bdb"
class Test::Unit::TestCase
end

22
test/txn_test.rb Normal file
View file

@ -0,0 +1,22 @@
require 'test_helper'
class TxnTest < Test::Unit::TestCase
# cTxnStat = rb_define_class_under(mBdb,"TxnStat",rb_cObject);
# rb_define_method(cTxnStat,"[]",stat_aref,1);
#
# cTxnStatActive =
# rb_define_class_under(cTxnStat,"Active",rb_cObject);
# rb_define_method(cTxnStatActive,"[]",stat_aref,1);
#
# cTxn = rb_define_class_under(mBdb,"Txn",rb_cObject);
# rb_define_method(cTxn,"commit",txn_commit,1);
# rb_define_method(cTxn,"abort",txn_abort,0);
# rb_define_method(cTxn,"discard",txn_discard,0);
# rb_define_method(cTxn,"tid",txn_id,0);
# rb_define_method(cTxn,"set_timeout",txn_set_timeout,2);
def test_x
end
end