GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — new static deleteAllFiles methods · restmad/objectbox-java@1615558

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • objectbox-java/src/main/java/io/objectbox
    • BoxStore.java
    • BoxStoreBuilder.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: objectbox-java/src/main/java/io/objectbox/BoxStore.java
+37-1Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,8 @@ public boolean deleteAllFiles() {
426426
* If you did not use a custom name with BoxStoreBuilder, you can pass "new File({@link
427427
* BoxStoreBuilder#DEFAULT_NAME})".
428428
*
429-
* @param objectStoreDirectory directory to be deleted
429+
* @param objectStoreDirectory directory to be deleted; this is the value you previously provided to {@link
430+
* BoxStoreBuilder#directory(File)}
430431
* @return true if the directory 1) was deleted successfully OR 2) did not exist in the first place.
431432
* Note: If false is returned, any number of files may have been deleted before the failure happened.
432433
*/
@@ -450,6 +451,41 @@ public static boolean deleteAllFiles(File objectStoreDirectory) {
450451
return objectStoreDirectory.delete();
451452
}
452453

454+
/**
455+
* Danger zone! This will delete all files in the given directory!
456+
* <p>
457+
* If you did not use a custom name with BoxStoreBuilder, you can pass "new File({@link
458+
* BoxStoreBuilder#DEFAULT_NAME})".
459+
*
460+
* @param androidContext provide an Android Context like Application or Service
461+
* @param customDbNameOrNull use null for default name, or the name you previously provided to {@link
462+
* BoxStoreBuilder#name(String)}.
463+
* @return true if the directory 1) was deleted successfully OR 2) did not exist in the first place.
464+
* Note: If false is returned, any number of files may have been deleted before the failure happened.
465+
*/
466+
public static boolean deleteAllFiles(Object androidContext, @Nullable String customDbNameOrNull) {
467+
File dbDir = BoxStoreBuilder.getAndroidDbDir(androidContext, customDbNameOrNull);
468+
return deleteAllFiles(dbDir);
469+
}
470+
471+
/**
472+
* Danger zone! This will delete all files in the given directory!
473+
* <p>
474+
* If you did not use a custom name with BoxStoreBuilder, you can pass "new File({@link
475+
* BoxStoreBuilder#DEFAULT_NAME})".
476+
*
477+
* @param baseDirectoryOrNull use null for no base dir, or the value you previously provided to {@link
478+
* BoxStoreBuilder#baseDirectory(File)}
479+
* @param customDbNameOrNull use null for default name, or the name you previously provided to {@link
480+
* BoxStoreBuilder#name(String)}.
481+
* @return true if the directory 1) was deleted successfully OR 2) did not exist in the first place.
482+
* Note: If false is returned, any number of files may have been deleted before the failure happened.
483+
*/
484+
public static boolean deleteAllFiles(@Nullable File baseDirectoryOrNull, @Nullable String customDbNameOrNull) {
485+
File dbDir = BoxStoreBuilder.getDbDir(baseDirectoryOrNull, customDbNameOrNull);
486+
return deleteAllFiles(dbDir);
487+
}
488+
453489
@Internal
454490
public void unregisterTransaction(Transaction transaction) {
455491
synchronized (transactions) {
Collapse file

‎objectbox-java/src/main/java/io/objectbox/BoxStoreBuilder.java‎

Copy file name to clipboardExpand all lines: objectbox-java/src/main/java/io/objectbox/BoxStoreBuilder.java
+28-13Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323

2424
import javax.annotation.Nonnull;
25+
import javax.annotation.Nullable;
2526

2627
import io.objectbox.annotation.apihint.Internal;
2728
import io.objectbox.ideasonly.ModelUpdate;
@@ -148,8 +149,7 @@ public BoxStoreBuilder androidContext(Object context) {
148149
if (context == null) {
149150
throw new NullPointerException("Context may not be null");
150151
}
151-
File filesDir = getAndroidFilesDir(context);
152-
File baseDir = new File(filesDir, "objectbox");
152+
File baseDir = getAndroidBaseDir(context);
153153
if (!baseDir.exists()) {
154154
baseDir.mkdir();
155155
if (!baseDir.exists()) { // check baseDir.exists() because of potential concurrent processes
@@ -164,8 +164,21 @@ public BoxStoreBuilder androidContext(Object context) {
164164
return this;
165165
}
166166

167+
static File getAndroidDbDir(Object context, String dbName) {
168+
File baseDir = getAndroidBaseDir(context);
169+
return new File(baseDir, dbName(dbName));
170+
}
171+
172+
private static String dbName(@Nullable String dbNameOrNull) {
173+
return dbNameOrNull != null ? dbNameOrNull : DEFAULT_NAME;
174+
}
175+
176+
static File getAndroidBaseDir(Object context) {
177+
return new File(getAndroidFilesDir(context), "objectbox");
178+
}
179+
167180
@Nonnull
168-
private File getAndroidFilesDir(Object context) {
181+
private static File getAndroidFilesDir(Object context) {
169182
File filesDir;
170183
try {
171184
Method getFilesDir = context.getClass().getMethod("getFilesDir");
@@ -205,7 +218,7 @@ public BoxStoreBuilder maxReaders(int maxReaders) {
205218
}
206219

207220
@Internal
208-
public <T> void entity(EntityInfo entityInfo) {
221+
public void entity(EntityInfo entityInfo) {
209222
entityInfoList.add(entityInfo);
210223
}
211224

@@ -223,7 +236,6 @@ BoxStoreBuilder modelUpdate(ModelUpdate modelUpdate) {
223236
* In general, a maximum size prevents the DB from growing indefinitely when something goes wrong
224237
* (for example you insert data in an infinite loop).
225238
*
226-
* @param maxSizeInKByte
227239
*/
228240
public BoxStoreBuilder maxSizeInKByte(long maxSizeInKByte) {
229241
this.maxSizeInKByte = maxSizeInKByte;
@@ -258,18 +270,21 @@ public BoxStoreBuilder debugRelations() {
258270
*/
259271
public BoxStore build() {
260272
if (directory == null) {
261-
if (name == null) {
262-
name = DEFAULT_NAME;
263-
}
264-
if (baseDirectory != null) {
265-
directory = new File(baseDirectory, name);
266-
} else {
267-
directory = new File(name);
268-
}
273+
name = dbName(name);
274+
directory = getDbDir(baseDirectory, name);
269275
}
270276
return new BoxStore(this);
271277
}
272278

279+
static File getDbDir(@Nullable File baseDirectoryOrNull, @Nullable String nameOrNull) {
280+
String name = dbName(nameOrNull);
281+
if (baseDirectoryOrNull != null) {
282+
return new File(baseDirectoryOrNull, name);
283+
} else {
284+
return new File(name);
285+
}
286+
}
287+
273288
/**
274289
* Builds the default {@link BoxStore} instance, which can be acquired using {@link BoxStore#getDefault()}.
275290
* For testability, please see the comment of {@link BoxStore#getDefault()}.

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.