From 7ffae260c3aab56574dd2b54737ea20302e3f349 Mon Sep 17 00:00:00 2001 From: Justin Balthrop Date: Thu, 6 Aug 2009 17:13:07 -0700 Subject: [PATCH] add support for ranges to bdb/simple --- lib/bdb/simple.rb | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/bdb/simple.rb b/lib/bdb/simple.rb index 01f7dc3..1b2ca9d 100644 --- a/lib/bdb/simple.rb +++ b/lib/bdb/simple.rb @@ -30,21 +30,33 @@ class Bdb::Simple db[Marshal.dump(key)] = Marshal.dump(value) end - def [](key) - key = Marshal.dump(key) - - if dup? + def [](key) + if key.kind_of?(Range) values = [] cursor = db.cursor(nil, 0) - data = cursor.get(key, nil, Bdb::DB_SET) - while data - values << Marshal.load(data[1]) - data = cursor.get(nil, nil, Bdb::DB_NEXT_DUP) + k,v = cursor.get(Marshal.dump(key.first), nil, Bdb::DB_SET_RANGE) + while k and key.include?(Marshal.load(k)) + values << Marshal.load(v) + k, v = cursor.get(nil, nil, Bdb::DB_NEXT) end cursor.close values 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