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

    open();

    PrimaryIndex<Integer, SeparateSequenceEntity1> priIndex1 =
        store.getPrimaryIndex(Integer.class, SeparateSequenceEntity1.class);

    PrimaryIndex<Integer, SeparateSequenceEntity2> priIndex2 =
        store.getPrimaryIndex(Integer.class, SeparateSequenceEntity2.class);

    Transaction txn = txnBegin();
    SeparateSequenceEntity1 e1 = new SeparateSequenceEntity1();
    SeparateSequenceEntity2 e2 = new SeparateSequenceEntity2();
    priIndex1.put(txn, e1);
    assertEquals(1, e1.key);
    priIndex2.putNoOverwrite(txn, e2);
    assertEquals(Integer.valueOf(1), e2.key);
    e1.key = 0;
    priIndex1.putNoOverwrite(txn, e1);
    assertEquals(2, e1.key);
    e2.key = null;
    priIndex2.put(txn, e2);
    assertEquals(Integer.valueOf(2), e2.key);
    txnCommit(txn);

    close();
  }
  /** 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();
  }
 public synchronized <K, E> boolean putNoOverwrite(PrimaryIndex<K, E> index, E entity) {
   try {
     return index.putNoOverwrite(getDbTransaction(), entity);
   } catch (DatabaseException e) {
     throw getPersistenceManager().convertDatabaseException(e);
   }
 }
Example #4
0
  public void testCompositeSequence() throws DatabaseException {

    open();

    PrimaryIndex<CompositeSequenceEntity1.Key, CompositeSequenceEntity1> priIndex1 =
        store.getPrimaryIndex(CompositeSequenceEntity1.Key.class, CompositeSequenceEntity1.class);

    PrimaryIndex<CompositeSequenceEntity2.Key, CompositeSequenceEntity2> priIndex2 =
        store.getPrimaryIndex(CompositeSequenceEntity2.Key.class, CompositeSequenceEntity2.class);

    Transaction txn = txnBegin();
    CompositeSequenceEntity1 e1 = new CompositeSequenceEntity1();
    CompositeSequenceEntity2 e2 = new CompositeSequenceEntity2();
    priIndex1.put(txn, e1);
    assertEquals(1, e1.key.key);
    priIndex2.putNoOverwrite(txn, e2);
    assertEquals(Integer.valueOf(1), e2.key.key);
    e1.key = null;
    priIndex1.putNoOverwrite(txn, e1);
    assertEquals(2, e1.key.key);
    e2.key = null;
    priIndex2.put(txn, e2);
    assertEquals(Integer.valueOf(2), e2.key.key);
    txnCommit(txn);

    EntityCursor<CompositeSequenceEntity1> c1 = priIndex1.entities();
    e1 = c1.next();
    assertEquals(2, e1.key.key);
    e1 = c1.next();
    assertEquals(1, e1.key.key);
    e1 = c1.next();
    assertNull(e1);
    c1.close();

    EntityCursor<CompositeSequenceEntity2> c2 = priIndex2.entities();
    e2 = c2.next();
    assertEquals(Integer.valueOf(2), e2.key.key);
    e2 = c2.next();
    assertEquals(Integer.valueOf(1), e2.key.key);
    e2 = c2.next();
    assertNull(e2);
    c2.close();

    close();
  }
Example #5
0
  public void testCustomCompare() throws DatabaseException {

    open();

    PrimaryIndex<ReverseIntKey, CustomCompareEntity> priIndex =
        store.getPrimaryIndex(ReverseIntKey.class, CustomCompareEntity.class);

    SecondaryIndex<ReverseIntKey, ReverseIntKey, CustomCompareEntity> secIndex1 =
        store.getSecondaryIndex(priIndex, ReverseIntKey.class, "secKey1");

    SecondaryIndex<ReverseIntKey, ReverseIntKey, CustomCompareEntity> secIndex2 =
        store.getSecondaryIndex(priIndex, ReverseIntKey.class, "secKey2");

    Transaction txn = txnBegin();
    for (int i = 1; i <= 5; i += 1) {
      assertTrue(priIndex.putNoOverwrite(txn, new CustomCompareEntity(i)));
    }
    txnCommit(txn);

    txn = txnBeginCursor();
    EntityCursor<CustomCompareEntity> c = priIndex.entities(txn, null);
    for (int i = 5; i >= 1; i -= 1) {
      CustomCompareEntity e = c.next();
      assertNotNull(e);
      assertEquals(new ReverseIntKey(i), e.key);
    }
    c.close();
    txnCommit(txn);

    txn = txnBeginCursor();
    c = secIndex1.entities(txn, null);
    for (int i = -1; i >= -5; i -= 1) {
      CustomCompareEntity e = c.next();
      assertNotNull(e);
      assertEquals(new ReverseIntKey(-i), e.key);
      assertEquals(new ReverseIntKey(i), e.secKey1);
    }
    c.close();
    txnCommit(txn);

    txn = txnBeginCursor();
    c = secIndex2.entities(txn, null);
    for (int i = -1; i >= -5; i -= 1) {
      CustomCompareEntity e = c.next();
      assertNotNull(e);
      assertEquals(new ReverseIntKey(-i), e.key);
      assertTrue(e.secKey2.contains(new ReverseIntKey(i)));
    }
    c.close();
    txnCommit(txn);

    close();
  }