Skip to content

Commit cd7ce37

Browse files
committed
Merge remote-tracking branch 'origin/35-drop-all-data' into dev
2 parents 6a0f1ec + 280be46 commit cd7ce37

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

objectbox-java/src/main/java/io/objectbox/BoxStore.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -575,18 +575,23 @@ public static boolean deleteAllFiles(@Nullable File baseDirectoryOrNull, @Nullab
575575
return deleteAllFiles(dbDir);
576576
}
577577

578+
/**
579+
* Removes all objects from all boxes, e.g. deletes all database content.
580+
*
581+
* Internally reads the current schema, drops all database content,
582+
* then restores the schema in a single transaction.
583+
*/
584+
public void removeAllObjects() {
585+
nativeDropAllData(handle);
586+
}
587+
578588
@Internal
579589
public void unregisterTransaction(Transaction transaction) {
580590
synchronized (transactions) {
581591
transactions.remove(transaction);
582592
}
583593
}
584594

585-
// TODO not implemented on native side; rename to "nukeData" (?)
586-
void dropAllData() {
587-
nativeDropAllData(handle);
588-
}
589-
590595
void txCommitted(Transaction tx, @Nullable int[] entityTypeIdsAffected) {
591596
// Only one write TX at a time, but there is a chance two writers race after commit: thus synchronize
592597
synchronized (txCommitCountLock) {

tests/objectbox-java-test/src/test/java/io/objectbox/BoxStoreTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,27 @@ public void testDeleteAllFiles_openStore() {
145145
BoxStore.deleteAllFiles(boxStoreDir);
146146
}
147147

148+
@Test
149+
public void removeAllObjects() {
150+
// Insert at least two different kinds.
151+
store.close();
152+
store.deleteAllFiles();
153+
store = createBoxStoreBuilderWithTwoEntities(false).build();
154+
putTestEntities(5);
155+
Box<TestEntityMinimal> minimalBox = store.boxFor(TestEntityMinimal.class);
156+
minimalBox.put(new TestEntityMinimal(0, "Sally"));
157+
assertEquals(5, getTestEntityBox().count());
158+
assertEquals(1, minimalBox.count());
159+
160+
store.removeAllObjects();
161+
assertEquals(0, getTestEntityBox().count());
162+
assertEquals(0, minimalBox.count());
163+
164+
// Assert inserting is still possible.
165+
putTestEntities(1);
166+
assertEquals(1, getTestEntityBox().count());
167+
}
168+
148169
private void closeStoreForTest() {
149170
assertTrue(boxStoreDir.exists());
150171
store.close();

0 commit comments

Comments
 (0)