@Override public synchronized void close() { if (!closed) { closed = true; // tx is null despite check in constructor in some tests (called by finalizer): // Null check avoids NPE in finalizer and seems to stabilize Android instrumentation perf tests. if (tx != null && !tx.getStore().isClosed()) { nativeDestroy(cursor); } } }
/** * Builds a {@link BoxStore} using any given configuration. */ public BoxStore build() { if (directory == null) { name = dbName(name); directory = getDbDir(baseDirectory, name); } checkProvisionInitialDbFile(); return new BoxStore(this); }
/** * Removes (deletes) ALL Objects in a single transaction. */ public void removeAll() { Cursor<T> cursor = getWriter(); try { cursor.deleteAll(); commitWriter(cursor); } finally { releaseWriter(cursor); } }
@Test public void testTwoReaders() { store.close(); store.deleteAllFiles(); store = createBoxStoreBuilderWithTwoEntities(false).build(); box = store.boxFor(TestEntity.class); box.count(); Box<TestEntityMinimal> box2 = store.boxFor(TestEntityMinimal.class); box2.count(); box.count(); }
/** * Builds the default {@link BoxStore} instance, which can be acquired using {@link BoxStore#getDefault()}. * For testability, please see the comment of {@link BoxStore#getDefault()}. * <p> * May be called once only (throws otherwise). */ public BoxStore buildDefault() { BoxStore store = build(); BoxStore.setDefault(store); return store; } }
@Override public Long call() throws Exception { TestEntity entity = new TestEntity(); entity.setId(-1); getTestEntityBox().put(entity); return null; } };
@Override public void run() { getTestEntityBox().put(new TestEntity()); } });
protected BoxStoreBuilder createBoxStoreBuilderWithTwoEntities(boolean withIndex) { BoxStoreBuilder builder = new BoxStoreBuilder(createTestModelWithTwoEntities(withIndex)).directory(boxStoreDir); builder.debugFlags(DebugFlags.LOG_TRANSACTIONS_READ | DebugFlags.LOG_TRANSACTIONS_WRITE); builder.entity(new TestEntity_()); builder.entity(new TestEntityMinimal_()); return builder; }
protected BoxStoreBuilder createBoxStoreBuilder(boolean withIndex) { BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(withIndex)).directory(boxStoreDir); builder.debugFlags(DebugFlags.LOG_TRANSACTIONS_READ | DebugFlags.LOG_TRANSACTIONS_WRITE); builder.entity(new TestEntity_()); return builder; }
protected byte[] createTestModel(boolean withIndex) { ModelBuilder modelBuilder = new ModelBuilder(); addTestEntity(modelBuilder, withIndex); modelBuilder.lastEntityId(lastEntityId, lastEntityUid); modelBuilder.lastIndexId(lastIndexId, lastIndexUid); return modelBuilder.build(); }
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(); }
@Override public void run() { counts[0] = box.count(); store.runInReadTx(new Runnable() { @Override public void run() { counts[1] = box.count(); } }); } });
@Override public long[] call() throws Exception { long count1 = store.callInReadTx(new Callable<Long>() { @Override public Long call() throws Exception { return box.count(); } }); return new long[]{box.count(), count1}; } });
@Override public void run() { // Dummy TX, still will be committed, should not add anything to objectCounts getTestEntityBox().count(); } });
/** * Removes (deletes) the Object by its ID. */ public void remove(long id) { Cursor<T> cursor = getWriter(); try { cursor.deleteEntity(id); commitWriter(cursor); } finally { releaseWriter(cursor); } }
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; }
Box(BoxStore store, Class<T> entityClass) { this.store = store; this.entityClass = entityClass; idGetter = store.getEntityInfo(entityClass).getIdGetter(); }
public KeyValueCursor createKeyValueCursor() { checkOpen(); long cursor = nativeCreateKeyValueCursor(transaction); return new KeyValueCursor(cursor); }
void publish() { box.getStore().internalScheduleThread(new Runnable() { @Override public void run() { List<T> result = query.find(); for (DataObserver<List<T>> observer : observers) { observer.onData(result); } } }); }
byte[] createTestModelWithTwoEntities(boolean withIndex) { ModelBuilder modelBuilder = new ModelBuilder(); addTestEntity(modelBuilder, withIndex); addTestEntityMinimal(modelBuilder, withIndex); modelBuilder.lastEntityId(lastEntityId, lastEntityUid); modelBuilder.lastIndexId(lastIndexId, lastIndexUid); return modelBuilder.build(); }