Is there any way with berkeley db 5.x C API to get
all the records with a key matching a prefix ?
(without scanning the entire database)
Maybe with a Btree prefix comparison custom callback ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are two parts to the solution. First, you need to use a custom key comparison function.
In the comparison function, make it so that the keys with the same prefix are sorted next to each other in the database. This might be as simple as a bunch of less-than compares, so that something like
0x000000000000000000000001is less than0x000000000000000000000002.To do the search, open a cursor and do a
cursor->get()with aDB_SET_RANGEflag. Set your starting key to be at the beginning of the prefix range. Say, if you were looking for keys with the prefix0x1138, you’d set the key to0x113800000000000000000000. Then keep callingcursor->get()withDB_NEXT. When you see a key that doesn’t have the0x1138prefix, you’re done.This technique does not have to scan the entire database, as the initial
DB_SET_RANGEcall starts the cursor in the right place, and the records are all right next to each other in the tree.