Class BoxStoreBuilder
- java.lang.Object
-
- io.objectbox.BoxStoreBuilder
-
public class BoxStoreBuilder extends java.lang.ObjectConfigures and builds aBoxStorewith reasonable defaults. To get an instance useMyObjectBox.builder().On Android, make sure to provide a Context to
androidContext(context).Some common defaults to override are:
- Name/location of Store: use either
name(String),baseDirectory(File),androidContext(Object)ordirectory(File)(default: name "objectbox), - Max DB size: see
maxSizeInKByte(long)(default: 1024 * 1024 KB = 1 GB), - Max readers: see
maxReaders(int)(default: 126),
- Name/location of Store: use either
-
-
Field Summary
Fields Modifier and Type Field Description private java.io.FilebaseDirectoryIgnored by BoxStore(package private) java.lang.ObjectcontextOn Android used for native library loading.(package private) intdebugFlags(package private) booleandebugRelationsstatic intDEFAULT_MAX_DB_SIZE_KBYTEThe default maximum size the DB can grow to, which can be overwritten usingmaxSizeInKByte.static java.lang.StringDEFAULT_NAMEThe default DB name, which can be overwritten usingname(String).(package private) java.io.FiledirectoryBoxStore uses this (not baseDirectory/name)(package private) java.util.List<EntityInfo<?>>entityInfoList(package private) TxCallback<?>failedReadTxAttemptCallback(package private) intfileModeprivate Factory<java.io.InputStream>initialDbFileFactoryprivate java.lang.StringinMemoryIf non-null, using an in-memory database with this identifier.(package private) longmaxDataSizeInKByte(package private) intmaxReaders(package private) longmaxSizeInKByteDefaults toDEFAULT_MAX_DB_SIZE_KBYTE.(package private) byte[]model(package private) ModelUpdatemodelUpdateprivate java.lang.StringnameIgnored by BoxStore(package private) booleannoReaderThreadLocals(package private) intqueryAttempts(package private) booleanreadOnly(package private) java.lang.Objectrelinker(package private) booleanskipReadSchemaFor DebugCursor.(package private) booleanusePreviousCommit(package private) shortvalidateOnOpenModeKv(package private) shortvalidateOnOpenModePages(package private) longvalidateOnOpenPageLimit
-
Constructor Summary
Constructors Modifier Constructor Description privateBoxStoreBuilder()BoxStoreBuilder(byte[] model)Called internally from the generated class "MyObjectBox".
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description BoxStoreBuilderandroidContext(java.lang.Object context)Use on Android to pass a Context for loading the native library and, if not aninMemory(String)database, for creating the base directory for database files in the files directory of the app.BoxStoreBuilderandroidReLinker(java.lang.Object reLinkerInstance)Pass a custom ReLinkerInstance, for exampleReLinker.log(logger)to use for loading the native library on Android devices.BoxStoreBuilderbaseDirectory(java.io.File baseDirectory)In combination withname(String), this lets you specify the location of where the DB files should be stored.BoxStorebuild()Builds aBoxStoreusing the current configuration of this builder.BoxStorebuildDefault()Builds the defaultBoxStoreinstance, which can be acquired usingBoxStore.getDefault().(package private) byte[]buildFlatStoreOptions(java.lang.String canonicalPath)private static voidcheckIsNull(java.lang.Object value, java.lang.String errorMessage)Use to check conflicting properties are not set.private voidcheckProvisionInitialDbFile()(package private) BoxStoreBuildercreateClone(java.lang.String namePostfix)static BoxStoreBuildercreateDebugWithoutModel()Not for application use, for DebugCursor.private static java.lang.StringdbName(java.lang.String dbNameOrNull)BoxStoreBuilderdebugFlags(int debugFlags)Debug flags typically enable additional logging, seeDebugFlagsfor valid values.BoxStoreBuilderdebugRelations()Enables some debug logging for relations.BoxStoreBuilderdebugTransactions()Deprecated.UsedebugFlagsinstead.BoxStoreBuilderdirectory(java.io.File directory)The directory where all database files should be placed in.voidentity(EntityInfo<?> entityInfo)BoxStoreBuilderfailedReadTxAttemptCallback(TxCallback<?> failedReadTxAttemptCallback)Define a callback for failed read transactions during retires (see alsoqueryAttempts(int)).BoxStoreBuilderfileMode(int mode)Specify unix-style file permissions for database files.(package private) static java.io.FilegetAndroidBaseDir(java.lang.Object context)(package private) static java.io.FilegetAndroidDbDir(java.lang.Object context, java.lang.String dbName)private static java.io.FilegetAndroidFilesDir(java.lang.Object context)private java.lang.ObjectgetApplicationContext(java.lang.Object context)(package private) static java.io.FilegetDbDir(java.io.File baseDirectoryOrNull, java.lang.String nameOrNull)BoxStoreBuilderinitialDbFile(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).BoxStoreBuilderinitialDbFile(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).BoxStoreBuilderinMemory(java.lang.String identifier)Switches to an in-memory database using the given name as its identifier.BoxStoreBuildermaxDataSizeInKByte(long maxDataSizeInKByte)Sets the maximum size the data stored in the database can grow to.BoxStoreBuildermaxReaders(int maxReaders)Sets the maximum number of concurrent readers.BoxStoreBuildermaxSizeInKByte(long maxSizeInKByte)Sets the maximum size the database file can grow to.(package private) BoxStoreBuildermodelUpdate(ModelUpdate modelUpdate)BoxStoreBuildername(java.lang.String name)Name of the database, which will be used as a directory for database files.BoxStoreBuildernoReaderThreadLocals()Disables the usage of thread locals for "readers" related to read transactions.BoxStoreBuilderqueryAttempts(int queryAttempts)For massive concurrent setups (app is using a lot of threads), you can enable automatic retries for queries.BoxStoreBuilderreadOnly()Open the store in read-only mode: no schema update, no write transactions are allowed (would throw).BoxStoreBuilderusePreviousCommit()Ignores the latest data snapshot (committed transaction state) and uses the previous snapshot instead.BoxStoreBuildervalidateOnOpen(short validateOnOpenModePages)When a database is opened, ObjectBox can perform additional consistency checks on its database structure.BoxStoreBuildervalidateOnOpenKv()When a database is opened, ObjectBox can perform additional consistency checks on its database structure.BoxStoreBuildervalidateOnOpenKv(short mode)When a database is opened, ObjectBox can perform additional consistency checks on its database structure.BoxStoreBuildervalidateOnOpenPageLimit(long limit)To fine-tunevalidateOnOpen(short), you can specify a limit on how much data is looked at.
-
-
-
Field Detail
-
DEFAULT_NAME
public static final java.lang.String DEFAULT_NAME
The default DB name, which can be overwritten usingname(String).- See Also:
- Constant Field Values
-
DEFAULT_MAX_DB_SIZE_KBYTE
public static final int DEFAULT_MAX_DB_SIZE_KBYTE
The default maximum size the DB can grow to, which can be overwritten usingmaxSizeInKByte.- See Also:
- Constant Field Values
-
model
final byte[] model
-
directory
java.io.File directory
BoxStore uses this (not baseDirectory/name)
-
baseDirectory
private java.io.File baseDirectory
Ignored by BoxStore
-
name
private java.lang.String name
Ignored by BoxStore
-
inMemory
private java.lang.String inMemory
If non-null, using an in-memory database with this identifier.
-
maxSizeInKByte
long maxSizeInKByte
Defaults toDEFAULT_MAX_DB_SIZE_KBYTE.
-
maxDataSizeInKByte
long maxDataSizeInKByte
-
context
@Nullable java.lang.Object context
On Android used for native library loading.
-
relinker
@Nullable java.lang.Object relinker
-
modelUpdate
ModelUpdate modelUpdate
-
debugFlags
int debugFlags
-
debugRelations
boolean debugRelations
-
fileMode
int fileMode
-
maxReaders
int maxReaders
-
noReaderThreadLocals
boolean noReaderThreadLocals
-
queryAttempts
int queryAttempts
-
skipReadSchema
boolean skipReadSchema
For DebugCursor.
-
readOnly
boolean readOnly
-
usePreviousCommit
boolean usePreviousCommit
-
validateOnOpenModePages
short validateOnOpenModePages
-
validateOnOpenPageLimit
long validateOnOpenPageLimit
-
validateOnOpenModeKv
short validateOnOpenModeKv
-
failedReadTxAttemptCallback
TxCallback<?> failedReadTxAttemptCallback
-
entityInfoList
final java.util.List<EntityInfo<?>> entityInfoList
-
initialDbFileFactory
private Factory<java.io.InputStream> initialDbFileFactory
-
-
Constructor Detail
-
BoxStoreBuilder
private BoxStoreBuilder()
-
BoxStoreBuilder
@Internal public BoxStoreBuilder(byte[] model)
Called internally from the generated class "MyObjectBox". Check MyObjectBox.builder() to get an instance.
-
-
Method Detail
-
createDebugWithoutModel
@Internal public static BoxStoreBuilder createDebugWithoutModel()
Not for application use, for DebugCursor.
-
name
public BoxStoreBuilder name(java.lang.String name)
Name of the database, which will be used as a directory for database files. You can also specify a base directory for this one usingbaseDirectory(File). Cannot be used in combination withdirectory(File)andinMemory(String).Default: "objectbox",
DEFAULT_NAME(unlessdirectory(File)is used)
-
directory
public BoxStoreBuilder directory(java.io.File directory)
The directory where all database files should be placed in.If the directory does not exist, it will be created. Make sure the process has permissions to write to this directory.
To switch to an in-memory database, use a file path with
BoxStore.IN_MEMORY_PREFIXand an identifier instead:
Alternatively, useBoxStore inMemoryStore = MyObjectBox.builder() .directory(BoxStore.IN_MEMORY_PREFIX + "notes-db") .build();inMemory(String).Can not be used in combination with
name(String),baseDirectory(File)orinMemory(String).
-
baseDirectory
public BoxStoreBuilder baseDirectory(java.io.File baseDirectory)
In combination withname(String), this lets you specify the location of where the DB files should be stored. Cannot be used in combination withdirectory(File)orinMemory(String).
-
inMemory
public BoxStoreBuilder inMemory(java.lang.String identifier)
Switches to an in-memory database using the given name as its identifier.Can not be used in combination with
name(String),directory(File)orbaseDirectory(File).
-
checkIsNull
private static void checkIsNull(@Nullable java.lang.Object value, java.lang.String errorMessage)Use to check conflicting properties are not set. If not null, throwsIllegalStateExceptionwith the given message.
-
androidContext
public BoxStoreBuilder androidContext(java.lang.Object context)
Use on Android to pass a Context for loading the native library and, if not aninMemory(String)database, for creating the base directory for database files in the files directory of the app.In more detail, upon
build()assigns the base directory (seebaseDirectory) tocontext.getFilesDir() + "/objectbox/". Thus, when using the default name (also "objectbox", unless overwritten usingname(String)), the default location of database files will be "objectbox/objectbox/" inside the app's files directory. If a custom name is specified, for example withname("foobar"), it would become "objectbox/foobar/".Use
baseDirectory(File)ordirectory(File)to specify a different directory for the database files.
-
getApplicationContext
private java.lang.Object getApplicationContext(java.lang.Object context)
-
androidReLinker
public BoxStoreBuilder androidReLinker(java.lang.Object reLinkerInstance)
Pass a custom ReLinkerInstance, for exampleReLinker.log(logger)to use for loading the native library on Android devices. Note that settingandroidContext(Object)is required for ReLinker to work.
-
getAndroidDbDir
static java.io.File getAndroidDbDir(java.lang.Object context, @Nullable java.lang.String dbName)
-
dbName
private static java.lang.String dbName(@Nullable java.lang.String dbNameOrNull)
-
getAndroidBaseDir
static java.io.File getAndroidBaseDir(java.lang.Object context)
-
getAndroidFilesDir
@Nonnull private static java.io.File getAndroidFilesDir(java.lang.Object context)
-
fileMode
public BoxStoreBuilder fileMode(int mode)
Specify unix-style file permissions for database files. E.g. for-rw-r----(owner, group, other) pass the octal code0640. 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.
-
maxReaders
public BoxStoreBuilder maxReaders(int maxReaders)
Sets the maximum number of concurrent readers. For most applications, the default is fine (about 126 readers).A "reader" is short for a thread involved in a read transaction. If the maximum is exceeded the store throws
DbMaxReadersExceededException. In this case check that your code only uses a reasonable amount of threads.For highly concurrent setups (e.g. you are using ObjectBox on the server side) it may make sense to increase the number.
Note: Each thread that performed a read transaction and is still alive holds on to a reader slot. These slots only get vacated when the thread ends. Thus, be mindful with the number of active threads. Alternatively, you can try the experimental
noReaderThreadLocals()option flag.
-
noReaderThreadLocals
public BoxStoreBuilder noReaderThreadLocals()
Disables the usage of thread locals for "readers" related to read transactions. This can make sense if you are using a lot of threads that are kept alive.Note: This is still experimental, as it comes with subtle behavior changes at a low level and may affect corner cases with e.g. transactions, which may not be fully tested at the moment.
-
entity
@Internal public void entity(EntityInfo<?> entityInfo)
-
modelUpdate
BoxStoreBuilder modelUpdate(ModelUpdate modelUpdate)
-
maxSizeInKByte
public BoxStoreBuilder maxSizeInKByte(long maxSizeInKByte)
Sets the maximum size the database file can grow to. When applying a transaction (e.g. putting an object) would exceed it aDbFullExceptionis thrown.By default, this is 1 GB, which should be sufficient for most applications. In general, a maximum size prevents the database from growing indefinitely when something goes wrong (for example data is put in an infinite loop).
This value can be changed, so increased or also decreased, each time when opening a store.
-
maxDataSizeInKByte
public BoxStoreBuilder maxDataSizeInKByte(long maxDataSizeInKByte)
Sets the maximum size the data stored in the database can grow to. When applying a transaction (e.g. putting an object) would exceed it aDbMaxDataSizeExceededExceptionis thrown.Must be below
maxSizeInKByte(long).Different from
maxSizeInKByte(long)this only counts bytes stored in objects, excluding system and metadata. However, it is more involved than database size tracking, e.g. it stores an internal counter. Only use this if a stricter, more accurate limit is required.When the data limit is reached, data can be removed to get below the limit again (assuming the database size limit is not also reached).
-
readOnly
public BoxStoreBuilder readOnly()
Open the store in read-only mode: no schema update, no write transactions are allowed (would throw).
-
usePreviousCommit
public BoxStoreBuilder usePreviousCommit()
Ignores the latest data snapshot (committed transaction state) and uses the previous snapshot instead. When used with care (e.g. backup the DB files first), this option may also recover data removed by the latest transaction.To ensure no data is lost accidentally, it is recommended to use this in combination with
readOnly()to examine and validate the database first.
-
validateOnOpen
public BoxStoreBuilder validateOnOpen(short validateOnOpenModePages)
When a database is opened, ObjectBox can perform additional consistency checks on its database structure. Reliable file systems already guarantee consistency, so this is primarily meant to deal with unreliable OSes, file systems, or hardware.Note: ObjectBox builds upon ACID storage, which already has strong consistency mechanisms in place.
See also
validateOnOpenPageLimit(long)to fine-tune this check andvalidateOnOpenKv(short)for additional checks.- Parameters:
validateOnOpenModePages- One ofValidateOnOpenModePages.
-
validateOnOpenPageLimit
public BoxStoreBuilder validateOnOpenPageLimit(long limit)
To fine-tunevalidateOnOpen(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
ValidateOnOpenModePages.RegularandValidateOnOpenModePages.WithLeaves.
-
validateOnOpenKv
public BoxStoreBuilder validateOnOpenKv()
When a database is opened, ObjectBox can perform additional consistency checks on its database structure. This enables validation checks on a key/value level.This is a shortcut for
validateOnOpenKv(ValidateOnOpenModeKv.Regular).
-
validateOnOpenKv
public BoxStoreBuilder validateOnOpenKv(short mode)
When a database is opened, ObjectBox can perform additional consistency checks on its database structure. This enables validation checks on a key/value level.See also
validateOnOpen(short)for additional consistency checks.- Parameters:
mode- One ofValidateOnOpenModeKv.
-
debugTransactions
@Deprecated public BoxStoreBuilder debugTransactions()
Deprecated.UsedebugFlagsinstead.
-
debugFlags
public BoxStoreBuilder debugFlags(int debugFlags)
Debug flags typically enable additional logging, seeDebugFlagsfor valid values.Example: debugFlags(
DebugFlags.LOG_TRANSACTIONS_READ|DebugFlags.LOG_TRANSACTIONS_WRITE);
-
debugRelations
public BoxStoreBuilder debugRelations()
Enables some debug logging for relations.
-
queryAttempts
@Experimental public BoxStoreBuilder queryAttempts(int queryAttempts)
For massive concurrent setups (app is using a lot of threads), you can enable automatic retries for queries. This can resolve situations in which resources are getting sparse (e.g.DbMaxReadersExceededExceptionor other variations ofDbExceptionare thrown during query execution).- Parameters:
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.
-
failedReadTxAttemptCallback
@Experimental public BoxStoreBuilder failedReadTxAttemptCallback(TxCallback<?> failedReadTxAttemptCallback)
Define a callback for failed read transactions during retires (see alsoqueryAttempts(int)). Useful for e.g. logging.
-
initialDbFile
@Experimental public 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).
-
initialDbFile
@Experimental public 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). The provider will only be called if no DB file exists yet.
-
buildFlatStoreOptions
byte[] buildFlatStoreOptions(java.lang.String canonicalPath)
-
build
public BoxStore build()
Builds aBoxStoreusing the current configuration of this builder.If
androidContext(Object)was called and nodirectory(File)orbaseDirectory(File)is configured, creates and setsbaseDirectory(File)as explained inandroidContext(Object).
-
checkProvisionInitialDbFile
private void checkProvisionInitialDbFile()
-
getDbDir
static java.io.File getDbDir(@Nullable java.io.File baseDirectoryOrNull, @Nullable java.lang.String nameOrNull)
-
buildDefault
public BoxStore buildDefault()
Builds the defaultBoxStoreinstance, which can be acquired usingBoxStore.getDefault(). For testability, please see the comment ofBoxStore.getDefault().May be called once only (throws otherwise).
-
createClone
@Internal BoxStoreBuilder createClone(java.lang.String namePostfix)
-
-