Exemplo n.º 1
0
  /** @deprecated use of Database.truncate */
  public void testOperationsNotAllowed() throws DatabaseException {

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

    /* Open secondary without a key creator. */
    try {
      env.openSecondaryDatabase(txn, "xxx", priDb, null);
      fail();
    } catch (NullPointerException expected) {
    }
    try {
      env.openSecondaryDatabase(txn, "xxx", priDb, new SecondaryConfig());
      fail();
    } catch (NullPointerException expected) {
    }

    /* Open secondary with both single and multi key creators. */
    SecondaryConfig config = new SecondaryConfig();
    config.setKeyCreator(new MyKeyCreator());
    config.setMultiKeyCreator(new SimpleMultiKeyCreator(new MyKeyCreator()));
    try {
      env.openSecondaryDatabase(txn, "xxx", priDb, config);
      fail();
    } catch (IllegalArgumentException expected) {
    }

    /* Database operations. */

    DatabaseEntry key = entry(1);
    DatabaseEntry data = entry(2);

    try {
      secDb.getSearchBoth(txn, key, data, LockMode.DEFAULT);
      fail();
    } catch (UnsupportedOperationException expected) {
    }

    try {
      secDb.put(txn, key, data);
      fail();
    } catch (UnsupportedOperationException expected) {
    }

    try {
      secDb.putNoOverwrite(txn, key, data);
      fail();
    } catch (UnsupportedOperationException expected) {
    }

    try {
      secDb.putNoDupData(txn, key, data);
      fail();
    } catch (UnsupportedOperationException expected) {
    }

    try {
      secDb.truncate(txn, true);
      fail();
    } catch (UnsupportedOperationException expected) {
    }

    try {
      secDb.join(new Cursor[0], null);
      fail();
    } catch (UnsupportedOperationException expected) {
    }

    /* Cursor operations. */

    txnCommit(txn);
    txn = txnBeginCursor();

    SecondaryCursor cursor = null;
    try {
      cursor = secDb.openSecondaryCursor(txn, null);

      try {
        cursor.getSearchBoth(key, data, LockMode.DEFAULT);
        fail();
      } catch (UnsupportedOperationException expected) {
      }

      try {
        cursor.getSearchBothRange(key, data, LockMode.DEFAULT);
        fail();
      } catch (UnsupportedOperationException expected) {
      }

      try {
        cursor.putCurrent(data);
        fail();
      } catch (UnsupportedOperationException expected) {
      }

      try {
        cursor.put(key, data);
        fail();
      } catch (UnsupportedOperationException expected) {
      }

      try {
        cursor.putNoOverwrite(key, data);
        fail();
      } catch (UnsupportedOperationException expected) {
      }

      try {
        cursor.putNoDupData(key, data);
        fail();
      } catch (UnsupportedOperationException expected) {
      }
    } finally {
      if (cursor != null) {
        cursor.close();
      }
    }

    txnCommit(txn);
    secDb.close();
    priDb.close();

    /* Primary with duplicates. */
    priDb = openDatabase(true, "testDBWithDups", false);
    try {
      openSecondary(priDb, true, "testSecDB", false, false);
      fail();
    } catch (IllegalArgumentException expected) {
    }

    priDb.close();

    /* Single secondary with two primaries.*/
    Database pri1 = openDatabase(false, "pri1", false);
    Database pri2 = openDatabase(false, "pri2", false);
    Database sec1 = openSecondary(pri1, false, "sec", false, false);
    try {
      openSecondary(pri2, false, "sec", false, false);
      fail();
    } catch (IllegalArgumentException expected) {
    }
    sec1.close();
    pri1.close();
    pri2.close();
  }
