public class EntityStore
extends java.lang.Object
implements java.io.Closeable
EntityStore objects are thread-safe. Multiple threads may safely
call the methods of a shared EntityStore object.
See the package
summary example for an example of using an EntityStore.
Before creating an EntityStore you must create an Environment object using the Berkeley DB engine API. The environment may
contain any number of entity stores and their associated databases, as well
as other databases not associated with an entity store.
An entity store is based on an EntityModel: a data model which
defines persistent classes (entity classes), primary keys,
secondary keys, and relationships between entities. A primary index is
created for each entity class. An associated secondary index is created for
each secondary key. The Entity, PrimaryKey and SecondaryKey annotations may be used to define entities and keys.
To use an EntityStore, first obtain PrimaryIndex and
SecondaryIndex objects by calling getPrimaryIndex and getSecondaryIndex. Then use
these indices to store and access entity records by key.
Although not normally needed, you can also use the entity store along
with the Base API. Methods in the PrimaryIndex and SecondaryIndex classes may be used to obtain
databases and bindings. The databases may be used directly for accessing
entity records. The bindings should be called explicitly to translate
between DatabaseEntry objects and entity model
objects.
Each primary and secondary index is associated internally with a Database. With any of the above mentioned use cases, methods are provided
that may be used for database performance tuning. The setPrimaryConfig and setSecondaryConfig methods may be called anytime before a database is
opened via getPrimaryIndex or getSecondaryIndex. The setSequenceConfig method may be called anytime before getSequence is called or getPrimaryIndex is called
for a primary index associated with that sequence.
The database names of primary and secondary indices are designed to be
unique within the environment and identifiable for debugging and use with
tools such as DbDump and DbLoad.
The syntax of a primary index database name is:
persist#STORE_NAME#ENTITY_CLASS
Where STORE_NAME is the name parameter passed to EntityStore and ENTITY_CLASS is name of the class passed to getPrimaryIndex.
The syntax of a secondary index database name is:
persist#STORE_NAME#ENTITY_CLASS#KEY_NAME
Where KEY_NAME is the secondary key name passed to getSecondaryIndex.
Although you should never have to construct these names manually, understanding their syntax is useful for several reasons:
EntityStore, to avoid naming conflicts the other
database names should not begin with "persist#".DbDump or DbLoad to perform a backup or copy databases between
environments, knowing the database names can be useful. Normally you will
dump or load all database names starting with
"persist#STORE_NAME#".If you are copying all databases in a store as mentioned in the last point above, there is one further consideration. There are two internal databases that must be kept with the other databases in the store in order for the store to be used. These contain the data formats and sequences for the store:
persist#STORE_NAME#com.sleepycat.persist.formats
persist#STORE_NAME#com.sleepycat.persist.sequences
These databases must normally be included with copies of other databases in the store. They should not be modified by the application.
For example, the following code snippet removes all databases for a given store in a single transaction.
Environment env = ...
EntityStore store = ...
Transaction txn = env.beginTransaction(null, null);
String prefix = "persist#" + store.getStoreName() + "#";
for (String dbName : env.getDatabaseNames()) {
if (dbName.startsWith(prefix)) {
env.removeDatabase(txn, dbName);
}
}
txn.commit();
| Constructor and Description |
|---|
EntityStore(Environment env,
java.lang.String storeName,
StoreConfig config)
Opens an entity store in a given environment.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes all databases and sequences that were opened via this store.
|
void |
closeClass(java.lang.Class entityClass)
Closes the primary and secondary databases for the given entity class
that were opened via this store.
|
EvolveStats |
evolve(EvolveConfig config)
Performs conversion of unevolved objects in order to reduce lazy
conversion overhead.
|
StoreConfig |
getConfig()
Returns a copy of the entity store configuration.
|
Environment |
getEnvironment()
Returns the environment associated with this store.
|
EntityModel |
getModel()
Returns the current entity model for this store.
|
Mutations |
getMutations()
Returns the set of mutations that were configured when the store was
opened, or if none were configured, the set of mutations that were
configured and stored previously.
|
DatabaseConfig |
getPrimaryConfig(java.lang.Class entityClass)
Returns the default primary database Berkeley DB engine API
configuration for an entity class.
|
<PK,E> PrimaryIndex<PK,E> |
getPrimaryIndex(java.lang.Class<PK> primaryKeyClass,
java.lang.Class<E> entityClass)
Returns the primary index for a given entity class, opening it if
necessary.
|
SecondaryConfig |
getSecondaryConfig(java.lang.Class entityClass,
java.lang.String keyName)
Returns the default secondary database Berkeley DB engine API
configuration for an entity class and key name.
|
<SK,PK,E> SecondaryIndex<SK,PK,E> |
getSecondaryIndex(PrimaryIndex<PK,E> primaryIndex,
java.lang.Class<SK> keyClass,
java.lang.String keyName)
Returns a secondary index for a given primary index and secondary key,
opening it if necessary.
|
Sequence |
getSequence(java.lang.String name)
Returns a named sequence for using Berkeley DB engine API directly,
opening it if necessary.
|
SequenceConfig |
getSequenceConfig(java.lang.String name)
Returns the default Berkeley DB engine API configuration for a named key
sequence.
|
java.lang.String |
getStoreName()
Returns the name of this store.
|
static java.util.Set<java.lang.String> |
getStoreNames(Environment env)
Returns the names of all entity stores in the given environment.
|
<SK,PK,E1,E2 extends E1> |
getSubclassIndex(PrimaryIndex<PK,E1> primaryIndex,
java.lang.Class<E2> entitySubclass,
java.lang.Class<SK> keyClass,
java.lang.String keyName)
Returns a secondary index for a secondary key in an entity subclass,
opening it if necessary.
|
void |
setPrimaryConfig(java.lang.Class entityClass,
DatabaseConfig config)
Configures the primary database for an entity class using the Berkeley
DB engine API.
|
void |
setSecondaryConfig(java.lang.Class entityClass,
java.lang.String keyName,
SecondaryConfig config)
Configures a secondary database for an entity class and key name using
the Berkeley DB engine API.
|
void |
setSequenceConfig(java.lang.String name,
SequenceConfig config)
Configures a named key sequence using the Berkeley DB engine API.
|
void |
sync()
Flushes each modified index to disk that was opened in deferred-write
mode.
|
void |
truncateClass(java.lang.Class entityClass)
Deletes all instances of this entity class and its (non-entity)
subclasses.
|
void |
truncateClass(Transaction txn,
java.lang.Class entityClass)
Deletes all instances of this entity class and its (non-entity)
subclasses.
|
public EntityStore(Environment env, java.lang.String storeName, StoreConfig config) throws StoreExistsException, StoreNotFoundException, IncompatibleClassException, DatabaseException
env - an open Berkeley DB Environment.storeName - the name of the entity store within the given
environment. An empty string is allowed. Named stores may be used to
distinguish multiple sets of persistent entities for the same entity
classes in a single environment. Underlying database names are prefixed
with the store name.config - the entity store configuration, or null to use default
configuration properties.StoreExistsException - when the ExclusiveCreate configuration parameter
is true and the store's internal catalog database already exists.StoreNotFoundException - when when the AllowCreate configuration parameter is false
and the store's internal catalog database does not exist.IncompatibleClassException - if an incompatible class change has
been made and mutations are not configured for handling the change. See
Class Evolution for more
information.
OperationFailureException - if one of the Read Operation
Failures occurs. If the store does not exist and the AllowCreate parameter is true, then one of
the Write
Operation Failures may also occur.EnvironmentFailureException - if an unexpected, internal or
environment-wide failure occurs.
DatabaseException - the base class for all BDB exceptions.public Environment getEnvironment()
public StoreConfig getConfig()
public java.lang.String getStoreName()
public static java.util.Set<java.lang.String> getStoreNames(Environment env) throws DatabaseException
OperationFailureException - if one of the Read Operation
Failures occurs.EnvironmentFailureException - if an unexpected, internal or
environment-wide failure occurs.DatabaseException - the base class for all BDB exceptions.public EntityModel getModel()
public Mutations getMutations()
public <PK,E> PrimaryIndex<PK,E> getPrimaryIndex(java.lang.Class<PK> primaryKeyClass, java.lang.Class<E> entityClass) throws DatabaseException
If they are not already open, the primary and secondary databases for the entity class are created/opened together in a single internal transaction. When the secondary indices are opened, that can cascade to open other related primary indices.
PK - the primary key class.E - the entity class.primaryKeyClass - the class of the entity's primary key field, or
the corresponding primitive wrapper class if the primary key field type
is a primitive.entityClass - the entity class for which to open the primary index.java.lang.IllegalArgumentException - if the entity class or classes
referenced by it are not persistent, or the primary key class does not
match the entity's primary key field, or if metadata for the entity or
primary key is invalid.
IndexNotAvailableException - in a replicated environment if this
Replica's persistent classes have been upgraded to define a new index,
but the Master has not yet been upgraded.OperationFailureException - if one of the Read Operation
Failures occurs. If the index does not exist and the ReadOnly parameter is false, then one of the Write
Operation Failures may also occur.EnvironmentFailureException - if an unexpected, internal or
environment-wide failure occurs.
DatabaseException - the base class for all BDB exceptions.public <SK,PK,E> SecondaryIndex<SK,PK,E> getSecondaryIndex(PrimaryIndex<PK,E> primaryIndex, java.lang.Class<SK> keyClass, java.lang.String keyName) throws DatabaseException
NOTE: If the secondary key field is declared in a subclass
of the entity class, use getSubclassIndex(com.sleepycat.persist.PrimaryIndex<PK, E1>, java.lang.Class<E2>, java.lang.Class<SK>, java.lang.String) instead.
If a SecondaryKey.relatedEntity() is used and the primary index
for the related entity is not already open, it will be opened by this
method. That will, in turn, open its secondary indices, which can
cascade to open other primary indices.
SK - the secondary key class.PK - the primary key class.E - the entity class.primaryIndex - the primary index associated with the returned
secondary index. The entity class of the primary index, or one of its
superclasses, must contain a secondary key with the given secondary key
class and key name.keyClass - the class of the secondary key field, or the
corresponding primitive wrapper class if the secondary key field type is
a primitive.keyName - the name of the secondary key field, or the SecondaryKey.name() if this name annotation property was specified.java.lang.IllegalArgumentException - if the entity class or one of its
superclasses does not contain a key field of the given key class and key
name, or if the metadata for the secondary key is invalid.
IndexNotAvailableException - in a replicated environment if this
Replica's persistent classes have been upgraded to define a new index,
but the Master has not yet been upgraded.OperationFailureException - if one of the Read Operation
Failures occurs. If the index does not exist and the ReadOnly parameter is false, then one of the Write
Operation Failures may also occur.EnvironmentFailureException - if an unexpected, internal or
environment-wide failure occurs.
DatabaseException - the base class for all BDB exceptions.public <SK,PK,E1,E2 extends E1> SecondaryIndex<SK,PK,E2> getSubclassIndex(PrimaryIndex<PK,E1> primaryIndex, java.lang.Class<E2> entitySubclass, java.lang.Class<SK> keyClass, java.lang.String keyName) throws DatabaseException
If a SecondaryKey.relatedEntity() is used and the primary index
for the related entity is not already open, it will be opened by this
method. That will, in turn, open its secondary indices, which can
cascade to open other primary indices.
SK - the secondary key class.PK - the primary key class.E1 - the entity class.E2 - the entity sub-class.primaryIndex - the primary index associated with the returned
secondary index. The entity class of the primary index, or one of its
superclasses, must contain a secondary key with the given secondary key
class and key name.entitySubclass - a subclass of the entity class for the primary
index. The entity subclass must contain a secondary key with the given
secondary key class and key name.keyClass - the class of the secondary key field, or the
corresponding primitive wrapper class if the secondary key field type is
a primitive.keyName - the name of the secondary key field, or the SecondaryKey.name() if this name annotation property was specified.java.lang.IllegalArgumentException - if the given entity subclass does not
contain a key field of the given key class and key name, or if the
metadata for the secondary key is invalid.
IndexNotAvailableException - in a replicated environment if this
Replica's persistent classes have been upgraded to define a new index,
but the Master has not yet been upgraded.OperationFailureException - if one of the Read Operation
Failures occurs. If the index does not exist and the ReadOnly parameter is false, then one of the Write
Operation Failures may also occur.EnvironmentFailureException - if an unexpected, internal or
environment-wide failure occurs.
DatabaseException - the base class for all BDB exceptions.public EvolveStats evolve(EvolveConfig config) throws DatabaseException
Conversion is performed one entity class at a time. An entity class
is converted only if it has Mutations associated with it via
StoreConfig.setMutations.
Conversion of an entity class is performed by reading each entity,
converting it if necessary, and updating it if conversion was performed.
When all instances of an entity class are converted, references to the
appropriate Mutations are deleted. Therefore, if this method is
called twice successfully without changing class definitions, the second
call will do nothing.
config - the EvolveConfig.OperationFailureException - if one of the Write
Operation Failures occurs.EnvironmentFailureException - if an unexpected, internal or
environment-wide failure occurs.
DatabaseException - the base class for all BDB exceptions.Class Evolutionpublic void truncateClass(java.lang.Class entityClass)
throws DatabaseException
The primary database for the given entity class will be truncated and all secondary databases will be removed. The primary and secondary databases associated with the entity class must not be open except by this store, since database truncation/removal is only possible when the database is not open.
The primary and secondary databases for the entity class will be
closed by this operation and the existing PrimaryIndex and
SecondaryIndex objects will be invalidated. To access the
indexes, the user must call getPrimaryIndex(java.lang.Class<PK>, java.lang.Class<E>) and getSecondaryIndex(com.sleepycat.persist.PrimaryIndex<PK, E>, java.lang.Class<SK>, java.lang.String) after this operation is complete.
Auto-commit is used implicitly if the store is transactional.
entityClass - the entity class whose instances are to be deleted.
OperationFailureException - if one of the Write
Operation Failures occurs.EnvironmentFailureException - if an unexpected, internal or
environment-wide failure occurs.
DatabaseException - the base class for all BDB exceptions.public void truncateClass(Transaction txn, java.lang.Class entityClass) throws DatabaseException
The primary database for the given entity class will be truncated and all secondary databases will be removed. The primary and secondary databases associated with the entity class must not be open except by this store, since database truncation/removal is only possible when the database is not open.
The primary and secondary databases for the entity class will be
closed by this operation and the existing PrimaryIndex and
SecondaryIndex objects will be invalidated. To access the
indexes, the user must call getPrimaryIndex(java.lang.Class<PK>, java.lang.Class<E>) and getSecondaryIndex(com.sleepycat.persist.PrimaryIndex<PK, E>, java.lang.Class<SK>, java.lang.String) after this operation is complete.
txn - the transaction used to protect this operation, null to use
auto-commit, or null if the store is non-transactional.entityClass - the entity class whose instances are to be deleted.
OperationFailureException - if one of the Write
Operation Failures occurs.EnvironmentFailureException - if an unexpected, internal or
environment-wide failure occurs.
DatabaseException - the base class for all BDB exceptions.public void sync()
throws DatabaseException
All indexes are opened in deferred-write mode if true was passed to
StoreConfig.setDeferredWrite(boolean) for the store.
Alternatively, individual databases may be configured for deferred
write using DatabaseConfig.setDeferredWrite(boolean) along with getPrimaryConfig(java.lang.Class) and setPrimaryConfig(java.lang.Class, com.sleepycat.je.DatabaseConfig). Caution should be
used when configuring only some databases for deferred-write, since
durability will be different for these databases than for other
databases in the same store.
This method is equivalent to calling Database.sync() for each
deferred-write index Database that is open for this store.
Instead of calling this method, Environment.sync() may be used.
The difference is that this method will only flush the databases for
this store, while Environment.sync() will sync all deferred-write
databases currently open for the environment and will also perform a
full checkpoint. This method is therefore less expensive than a full
sync of the environment.
DatabaseException - the base class for all BDB exceptions.public void closeClass(java.lang.Class entityClass)
throws DatabaseException
The primary and secondary databases for the entity class will be
closed by this operation and the existing PrimaryIndex and
SecondaryIndex objects will be invalidated. To access the
indexes, the user must call getPrimaryIndex(java.lang.Class<PK>, java.lang.Class<E>) and getSecondaryIndex(com.sleepycat.persist.PrimaryIndex<PK, E>, java.lang.Class<SK>, java.lang.String) after this operation is complete.
entityClass - the entity class whose databases are to be closed.DatabaseException - the base class for all BDB exceptions.public void close()
throws DatabaseException
WARNING: To prevent memory leaks, the application must call this method even when the Environment has become invalid. While this is not necessary for Database objects, it is necessary for EntityStore objects to prevent the accumulation of memory in the global DPL metadata cache.
WARNING: To guard against memory leaks, the application should discard all references to the closed handle. While BDB makes an effort to discard references from closed objects to the allocated memory for an environment, this behavior is not guaranteed. The safe course of action for an application is to discard all references to closed BDB objects.
close in interface java.io.Closeableclose in interface java.lang.AutoCloseableDatabaseException - the base class for all BDB exceptions.public Sequence getSequence(java.lang.String name) throws DatabaseException
name - the sequence name, which is normally defined using the
PrimaryKey.sequence() annotation property.DatabaseException - the base class for all BDB exceptions.public SequenceConfig getSequenceConfig(java.lang.String name)
The returned configuration is as follows. All other properties have default values.
InitialValue is one.Range minimum is one.CacheSize is 100.AutoCommitNoSync is
true.AllowCreate is set to the
inverse of the store ReadOnly.
setting.name - the sequence name, which is normally defined using the
PrimaryKey.sequence() annotation property.public void setSequenceConfig(java.lang.String name,
SequenceConfig config)
To be compatible with the entity model and the Direct Persistence
Layer, the configuration should be retrieved using getSequenceConfig, modified, and then passed to this
method. The following configuration properties may not be changed:
In addition, AllowCreate must be
the inverse of ReadOnly
If the range is changed to include the value zero, see PrimaryKey for restrictions.
name - the sequence name, which is normally defined using the
PrimaryKey.sequence() annotation property.config - the configuration to use for the given sequence name.java.lang.IllegalArgumentException - if the configuration is incompatible
with the entity model or the Direct Persistence Layer.java.lang.IllegalStateException - if the sequence has already been opened.public DatabaseConfig getPrimaryConfig(java.lang.Class entityClass)
The returned configuration is as follows. All other properties have default values.
Transactional is set to
match StoreConfig.AllowCreate is set to the
inverse of the store ReadOnly.
setting.ReadOnly is set to match
StoreConfig.DeferredWrite is set to
match StoreConfig.Temporary is set to
match StoreConfig.BtreeComparator is set to
an internal class if a key comparator is used.entityClass - the entity class identifying the primary database.public void setPrimaryConfig(java.lang.Class entityClass,
DatabaseConfig config)
To be compatible with the entity model and the Direct Persistence
Layer, the configuration should be retrieved using getPrimaryConfig, modified, and then passed to this
method. The following configuration properties may not be changed:
In addition, AllowCreate must be
the inverse of ReadOnly
entityClass - the entity class identifying the primary database.config - the configuration to use for the given entity class.java.lang.IllegalArgumentException - if the configuration is incompatible
with the entity model or the Direct Persistence Layer.java.lang.IllegalStateException - if the database has already been opened.public SecondaryConfig getSecondaryConfig(java.lang.Class entityClass, java.lang.String keyName)
The returned configuration is as follows. All other properties have default values.
Transactional is set to
match the primary database.AllowCreate is set to the
inverse of the primary database ReadOnly setting.ReadOnly is set to match
the primary database.DeferredWrite is set to
match the primary database.Temporary is set to
match StoreConfig.BtreeComparator is set to
an internal class if a key comparator is used.SortedDuplicates is set
according to SecondaryKey.relate().AllowPopulate is set to
true when a secondary key is added to an existing primary index.KeyCreator or MultiKeyCreator is set to an
internal instance.ForeignMultiKeyNullifier is set to an internal instance if SecondaryKey.onRelatedEntityDelete() is DeleteAction.NULLIFY.entityClass - the entity class containing the given secondary key
name.keyName - the name of the secondary key field, or the SecondaryKey.name() if this name annotation property was specified.public void setSecondaryConfig(java.lang.Class entityClass,
java.lang.String keyName,
SecondaryConfig config)
To be compatible with the entity model and the Direct Persistence
Layer, the configuration should be retrieved using getSecondaryConfig, modified, and then passed to
this method. The following configuration properties may not be
changed:
ExclusiveCreateSortedDuplicatesBtreeComparatorDuplicateComparatorTemporaryAllowPopulateKeyCreatorMultiKeyCreatorForeignKeyNullifierForeignMultiKeyNullifierForeignKeyDeleteActionForeignKeyDatabaseIn addition, AllowCreate must be
the inverse of ReadOnly
entityClass - the entity class containing the given secondary key
name.keyName - the name of the secondary key field, or the SecondaryKey.name() if this name annotation property was specified.config - the configuration to use for the given secondary key.java.lang.IllegalArgumentException - if the configuration is incompatible
with the entity model or the Direct Persistence Layer.java.lang.IllegalStateException - if the database has already been opened.Copyright (c) 2002, 2017 Oracle and/or its affiliates. All rights reserved.