From ff2e44404751a371aa5e16dc6f0cec43786de0ee Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Fri, 5 Feb 2010 19:08:30 +0100 Subject: [PATCH] SBDB::DB#[]=: If set to nil, it will delete item. SBDB::Cursor#each: Integer-keys for Recno and Queue. --- lib/sbdb/cursor.rb | 20 +++++++++++++------- lib/sbdb/db.rb | 15 ++++++++++++++- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/lib/sbdb/cursor.rb b/lib/sbdb/cursor.rb index acc7d3d..1a4a122 100644 --- a/lib/sbdb/cursor.rb +++ b/lib/sbdb/cursor.rb @@ -12,12 +12,12 @@ module SBDB include Enumerable def bdb_object() @cursor end def close() @cursor.close end - def get( k, v, f) @cursor.get k, v, f end + def get( k, v, f) @cursor.get( k, v, f) end def count() @cursor.count end - def first( k = nil, v = nil) @cursor.get k, v, FIRST end - def last( k = nil, v = nil) @cursor.get k, v, LAST end - def next( k = nil, v = nil) @cursor.get k, v, NEXT end - def prev( k = nil, v = nil) @cursor.get k, v, PREV end + def first( k = nil, v = nil) get k, v, FIRST end + def last( k = nil, v = nil) get k, v, LAST end + def next( k = nil, v = nil) get k, v, NEXT end + def prev( k = nil, v = nil) get k, v, PREV end def self.new *p x = super *p @@ -33,6 +33,12 @@ module SBDB when Bdb::Db::Cursor then [ref] else [ref.bdb_object.cursor( nil, 0), ref] end + if [Recno, Queue].any? {|t| t === @db } + def get k, v, f + l, w = @cursor.get( k, v, f) + l.nil? ? nil : [l.unpack('I')[0], w] + end + end end def reverse k = nil, v = nil, &e @@ -42,10 +48,10 @@ module SBDB def each k = nil, v = nil, f = nil, n = nil return Enumerator.new( self, :each, k, v, f, n) unless block_given? n ||= NEXT - e = @cursor.get k, v, f || FIRST + e = get k, v, f || FIRST return unless e yield *e - yield *e while e = @cursor.get( k, v, n) + yield *e while e = get( k, v, n) nil end end diff --git a/lib/sbdb/db.rb b/lib/sbdb/db.rb index b52963f..e0ba0ae 100644 --- a/lib/sbdb/db.rb +++ b/lib/sbdb/db.rb @@ -9,6 +9,8 @@ module SBDB QUEUE = Bdb::Db::QUEUE ARRAY = RECNO = Bdb::Db::RECNO RDONLY = READLONY = Bdb::DB_RDONLY + CONSUME = Bdb::DB_CONSUME + CONSUME_WAIT = Bdb::DB_CONSUME_WAIT attr_reader :home include Enumerable @@ -16,9 +18,16 @@ module SBDB def sync() @db.sync end def close( f = nil) @db.close f || 0 end def []( k) @db.get nil, k.nil? ? nil : k.to_s, nil, 0 end - def []=( k, v) @db.put nil, k.nil? ? nil : k.to_s, v.nil? ? nil : v.to_s, 0 end def cursor( &e) Cursor.new self, &e end + def []= k, v + if v.nil? + @db.del nil, k.to_s, 0 + else + @db.put nil, k.nil? ? nil : k.to_s, v.to_s, 0 + end + end + class << self def new *p, &e x = super *p @@ -112,5 +121,9 @@ module SBDB def []= k, v super [k].pack('I'), v end + + def unshift + get nil, nil, nil, Bdb::DB_CONSUME + end end end