views:

710

answers:

2

I'd like to be able to get all the key-values stored in a Berkeley DB using the Ruby bindings from http://github.com/mattbauer/bdb/tree/master but I'm not sure how to proceed. Any pointers will be appreciated.

UPDATE

Here's a small script that loops over the keys and prints them. Based on Pax' answer:

require 'rubygems'
require 'bdb'

env = Bdb::Env.new(0)
env.open('foo', Bdb::DB_CREATE,0)

db = env.db
db.open(nil, 'db1.db', nil, Bdb::Db::BTREE, Bdb::DB_CREATE,0)

db.put(nil, 'key',  'value',  0)
db.put(nil, 'key1', 'value1', 0)
db.put(nil, 'key2', 'value2', 0)

dbc = db.cursor(nil,0)
key,val = dbc.get(nil,nil,Bdb::DB_FIRST)
while key
  p key,val
  key,val = dbc.get(nil,nil,Bdb::DB_NEXT)
end
dbc.close
db.close(0)
env.close
+1  A: 

You need to use cursors in Berkeley DB to run through the entire key/value space.

In Berkeley DB itself, you would create a cursor then use it with the DB_FIRST flag followed by multiple invocations with the DB_NEXT flag until you run out of key/value pairs. You can simplify the code by using only DB_NEXT since, if you do that to a newly created cursor, it's the same as using DB_FIRST.

With the Ruby bindings, this appears to be done with (based on my very rudimentary knowledge of Ruby - you should be able to clean this up):

dbc = db.cursor(nil,0)
key,val = dbc.get(nil,nil,Bdb::DB_FIRST)
while key != nil do
   # Process key and val as needed.
   key,val = dbc.get(nil,nil,Bdb::DB_NEXT)
   # or possibly .. (key,val,Bdb::DB_NEXT)
end
dbc.close()
paxdiablo
A: 

This works for me:

require 'bdb'
db=BDB::Hash.open("test.db")
keyvalues=db.to_hash
Jonas Elfström