[pybsddb] Cursor form of pget() for secondary indicies with duplicates?

denis.papathanasiou at gmail.com denis.papathanasiou at gmail.com
Tue Dec 2 18:46:18 CET 2008


I've got a simple hash saved as a Berkeley DB file.

The keys are: ['a', 'b', '0', 'c', '1', 'd', '2'] and the values are:
['letter', 'letter', 'number', 'letter', 'number', 'letter', 'number'].

I created the db with a secondary index, so that I can lookup by value,
e.g. call pget('letter') and get a list of ['a', 'b', 'c', 'd'] as the
result.

Except, it doesn't work; in that example, pget('letter') returns just 'a'.

Then I read in the Oracle docs that secondary indices with duplicates
need to use the cursor form of pget().

But when I tried that, I got back the entire db.

Any suggestions?

Or are there examples of using the cursor form of pget() in bsddb3 I
could look at?

Here's my code:

from bsddb3 import db

pdb_object = db.DB()
pdb_object.open("alphanumeric.db", None, db.DB_BTREE, db.DB_CREATE)
sdb_object = db.DB()
sdb_object.set_flags(db.DB_DUP | db.DB_DUPSORT)
sdb_object.open("alphanumeric_secondary.db", None, db.DB_BTREE,
db.DB_CREATE)
pdb_object.associate(sdb_object, (lambda primary_key, primary_data:
primary_data))

pdb_object['a'] = 'letter'
pdb_object['b'] = 'letter'
pdb_object['0'] = 'number'
pdb_object['c'] = 'letter'
pdb_object['1'] = 'number'
pdb_object['d'] = 'letter'
pdb_object['2'] = 'number'

# call pget():

>>> sdb_object.pget('letter')
('a', 'letter')
# returns just one value, not all of them...


# call pget() with a cursor:

cur = sdb_object.cursor()
rec = cur.pget('letter', db.DB_FIRST)
>>> rec
('letter', 'a', 'letter')
# so far so good...

>>> while rec:
...   rec = cur.pget('letter', db.DB_NEXT)
...   rec
...
('letter', 'b', 'letter')
('letter', 'c', 'letter')
('letter', 'd', 'letter')
('number', '0', 'number')
('number', '1', 'number')
('number', '2', 'number')

# why were 'number' types returned? It looks like the pget() key was
ignored...




More information about the pybsddb mailing list