public synchronized <V> V first(EntityCursor<V> cursor) {
   try {
     return cursor.first();
   } catch (DatabaseException e) {
     throw getPersistenceManager().convertDatabaseException(e);
   }
 }
  private <T> void checkCursor(
      EntityCursor<T> cursor,
      Collection<T> collection,
      boolean collectionIsKeySet,
      List<List<Integer>> expected,
      Getter<T> getter)
      throws DatabaseException {

    boolean first;
    boolean firstDup;
    Iterator<T> iterator = collection.iterator();

    for (List<Integer> dups : expected) {
      for (int i : dups) {
        T o = cursor.next();
        assertNotNull(o);
        assertEquals(i, getter.getKey(o));
        /* Value iterator over duplicates. */
        if (!collectionIsKeySet) {
          assertTrue(iterator.hasNext());
          o = iterator.next();
          assertNotNull(o);
          assertEquals(i, getter.getKey(o));
        }
      }
    }

    first = true;
    for (List<Integer> dups : expected) {
      firstDup = true;
      for (int i : dups) {
        T o = first ? cursor.first() : (firstDup ? cursor.next() : cursor.nextDup());
        assertNotNull(o);
        assertEquals(i, getter.getKey(o));
        first = false;
        firstDup = false;
      }
    }

    first = true;
    for (List<Integer> dups : expected) {
      if (!dups.isEmpty()) {
        int i = dups.get(0);
        T o = first ? cursor.first() : cursor.nextNoDup();
        assertNotNull(o);
        assertEquals(i, getter.getKey(o));
        /* Key iterator over non-duplicates. */
        if (collectionIsKeySet) {
          assertTrue(iterator.hasNext());
          o = iterator.next();
          assertNotNull(o);
          assertEquals(i, getter.getKey(o));
        }
        first = false;
      }
    }

    List<List<Integer>> reversed = new ArrayList<List<Integer>>();
    for (List<Integer> dups : expected) {
      ArrayList<Integer> reversedDups = new ArrayList<Integer>(dups);
      Collections.reverse(reversedDups);
      reversed.add(reversedDups);
    }
    Collections.reverse(reversed);

    first = true;
    for (List<Integer> dups : reversed) {
      for (int i : dups) {
        T o = first ? cursor.last() : cursor.prev();
        assertNotNull(o);
        assertEquals(i, getter.getKey(o));
        first = false;
      }
    }

    first = true;
    for (List<Integer> dups : reversed) {
      firstDup = true;
      for (int i : dups) {
        T o = first ? cursor.last() : (firstDup ? cursor.prev() : cursor.prevDup());
        assertNotNull(o);
        assertEquals(i, getter.getKey(o));
        first = false;
        firstDup = false;
      }
    }

    first = true;
    for (List<Integer> dups : reversed) {
      if (!dups.isEmpty()) {
        int i = dups.get(0);
        T o = first ? cursor.last() : cursor.prevNoDup();
        assertNotNull(o);
        assertEquals(i, getter.getKey(o));
        first = false;
      }
    }

    cursor.close();
  }
Esempio n. 3
0
  public void testDeleteFromSubIndex() throws DatabaseException {

    open();

    PrimaryIndex<Integer, MyEntity> priIndex = store.getPrimaryIndex(Integer.class, MyEntity.class);

    SecondaryIndex<Integer, Integer, MyEntity> secIndex =
        store.getSecondaryIndex(priIndex, Integer.class, "secKey");

    Transaction txn = txnBegin();
    MyEntity e = new MyEntity();
    e.secKey = 1;
    e.priKey = 1;
    priIndex.put(txn, e);
    e.priKey = 2;
    priIndex.put(txn, e);
    e.priKey = 3;
    priIndex.put(txn, e);
    e.priKey = 4;
    priIndex.put(txn, e);
    txnCommit(txn);

    EntityIndex<Integer, MyEntity> subIndex = secIndex.subIndex(1);
    txn = txnBeginCursor();
    e = subIndex.get(txn, 1, null);
    assertEquals(1, e.priKey);
    assertEquals(Integer.valueOf(1), e.secKey);
    e = subIndex.get(txn, 2, null);
    assertEquals(2, e.priKey);
    assertEquals(Integer.valueOf(1), e.secKey);
    e = subIndex.get(txn, 3, null);
    assertEquals(3, e.priKey);
    assertEquals(Integer.valueOf(1), e.secKey);
    e = subIndex.get(txn, 5, null);
    assertNull(e);

    boolean deleted = subIndex.delete(txn, 1);
    assertTrue(deleted);
    assertNull(subIndex.get(txn, 1, null));
    assertNotNull(subIndex.get(txn, 2, null));

    EntityCursor<MyEntity> cursor = subIndex.entities(txn, null);
    boolean saw4 = false;
    for (MyEntity e2 = cursor.first(); e2 != null; e2 = cursor.next()) {
      if (e2.priKey == 3) {
        cursor.delete();
      }
      if (e2.priKey == 4) {
        saw4 = true;
      }
    }
    cursor.close();
    assertTrue(saw4);
    assertNull(subIndex.get(txn, 1, null));
    assertNull(subIndex.get(txn, 3, null));
    assertNotNull(subIndex.get(txn, 2, null));
    assertNotNull(subIndex.get(txn, 4, null));

    txnCommit(txn);
    close();
  }