Example #1
0
  public void testPutAndDelete() throws DatabaseException {

    SecondaryDatabase secDb = initDb();
    Database priDb = secDb.getPrimaryDatabase();

    DatabaseEntry data = new DatabaseEntry();
    DatabaseEntry key = new DatabaseEntry();
    OperationStatus status;
    Transaction txn = txnBegin();

    /* Database.put() */
    status = priDb.put(txn, entry(1), entry(2));
    assertSame(OperationStatus.SUCCESS, status);
    status = secDb.get(txn, entry(102), key, data, LockMode.DEFAULT);
    assertSame(OperationStatus.SUCCESS, status);
    assertDataEquals(entry(1), key);
    assertDataEquals(entry(2), data);

    /* Database.putNoOverwrite() */
    status = priDb.putNoOverwrite(txn, entry(1), entry(1));
    assertSame(OperationStatus.KEYEXIST, status);
    status = secDb.get(txn, entry(102), key, data, LockMode.DEFAULT);
    assertSame(OperationStatus.SUCCESS, status);
    assertDataEquals(entry(1), key);
    assertDataEquals(entry(2), data);

    /* Database.put() overwrite */
    status = priDb.put(txn, entry(1), entry(3));
    assertSame(OperationStatus.SUCCESS, status);
    status = secDb.get(txn, entry(102), key, data, LockMode.DEFAULT);
    assertSame(OperationStatus.NOTFOUND, status);
    status = secDb.get(txn, entry(103), key, data, LockMode.DEFAULT);
    assertSame(OperationStatus.SUCCESS, status);
    assertDataEquals(entry(1), key);
    assertDataEquals(entry(3), data);

    /* Database.delete() */
    status = priDb.delete(txn, entry(1));
    assertSame(OperationStatus.SUCCESS, status);
    status = priDb.delete(txn, entry(1));
    assertSame(OperationStatus.NOTFOUND, status);
    status = secDb.get(txn, entry(103), key, data, LockMode.DEFAULT);
    assertSame(OperationStatus.NOTFOUND, status);

    /* SecondaryDatabase.delete() */
    status = priDb.put(txn, entry(1), entry(1));
    assertSame(OperationStatus.SUCCESS, status);
    status = priDb.put(txn, entry(2), entry(1));
    assertSame(OperationStatus.SUCCESS, status);
    status = secDb.get(txn, entry(101), key, data, LockMode.DEFAULT);
    assertSame(OperationStatus.SUCCESS, status);
    assertDataEquals(entry(1), key);
    assertDataEquals(entry(1), data);
    status = secDb.delete(txn, entry(101));
    assertSame(OperationStatus.SUCCESS, status);
    status = secDb.delete(txn, entry(101));
    assertSame(OperationStatus.NOTFOUND, status);
    status = secDb.get(txn, entry(101), key, data, LockMode.DEFAULT);
    assertSame(OperationStatus.NOTFOUND, status);
    status = priDb.get(txn, entry(1), data, LockMode.DEFAULT);
    assertSame(OperationStatus.NOTFOUND, status);
    status = priDb.get(txn, entry(2), data, LockMode.DEFAULT);
    assertSame(OperationStatus.NOTFOUND, status);

    /*
     * Database.putNoDupData() cannot be called since the primary cannot be
     * configured for duplicates.
     */

    /* Primary and secondary are empty now. */

    /* Get a txn for a cursor. */
    txnCommit(txn);
    txn = txnBeginCursor();

    Cursor priCursor = null;
    SecondaryCursor secCursor = null;
    try {
      priCursor = priDb.openCursor(txn, null);
      secCursor = secDb.openSecondaryCursor(txn, null);

      /* Cursor.putNoOverwrite() */
      status = priCursor.putNoOverwrite(entry(1), entry(2));
      assertSame(OperationStatus.SUCCESS, status);
      status = secCursor.getSearchKey(entry(102), key, data, LockMode.DEFAULT);
      assertSame(OperationStatus.SUCCESS, status);
      assertDataEquals(entry(1), key);
      assertDataEquals(entry(2), data);

      /* Cursor.putCurrent() */
      status = priCursor.putCurrent(entry(3));
      assertSame(OperationStatus.SUCCESS, status);
      status = secCursor.getSearchKey(entry(102), key, data, LockMode.DEFAULT);
      assertSame(OperationStatus.NOTFOUND, status);
      status = secCursor.getSearchKey(entry(103), key, data, LockMode.DEFAULT);
      assertSame(OperationStatus.SUCCESS, status);
      assertDataEquals(entry(1), key);
      assertDataEquals(entry(3), data);

      /* Cursor.delete() */
      status = priCursor.delete();
      assertSame(OperationStatus.SUCCESS, status);
      status = priCursor.delete();
      assertSame(OperationStatus.KEYEMPTY, status);
      status = secCursor.getSearchKey(entry(103), key, data, LockMode.DEFAULT);
      assertSame(OperationStatus.NOTFOUND, status);
      status = priCursor.getSearchKey(entry(1), data, LockMode.DEFAULT);
      assertSame(OperationStatus.NOTFOUND, status);

      /* Cursor.put() */
      status = priCursor.put(entry(1), entry(4));
      assertSame(OperationStatus.SUCCESS, status);
      status = secCursor.getSearchKey(entry(104), key, data, LockMode.DEFAULT);
      assertSame(OperationStatus.SUCCESS, status);
      assertDataEquals(entry(1), key);
      assertDataEquals(entry(4), data);

      /* SecondaryCursor.delete() */
      status = secCursor.delete();
      assertSame(OperationStatus.SUCCESS, status);
      status = secCursor.delete();
      assertSame(OperationStatus.KEYEMPTY, status);
      status = secCursor.getCurrent(new DatabaseEntry(), key, data, LockMode.DEFAULT);
      assertSame(OperationStatus.KEYEMPTY, status);
      status = secCursor.getSearchKey(entry(104), key, data, LockMode.DEFAULT);
      assertSame(OperationStatus.NOTFOUND, status);
      status = priCursor.getSearchKey(entry(1), data, LockMode.DEFAULT);
      assertSame(OperationStatus.NOTFOUND, status);

      /*
       * Cursor.putNoDupData() cannot be called since the primary cannot
       * be configured for duplicates.
       */

      /* Primary and secondary are empty now. */
    } finally {
      if (secCursor != null) {
        secCursor.close();
      }
      if (priCursor != null) {
        priCursor.close();
      }
    }

    txnCommit(txn);
    secDb.close();
    priDb.close();
  }
