Search code examples
databasecursorberkeley-db

How Does Berkley DB Cursor Iterate Records


I'm using BDB cursor and calling getSaerchKeyRange. From the docs

getSaerchKeyRange: returns the smallest key greater than or equal to the specified key

Lets say our DB is the following

Key     Value
1       a
2       b
3       c
4       d
5       e

After running cursor.getSearchKeyRange(6) does the cursor

  1. point to 5
  2. one record past 5 (which is null? because there are no more records)
  3. stay where it was (if this was the first search then its null?)

So far I've tried reading multiple sources of documentation but the answer isn't clear to me.


Solution

  • It returns "not found". The way it returns "not found" depends on which programming language you're using. If you're using Java, it returns OperationStatus.NOTFOUND.

    In your example, there are no keys in the database that are greater than or equal to your search key of 6. This line in the documentation describes the behavior:

    The returned key/data pair is for the smallest key greater than or equal to the specified key (as determined by the key comparison function), permitting partial key matches and range searches. source: Oracle Sleepycat Docs

    To round this out, expand on your example by adding some more larger keys:

    Key     Value
    1       a
    2       b
    3       c
    4       d
    5       e
    37      f
    52      g
    

    Now, if you do a getSearchKeyRange(6), it returns key 37. That's the first key that's greater than or equal to 6.