/** * Puts the given entities in a box using a single transaction. * * @param entities It is fine to pass null or an empty collection: * this case is handled efficiently without overhead. */ public void put(@Nullable Collection<T> entities) { if (entities == null || entities.isEmpty()) { return; } Cursor<T> cursor = getWriter(); try { for (T entity : entities) { cursor.put(entity); } commitWriter(cursor); } finally { releaseWriter(cursor); } }
/** * Puts the given entities in a box using a single transaction. */ public void put(@Nullable T... entities) { if (entities == null || entities.length == 0) { return; } Cursor<T> cursor = getWriter(); try { for (T entity : entities) { cursor.put(entity); } commitWriter(cursor); } finally { releaseWriter(cursor); } }
@Internal public void internalPutTarget(Cursor<TARGET> targetCursor) { checkIdOfTargetForPut = false; long id = targetCursor.put(target); setTargetId(id); setResolvedTarget(target, id); }
/** * Puts the given object in the box (aka persisting it). If this is a new entity (its ID property is 0), a new ID * will be assigned to the entity (and returned). If the entity was already put in the box before, it will be * overwritten. * <p> * Performance note: if you want to put several entities, consider {@link #put(Collection)}, * {@link #put(Object[])}, {@link BoxStore#runInTx(Runnable)}, etc. instead. */ public long put(T entity) { Cursor<T> cursor = getWriter(); try { long key = cursor.put(entity); commitWriter(cursor); return key; } finally { releaseWriter(cursor); } }
targetCursor.put(target);
private TestEntity putEntity(Cursor<TestEntity> cursor, String text, int number) { TestEntity entity = new TestEntity(); entity.setSimpleString(text); entity.setSimpleInt(number); cursor.put(entity); return entity; }
@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 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()); }
@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(); }