Exemplo n.º 2
0
  /** Test that null can be passed for the LockMode to all get methods. */
  public void testNullLockMode() 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();
    DatabaseEntry found3 = new DatabaseEntry();

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

    /* Database operations. */

    assertEquals(OperationStatus.SUCCESS, priDb.get(txn, key, found, null));
    assertEquals(OperationStatus.SUCCESS, priDb.getSearchBoth(txn, key, data, null));
    assertEquals(OperationStatus.SUCCESS, secDb.get(txn, secKey, found, null));
    assertEquals(OperationStatus.SUCCESS, secDb.get(txn, secKey, found, found2, null));
    assertEquals(OperationStatus.SUCCESS, secDb.getSearchBoth(txn, secKey, key, found, null));

    /* Cursor operations. */

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

    assertEquals(OperationStatus.SUCCESS, cursor.getSearchKey(key, found, null));
    assertEquals(OperationStatus.SUCCESS, cursor.getSearchBoth(key, data, null));
    assertEquals(OperationStatus.SUCCESS, cursor.getSearchKeyRange(key, found, null));
    assertEquals(OperationStatus.SUCCESS, cursor.getSearchBothRange(key, data, null));
    assertEquals(OperationStatus.SUCCESS, cursor.getFirst(found, found2, null));
    assertEquals(OperationStatus.SUCCESS, cursor.getNext(found, found2, null));
    assertEquals(OperationStatus.SUCCESS, cursor.getPrev(found, found2, null));
    assertEquals(OperationStatus.NOTFOUND, cursor.getNextDup(found, found2, null));
    assertEquals(OperationStatus.NOTFOUND, cursor.getPrevDup(found, found2, null));
    assertEquals(OperationStatus.SUCCESS, cursor.getNextNoDup(found, found2, null));
    assertEquals(OperationStatus.SUCCESS, cursor.getPrevNoDup(found, found2, null));
    assertEquals(OperationStatus.SUCCESS, cursor.getLast(found, found2, null));

    assertEquals(OperationStatus.SUCCESS, secCursor.getSearchKey(secKey, found, null));
    assertEquals(OperationStatus.SUCCESS, secCursor.getSearchKeyRange(secKey, found, null));
    assertEquals(OperationStatus.SUCCESS, secCursor.getFirst(found, found2, null));
    assertEquals(OperationStatus.SUCCESS, secCursor.getNext(found, found2, null));
    assertEquals(OperationStatus.SUCCESS, secCursor.getPrev(found, found2, null));
    assertEquals(OperationStatus.SUCCESS, secCursor.getNextDup(found, found2, null));
    assertEquals(OperationStatus.SUCCESS, secCursor.getPrevDup(found, found2, null));
    assertEquals(OperationStatus.SUCCESS, secCursor.getNextNoDup(found, found2, null));
    assertEquals(OperationStatus.SUCCESS, secCursor.getPrevNoDup(found, found2, null));
    assertEquals(OperationStatus.SUCCESS, secCursor.getLast(found, found2, null));

    assertEquals(OperationStatus.SUCCESS, secCursor.getSearchKey(secKey, found, found2, null));
    assertEquals(OperationStatus.SUCCESS, secCursor.getSearchBoth(secKey, data, found, null));
    assertEquals(OperationStatus.SUCCESS, secCursor.getSearchKeyRange(secKey, found, found2, null));
    assertEquals(OperationStatus.SUCCESS, secCursor.getSearchBothRange(secKey, data, found, null));
    assertEquals(OperationStatus.SUCCESS, secCursor.getFirst(found, found2, found3, null));
    assertEquals(OperationStatus.SUCCESS, secCursor.getNext(found, found2, found3, null));
    assertEquals(OperationStatus.SUCCESS, secCursor.getPrev(found, found2, found3, null));
    assertEquals(OperationStatus.SUCCESS, secCursor.getNextDup(found, found2, found3, null));
    assertEquals(OperationStatus.SUCCESS, secCursor.getPrevDup(found, found2, found3, null));
    assertEquals(OperationStatus.SUCCESS, secCursor.getNextNoDup(found, found2, found3, null));
    assertEquals(OperationStatus.SUCCESS, secCursor.getPrevNoDup(found, found2, found3, null));
    assertEquals(OperationStatus.SUCCESS, secCursor.getLast(found, found2, found3, null));

    secCursor.close();
    cursor.close();
    txnCommit(txn);
    secDb.close();
    priDb.close();
    env.close();
    env = null;
  }
