Question

I've created a database using Berkley DB that stores N records where a record is a key/value pair. I originally populated it with only 20 records. With 20 records I managed to do a Key Search, and a Data Search (where I search through the database record by record for a data value that matches the string data inputted by the user).

public String dataSearch (String dataInput) {
    String foundKey = null;
    String foundData = null;
    Cursor cursor = null;
    try {
        cursor = myDb.openCursor(null, null);
        DatabaseEntry theKey = new DatabaseEntry();
        DatabaseEntry theData = new DatabaseEntry();
        while (cursor.getNext(theKey, theData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
            foundKey = new String(theKey.getData(), "UTF-8");
            foundData = new String(theData.getData(), "UTF-8");
                // this is to see each key - data - inputdata as I was having an issue
            System.out.println("KEY: " + foundKey + 
                    "\nDATA: " + foundData +
                    "\nINPUT_DATA: " + dataInput + "\n\n");

            if (foundData.equals(dataInput)) {
                System.out.println("-----------------------------------\n\n");
                System.out.println("Found record: " + foundKey +
                            "\nwith data: " + foundData);
                System.out.println("\n\n-----------------------------------");
            }
        }

 /* I then close the cursor and catch exceptions and such */

this works fine when I have less than (or equal to) 20 records... but when I use a bigger number I seem to have some funny behaviour. I set the number of records to 1000... the last key/data values to be inserted into the database are:

KEY: zghxnbujnsztazmnrmrlhjsjfeexohxqotjafliiktlptsquncuejcrebaohblfsqazznheurdqbqbxjmyqr

DATA: jzpqaymwwnoqzvxykowdhxvfbuhrsfojivugrmvmybbvurxmdvmrclalzfscmeknyzkqmrcflzdooyupwznvxikermrbicapynwspbbritjyeltywmmslpeuzsmh

I had it print out the last values to be inserted into the database then did a key search on the above key to ensure that the data above was infact the data associated with that key in the database. However, when I do a data search on the data listed above I get no found matching record (whereas the same process found a record when there was 20 records). I looked into it a bit more and got each my data search to print each key/data pair that it returned and found the following result:

KEY: zghxnbujnsztazmnrmrlhjsjfeexohxqotjafliiktlptsquncuejcrebaohblfsqazznheurdqbqbxjmyqrpzlyvnmdlvgyvzhbceeftcqssbeckxkuepxyphsgdzd
DATA: jzpqaymwwnoqzvxykowdhxvfbuhrsfojivugrmvmybbvurxmdvmrclalzfscmeknyzkqmrcflzdooyupwznvxikermrbicapynwspbbritjyeltywmmslpeuzsmhozy
INPUT DATA: jzpqaymwwnoqzvxykowdhxvfbuhrsfojivugrmvmybbvurxmdvmrclalzfscmeknyzkqmrcflzdooyupwznvxikermrbicapynwspbbritjyeltywmmslpeuzsmh

as you can see it seems to have randomly appended some extra bytes to the data value. however if I do a key search these extra bytes don't show up. So I think the problem is in the dataSearch function. The same results occur if I use b+tree or hash.

Any Ideas?

Thanks

Was it helpful?

Solution

After a long time looking at this I realized my error was that I was not reinitializing the theKey & theData variables.

the fix is in the while loop

       while (cursor.getNext(theKey, theData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
            foundKey = new String(theKey.getData(), "UTF-8");
            foundData = new String(theData.getData(), "UTF-8");
                // this is to see each key - data - inputdata as I was having an issue
            System.out.println("KEY: " + foundKey + 
                    "\nDATA: " + foundData +
                    "\nINPUT_DATA: " + dataInput + "\n\n");

            if (foundData.equals(dataInput)) {
                System.out.println("-----------------------------------\n\n");
                System.out.println("Found record: " + foundKey +
                            "\nwith data: " + foundData);
                System.out.println("\n\n-----------------------------------");
            }
            // THIS IS THE FIX 
            theKey = new DatabaseEntry();
            theData = new DatabaseEntry();
            // ----------------------------

        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top