public synchronized <V> V nextDup(EntityCursor<V> cursor) {
   try {
     return cursor.nextDup();
   } catch (DatabaseException e) {
     throw getPersistenceManager().convertDatabaseException(e);
   }
 }
Esempio n. 2
0
  public void testUninitializedCursor() throws DatabaseException {

    open();

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

    Transaction txn = txnBeginCursor();

    MyEntity e = new MyEntity();
    e.priKey = 1;
    e.secKey = 1;
    priIndex.put(txn, e);

    EntityCursor<MyEntity> entities = priIndex.entities(txn, null);
    try {
      entities.nextDup();
      fail();
    } catch (IllegalStateException expected) {
    }
    try {
      entities.prevDup();
      fail();
    } catch (IllegalStateException expected) {
    }
    try {
      entities.current();
      fail();
    } catch (IllegalStateException expected) {
    }
    try {
      entities.delete();
      fail();
    } catch (IllegalStateException expected) {
    }
    try {
      entities.update(e);
      fail();
    } catch (IllegalStateException expected) {
    }
    try {
      entities.count();
      fail();
    } catch (IllegalStateException expected) {
    }

    entities.close();
    txnCommit(txn);
    close();
  }
  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();
  }