Skip to content

Commit c9ff41f

Browse files
In-memory: use native implementation for deleteAllFiles #194
The native implementation is capable of cleaning up in-memory databases as well.
1 parent a11f702 commit c9ff41f

File tree

5 files changed

+34
-35
lines changed

5 files changed

+34
-35
lines changed

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

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package io.objectbox;
1818

19+
import org.greenrobot.essentials.collections.LongHashMap;
20+
1921
import java.io.Closeable;
2022
import java.io.File;
2123
import java.io.IOException;
@@ -55,7 +57,6 @@
5557
import io.objectbox.reactive.DataPublisher;
5658
import io.objectbox.reactive.SubscriptionBuilder;
5759
import io.objectbox.sync.SyncClient;
58-
import org.greenrobot.essentials.collections.LongHashMap;
5960

6061
/**
6162
* An ObjectBox database that provides {@link Box Boxes} to put and get objects of specific entity classes
@@ -140,6 +141,12 @@ public static String getVersionNative() {
140141
return nativeGetVersion();
141142
}
142143

144+
/**
145+
* @return true if DB files did not exist or were successfully removed,
146+
* false if DB files exist that could not be removed.
147+
*/
148+
static native boolean nativeRemoveDbFiles(String directory, boolean removeDir);
149+
143150
/**
144151
* Creates a native BoxStore instance with FlatBuffer {@link FlatStoreOptions} {@code options}
145152
* and a {@link ModelBuilder} {@code model}. Returns the handle of the native store instance.
@@ -690,38 +697,30 @@ public boolean deleteAllFiles() {
690697
/**
691698
* Danger zone! This will delete all files in the given directory!
692699
* <p>
693-
* No {@link BoxStore} may be alive using the given directory.
700+
* No {@link BoxStore} may be alive using the given directory. E.g. call this before building a store. When calling
701+
* this after {@link #close() closing} a store, read the docs of that method carefully first!
694702
* <p>
695-
* If you did not use a custom name with BoxStoreBuilder, you can pass "new File({@link
696-
* BoxStoreBuilder#DEFAULT_NAME})".
703+
* If no {@link BoxStoreBuilder#name(String) name} was specified when building the store, use like:
704+
*
705+
* <pre>{@code
706+
* BoxStore.deleteAllFiles(new File(BoxStoreBuilder.DEFAULT_NAME));
707+
* }</pre>
708+
*
709+
* <p>For an {@link BoxStoreBuilder#inMemory(String) in-memory} database, this will just clean up the in-memory
710+
* database.
697711
*
698712
* @param objectStoreDirectory directory to be deleted; this is the value you previously provided to {@link
699713
* BoxStoreBuilder#directory(File)}
700714
* @return true if the directory 1) was deleted successfully OR 2) did not exist in the first place.
701715
* Note: If false is returned, any number of files may have been deleted before the failure happened.
702-
* @throws IllegalStateException if the given directory is still used by a open {@link BoxStore}.
716+
* @throws IllegalStateException if the given directory is still used by an open {@link BoxStore}.
703717
*/
704718
public static boolean deleteAllFiles(File objectStoreDirectory) {
705-
if (!objectStoreDirectory.exists()) {
706-
return true;
707-
}
708-
if (isFileOpen(getCanonicalPath(objectStoreDirectory))) {
719+
String canonicalPath = getCanonicalPath(objectStoreDirectory);
720+
if (isFileOpen(canonicalPath)) {
709721
throw new IllegalStateException("Cannot delete files: store is still open");
710722
}
711-
712-
File[] files = objectStoreDirectory.listFiles();
713-
if (files == null) {
714-
return false;
715-
}
716-
for (File file : files) {
717-
if (!file.delete()) {
718-
// OK if concurrently deleted. Fail fast otherwise.
719-
if (file.exists()) {
720-
return false;
721-
}
722-
}
723-
}
724-
return objectStoreDirectory.delete();
723+
return nativeRemoveDbFiles(canonicalPath, true);
725724
}
726725

727726
/**

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ protected File prepareTempDir(String prefix) throws IOException {
117117
if (IN_MEMORY) {
118118
// Instead of random temp directory, use random suffix for each test to avoid re-using existing database
119119
// from other tests in case clean-up fails.
120-
// Note: all clean-up code will gracefully fail (e.g. deleting the database files will do nothing as the
121-
// directory does not exist).
120+
// Note: tearDown code will still work as the directory does not exist.
122121
String randomPart = Long.toUnsignedString(random.nextLong());
123122
return new File(BoxStore.IN_MEMORY_PREFIX + prefix + randomPart);
124123
} else {
@@ -178,10 +177,13 @@ public void tearDown() {
178177
logError("Could not clean up test", e);
179178
}
180179
}
181-
deleteAllFiles(boxStoreDir);
180+
cleanUpAllFiles(boxStoreDir);
182181
}
183182

184-
protected void deleteAllFiles(@Nullable File boxStoreDir) {
183+
/**
184+
* Manually clean up any leftover files to prevent interference with other tests.
185+
*/
186+
protected void cleanUpAllFiles(@Nullable File boxStoreDir) {
185187
if (boxStoreDir != null && boxStoreDir.exists()) {
186188
try (Stream<Path> stream = Files.walk(boxStoreDir.toPath())) {
187189
stream.sorted(Comparator.reverseOrder())

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public void directoryUnicodePath() throws IOException {
120120
}
121121
}
122122

123-
deleteAllFiles(parentTestDir);
123+
cleanUpAllFiles(parentTestDir);
124124
}
125125

126126
@Test

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ public void testOpenTwoBoxStoreTwoFiles() {
181181

182182
@Test
183183
public void testDeleteAllFiles() {
184-
assumeFalse(IN_MEMORY);
184+
// Note: for in-memory can not really assert database is gone,
185+
// relying on native code returning true for deleteAllFiles.
185186
closeStoreForTest();
186187
}
187188

@@ -228,7 +229,6 @@ public void testDeleteAllFiles_baseDirName() {
228229

229230
@Test(expected = IllegalStateException.class)
230231
public void testDeleteAllFiles_openStore() {
231-
assumeFalse(IN_MEMORY);
232232
BoxStore.deleteAllFiles(boxStoreDir);
233233
}
234234

@@ -258,9 +258,7 @@ private void closeStoreForTest() {
258258
assertTrue(boxStoreDir.exists());
259259
}
260260
store.close();
261-
if (!IN_MEMORY) {
262-
assertTrue(store.deleteAllFiles());
263-
}
261+
assertTrue(store.deleteAllFiles());
264262
assertFalse(boxStoreDir.exists());
265263
}
266264

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void validateOnOpenCorruptFile() throws IOException {
8787
assertEquals("Validating pages failed (page not found)", ex.getMessage());
8888

8989
// Clean up
90-
deleteAllFiles(dir);
90+
cleanUpAllFiles(dir);
9191
}
9292

9393
@Test
@@ -160,7 +160,7 @@ public void validateOnOpenKvCorruptFile() throws IOException {
160160
ex.getMessage());
161161

162162
// Clean up
163-
deleteAllFiles(dir);
163+
cleanUpAllFiles(dir);
164164
}
165165

166166
/**

0 commit comments

Comments
 (0)