public synchronized <V> boolean delete(EntityCursor<V> cursor) {
   try {
     return cursor.delete();
   } catch (DatabaseException e) {
     throw getPersistenceManager().convertDatabaseException(e);
   }
 }
Example #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();
  }
Example #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();
  }
Example #4
0
  public void testCursorDelete() 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 = txnBeginCursor();

    /* delete() with primary and secondary entities cursor. */

    for (EntityIndex index : new EntityIndex[] {priIndex, secIndex}) {

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

      EntityCursor<MyEntity> cursor = index.entities(txn, null);

      e = cursor.next();
      assertNotNull(e);
      assertEquals(1, e.priKey);
      e = cursor.current();
      assertNotNull(e);
      assertEquals(1, e.priKey);
      assertTrue(cursor.delete());
      assertTrue(!cursor.delete());
      assertNull(cursor.current());

      e = cursor.next();
      assertNotNull(e);
      assertEquals(2, e.priKey);
      e = cursor.current();
      assertNotNull(e);
      assertEquals(2, e.priKey);
      assertTrue(cursor.delete());
      assertTrue(!cursor.delete());
      assertNull(cursor.current());

      e = cursor.next();
      assertNull(e);

      if (index == priIndex) {
        e = new MyEntity();
        e.priKey = 2;
        e.secKey = 1;
        assertTrue(!cursor.update(e));
      }

      cursor.close();
    }

    /* delete() with primary and secondary keys cursor. */

    for (EntityIndex index : new EntityIndex[] {priIndex, secIndex}) {

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

      EntityCursor<Integer> cursor = index.keys(txn, null);

      Integer k = cursor.next();
      assertNotNull(k);
      assertEquals(1, k.intValue());
      k = cursor.current();
      assertNotNull(k);
      assertEquals(1, k.intValue());
      assertTrue(cursor.delete());
      assertTrue(!cursor.delete());
      assertNull(cursor.current());

      int expectKey = (index == priIndex) ? 2 : 1;
      k = cursor.next();
      assertNotNull(k);
      assertEquals(expectKey, k.intValue());
      k = cursor.current();
      assertNotNull(k);
      assertEquals(expectKey, k.intValue());
      assertTrue(cursor.delete());
      assertTrue(!cursor.delete());
      assertNull(cursor.current());

      k = cursor.next();
      assertNull(k);

      cursor.close();
    }

    txnCommit(txn);
    close();
  }