public class BoxStoreBuilder
extends java.lang.Object
BoxStore with reasonable defaults. To get an instance use MyObjectBox.builder().
On Android, make sure to provide a Context to androidContext(context).
Some common defaults to override are:
name(String), baseDirectory(File),
androidContext(Object) or directory(File) (default: name "objectbox),maxSizeInKByte(long) (default: 1024 * 1024 KB = 1 GB),maxReaders(int) (default: 126),| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_MAX_DB_SIZE_KBYTE
The default maximum size the DB can grow to, which can be overwritten using
maxSizeInKByte. |
static java.lang.String |
DEFAULT_NAME
The default DB name, which can be overwritten using
name(String). |
| Constructor and Description |
|---|
BoxStoreBuilder(byte[] model)
Called internally from the generated class "MyObjectBox".
|
| Modifier and Type | Method and Description |
|---|---|
BoxStoreBuilder |
androidContext(java.lang.Object context)
On Android, you can pass a Context to set the base directory using this method.
|
BoxStoreBuilder |
androidReLinker(java.lang.Object reLinkerInstance)
Pass a custom ReLinkerInstance, for example
ReLinker.log(logger) to use for loading the native library
on Android devices. |
BoxStoreBuilder |
baseDirectory(java.io.File baseDirectory)
In combination with
name(String), this lets you specify the location of where the DB files should be
stored. |
BoxStore |
build()
Builds a
BoxStore using any given configuration. |
BoxStore |
buildDefault()
Builds the default
BoxStore instance, which can be acquired using BoxStore.getDefault(). |
static BoxStoreBuilder |
createDebugWithoutModel()
Not for application use, for DebugCursor.
|
BoxStoreBuilder |
debugFlags(int debugFlags)
Debug flags typically enable additional logging, see
DebugFlags for valid values. |
BoxStoreBuilder |
debugRelations()
Enables some debug logging for relations.
|
BoxStoreBuilder |
debugTransactions()
Deprecated.
Use
debugFlags instead. |
BoxStoreBuilder |
directory(java.io.File directory)
The directory where all DB files should be placed in.
|
void |
entity(EntityInfo<?> entityInfo) |
BoxStoreBuilder |
failedReadTxAttemptCallback(TxCallback<?> failedReadTxAttemptCallback)
Define a callback for failed read transactions during retires (see also
queryAttempts(int)). |
BoxStoreBuilder |
fileMode(int mode)
Specify
unix-style file permissions
for database files.
|
BoxStoreBuilder |
initialDbFile(Factory<java.io.InputStream> initialDbFileFactory)
Let's you specify a provider for a DB file to be used during initial start of the app (no DB file exists yet).
|
BoxStoreBuilder |
initialDbFile(java.io.File initialDbFile)
Let's you specify an DB file to be used during initial start of the app (no DB file exists yet).
|
BoxStoreBuilder |
maxReaders(int maxReaders)
Sets the maximum number of concurrent readers.
|
BoxStoreBuilder |
maxSizeInKByte(long maxSizeInKByte)
Sets the maximum size the database file can grow to.
|
BoxStoreBuilder |
name(java.lang.String name)
Name of the database, which will be used as a directory for DB files.
|
BoxStoreBuilder |
queryAttempts(int queryAttempts)
For massive concurrent setups (app is using a lot of threads), you can enable automatic retries for queries.
|
BoxStoreBuilder |
readOnly()
Open the store in read-only mode: no schema update, no write transactions are allowed (would throw).
|
BoxStoreBuilder |
usePreviousCommit()
Ignores the latest data snapshot (committed transaction state) and uses the previous snapshot instead.
|
BoxStoreBuilder |
validateOnOpen(short validateOnOpenMode)
When a database is opened, ObjectBox can perform additional consistency checks on its database structure.
|
BoxStoreBuilder |
validateOnOpenPageLimit(long limit)
To fine-tune
validateOnOpen(short), you can specify a limit on how much data is looked at. |
public static final java.lang.String DEFAULT_NAME
name(String).public static final int DEFAULT_MAX_DB_SIZE_KBYTE
maxSizeInKByte.@Internal public BoxStoreBuilder(byte[] model)
@Internal public static BoxStoreBuilder createDebugWithoutModel()
public BoxStoreBuilder name(java.lang.String name)
baseDirectory(File).
Cannot be used in combination with directory(File).
Default: "objectbox", DEFAULT_NAME (unless directory(File) is used)
public BoxStoreBuilder directory(java.io.File directory)
name(String)/baseDirectory(File).public BoxStoreBuilder baseDirectory(java.io.File baseDirectory)
name(String), this lets you specify the location of where the DB files should be
stored.
Cannot be used in combination with directory(File).public BoxStoreBuilder androidContext(java.lang.Object context)
In more detail, this assigns the base directory (see baseDirectory) to
context.getFilesDir() + "/objectbox/".
Thus, when using the default name (also "objectbox" unless overwritten using name(String)), the default
location of DB files will be "objectbox/objectbox/" inside the app files directory.
If you specify a custom name, for example with name("foobar"), it would become
"objectbox/foobar/".
Alternatively, you can also use baseDirectory or directory(File) instead.
public BoxStoreBuilder androidReLinker(java.lang.Object reLinkerInstance)
ReLinker.log(logger) to use for loading the native library
on Android devices. Note that setting androidContext(Object) is required for ReLinker to work.public BoxStoreBuilder fileMode(int mode)
-rw-r---- (owner, group, other) pass the octal code 0640.
Any newly generated directory additionally gets searchable (01) for groups with read or write permissions.
It's not allowed to pass in an executable flag.public BoxStoreBuilder maxReaders(int maxReaders)
A "reader" is short for a thread involved in a read transaction.
If you hit DbMaxReadersExceededException, you should first worry about the
amount of threads you are using.
For highly concurrent setups (e.g. you are using ObjectBox on the server side) it may make sense to increase the
number.
@Internal public void entity(EntityInfo<?> entityInfo)
public BoxStoreBuilder maxSizeInKByte(long maxSizeInKByte)
In general, a maximum size prevents the DB from growing indefinitely when something goes wrong (for example you insert data in an infinite loop).
public BoxStoreBuilder readOnly()
public BoxStoreBuilder usePreviousCommit()
To ensure no data is lost accidentally, it is recommended to use this in combination with readOnly()
to examine and validate the database first.
public BoxStoreBuilder validateOnOpen(short validateOnOpenMode)
Note: ObjectBox builds upon ACID storage, which already has strong consistency mechanisms in place.
validateOnOpenMode - One of ValidateOnOpenMode.public BoxStoreBuilder validateOnOpenPageLimit(long limit)
validateOnOpen(short), you can specify a limit on how much data is looked at.
This is measured in "pages" with a page typically holding 4000.
Usually a low number (e.g. 1-20) is sufficient and does not impact startup performance significantly.
This can only be used with ValidateOnOpenMode.Regular and ValidateOnOpenMode.WithLeaves.
@Deprecated public BoxStoreBuilder debugTransactions()
debugFlags instead.public BoxStoreBuilder debugFlags(int debugFlags)
DebugFlags for valid values.
Example: debugFlags(DebugFlags.LOG_TRANSACTIONS_READ | DebugFlags.LOG_TRANSACTIONS_WRITE);
public BoxStoreBuilder debugRelations()
@Experimental public BoxStoreBuilder queryAttempts(int queryAttempts)
DbMaxReadersExceededException or other variations of
DbException are thrown during query execution).queryAttempts - number of attempts a query find operation will be executed before failing.
Recommended values are in the range of 2 to 5, e.g. a value of 3 as a starting point.@Experimental public BoxStoreBuilder failedReadTxAttemptCallback(TxCallback<?> failedReadTxAttemptCallback)
queryAttempts(int)).
Useful for e.g. logging.@Experimental public BoxStoreBuilder initialDbFile(java.io.File initialDbFile)
@Experimental public BoxStoreBuilder initialDbFile(Factory<java.io.InputStream> initialDbFileFactory)
public BoxStore buildDefault()
BoxStore instance, which can be acquired using BoxStore.getDefault().
For testability, please see the comment of BoxStore.getDefault().
May be called once only (throws otherwise).