Skip to content

Commit 28091b8

Browse files
Share db file: add API to check if database files are open.
1 parent 34af3ae commit 28091b8

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,41 @@ static boolean isFileOpenSync(String canonicalPath, boolean runFinalization) {
384384
}
385385
}
386386

387+
/**
388+
* Using an Android Context and an optional database name, as configured with {@link BoxStoreBuilder#name(String)},
389+
* checks if the associated database files are in use by a BoxStore instance.
390+
* <p>
391+
* Use this to check that database files are not open before copying or deleting them.
392+
*/
393+
public static boolean isDatabaseOpen(Object context, @Nullable String dbNameOrNull) throws IOException {
394+
File dbDir = BoxStoreBuilder.getAndroidDbDir(context, dbNameOrNull);
395+
return isFileOpen(dbDir.getCanonicalPath());
396+
}
397+
398+
/**
399+
* Using an optional base directory, as configured with {@link BoxStoreBuilder#baseDirectory(File)},
400+
* and an optional database name, as configured with {@link BoxStoreBuilder#name(String)},
401+
* checks if the associated database files are in use by a BoxStore instance.
402+
* <p>
403+
* Use this to check that database files are not open before copying or deleting them.
404+
*/
405+
public static boolean isDatabaseOpen(@Nullable File baseDirectoryOrNull,
406+
@Nullable String dbNameOrNull) throws IOException {
407+
408+
File dbDir = BoxStoreBuilder.getDbDir(baseDirectoryOrNull, dbNameOrNull);
409+
return isFileOpen(dbDir.getCanonicalPath());
410+
}
411+
412+
/**
413+
* Using a directory, as configured with {@link BoxStoreBuilder#directory(File)},
414+
* checks if the associated database files are in use by a BoxStore instance.
415+
* <p>
416+
* Use this to check that database files are not open before copying or deleting them.
417+
*/
418+
public static boolean isDatabaseOpen(File directory) throws IOException {
419+
return isFileOpen(directory.getCanonicalPath());
420+
}
421+
387422
/**
388423
* The size in bytes occupied by the data file on disk.
389424
*

0 commit comments

Comments
 (0)