4 Replies Latest reply: Feb 24, 2009 2:17 PM by greybird RSS

    Javadoc clarification: SecondaryCursor.dup() fails if not initialized

    679075
      Problem: the API Javadoc for Cursor.dup() and SecondaryCursor.dup() are basically the same. However, they behave very differently, as shown by the output of the test program below: Cursor.dup(false) succeeds when the cursor is not initialized, while SecondaryCursor.dup(false) throws an exception.

      Please fix the Javadoc for SecondaryCursor.dup() to clarify when an exception will be thrown.

      Program output:
      Testing Cursor.dup(false) on uninitialized cursor: SUCCESS
      Testing SecondaryCursor.dup(false) on uninitialized cursor: FAIL: (JE 3.3.75) Cursor Not Initialized.
      Test program:
      import com.sleepycat.je.*;
      import java.io.File;
      
      public class cursortest {
          public static void main(String[] args) throws Exception {
      
              // Create temp dir
              File dir = new File("/tmp/cursortest");
              if (!dir.mkdir())
                  throw new Exception("can't create directory: " + dir);
      
              // Create environment
              EnvironmentConfig envConfig = new EnvironmentConfig();
              envConfig.setAllowCreate(true);
              envConfig.setTransactional(true);
              Environment env = new Environment(dir, envConfig);
      
              // Open primary db
              DatabaseConfig dbConfig = new DatabaseConfig();
              dbConfig.setAllowCreate(true);
              dbConfig.setTransactional(true);
              Database db = env.openDatabase(null, "primary", dbConfig);
      
              // Open secondary db
              SecondaryConfig sdbConfig = new SecondaryConfig();
              sdbConfig.setAllowCreate(true);
              sdbConfig.setAllowPopulate(true);
              sdbConfig.setTransactional(true);
              sdbConfig.setKeyCreator(new SecondaryKeyCreator() {
                  public boolean createSecondaryKey(SecondaryDatabase sdb,
                    DatabaseEntry key, DatabaseEntry data, DatabaseEntry result) {
                      return false;
                  }
              });
              SecondaryDatabase sdb = env.openSecondaryDatabase(null, "secondary", db, sdbConfig);
      
              // Test primary cursor
              Cursor cursor = db.openCursor(null, new CursorConfig());
              testDup(cursor);
              cursor.close();
      
              // Test secondary cursor
              cursor = sdb.openSecondaryCursor(null, new CursorConfig());
              testDup(cursor);
              cursor.close();
          }
      
          public static void testDup(Cursor cursor) throws DatabaseException {
              System.out.print("Testing " + cursor.getClass().getSimpleName() + ".dup(false) on uninitialized cursor: ");
              Cursor dup = null;
              try {
                  dup = cursor.dup(false);
                  System.out.println("SUCCESS");
              } catch (DatabaseException e) {
                  System.out.println("FAIL: " + e.getMessage());
              }
              if (dup != null)
                  dup.close();
          }
      }
      Added better code formatting.
        • 1. Re: Javadoc clarification: SecondaryCursor.dup() fails if not initialized
          679075
          On second thought, this really seems like a functional bug rather than a documentation bug. Why shouldn't SecondaryCursor.dup(false) succeed when the cursor is not initialized?
          • 2. Re: Javadoc clarification: SecondaryCursor.dup() fails if not initialized
            679075
            Patch to fix.... ?
            --- bdb/com/sleepycat/je/SecondaryCursor.java   2008-06-10 10:52:08.000000000 -0500
            +++ src/java/com/sleepycat/je/SecondaryCursor.java      2009-02-24 12:11:56.531382458 -0600
            @@ -117,7 +117,7 @@
                 public Cursor dup(boolean samePosition)
                     throws DatabaseException {
            
            -        checkState(true);
            +        checkState(false);
                     return new SecondaryCursor(this, samePosition);
                 }
            Edited by: archie172 on Feb 24, 2009 10:53 AM (added better code formatting)
            • 3. Re: Javadoc clarification: SecondaryCursor.dup() fails if not initialized
              524761
              archie172 wrote:

              P.S. when is this forum going to add a pre-formatted (fixed-width) text option?
              You mean like this?
              diff -r 671c95c08dd4 src/com/sleepycat/je/SecondaryCursor.java
              --- a/src/com/sleepycat/je/SecondaryCursor.java
              +++ b/src/com/sleepycat/je/SecondaryCursor.java
              @@ -117,7 +117,7 @@
                   public Cursor dup(boolean samePosition)
                       throws DatabaseException {
               
              -        checkState(true);
              +        checkState(false);
                       return new SecondaryCursor(this, samePosition);
                   }
               
              Please see the forum FAQ.
              • 4. Re: Javadoc clarification: SecondaryCursor.dup() fails if not initialized
                greybird
                Archie,

                Yes and yes -- this is a bug and that is the correct fix.

                Thanks!! We'll fix this in the next patch release.

                --mark