protected <TARGET> Cursor<TARGET> getRelationTargetCursor(Class<TARGET> targetClass) { // minor to do: optimize by using existing native cursor handle? // (Note: Cursor should not destroy the native cursor then.) return tx.createCursor(targetClass); }
Cursor<T> getActiveTxCursor() { Transaction activeTx = store.activeTx.get(); if (activeTx != null) { if (activeTx.isClosed()) { throw new IllegalStateException("Active TX is closed"); } Cursor<T> cursor = activeTxCursor.get(); if (cursor == null || cursor.getTx().isClosed()) { cursor = activeTx.createCursor(entityClass); activeTxCursor.set(cursor); } return cursor; } return null; }
Cursor<T> getWriter() { Cursor<T> cursor = getActiveTxCursor(); if (cursor != null) { return cursor; } else { Transaction tx = store.beginTx(); try { return tx.createCursor(entityClass); } catch (RuntimeException e) { tx.close(); throw e; } } }
Cursor<T> getReader() { Cursor<T> cursor = getActiveTxCursor(); if (cursor != null) { return cursor; } else { cursor = threadLocalReader.get(); if (cursor != null) { Transaction tx = cursor.tx; if (tx.isClosed() || !tx.isRecycled()) { throw new IllegalStateException("Illegal reader TX state"); } tx.renew(); cursor.renew(); } else { cursor = store.beginReadTx().createCursor(entityClass); threadLocalReader.set(cursor); } } return cursor; }
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 testClose() { Transaction tx = store.beginReadTx(); try { Cursor<TestEntity> cursor = tx.createCursor(TestEntity.class); assertFalse(cursor.isClosed()); cursor.close(); assertTrue(cursor.isClosed()); // Double close should be fine cursor.close(); } finally { tx.close(); } }
@Test public void testPutAndGetPrimitives() { Order order1 = putOrder(null, "order1"); Order order2 = putOrder(null, "order2"); Customer customer = putCustomer(); long customerId = customer.getId(); Cursor<Customer> cursorSource = InternalAccess.getWriter(customerBox); long[] orderIds = {order1.getId(), order2.getId()}; cursorSource.modifyRelations(1, customerId, orderIds, false); RelationInfo<Customer, Order> info = Customer_.ordersStandalone; int sourceEntityId = info.sourceInfo.getEntityId(); Cursor<Order> targetCursor = cursorSource.getTx().createCursor(Order.class); List<Order> related = targetCursor.getRelationEntities(sourceEntityId, info.relationId, customerId, false); assertEquals(2, related.size()); assertEquals(order1.getId(), related.get(0).getId()); assertEquals(order2.getId(), related.get(1).getId()); // Also InternalAccess.commitWriter(customerBox, cursorSource); assertEquals(2, orderBox.internalGetRelationEntities(sourceEntityId, info.relationId, customerId, false).size()); }
@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 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 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 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()); }
entity.setSimpleString(value1); Transaction transaction = store.beginTx(); Cursor<TestEntity> cursor = transaction.createCursor(TestEntity.class); long key = cursor.put(entity);