add support for ranges to bdb/simple

This commit is contained in:
Justin Balthrop 2009-08-06 17:13:07 -07:00
parent 36cd2b36e3
commit 7ffae260c3

View file

@ -30,21 +30,33 @@ class Bdb::Simple
db[Marshal.dump(key)] = Marshal.dump(value) db[Marshal.dump(key)] = Marshal.dump(value)
end end
def [](key) def [](key)
key = Marshal.dump(key) if key.kind_of?(Range)
if dup?
values = [] values = []
cursor = db.cursor(nil, 0) cursor = db.cursor(nil, 0)
data = cursor.get(key, nil, Bdb::DB_SET) k,v = cursor.get(Marshal.dump(key.first), nil, Bdb::DB_SET_RANGE)
while data while k and key.include?(Marshal.load(k))
values << Marshal.load(data[1]) values << Marshal.load(v)
data = cursor.get(nil, nil, Bdb::DB_NEXT_DUP) k, v = cursor.get(nil, nil, Bdb::DB_NEXT)
end end
cursor.close cursor.close
values values
else else
Marshal.load(db.get(nil, key, nil, 0)) key = Marshal.dump(key)
if dup?
values = []
cursor = db.cursor(nil, 0)
k,v = cursor.get(key, nil, Bdb::DB_SET)
while data
values << Marshal.load(v)
k,v = cursor.get(nil, nil, Bdb::DB_NEXT_DUP)
end
cursor.close
values
else
v = db.get(nil, key, nil, 0)
Marshal.load(v) if v
end
end end
end end