public synchronized <V> V current(EntityCursor<V> cursor) { try { return cursor.current(); } catch (DatabaseException e) { throw getPersistenceManager().convertDatabaseException(e); } }
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(); }
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(); }
public void testCursorUpdate() 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(); Integer k; MyEntity e = new MyEntity(); e.priKey = 1; e.secKey = 2; priIndex.put(txn, e); /* update() with primary entity cursor. */ EntityCursor<MyEntity> entities = priIndex.entities(txn, null); e = entities.next(); assertNotNull(e); assertEquals(1, e.priKey); assertEquals(Integer.valueOf(2), e.secKey); e.secKey = null; assertTrue(entities.update(e)); e = entities.current(); assertNotNull(e); assertEquals(1, e.priKey); assertEquals(null, e.secKey); e.secKey = 3; assertTrue(entities.update(e)); e = entities.current(); assertNotNull(e); assertEquals(1, e.priKey); assertEquals(Integer.valueOf(3), e.secKey); entities.close(); /* update() with primary keys cursor. */ EntityCursor<Integer> keys = priIndex.keys(txn, null); k = keys.next(); assertNotNull(k); assertEquals(Integer.valueOf(1), k); try { keys.update(2); fail(); } catch (UnsupportedOperationException expected) { } keys.close(); /* update() with secondary entity cursor. */ entities = secIndex.entities(txn, null); e = entities.next(); assertNotNull(e); assertEquals(1, e.priKey); assertEquals(Integer.valueOf(3), e.secKey); try { entities.update(e); fail(); } catch (UnsupportedOperationException expected) { } catch (IllegalArgumentException expectedForDbCore) { } entities.close(); /* update() with secondary keys cursor. */ keys = secIndex.keys(txn, null); k = keys.next(); assertNotNull(k); assertEquals(Integer.valueOf(3), k); try { keys.update(k); fail(); } catch (UnsupportedOperationException expected) { } keys.close(); txnCommit(txn); close(); }