Forum Stats

  • 3,768,743 Users
  • 2,252,843 Discussions


Partial Key Retrieval?

444876 Member Posts: 21
edited Sep 20, 2006 12:33PM in Berkeley DB Java Edition

Can I use an override comparator to retrieve a record based on a partial key? The documentation says that the override comparator has to return records in the same order as the original (or default) comparator, so I guess I'm confused as to what good it is, unless it can be used with a partial key.


Andrew Bell
-- [email protected]


  • I'm not sure I am interpreting your question correctly, but I'll start out with some information on comparators, and see if that answers your question.

    Internally, JE databases are implemented as BTrees, and the default key comparator is a byte-by-byte lexicographic comparison. The key comparator is used in all btree operations, both to update and to retrieve from the btree. The DatabaseConfig.setBtreeComparator(), setDuplicateComparator() methods let you provide an alternate comparison method.

    If you change the comparator for an existing database, there's the potential for database corruption if you accidentally make the comparison behavior change, because existing data records will be in the wrong order. So we added the DatabaseConfig.setOverride{Btree,Duplicate}Comparator method, to make the configuration semantics very clear to JE code, and to make the application do something explicit when taking this step. You may have seen this already, but here's the page in the Getting Started Guide on comparators:

    There are two things that I think you might be thinking of when you refer to partial keys. If you are using the base api, with DatabaseEntry, you can specify the offset and size of the value you pass in as a key or data record. For example, suppose your database has these keys


    and you have a DatabaseEntry with the value "pineapple" that you are using as a key parameter. You could specify the size and offset of your DatabaseEntry so the value used by JE is just "apple", skipping the "pine". Or another aspect might be that you can do a "partial key" match when you use the range search operations. For example, if you do a range search with the value "pea" as the key, you'll get all the records starting at "'peach". But in both these cases, the partial key has more to do with what portion of a value is used in a operation, rather than the key comparison.

    Let us know if that doesn't answer your question.


This discussion has been closed.