Example #2
0
  /**
   * Test that an exception is thrown when a cursor is used in the wrong state. No put or get is
   * allowed in the closed state, and certain gets and puts are not allowed in the uninitialized
   * state.
   */
  public void testCursorState() throws DatabaseException {

    SecondaryDatabase secDb = initDb();
    Database priDb = secDb.getPrimaryDatabase();
    Transaction txn = txnBegin();

    DatabaseEntry key = entry(0);
    DatabaseEntry data = entry(0);
    DatabaseEntry secKey = entry(KEY_OFFSET);
    DatabaseEntry found = new DatabaseEntry();
    DatabaseEntry found2 = new DatabaseEntry();

    assertEquals(OperationStatus.SUCCESS, priDb.put(txn, key, data));

    txnCommit(txn);
    txn = txnBeginCursor();
    Cursor cursor = priDb.openCursor(txn, null);
    SecondaryCursor secCursor = secDb.openSecondaryCursor(txn, null);

    /* Check the uninitialized state for certain operations. */

    try {
      cursor.count();
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.delete();
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.putCurrent(data);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.getCurrent(key, data, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.getNextDup(found, found2, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.getPrevDup(found, found2, null);
      fail();
    } catch (DatabaseException expected) {
    }

    try {
      secCursor.count();
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      secCursor.delete();
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      secCursor.getCurrent(key, data, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      secCursor.getNextDup(found, found2, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      secCursor.getPrevDup(found, found2, null);
      fail();
    } catch (DatabaseException expected) {
    }

    /* Initialize, then close, then check all operations. */

    assertEquals(OperationStatus.SUCCESS, cursor.getSearchKey(key, found, null));
    assertEquals(OperationStatus.SUCCESS, secCursor.getSearchKey(secKey, found, null));
    secCursor.close();
    cursor.close();

    try {
      cursor.close();
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.count();
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.delete();
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.put(key, data);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.putNoOverwrite(key, data);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.putNoDupData(key, data);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.putCurrent(data);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.getCurrent(key, data, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.getSearchKey(key, found, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.getSearchBoth(key, data, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.getSearchKeyRange(key, found, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.getSearchBothRange(key, data, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.getFirst(found, found2, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.getNext(found, found2, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.getPrev(found, found2, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.getNextDup(found, found2, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.getPrevDup(found, found2, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.getNextNoDup(found, found2, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.getPrevNoDup(found, found2, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      cursor.getLast(found, found2, null);
      fail();
    } catch (DatabaseException expected) {
    }

    try {
      secCursor.close();
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      secCursor.count();
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      secCursor.delete();
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      secCursor.getCurrent(key, data, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      secCursor.getSearchKey(secKey, found, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      secCursor.getSearchKeyRange(secKey, found, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      secCursor.getFirst(found, found2, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      secCursor.getNext(found, found2, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      secCursor.getPrev(found, found2, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      secCursor.getNextDup(found, found2, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      secCursor.getPrevDup(found, found2, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      secCursor.getNextNoDup(found, found2, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      secCursor.getPrevNoDup(found, found2, null);
      fail();
    } catch (DatabaseException expected) {
    }
    try {
      secCursor.getLast(found, found2, null);
      fail();
    } catch (DatabaseException expected) {
    }

    txnCommit(txn);
    secDb.close();
    priDb.close();
    env.close();
    env = null;
  }