diff --git a/lib/sbdb.rb b/lib/sbdb.rb index f3f2d1c..57e91c7 100644 --- a/lib/sbdb.rb +++ b/lib/sbdb.rb @@ -16,4 +16,10 @@ module SBDB def queue( *p) Queue.new *p end def unknown( *p) Unknown.new *p end alias open_db unknown + + def raise_barrier *ps, &e + e.call *ps + rescue Object + $stderr.puts [$!.class,$!,$!.backtrace].inspect + end end diff --git a/lib/sbdb/db.rb b/lib/sbdb/db.rb index 6722e00..2aff0fe 100644 --- a/lib/sbdb/db.rb +++ b/lib/sbdb/db.rb @@ -46,20 +46,14 @@ module SBDB alias del delete class << self - def new *p, &e - x = super *p - return x unless e - begin e.call x + def new *ps, &exe + ret = obj = super( *ps) + begin ret = e.call obj ensure - begin x.sync - rescue Object - $stderr.puts [$!.class,$!,$!.backtrace].inspect - end - begin x.close - rescue Object - $stderr.puts [$!.class,$!,$!.backtrace].inspect - end - end + SBDB::raise_barrier obj.method(:sync) + SBDB::raise_barrier obj.method(:close) + end if exe + ret end alias open new end @@ -119,7 +113,7 @@ module SBDB end TYPES[DB::HASH] = Hash - class Recno < DB + module Arrayisch def [] k super [k].pack('I') end @@ -132,25 +126,18 @@ module SBDB @db.put _txn(txn), "\0\0\0\0", v, Bdb::DB_APPEND end end + + class Recno < DB + extend Arrayisch + end Array = Recno TYPES[DB::RECNO] = Recno - class Queue < DB - def [] k - super [k].pack('I') - end - - def []= k, v - super [k].pack('I'), v - end - + class Queue < Arrayisch + extend Arrayisch def unshift txn = nil @db.get _txn(txn), "\0\0\0\0", nil, Bdb::DB_CONSUME end - - def push v, txn = nil - @db.put _txn(txn), "\0\0\0\0", v, Bdb::DB_APPEND - end end TYPES[DB::QUEUE] = Queue end diff --git a/lib/sbdb/environment.rb b/lib/sbdb/environment.rb index 2712b44..cb178ed 100644 --- a/lib/sbdb/environment.rb +++ b/lib/sbdb/environment.rb @@ -75,11 +75,7 @@ module SBDB def self.new *args obj = r = super( *args) begin r = yield obj - ensure - begin obj.close - rescue Object - $stderr.puts [$!.class,$!,$!.backtrace].inspect - end + ensure SBDB::raise_barrier obj.method(:close) end if block_given? r end @@ -112,9 +108,10 @@ module SBDB def [] file, *p, &e p.push ::Hash.new unless ::Hash === p.last p.last[:env] = self - name = String === p[0] ? p[0] : p.last[:name] - flags = Fixnum === p[1] ? p[1] : p.last[:flags] - type = Fixnum === p[2] ? p[2] : p.last[:type] + name, flags, type = + String === p[0] ? p[0] : p.last[:name], + Fixnum === p[1] ? p[1] : p.last[:flags], + Fixnum === p[2] ? p[2] : p.last[:type] @dbs[ [file, name, flags | CREATE]] ||= (type || SBDB::Unknown).new file, *p, &e end end