void txCommitted(Transaction tx) { // Thread local readers will be renewed on next get, so we do not need clean them up Cursor<T> cursor = activeTxCursor.get(); if (cursor != null) { activeTxCursor.remove(); cursor.close(); } }
/** * Like {@link BoxStore#closeThreadResources()}, but limited to only this Box. * <p> * Rule of thumb: prefer {@link BoxStore#closeThreadResources()} unless you know that your thread only interacted * with this Box. */ public void closeThreadResources() { Cursor<T> cursor = threadLocalReader.get(); if (cursor != null) { cursor.close(); threadLocalReader.remove(); } }
/** * Called by {@link BoxStore#callInReadTx(Callable)} - does not throw so caller does not need try/finally. */ void readTxFinished(Transaction tx) { Cursor<T> cursor = activeTxCursor.get(); if (cursor != null && cursor.getTx() == tx) { activeTxCursor.remove(); cursor.close(); } }
@Override protected void finalize() throws Throwable { if (!closed) { // By default only complain about write cursors if (!readOnly || LOG_READ_NOT_CLOSED) { System.err.println("Cursor was not closed."); if (creationThrowable != null) { System.err.println("Cursor was initially created here:"); creationThrowable.printStackTrace(); } System.err.flush(); } close(); super.finalize(); } }
void commitWriter(Cursor<T> cursor) { // NOP if TX is ongoing if (activeTxCursor.get() == null) { cursor.close(); cursor.getTx().commitAndClose(); } }
void releaseWriter(Cursor<T> cursor) { // NOP if TX is ongoing if (activeTxCursor.get() == null) { Transaction tx = cursor.getTx(); if (!tx.isClosed()) { cursor.close(); tx.abort(); tx.close(); } } }
protected <TARGET> void checkApplyToManyToDb(List<TARGET> orders, Class<TARGET> targetClass) { if (orders instanceof ToMany) { ToMany<TARGET> toMany = (ToMany<TARGET>) orders; if (toMany.internalCheckApplyToDbRequired()) { Cursor<TARGET> targetCursor = getRelationTargetCursor(targetClass); try { toMany.internalApplyToDb(this, targetCursor); } finally { targetCursor.close(); } } } }
private void insertTestEntities(String... texts) { Transaction transaction = store.beginTx(); Cursor<TestEntity> cursor = transaction.createCursor(TestEntity.class); for (String text : texts) { putEntity(cursor, text, 0); } cursor.close(); transaction.commitAndClose(); }
@Test(expected = IllegalArgumentException.class) public void testPutEntityWithInvalidId() { TestEntity entity = new TestEntity(); entity.setId(777); Transaction transaction = store.beginTx(); Cursor<TestEntity> cursor = transaction.createCursor(TestEntity.class); try { cursor.put(entity); } finally { cursor.close(); transaction.close(); } }
@Test public void testRenew() throws IOException { insertTestEntities("orange"); Transaction transaction = store.beginReadTx(); Cursor<TestEntity> cursor = transaction.createCursor(TestEntity.class); transaction.recycle(); transaction.renew(); cursor.renew(); assertEquals("orange", cursor.get(1).getSimpleString()); cursor.close(); transaction.close(); }
@Test public void testLookupKeyUsingIndex() throws IOException { insertTestEntities("find me", "not me"); Transaction transaction = store.beginTx(); Cursor<TestEntity> cursor = transaction.createCursor(TestEntity.class); assertEquals(2, cursor.lookupKeyUsingIndex(9, "not me")); assertEquals(1, cursor.lookupKeyUsingIndex(9, "find me")); assertEquals(0, cursor.lookupKeyUsingIndex(9, "peter pan")); cursor.close(); transaction.abort(); }
@Test public void testGetPropertyId() { Transaction transaction = store.beginReadTx(); Cursor<TestEntity> cursor = transaction.createCursor(TestEntity.class); assertEquals(1, cursor.getPropertyId("id")); assertEquals(2, cursor.getPropertyId("simpleBoolean")); assertEquals(3, cursor.getPropertyId("simpleByte")); assertEquals(4, cursor.getPropertyId("simpleShort")); cursor.close(); transaction.abort(); }
@Test public void testPutAndGetEntity() { TestEntity entity = new TestEntity(); entity.setSimpleInt(1977); Transaction transaction = store.beginTx(); Cursor<TestEntity> cursor = transaction.createCursor(TestEntity.class); long key = cursor.put(entity); TestEntity entityRead = cursor.get(key); assertNotNull(entityRead); assertEquals(1977, entityRead.getSimpleInt()); cursor.close(); transaction.abort(); }
@Test public void testLookupKeyUsingIndex_samePrefix() { insertTestEntities("aaa", "aa"); Transaction transaction = store.beginTx(); Cursor<TestEntity> cursor = transaction.createCursor(TestEntity.class); assertEquals(0, cursor.lookupKeyUsingIndex(9, "a")); assertEquals(2, cursor.lookupKeyUsingIndex(9, "aa")); assertEquals(1, cursor.lookupKeyUsingIndex(9, "aaa")); assertEquals(0, cursor.lookupKeyUsingIndex(9, "aaaa")); cursor.close(); transaction.abort(); }
@Test public void testGetNextEntity() { insertTestEntities("hello", "bye", "dummy"); Transaction transaction = store.beginReadTx(); Cursor<TestEntity> cursor = transaction.createCursor(TestEntity.class); TestEntity entity1 = cursor.get(1); assertEquals("hello", entity1.getSimpleString()); TestEntity entity2 = cursor.next(); assertEquals("bye", entity2.getSimpleString()); TestEntity entity3 = cursor.next(); assertEquals("dummy", entity3.getSimpleString()); assertNull(cursor.next()); cursor.close(); transaction.abort(); }
@Test public void testPutSameIndexValue() { TestEntity entity = new TestEntity(); String value = "lulu321"; entity.setSimpleString(value); Transaction transaction = store.beginTx(); TestEntity read; try { Cursor<TestEntity> cursor = transaction.createCursor(TestEntity.class); long key = cursor.put(entity); // And again entity.setSimpleInt(1977); cursor.put(entity); assertEquals(key, cursor.lookupKeyUsingIndex(9, value)); read = cursor.get(key); cursor.close(); } finally { transaction.close(); } assertEquals(1977, read.getSimpleInt()); assertEquals(value, read.getSimpleString()); }
cursor.close(); transaction.abort();