Ejemplo n.º 1
0
  /** Primary keys: {0, 1, 2, 3, 4} */
  @Test
  public void testPrimary() throws DatabaseException {

    SortedMap<Integer, SortedSet<Integer>> expected = new TreeMap<Integer, SortedSet<Integer>>();

    for (int priKey = 0; priKey < N_RECORDS; priKey += 1) {
      SortedSet<Integer> values = new TreeSet<Integer>();
      values.add(priKey);
      expected.put(priKey, values);
    }

    open();
    addEntities(primary);
    checkIndex(primary, expected, keyGetter, entityGetter);
    checkIndex(primaryRaw, expected, rawKeyGetter, rawEntityGetter);

    /* Close and reopen, then recheck indices. */
    close();
    open();
    checkIndex(primary, expected, keyGetter, entityGetter);
    checkIndex(primaryRaw, expected, rawKeyGetter, rawEntityGetter);

    /* Check primary delete, last key first for variety. */
    for (int priKey = N_RECORDS - 1; priKey >= 0; priKey -= 1) {
      boolean useRaw = ((priKey & 1) != 0);
      Transaction txn = txnBegin();
      if (useRaw) {
        primaryRaw.delete(txn, priKey);
      } else {
        primary.delete(txn, priKey);
      }
      txnCommit(txn);
      expected.remove(priKey);
      checkIndex(primary, expected, keyGetter, entityGetter);
    }
    checkAllEmpty();

    /* Check PrimaryIndex put operations. */
    MyEntity e;
    Transaction txn = txnBegin();
    /* put() */
    e = primary.put(txn, new MyEntity(1));
    assertNull(e);
    e = primary.get(txn, 1, null);
    assertEquals(1, e.key);
    /* putNoReturn() */
    primary.putNoReturn(txn, new MyEntity(2));
    e = primary.get(txn, 2, null);
    assertEquals(2, e.key);
    /* putNoOverwrite */
    assertTrue(!primary.putNoOverwrite(txn, new MyEntity(1)));
    assertTrue(!primary.putNoOverwrite(txn, new MyEntity(2)));
    assertTrue(primary.putNoOverwrite(txn, new MyEntity(3)));
    e = primary.get(txn, 3, null);
    assertEquals(3, e.key);
    txnCommit(txn);
    close();
  }
Ejemplo n.º 2
0
  @Override
  public void deleteDocument(DsSessionDto aSessionDto, String aDocumentId) {

    if ("bug".equals(aSessionDto.getUsername())) {
      throw new IllegalStateException("this is a bug");
    }

    Transaction tx = entityStore.getEnvironment().beginTransaction(null, null);
    documentPrimaryIndex.delete(Long.valueOf(aDocumentId));
    tx.commit();
  }
Ejemplo n.º 3
0
  private void checkDelete(
      SecondaryIndex<Integer, Integer, MyEntity> index,
      SecondaryIndex<Object, Object, RawObject> indexRaw,
      SortedMap<Integer, SortedSet<Integer>> expected)
      throws DatabaseException {

    SortedMap<Integer, SortedSet<Integer>> expectedSubIndex =
        new TreeMap<Integer, SortedSet<Integer>>();

    while (expected.size() > 0) {
      Integer delSecKey = expected.firstKey();
      SortedSet<Integer> deletedPriKeys = expected.remove(delSecKey);
      for (SortedSet<Integer> priKeys : expected.values()) {
        priKeys.removeAll(deletedPriKeys);
      }
      Transaction txn = txnBegin();
      boolean deleted = index.delete(txn, delSecKey);
      assertEquals(deleted, !deletedPriKeys.isEmpty());
      deleted = index.delete(txn, delSecKey);
      assertTrue(!deleted);
      assertNull(index.get(txn, delSecKey, null));
      txnCommit(txn);
      checkSecondary(index, indexRaw, expected);
    }

    /*
     * Delete remaining records so that the primary index is empty.  Use
     * the RawStore for variety.
     */
    Transaction txn = txnBegin();
    for (int priKey = 0; priKey < N_RECORDS; priKey += 1) {
      primaryRaw.delete(txn, priKey);
    }
    txnCommit(txn);
    checkAllEmpty();
  }
Ejemplo n.º 4
0
  /**
   * When Y is opened and X has a key with relatedEntity=Y.class, X should be opened automatically.
   * If X is not opened, foreign key constraints will not be enforced. [#15358]
   */
  public void testAutoOpenRelatedEntity() throws DatabaseException {

    PrimaryIndex<Integer, RelatedY> priY;
    PrimaryIndex<Integer, RelatedX> priX;

    /* Opening X should create (and open) Y and enforce constraints. */
    open();
    priX = store.getPrimaryIndex(Integer.class, RelatedX.class);
    PersistTestUtils.assertDbExists(true, env, STORE_NAME, RelatedY.class.getName(), null);
    if (isTransactional) {
      /* Constraint enforcement requires transactions. */
      try {
        priX.put(new RelatedX());
        fail();
      } catch (DatabaseException e) {
        assertTrue(
            "" + e.getMessage(),
            (e.getMessage().indexOf("foreign key not allowed: it is not present") >= 0)
                || (e.getMessage().indexOf("DB_FOREIGN_CONFLICT") >= 0));
      }
    }
    priY = store.getPrimaryIndex(Integer.class, RelatedY.class);
    priY.put(new RelatedY());
    priX.put(new RelatedX());
    close();

    /* Delete should cascade even when X is not opened explicitly. */
    open();
    priY = store.getPrimaryIndex(Integer.class, RelatedY.class);
    assertEquals(1, priY.count());
    priY.delete(88);
    assertEquals(0, priY.count());
    priX = store.getPrimaryIndex(Integer.class, RelatedX.class);
    assertEquals(0, priX.count()); /* Failed prior to [#15358] fix. */
    close();
  }