Exemplo n.º 3
0
  public void testGet() throws DatabaseException {

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

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

    /*
     * For parameters that do not require initialization with a non-null
     * data array, we set them to null to make sure this works. [#12121]
     */

    /* Add one record for each key with one data/duplicate. */
    for (int i = 0; i < NUM_RECS; i += 1) {
      status = priDb.put(txn, entry(i), entry(i));
      assertSame(OperationStatus.SUCCESS, status);
    }

    /* SecondaryDatabase.get() */
    for (int i = 0; i < NUM_RECS; i += 1) {

      data.setData(null);
      status = secDb.get(txn, entry(i + KEY_OFFSET), key, data, LockMode.DEFAULT);
      assertSame(OperationStatus.SUCCESS, status);
      assertDataEquals(entry(i), key);
      assertDataEquals(entry(i), data);
    }
    data.setData(null);
    status = secDb.get(txn, entry(NUM_RECS + KEY_OFFSET), key, data, LockMode.DEFAULT);
    assertSame(OperationStatus.NOTFOUND, status);

    /* SecondaryDatabase.getSearchBoth() */
    for (int i = 0; i < NUM_RECS; i += 1) {
      data.setData(null);
      status = secDb.getSearchBoth(txn, entry(i + KEY_OFFSET), entry(i), data, LockMode.DEFAULT);
      assertSame(OperationStatus.SUCCESS, status);
      assertDataEquals(entry(i), data);
    }
    data.setData(null);
    status =
        secDb.getSearchBoth(
            txn, entry(NUM_RECS + KEY_OFFSET), entry(NUM_RECS), data, LockMode.DEFAULT);
    assertSame(OperationStatus.NOTFOUND, status);

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

    SecondaryCursor cursor = secDb.openSecondaryCursor(txn, null);
    try {
      /* SecondaryCursor.getFirst()/getNext() */
      secKey.setData(null);
      key.setData(null);
      data.setData(null);
      status = cursor.getFirst(secKey, key, data, LockMode.DEFAULT);
      for (int i = 0; i < NUM_RECS; i += 1) {
        assertSame(OperationStatus.SUCCESS, status);
        assertDataEquals(entry(i + KEY_OFFSET), secKey);
        assertDataEquals(entry(i), key);
        assertDataEquals(entry(i), data);
        assertPriLocked(priDb, key);
        secKey.setData(null);
        key.setData(null);
        data.setData(null);
        status = cursor.getNext(secKey, key, data, LockMode.DEFAULT);
      }
      assertSame(OperationStatus.NOTFOUND, status);

      /* SecondaryCursor.getCurrent() (last) */
      secKey.setData(null);
      key.setData(null);
      data.setData(null);
      status = cursor.getCurrent(secKey, key, data, LockMode.DEFAULT);
      assertSame(OperationStatus.SUCCESS, status);
      assertDataEquals(entry(NUM_RECS - 1 + KEY_OFFSET), secKey);
      assertDataEquals(entry(NUM_RECS - 1), key);
      assertDataEquals(entry(NUM_RECS - 1), data);
      assertPriLocked(priDb, key);

      /* SecondaryCursor.getLast()/getPrev() */
      secKey.setData(null);
      key.setData(null);
      data.setData(null);
      status = cursor.getLast(secKey, key, data, LockMode.DEFAULT);
      for (int i = NUM_RECS - 1; i >= 0; i -= 1) {
        assertSame(OperationStatus.SUCCESS, status);
        assertDataEquals(entry(i + KEY_OFFSET), secKey);
        assertDataEquals(entry(i), key);
        assertDataEquals(entry(i), data);
        assertPriLocked(priDb, key);
        secKey.setData(null);
        key.setData(null);
        data.setData(null);
        status = cursor.getPrev(secKey, key, data, LockMode.DEFAULT);
      }
      assertSame(OperationStatus.NOTFOUND, status);

      /* SecondaryCursor.getCurrent() (first) */
      secKey.setData(null);
      key.setData(null);
      data.setData(null);
      status = cursor.getCurrent(secKey, key, data, LockMode.DEFAULT);
      assertSame(OperationStatus.SUCCESS, status);
      assertDataEquals(entry(0 + KEY_OFFSET), secKey);
      assertDataEquals(entry(0), key);
      assertDataEquals(entry(0), data);
      assertPriLocked(priDb, key);

      /* SecondaryCursor.getSearchKey() */
      key.setData(null);
      data.setData(null);
      status = cursor.getSearchKey(entry(KEY_OFFSET - 1), key, data, LockMode.DEFAULT);
      assertSame(OperationStatus.NOTFOUND, status);
      for (int i = 0; i < NUM_RECS; i += 1) {
        key.setData(null);
        data.setData(null);
        status = cursor.getSearchKey(entry(i + KEY_OFFSET), key, data, LockMode.DEFAULT);
        assertSame(OperationStatus.SUCCESS, status);
        assertDataEquals(entry(i), key);
        assertDataEquals(entry(i), data);
        assertPriLocked(priDb, key);
      }
      key.setData(null);
      data.setData(null);
      status = cursor.getSearchKey(entry(NUM_RECS + KEY_OFFSET), key, data, LockMode.DEFAULT);
      assertSame(OperationStatus.NOTFOUND, status);

      /* SecondaryCursor.getSearchBoth() */
      data.setData(null);
      status = cursor.getSearchKey(entry(KEY_OFFSET - 1), entry(0), data, LockMode.DEFAULT);
      assertSame(OperationStatus.NOTFOUND, status);
      for (int i = 0; i < NUM_RECS; i += 1) {
        data.setData(null);
        status = cursor.getSearchBoth(entry(i + KEY_OFFSET), entry(i), data, LockMode.DEFAULT);
        assertSame(OperationStatus.SUCCESS, status);
        assertDataEquals(entry(i), data);
        assertPriLocked(priDb, entry(i));
      }
      data.setData(null);
      status =
          cursor.getSearchBoth(
              entry(NUM_RECS + KEY_OFFSET), entry(NUM_RECS), data, LockMode.DEFAULT);
      assertSame(OperationStatus.NOTFOUND, status);

      /* SecondaryCursor.getSearchKeyRange() */
      key.setData(null);
      data.setData(null);
      status = cursor.getSearchKeyRange(entry(KEY_OFFSET - 1), key, data, LockMode.DEFAULT);
      assertSame(OperationStatus.SUCCESS, status);
      assertDataEquals(entry(0), key);
      assertDataEquals(entry(0), data);
      assertPriLocked(priDb, key);
      for (int i = 0; i < NUM_RECS; i += 1) {
        key.setData(null);
        data.setData(null);
        status = cursor.getSearchKeyRange(entry(i + KEY_OFFSET), key, data, LockMode.DEFAULT);
        assertSame(OperationStatus.SUCCESS, status);
        assertDataEquals(entry(i), key);
        assertDataEquals(entry(i), data);
        assertPriLocked(priDb, key);
      }
      key.setData(null);
      data.setData(null);
      status = cursor.getSearchKeyRange(entry(NUM_RECS + KEY_OFFSET), key, data, LockMode.DEFAULT);
      assertSame(OperationStatus.NOTFOUND, status);

      /* SecondaryCursor.getSearchBothRange() */
      data.setData(null);
      status = cursor.getSearchBothRange(entry(1 + KEY_OFFSET), entry(1), data, LockMode.DEFAULT);
      assertSame(OperationStatus.SUCCESS, status);
      assertDataEquals(entry(1), data);
      assertPriLocked(priDb, entry(1));
      for (int i = 0; i < NUM_RECS; i += 1) {
        data.setData(null);
        status = cursor.getSearchBothRange(entry(i + KEY_OFFSET), entry(i), data, LockMode.DEFAULT);
        assertSame(OperationStatus.SUCCESS, status);
        assertDataEquals(entry(i), data);
        assertPriLocked(priDb, entry(i));
      }
      data.setData(null);
      status =
          cursor.getSearchBothRange(
              entry(NUM_RECS + KEY_OFFSET), entry(NUM_RECS), data, LockMode.DEFAULT);
      assertSame(OperationStatus.NOTFOUND, status);

      /* Add one duplicate for each key. */
      Cursor priCursor = priDb.openCursor(txn, null);
      try {
        for (int i = 0; i < NUM_RECS; i += 1) {
          status = priCursor.put(entry(i + KEY_OFFSET), entry(i));
          assertSame(OperationStatus.SUCCESS, status);
        }
      } finally {
        priCursor.close();
      }

      /* SecondaryCursor.getNextDup() */
      secKey.setData(null);
      key.setData(null);
      data.setData(null);
      status = cursor.getFirst(secKey, key, data, LockMode.DEFAULT);
      for (int i = 0; i < NUM_RECS; i += 1) {
        assertSame(OperationStatus.SUCCESS, status);
        assertDataEquals(entry(i + KEY_OFFSET), secKey);
        assertDataEquals(entry(i), key);
        assertDataEquals(entry(i), data);
        assertPriLocked(priDb, key, data);
        secKey.setData(null);
        key.setData(null);
        data.setData(null);
        status = cursor.getNextDup(secKey, key, data, LockMode.DEFAULT);
        assertSame(OperationStatus.SUCCESS, status);
        assertDataEquals(entry(i + KEY_OFFSET), secKey);
        assertDataEquals(entry(i + KEY_OFFSET), key);
        assertDataEquals(entry(i), data);
        assertPriLocked(priDb, key, data);
        secKey.setData(null);
        key.setData(null);
        data.setData(null);
        status = cursor.getNextDup(secKey, key, data, LockMode.DEFAULT);
        assertSame(OperationStatus.NOTFOUND, status);
        secKey.setData(null);
        key.setData(null);
        data.setData(null);
        status = cursor.getNext(secKey, key, data, LockMode.DEFAULT);
      }
      assertSame(OperationStatus.NOTFOUND, status);

      /* SecondaryCursor.getNextNoDup() */
      secKey.setData(null);
      key.setData(null);
      data.setData(null);
      status = cursor.getFirst(secKey, key, data, LockMode.DEFAULT);
      for (int i = 0; i < NUM_RECS; i += 1) {
        assertSame(OperationStatus.SUCCESS, status);
        assertDataEquals(entry(i + KEY_OFFSET), secKey);
        assertDataEquals(entry(i), key);
        assertDataEquals(entry(i), data);
        assertPriLocked(priDb, key, data);
        secKey.setData(null);
        key.setData(null);
        data.setData(null);
        status = cursor.getNextNoDup(secKey, key, data, LockMode.DEFAULT);
      }
      assertSame(OperationStatus.NOTFOUND, status);

      /* SecondaryCursor.getPrevDup() */
      secKey.setData(null);
      key.setData(null);
      data.setData(null);
      status = cursor.getLast(secKey, key, data, LockMode.DEFAULT);
      for (int i = NUM_RECS - 1; i >= 0; i -= 1) {
        assertSame(OperationStatus.SUCCESS, status);
        assertDataEquals(entry(i + KEY_OFFSET), secKey);
        assertDataEquals(entry(i + KEY_OFFSET), key);
        assertDataEquals(entry(i), data);
        assertPriLocked(priDb, key, data);
        secKey.setData(null);
        key.setData(null);
        data.setData(null);
        status = cursor.getPrevDup(secKey, key, data, LockMode.DEFAULT);
        assertSame(OperationStatus.SUCCESS, status);
        assertDataEquals(entry(i + KEY_OFFSET), secKey);
        assertDataEquals(entry(i), key);
        assertDataEquals(entry(i), data);
        assertPriLocked(priDb, key, data);
        secKey.setData(null);
        key.setData(null);
        data.setData(null);
        status = cursor.getPrevDup(secKey, key, data, LockMode.DEFAULT);
        assertSame(OperationStatus.NOTFOUND, status);
        secKey.setData(null);
        key.setData(null);
        data.setData(null);
        status = cursor.getPrev(secKey, key, data, LockMode.DEFAULT);
      }
      assertSame(OperationStatus.NOTFOUND, status);

      /* SecondaryCursor.getPrevNoDup() */
      secKey.setData(null);
      key.setData(null);
      data.setData(null);
      status = cursor.getLast(secKey, key, data, LockMode.DEFAULT);
      for (int i = NUM_RECS - 1; i >= 0; i -= 1) {
        assertSame(OperationStatus.SUCCESS, status);
        assertDataEquals(entry(i + KEY_OFFSET), secKey);
        assertDataEquals(entry(i + KEY_OFFSET), key);
        assertDataEquals(entry(i), data);
        assertPriLocked(priDb, key, data);
        secKey.setData(null);
        key.setData(null);
        data.setData(null);
        status = cursor.getPrevNoDup(secKey, key, data, LockMode.DEFAULT);
      }
      assertSame(OperationStatus.NOTFOUND, status);
    } finally {
      cursor.close();
    }

    txnCommit(txn);
    secDb.close();
    priDb.close();
  }