/** * 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; } }
protected BoxStore createBoxStore() { return createBoxStoreBuilderWithTwoEntities(false).build(); }
protected BoxStore createBoxStore(boolean withIndex) { return createBoxStoreBuilder(withIndex).build(); }
@Override protected BoxStore createBoxStore() { return MyObjectBox.builder().directory(boxStoreDir).build(); } }
protected BoxStore createBoxStore() { return MyObjectBox.builder().directory(boxStoreDir).build(); }
@Override protected BoxStore createBoxStore() { return MyObjectBox.builder().baseDirectory(boxStoreDir).debugTransactions().build(); }
@Test public void testDeleteAllFiles_staticDir() { closeStoreForTest(); File boxStoreDir2 = new File(boxStoreDir.getAbsolutePath() + "-2"); BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(false)).directory(boxStoreDir2); BoxStore store2 = builder.build(); store2.close(); assertTrue(boxStoreDir2.exists()); assertTrue(BoxStore.deleteAllFiles(boxStoreDir2)); assertFalse(boxStoreDir2.exists()); }
@Test public void testDeleteAllFiles_baseDirName() { closeStoreForTest(); File basedir = new File("test-base-dir"); String name = "mydb"; basedir.mkdir(); assertTrue(basedir.isDirectory()); File dbDir = new File(basedir, name); assertFalse(dbDir.exists()); BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(false)).baseDirectory(basedir).name(name); BoxStore store2 = builder.build(); store2.close(); assertTrue(dbDir.exists()); assertTrue(BoxStore.deleteAllFiles(basedir, name)); assertFalse(dbDir.exists()); assertTrue(basedir.delete()); }
@Test public void testMaxReaders() throws InterruptedException { builder = createBoxStoreBuilder(false); store = builder.maxReaders(1).build(); final Exception[] exHolder = {null}; final Thread thread = new Thread(new Runnable() {
@Test public void testDebugCursor_seekOrNext_get_withoutModel() { TestEntity entity1 = putTestEntity("foo", 23); TestEntity entity2 = putTestEntity("bar", 42); store.close(); store = BoxStoreBuilder.createDebugWithoutModel().directory(boxStoreDir).build(); runTest(entity1, entity2); }
@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(); }
@Test public void testDateParam() { store.close(); assertTrue(store.deleteAllFiles()); store = MyObjectBox.builder().baseDirectory(boxStoreDir).debugFlags(DebugFlags.LOG_QUERY_PARAMETERS).build(); Date now = new Date(); Order order = new Order(); order.setDate(now); Box<Order> box = store.boxFor(Order.class); box.put(order); Query<Order> query = box.query().equal(Order_.date, 0).build(); assertEquals(0, query.count()); query.setParameter(Order_.date, now); }
@Test public void testCallInReadTxWithRetry_callback() { closeStoreForTest(); final int[] countHolder = {0}; final int[] countHolderCallback = {0}; BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(false)).directory(boxStoreDir) .failedReadTxAttemptCallback(new TxCallback() { @Override public void txFinished(@Nullable Object result, @Nullable Throwable error) { assertNotNull(error); countHolderCallback[0]++; } }); store = builder.build(); String value = store.callInReadTxWithRetry(createTestCallable(countHolder), 5, 0, true); assertEquals("42", value); assertEquals(5, countHolder[0]); assertEquals(4, countHolderCallback[0]); }
@Test public void testPutAndGetTwoEntities() { store.close(); store.deleteAllFiles(); store = createBoxStoreBuilderWithTwoEntities(false).build(); box = store.boxFor(TestEntity.class); long key = putTestEntity(null, 1977).getId(); TestEntity entityRead = box.get(key); assertEquals(1977, entityRead.getSimpleInt()); Box<TestEntityMinimal> box2 = store.boxFor(TestEntityMinimal.class); TestEntityMinimal entity2 = new TestEntityMinimal(); entity2.setText("foo"); long key2 = box2.put(entity2); TestEntityMinimal entity2Read = box2.get(key2); assertEquals("foo", entity2Read.getText()); }
@Test // TODO can we improve? More than just "still works"? public void testQueryAttempts() { store.close(); BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(false)).directory(boxStoreDir) .queryAttempts(5) .failedReadTxAttemptCallback(new TxCallback() { @Override public void txFinished(@Nullable Object result, @Nullable Throwable error) { error.printStackTrace(); } }); builder.entity(new TestEntity_()); store = builder.build(); putTestEntitiesScalars(); Query<TestEntity> query = store.boxFor(TestEntity.class).query().equal(simpleInt, 2007).build(); assertEquals(2007, query.findFirst().getSimpleInt()); }
@Override public void onCreate() { super.onCreate(); mInstance = this; mBoxStore = MyObjectBox.builder().androidContext(mInstance).build(); Stetho.initializeWithDefaults(this); ResUtil.init(); }
@Override public void onCreate() { super.onCreate(); boxStore = MyObjectBox.builder().androidContext(App.this).build(); if (BuildConfig.DEBUG) { new AndroidObjectBrowser(boxStore).start(this); } Log.d("App", "Using ObjectBox " + BoxStore.getVersion() + " (" + BoxStore.getVersionNative() + ")"); }
public static void main(String[] args) { BoxStore store = MyObjectBox.builder().name("objectbox-notes-db").build(); Box<Note> box = store.boxFor(Note.class); String text = args.length > 0 ? String.join(" ", args) : "No text given"; box.put(new Note(text)); System.out.println(box.count() + " notes in ObjectBox database:"); for (Note note : box.getAll()) { System.out.println(note); } store.close(); } }
@Before public void setUp() throws IOException { // store the database in the systems temporary files folder File tempFile = File.createTempFile("object-store-test", ""); // ensure file does not exist so builder creates a directory instead tempFile.delete(); boxStoreDir = tempFile; store = MyObjectBox.builder().directory(boxStoreDir).build(); }
public ObjectboxWaypointsRepo(@AppContext Context context, EventBus eventBus, Preferences preferences) { super(eventBus); BoxStore boxStore = MyObjectBox.builder().androidContext(context).build(); this.box = boxStore.boxFor(org.owntracks.android.data.WaypointModel.class); this.preferences = preferences; if(!preferences.isObjectboxMigrated()) { migrateLegacyData(context); } }