@ThreadSafe
public class BoxStore
extends java.lang.Object
implements java.io.Closeable
Boxes
to put and get Objects of a specific Entity class
(see boxFor(Class)
). To get an instance of this class use MyObjectBox.builder()
.Modifier and Type | Field and Description |
---|---|
static java.lang.String |
JNI_VERSION
Change so ReLinker will update native library when using workaround loading.
|
Modifier and Type | Method and Description |
---|---|
io.objectbox.Transaction |
beginReadTx()
Internal, low level method: use
runInReadTx(Runnable) instead. |
io.objectbox.Transaction |
beginTx()
Internal, low level method: use
runInTx(Runnable) instead. |
<T> Box<T> |
boxFor(java.lang.Class<T> entityClass)
Returns a Box for the given type.
|
<T> T |
callInReadTx(java.util.concurrent.Callable<T> callable)
Calls the given callable inside a read(-only) transaction.
|
<T> T |
callInReadTxWithRetry(java.util.concurrent.Callable<T> callable,
int attempts,
int initialBackOffInMs,
boolean logAndHeal)
Calls
callInReadTx(Callable) and retries in case a DbException is thrown. |
<R> R |
callInTx(java.util.concurrent.Callable<R> callable)
Like
runInTx(Runnable) , but allows returning a value and throwing an exception. |
<R> void |
callInTxAsync(java.util.concurrent.Callable<R> callable,
TxCallback<R> callback)
Runs the given Runnable as a transaction in a separate thread.
|
<R> R |
callInTxNoException(java.util.concurrent.Callable<R> callable)
Like
callInTx(Callable) , but throws no Exception. |
int |
cleanStaleReadTransactions() |
static boolean |
clearDefaultStore()
Clears the convenience instance.
|
void |
close()
Closes the BoxStore and frees associated resources.
|
void |
closeThreadResources()
Call this method from a thread that is about to be shutdown or likely not to use ObjectBox anymore:
it frees any cached resources tied to the calling thread (e.g.
|
boolean |
deleteAllFiles()
Danger zone! This will delete all data (files) of this BoxStore!
You must call
close() before and read the docs of that method carefully! |
static boolean |
deleteAllFiles(java.io.File objectStoreDirectory)
Danger zone! This will delete all files in the given directory!
|
static boolean |
deleteAllFiles(java.io.File baseDirectoryOrNull,
java.lang.String customDbNameOrNull)
Danger zone! This will delete all files in the given directory!
|
static boolean |
deleteAllFiles(java.lang.Object androidContext,
java.lang.String customDbNameOrNull)
Danger zone! This will delete all files in the given directory!
|
java.lang.String |
diagnose()
Gives info that can be useful for debugging.
|
protected void |
finalize()
Explicitly call
close() instead to avoid expensive finalization. |
java.util.Collection<java.lang.Class<?>> |
getAllEntityClasses() |
static java.lang.Object |
getContext() |
static BoxStore |
getDefault()
Convenience singleton instance which gets set up using
BoxStoreBuilder.buildDefault() . |
int |
getEntityTypeIdOrThrow(java.lang.Class<?> entityClass) |
long |
getNativeStore()
If you want to use the same ObjectBox store using the C API, e.g.
|
int |
getObjectBrowserPort() |
static java.lang.Object |
getRelinker() |
SyncClient |
getSyncClient()
Returns the
SyncClient associated with this store. |
static java.lang.String |
getVersion()
Gets the Version of ObjectBox Java.
|
static java.lang.String |
getVersionNative()
Gets the Version of ObjectBox Core.
|
TxCallback<?> |
internalFailedReadTxAttemptCallback() |
int |
internalQueryAttempts() |
java.util.concurrent.Future<?> |
internalScheduleThread(java.lang.Runnable runnable) |
java.util.concurrent.ExecutorService |
internalThreadPool() |
boolean |
isClosed() |
static boolean |
isDatabaseOpen(java.io.File directory)
Using a directory, as configured with
BoxStoreBuilder.directory(File) ,
checks if the associated database files are in use by a BoxStore instance. |
static boolean |
isDatabaseOpen(java.io.File baseDirectoryOrNull,
java.lang.String dbNameOrNull)
Using an optional base directory, as configured with
BoxStoreBuilder.baseDirectory(File) ,
and an optional database name, as configured with BoxStoreBuilder.name(String) ,
checks if the associated database files are in use by a BoxStore instance. |
static boolean |
isDatabaseOpen(java.lang.Object context,
java.lang.String dbNameOrNull)
Using an Android Context and an optional database name, as configured with
BoxStoreBuilder.name(String) ,
checks if the associated database files are in use by a BoxStore instance. |
boolean |
isDebugRelations() |
static boolean |
isObjectBrowserAvailable() |
boolean |
isObjectBrowserRunning() |
boolean |
isReadOnly()
Whether the store was created using read-only mode.
|
static boolean |
isSyncAvailable() |
static boolean |
isSyncServerAvailable() |
void |
removeAllObjects()
Removes all objects from all types ("boxes"), e.g.
|
void |
runInReadTx(java.lang.Runnable runnable)
Runs the given runnable inside a read(-only) transaction.
|
void |
runInTx(java.lang.Runnable runnable)
Runs the given runnable inside a transaction.
|
void |
runInTxAsync(java.lang.Runnable runnable,
TxCallback<java.lang.Void> callback)
Runs the given Runnable as a transaction in a separate thread.
|
void |
setDbExceptionListener(DbExceptionListener dbExceptionListener)
Sets a listener that will be called when an exception is thrown.
|
long |
sizeOnDisk()
The size in bytes occupied by the data file on disk.
|
java.lang.String |
startObjectBrowser() |
java.lang.String |
startObjectBrowser(int port) |
java.lang.String |
startObjectBrowser(java.lang.String urlToBindTo) |
boolean |
stopObjectBrowser() |
SubscriptionBuilder<java.lang.Class> |
subscribe()
A
DataObserver can be subscribed to data changes using the returned builder. |
<T> SubscriptionBuilder<java.lang.Class<T>> |
subscribe(java.lang.Class<T> forClass)
Like
subscribe() , but wires the supplied @DataObserver only to the given
object class for notifications. |
static void |
testUnalignedMemoryAccess()
Diagnostics: If this method crashes on a device, please send us the logcat output.
|
void |
unregisterTransaction(io.objectbox.Transaction transaction) |
long |
validate(long pageLimit,
boolean checkLeafLevel)
Validate database pages, a lower level storage unit (integrity check).
|
public static final java.lang.String JNI_VERSION
@Nullable @Internal public static java.lang.Object getContext()
@Nullable @Internal public static java.lang.Object getRelinker()
public static BoxStore getDefault()
BoxStoreBuilder.buildDefault()
.
Note: for better testability, you can usually avoid singletons by storing
a BoxStore
instance in some application scope object and pass it along.
public static boolean clearDefaultStore()
Note: This is usually not required (for testability, please see the comment of getDefault()
).
public static java.lang.String getVersion()
public static java.lang.String getVersionNative()
public static void testUnalignedMemoryAccess()
public static boolean isObjectBrowserAvailable()
public static boolean isSyncAvailable()
public static boolean isSyncServerAvailable()
public static boolean isDatabaseOpen(java.lang.Object context, @Nullable java.lang.String dbNameOrNull) throws java.io.IOException
BoxStoreBuilder.name(String)
,
checks if the associated database files are in use by a BoxStore instance.
Use this to check that database files are not open before copying or deleting them.
java.io.IOException
public static boolean isDatabaseOpen(@Nullable java.io.File baseDirectoryOrNull, @Nullable java.lang.String dbNameOrNull) throws java.io.IOException
BoxStoreBuilder.baseDirectory(File)
,
and an optional database name, as configured with BoxStoreBuilder.name(String)
,
checks if the associated database files are in use by a BoxStore instance.
Use this to check that database files are not open before copying or deleting them.
java.io.IOException
public static boolean isDatabaseOpen(java.io.File directory) throws java.io.IOException
BoxStoreBuilder.directory(File)
,
checks if the associated database files are in use by a BoxStore instance.
Use this to check that database files are not open before copying or deleting them.
java.io.IOException
public long sizeOnDisk()
protected void finalize() throws java.lang.Throwable
close()
instead to avoid expensive finalization.finalize
in class java.lang.Object
java.lang.Throwable
@Internal public int getEntityTypeIdOrThrow(java.lang.Class<?> entityClass)
public java.util.Collection<java.lang.Class<?>> getAllEntityClasses()
@Internal public io.objectbox.Transaction beginTx()
runInTx(Runnable)
instead.@Internal public io.objectbox.Transaction beginReadTx()
runInReadTx(Runnable)
instead.
Begins a transaction for read access only. Note: there may be only one read transaction per thread.public boolean isClosed()
public boolean isReadOnly()
public void close()
WARNING: This is a somewhat delicate thing to do if you have threads running that may potentially still use the BoxStore. This results in undefined behavior, including the possibility of crashing.
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
public boolean deleteAllFiles()
close()
before and read the docs of that method carefully!
A safer alternative: use the static deleteAllFiles(File)
method before opening the BoxStore.
public static boolean deleteAllFiles(java.io.File objectStoreDirectory)
No BoxStore
may be alive using the given directory.
If you did not use a custom name with BoxStoreBuilder, you can pass "new File(BoxStoreBuilder.DEFAULT_NAME
)".
objectStoreDirectory
- directory to be deleted; this is the value you previously provided to BoxStoreBuilder.directory(File)
java.lang.IllegalStateException
- if the given directory is still used by a open BoxStore
.public static boolean deleteAllFiles(java.lang.Object androidContext, @Nullable java.lang.String customDbNameOrNull)
No BoxStore
may be alive using the given name.
If you did not use a custom name with BoxStoreBuilder, you can pass "new File(BoxStoreBuilder.DEFAULT_NAME
)".
androidContext
- provide an Android Context like Application or ServicecustomDbNameOrNull
- use null for default name, or the name you previously provided to BoxStoreBuilder.name(String)
.java.lang.IllegalStateException
- if the given name is still used by a open BoxStore
.public static boolean deleteAllFiles(@Nullable java.io.File baseDirectoryOrNull, @Nullable java.lang.String customDbNameOrNull)
No BoxStore
may be alive using the given directory.
If you did not use a custom name with BoxStoreBuilder, you can pass "new File(BoxStoreBuilder.DEFAULT_NAME
)".
baseDirectoryOrNull
- use null for no base dir, or the value you previously provided to BoxStoreBuilder.baseDirectory(File)
customDbNameOrNull
- use null for default name, or the name you previously provided to BoxStoreBuilder.name(String)
.java.lang.IllegalStateException
- if the given directory (+name) is still used by a open BoxStore
.public void removeAllObjects()
Box.removeAll()
).
Note that this does not reclaim disk space: the already reserved space for the DB file(s) is used in the future resulting in better performance because no/less disk allocation has to be done.
If you want to reclaim disk space, delete the DB file(s) instead:
close()
the BoxStore (and ensure that no thread access it)deleteAllFiles()
of the BoxStore@Internal public void unregisterTransaction(io.objectbox.Transaction transaction)
public <T> Box<T> boxFor(java.lang.Class<T> entityClass)
Creates a Box only once and then always returns the cached instance.
public void runInTx(java.lang.Runnable runnable)
Efficiency notes: it is advised to run multiple puts in a transaction because each commit requires an expensive disk synchronization.
public void runInReadTx(java.lang.Runnable runnable)
@Experimental public <T> T callInReadTxWithRetry(java.util.concurrent.Callable<T> callable, int attempts, int initialBackOffInMs, boolean logAndHeal)
callInReadTx(Callable)
and retries in case a DbException is thrown.
If the given amount of attempts is reached, the last DbException will be thrown.
Experimental: API might change.public <T> T callInReadTx(java.util.concurrent.Callable<T> callable)
public <R> R callInTx(java.util.concurrent.Callable<R> callable) throws java.lang.Exception
runInTx(Runnable)
, but allows returning a value and throwing an exception.java.lang.Exception
public <R> R callInTxNoException(java.util.concurrent.Callable<R> callable)
callInTx(Callable)
, but throws no Exception.
Any Exception thrown in the Callable is wrapped in a RuntimeException.public void runInTxAsync(java.lang.Runnable runnable, @Nullable TxCallback<java.lang.Void> callback)
See also runInTx(Runnable)
.
public <R> void callInTxAsync(java.util.concurrent.Callable<R> callable, @Nullable TxCallback<R> callback)
* See also callInTx(Callable)
.
public java.lang.String diagnose()
@Beta public long validate(long pageLimit, boolean checkLeafLevel)
pageLimit
- the maximum of pages to validate (e.g. to limit time spent on validation).
Pass zero set no limit and thus validate all pages.checkLeafLevel
- Flag to validate leaf pages. These do not point to other pages but contain data.DbException
- if validation failed to run (does not tell anything about DB file consistency).FileCorruptException
- if the DB file is actually inconsistent (corrupt).public int cleanStaleReadTransactions()
public void closeThreadResources()
Box.closeThreadResources()
for all initiated boxes (boxFor(Class)
).public SubscriptionBuilder<java.lang.Class> subscribe()
DataObserver
can be subscribed to data changes using the returned builder.
The observer is supplied via SubscriptionBuilder.observer(DataObserver)
and will be notified once a
transaction is committed and will receive changes to any object class.
Threading notes: All observers are notified from one separate thread (pooled). Observers are not notified in parallel. The notification order is the same as the subscription order, although this may not always be guaranteed in the future.
Note that failed or aborted transaction do not trigger observers.
@Experimental @Nullable public java.lang.String startObjectBrowser()
@Experimental @Nullable public java.lang.String startObjectBrowser(int port)
@Experimental @Nullable public java.lang.String startObjectBrowser(java.lang.String urlToBindTo)
@Experimental public boolean stopObjectBrowser()
@Experimental public int getObjectBrowserPort()
public boolean isObjectBrowserRunning()
public void setDbExceptionListener(@Nullable DbExceptionListener dbExceptionListener)
null
to remove the listener.
This for example allows central error handling or special logging for database-related exceptions.
public <T> SubscriptionBuilder<java.lang.Class<T>> subscribe(java.lang.Class<T> forClass)
subscribe()
, but wires the supplied @DataObserver
only to the given
object class for notifications.@Internal public java.util.concurrent.Future<?> internalScheduleThread(java.lang.Runnable runnable)
@Internal public java.util.concurrent.ExecutorService internalThreadPool()
@Internal public boolean isDebugRelations()
@Internal public int internalQueryAttempts()
@Internal public TxCallback<?> internalFailedReadTxAttemptCallback()
public long getNativeStore()
Note: Once you close()
this BoxStore, do not use it from the C API.
@Nullable public SyncClient getSyncClient()
SyncClient
associated with this store. To create one see Sync
.