1 Reply Latest reply on Jun 20, 2013 5:43 AM by Bogdan Coman-Oracle

    Key Index





      Is it possible to get the index of the key I inserted in berkeley? I need to know the arrangement of the keys I inserted. Are there methods in getting the index of the keys?



      Example: I put the following in the order given:



      Order Key Index

      1st key1 0

      2nd key2 1

      3rd key3 2




      If I will get the index of key2, it should return 2.




      Thank you in advance.

        • 1. Re: Key Index
          Bogdan Coman-Oracle


          I will clarify a few things about how JE's storing the records to make sure we are on the same page. Also, I don't know what API you use, I assume you use the base API.


          JE stores and retrieves data using key-data pairs. You store records in a Database by calling one of the put methods on a Database handle. JE automatically determines the record's proper placement in the database's internal B-Tree using whatever key and data comparison functions that are available to it. To iterate (which I think it's what you're looking for), just call getNext() until the last database record has been read. getFirst moves the cursor to the first key/data pair of the database, and returns that pair, but if If the cursor is not yet initialized, getNext moves the cursor to the first key/data pair of the database too. All cursor operations return an OperationStatus, so just read until you no longer see OperationStatus.SUCCESS. This will give you the order.

          Also, instead of scanning the database in the record's sort order, you can use cursors to retrieve records based on how they are stored on disk by using the DiskOrderedCursor class.


          Getting Records Using the Cursor: http://docs.oracle.com/cd/E17277_02/html/GettingStartedGuide/Positioning.html

          Cursor: http://docs.oracle.com/cd/E17277_02/html/java/com/sleepycat/je/Cursor.html

          DiskOrderedCursor: http://docs.oracle.com/cd/E17277_02/html/java/com/sleepycat/je/DiskOrderedCursor.html