6 Replies Latest reply: Jun 16, 2012 1:04 PM by 940938 RSS

    Duplicate Keys

    940938
      Hi,
      I'm using Oracle Berkeley DB Java Edition with tables having key/value format. I'm trying to insert duplicate keys, but keep getting SecondaryIntegrityException. According to Oracle, if the setSortedDuplicates() is set to true, then duplicates are allowed. This does not work in my case. Below is some code with key=bob, value=smith. The first I run it, it runs as expected. If I run it a second time changing only value=johnson, I get SecondaryIntegrityException. Is there something I'm doing wrong? Thanks.






      String key = "bob";
      String value = "smith";

      EnvironmentConfig envConfig = new EnvironmentConfig();
      envConfig.setAllowCreate(true);
      envConfig.setTransactional(false);
      Environment myDBenvironment = new Environment(new File(filePath), envConfig);


      DatabaseConfig dbConfig = new DatabaseConfig();
      dbConfig.setAllowCreate(true);
      dbConfig.setTransactional(false);
      Database myDatabase = myDBenvironment.openDatabase(null, dbname,
      dbConfig);

      // create secondary database
      SecondaryConfig mySecConfig = new SecondaryConfig();
      mySecConfig.setAllowCreate(true);
      mySecConfig.setSortedDuplicates(true);
      mySecConfig.setTransactional(false);
      mySecConfig.setKeyCreator(new SecondKeyCreator());
      SecondaryDatabase mySecondaryDatabase = myDBenvironment
      .openSecondaryDatabase(null, secdbname, myDatabase,
      mySecConfig);


      DatabaseEntry myKey = new DatabaseEntry(key.getBytes("UTF-8"));

      Record mydata = new Record();
      mydata.setobjectVal(value);
      DatabaseEntry myrecord = new DatabaseEntry();
      new RecordTupleBinding().objectToEntry(mydata, myrecord);
      myDatabase.put(null, myKey, myrecord);

      mySecondaryDatabase.close();
      myDatabase.close();
      myDBenvironment.close();







      public class SecondKeyCreator implements SecondaryKeyCreator{  

      @Override
      public boolean createSecondaryKey(SecondaryDatabase arg0,
      DatabaseEntry key, DatabaseEntry data, DatabaseEntry secondKey) {
      RecordTupleBinding binding = new RecordTupleBinding();
      Record record = (Record) binding.entryToObject(data);

      try {
      secondKey.setData(data.getData());
      } catch (Exception e) {
      e.printStackTrace();
      }

      return true;
      }

      }
        • 1. Re: Duplicate Keys
          Greybird-Oracle
          Hi,

          Please post the stack trace.

          --mark                                                                                                                                                                                                                   
          • 2. Re: Duplicate Keys
            940938
            Well, originally it was:

            Exception in thread "main" com.sleepycat.je.SecondaryIntegrityException: (JE 5.0.34) Secondary is corrupt: the primary record contains a key that is not present in the secondary
                 at com.sleepycat.je.SecondaryDatabase.deleteKey(SecondaryDatabase.java:937)
                 at com.sleepycat.je.SecondaryDatabase.updateSecondary(SecondaryDatabase.java:852)
                 at com.sleepycat.je.SecondaryTrigger.databaseUpdated(SecondaryTrigger.java:41)
                 at com.sleepycat.je.Database.notifyTriggers(Database.java:2132)
                 at com.sleepycat.je.Cursor.putNotify(Cursor.java:2124)
                 at com.sleepycat.je.Cursor.putNoDups(Cursor.java:2040)
                 at com.sleepycat.je.Cursor.putInternal(Cursor.java:2008)
                 at com.sleepycat.je.Database.putInternal(Database.java:1314)
                 at com.sleepycat.je.Database.put(Database.java:1184)
                 at Dim3.Test.main(Test.java:112)


            But I've been playing around and now all I get is "error" after executing the "myDatabase.put(null, myKey, myrecord);" when myKey is the first duplicate. I can't find any info on why I'm getting "error".
            • 3. Re: Duplicate Keys
              Greybird-Oracle
              I suspect you created the data record (in the primary DB) and then changed the key creator. I bet the error will go away if you delete the environment and start from scratch. The key creator looks incomplete as well. I would fix the key creator, then start with a fresh environment.

              --mark                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
              • 4. Re: Duplicate Keys
                940938
                What do you mean my key creator looks incomplete? Should I be doing something different? I deleted everything and started from scratch.
                • 5. Re: Duplicate Keys
                  Greybird-Oracle
                  public boolean createSecondaryKey(SecondaryDatabase arg0,
                  DatabaseEntry key, DatabaseEntry data, DatabaseEntry secondKey) {
                  RecordTupleBinding binding = new RecordTupleBinding();
                  Record record = (Record) binding.entryToObject(data);

                  try {
                  secondKey.setData(data.getData());
                  } catch (Exception e) {
                  e.printStackTrace();
                  }
                  >

                  You're setting the sec key to the entire pri key data, and you're not using the record object you deserialized. It looks like you changed it during debugging?

                  --mark                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
                  • 6. Re: Duplicate Keys
                    940938
                    Thanks, it's working now.