Package dev.morphia
Class DatastoreImpl
- java.lang.Object
-
- dev.morphia.DatastoreImpl
-
- All Implemented Interfaces:
AdvancedDatastore
,Datastore
- Direct Known Subclasses:
BaseMorphiaSession
public class DatastoreImpl extends Object implements AdvancedDatastore
A generic (type-safe) wrapper around mongodb collectionsDeveloper note.This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
DatastoreImpl(com.mongodb.client.MongoClient mongoClient, MapperOptions options, String dbName)
DatastoreImpl(com.mongodb.client.MongoDatabase database, com.mongodb.client.MongoClient mongoClient, Mapper mapper, QueryFactory queryFactory)
Copy constructor for a datastore
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description AggregationPipeline
createAggregation(Class source)
Returns a new query bound to the kind (a specificDBCollection
)AggregationPipeline
createAggregation(String collection, Class<?> clazz)
Returns anAggregationPipeline
bound to the given collection and class.<T> Query<T>
createQuery(Class<T> clazz, org.bson.Document q)
<T> Query<T>
createQuery(String collection, Class<T> type)
<T,V>
com.mongodb.DBRefcreateRef(Class<T> clazz, V id)
Creates a reference to the entity (using the current DB -can be null-, the collectionName, and id)<T> com.mongodb.DBRef
createRef(T entity)
Creates a reference to the entity (using the current DB -can be null-, the collectionName, and id)<T> UpdateOperations<T>
createUpdateOperations(Class<T> type, org.bson.Document ops)
Creates an UpdateOperations instance for the given type.<T> com.mongodb.client.result.DeleteResult
delete(T entity)
Deletes the given entity (by @Id)<T> com.mongodb.client.result.DeleteResult
delete(T entity, DeleteOptions options)
Deletes the given entity (by @Id), with the WriteConcernvoid
enableDocumentValidation()
Process anyValidation
annotations for document validation.void
ensureCaps()
ensure capped collections forEntity
(s)void
ensureIndexes()
Ensures (creating if necessary) the indexes found during class mapping<T> void
ensureIndexes(Class<T> clazz)
Ensures (creating if necessary) the indexes found during class mapping<T> Query<T>
find(Class<T> clazz)
Find all instances by type<T> Query<T>
find(String collection)
Find all instances by type in a different collection than what is mapped on the class given.com.mongodb.client.ClientSession
findSession(dev.morphia.internal.SessionConfigurable configurable)
<T,V>
Query<T>get(Class<T> clazz, Iterable<V> ids)
Find the given entities (by id); shorthand forfind("_id in", ids)
<T> T
getByKey(Class<T> clazz, Key<T> key)
Find the given entity (by collectionName/id);<T> List<T>
getByKeys(Class<T> clazz, Iterable<Key<T>> keys)
Find the given entities (by id), verifying they are of the correct type; shorthand forfind("_id in", ids)
<T> List<T>
getByKeys(Iterable<Key<T>> keys)
Find the given entities (by id); shorthand forfind("_id in", ids)
<T> com.mongodb.client.MongoCollection<T>
getCollection(Class<T> type)
com.mongodb.client.MongoDatabase
getDatabase()
<T> Key<T>
getKey(T entity)
Deprecated.Mapper
getMapper()
QueryFactory
getQueryFactory()
protected <T> void
insert(com.mongodb.client.MongoCollection collection, T entity, InsertOneOptions options)
<T> void
insert(List<T> entities, InsertManyOptions options)
Inserts entities in to the mapped collection.<T> void
insert(T entity)
Inserts an entity in to the mapped collection.<T> void
insert(T entity, InsertOneOptions options)
Inserts an entity in to the mapped collection.<T> T
merge(T entity)
Work as if you did an update with each field in the entity doing a $set; Only at the top level of the entity.<T> void
merge(T entity, com.mongodb.WriteConcern wc)
Work as if you did an update with each field in the entity doing a $set; Only at the top level of the entity.<T> T
merge(T entity, InsertOneOptions options)
Work as if you did an update with each field in the entity doing a $set; Only at the top level of the entity.protected <T> Query<T>
newQuery(Class<T> type)
protected <T> Query<T>
newQuery(Class<T> type, org.bson.Document query)
<T> Query<T>
queryByExample(String collection, T ex)
Returns a new query based on the example object<T> Query<T>
queryByExample(T example)
Returns a new query based on the example object<T> List<T>
save(List<T> entities, InsertManyOptions options)
Saves the entities (Objects) and updates the @Id field<T> T
save(T entity)
Saves an entity (Object) and updates the @Id field<T> T
save(T entity, InsertOneOptions options)
Saves an entity (Object) and updates the @Id fieldprotected <T> void
saveDocument(T entity, com.mongodb.client.MongoCollection<T> collection, InsertOneOptions options)
void
setMapper(Mapper mapper)
Sets the Mapper this Datastore usesvoid
setQueryFactory(QueryFactory queryFactory)
Replaces the currentQueryFactory
with the given value.MorphiaSession
startSession()
Starts a new session on the server.MorphiaSession
startSession(com.mongodb.ClientSessionOptions options)
Starts a new session on the server.<T> T
withTransaction(com.mongodb.ClientSessionOptions options, MorphiaTransaction<T> transaction)
<T> T
withTransaction(MorphiaTransaction<T> body)
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface dev.morphia.AdvancedDatastore
createUpdateOperations, insert, insert, insert
-
Methods inherited from interface dev.morphia.Datastore
createQuery, createUpdateOperations, delete, delete, findAndDelete, findAndDelete, findAndModify, get, getSession, save, save, save, save, update, update
-
-
-
-
Constructor Detail
-
DatastoreImpl
protected DatastoreImpl(com.mongodb.client.MongoClient mongoClient, MapperOptions options, String dbName)
-
DatastoreImpl
public DatastoreImpl(com.mongodb.client.MongoDatabase database, com.mongodb.client.MongoClient mongoClient, Mapper mapper, QueryFactory queryFactory)
Copy constructor for a datastore- Parameters:
database
- the databasemongoClient
- the clientmapper
- the mapperqueryFactory
- the query factory- Since:
- 2.0
Developer note.This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
-
-
Method Detail
-
createAggregation
public AggregationPipeline createAggregation(String collection, Class<?> clazz)
Description copied from interface:AdvancedDatastore
Returns anAggregationPipeline
bound to the given collection and class.- Specified by:
createAggregation
in interfaceAdvancedDatastore
- Parameters:
collection
- the collection to queryclazz
- The class to create aggregation against- Returns:
- the aggregation pipeline
-
createQuery
public <T> Query<T> createQuery(String collection, Class<T> type)
- Specified by:
createQuery
in interfaceAdvancedDatastore
- Type Parameters:
T
- The type of the entity- Parameters:
collection
- the collection to querytype
- the class of objects to be returned- Returns:
- Query for the specified class clazz
-
createQuery
public <T> Query<T> createQuery(Class<T> clazz, org.bson.Document q)
- Specified by:
createQuery
in interfaceAdvancedDatastore
- Type Parameters:
T
- The type of the entity- Parameters:
clazz
- the class of objects to be returnedq
- the query which will be passed to aQueryFactory
- Returns:
- Query for the specified class clazz
-
createRef
public <T,V> com.mongodb.DBRef createRef(Class<T> clazz, V id)
Description copied from interface:AdvancedDatastore
Creates a reference to the entity (using the current DB -can be null-, the collectionName, and id)- Specified by:
createRef
in interfaceAdvancedDatastore
- Type Parameters:
T
- The type of the entityV
- The type of the ID value- Parameters:
clazz
- The type of the entityid
- The ID value of the entity- Returns:
- the DBRef for the entity
-
createRef
public <T> com.mongodb.DBRef createRef(T entity)
Description copied from interface:AdvancedDatastore
Creates a reference to the entity (using the current DB -can be null-, the collectionName, and id)- Specified by:
createRef
in interfaceAdvancedDatastore
- Type Parameters:
T
- The type of the entity- Parameters:
entity
- the entity to create a DBRef for- Returns:
- the DBRef for the entity
-
createUpdateOperations
public <T> UpdateOperations<T> createUpdateOperations(Class<T> type, org.bson.Document ops)
Description copied from interface:AdvancedDatastore
Creates an UpdateOperations instance for the given type.- Specified by:
createUpdateOperations
in interfaceAdvancedDatastore
- Type Parameters:
T
- The type of the entity- Parameters:
type
- The type of the entityops
- The operations to perform- Returns:
- the UpdateOperations instance
-
find
public <T> Query<T> find(String collection)
Description copied from interface:AdvancedDatastore
Find all instances by type in a different collection than what is mapped on the class given.- Specified by:
find
in interfaceAdvancedDatastore
- Type Parameters:
T
- the type to query- Parameters:
collection
- the collection to query against- Returns:
- the query
-
insert
public <T> void insert(T entity)
Description copied from interface:AdvancedDatastore
Inserts an entity in to the mapped collection.- Specified by:
insert
in interfaceAdvancedDatastore
- Type Parameters:
T
- the type of the entity- Parameters:
entity
- the entity to insert
-
insert
public <T> void insert(T entity, InsertOneOptions options)
Description copied from interface:AdvancedDatastore
Inserts an entity in to the mapped collection.- Specified by:
insert
in interfaceAdvancedDatastore
- Type Parameters:
T
- the type of the entity- Parameters:
entity
- the entity to insertoptions
- the options to apply to the insert operation
-
insert
public <T> void insert(List<T> entities, InsertManyOptions options)
Description copied from interface:AdvancedDatastore
Inserts entities in to the mapped collection.- Specified by:
insert
in interfaceAdvancedDatastore
- Type Parameters:
T
- the type of the entity- Parameters:
entities
- the entities to insertoptions
- the options to apply to the insert operation
-
queryByExample
public <T> Query<T> queryByExample(String collection, T ex)
Description copied from interface:AdvancedDatastore
Returns a new query based on the example object- Specified by:
queryByExample
in interfaceAdvancedDatastore
- Type Parameters:
T
- the type of the entity- Parameters:
collection
- the collection to queryex
- the example entity to use when building the query- Returns:
- the query
-
insert
protected <T> void insert(com.mongodb.client.MongoCollection collection, T entity, InsertOneOptions options)
-
findSession
public com.mongodb.client.ClientSession findSession(dev.morphia.internal.SessionConfigurable configurable)
- Parameters:
configurable
- the configurable- Returns:
- any session found first on the configurable then on this
Developer note.This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
-
withTransaction
public <T> T withTransaction(MorphiaTransaction<T> body)
- Specified by:
withTransaction
in interfaceDatastore
- Type Parameters:
T
- the return type- Parameters:
body
- the transaction wrapper- Returns:
- the return value
-
startSession
public MorphiaSession startSession()
Description copied from interface:Datastore
Starts a new session on the server.- Specified by:
startSession
in interfaceDatastore
- Returns:
- the new session reference
-
startSession
public MorphiaSession startSession(com.mongodb.ClientSessionOptions options)
Description copied from interface:Datastore
Starts a new session on the server.- Specified by:
startSession
in interfaceDatastore
- Parameters:
options
- the options to apply- Returns:
- the new session reference
-
withTransaction
public <T> T withTransaction(com.mongodb.ClientSessionOptions options, MorphiaTransaction<T> transaction)
- Specified by:
withTransaction
in interfaceDatastore
- Type Parameters:
T
- the return type- Parameters:
options
- the session options to applytransaction
- the transaction wrapper- Returns:
- the return value
-
createAggregation
public AggregationPipeline createAggregation(Class source)
Description copied from interface:Datastore
Returns a new query bound to the kind (a specificDBCollection
)- Specified by:
createAggregation
in interfaceDatastore
- Parameters:
source
- the initial type/collection to aggregate against- Returns:
- a new query bound to the kind (a specific
MongoCollection
)
-
find
public <T> Query<T> find(Class<T> clazz)
Description copied from interface:Datastore
Find all instances by type
-
getMapper
public Mapper getMapper()
-
delete
public <T> com.mongodb.client.result.DeleteResult delete(T entity)
Description copied from interface:Datastore
Deletes the given entity (by @Id)
-
delete
public <T> com.mongodb.client.result.DeleteResult delete(T entity, DeleteOptions options)
Deletes the given entity (by @Id), with the WriteConcern
-
ensureCaps
public void ensureCaps()
Description copied from interface:Datastore
ensure capped collections forEntity
(s)- Specified by:
ensureCaps
in interfaceDatastore
-
enableDocumentValidation
public void enableDocumentValidation()
Description copied from interface:Datastore
Process anyValidation
annotations for document validation.- Specified by:
enableDocumentValidation
in interfaceDatastore
-
ensureIndexes
public void ensureIndexes()
Description copied from interface:Datastore
Ensures (creating if necessary) the indexes found during class mapping- Specified by:
ensureIndexes
in interfaceDatastore
- See Also:
Indexes
,Indexed
,Text
-
ensureIndexes
public <T> void ensureIndexes(Class<T> clazz)
Description copied from interface:Datastore
Ensures (creating if necessary) the indexes found during class mapping- Specified by:
ensureIndexes
in interfaceDatastore
- Type Parameters:
T
- the type to index- Parameters:
clazz
- the class from which to get the index definitions- See Also:
Indexes
,Indexed
,Text
-
get
public <T,V> Query<T> get(Class<T> clazz, Iterable<V> ids)
Description copied from interface:Datastore
Find the given entities (by id); shorthand forfind("_id in", ids)
-
getByKey
public <T> T getByKey(Class<T> clazz, Key<T> key)
Description copied from interface:Datastore
Find the given entity (by collectionName/id);
-
getByKeys
public <T> List<T> getByKeys(Class<T> clazz, Iterable<Key<T>> keys)
Description copied from interface:Datastore
Find the given entities (by id), verifying they are of the correct type; shorthand forfind("_id in", ids)
-
getByKeys
public <T> List<T> getByKeys(Iterable<Key<T>> keys)
Description copied from interface:Datastore
Find the given entities (by id); shorthand forfind("_id in", ids)
-
getCollection
public <T> com.mongodb.client.MongoCollection<T> getCollection(Class<T> type)
- Specified by:
getCollection
in interfaceDatastore
- Type Parameters:
T
- the class type- Parameters:
type
- the type look up- Returns:
- the collection mapped for this class
-
getDatabase
public com.mongodb.client.MongoDatabase getDatabase()
- Specified by:
getDatabase
in interfaceDatastore
- Returns:
- the MongoDatabase used by this DataStore
-
getKey
@Deprecated public <T> Key<T> getKey(T entity)
Deprecated.Description copied from interface:Datastore
Creates a (type-safe) reference to the entity; if stored this will become aDBRef
-
getQueryFactory
public QueryFactory getQueryFactory()
- Specified by:
getQueryFactory
in interfaceDatastore
- Returns:
- the current
QueryFactory
. - See Also:
QueryFactory
-
setQueryFactory
public void setQueryFactory(QueryFactory queryFactory)
Description copied from interface:Datastore
Replaces the currentQueryFactory
with the given value.- Specified by:
setQueryFactory
in interfaceDatastore
- Parameters:
queryFactory
- the QueryFactory to use- See Also:
QueryFactory
-
merge
public <T> T merge(T entity)
Description copied from interface:Datastore
Work as if you did an update with each field in the entity doing a $set; Only at the top level of the entity.
-
merge
public <T> T merge(T entity, InsertOneOptions options)
Description copied from interface:Datastore
Work as if you did an update with each field in the entity doing a $set; Only at the top level of the entity.
-
merge
public <T> void merge(T entity, com.mongodb.WriteConcern wc)
Description copied from interface:Datastore
Work as if you did an update with each field in the entity doing a $set; Only at the top level of the entity.
-
queryByExample
public <T> Query<T> queryByExample(T example)
Description copied from interface:Datastore
Returns a new query based on the example object- Specified by:
queryByExample
in interfaceDatastore
- Type Parameters:
T
- the type of the entity- Parameters:
example
- the example entity to use when creating the query- Returns:
- the query
-
save
public <T> List<T> save(List<T> entities, InsertManyOptions options)
Description copied from interface:Datastore
Saves the entities (Objects) and updates the @Id field
-
save
public <T> T save(T entity)
Description copied from interface:Datastore
Saves an entity (Object) and updates the @Id field
-
save
public <T> T save(T entity, InsertOneOptions options)
Description copied from interface:Datastore
Saves an entity (Object) and updates the @Id field
-
saveDocument
protected <T> void saveDocument(T entity, com.mongodb.client.MongoCollection<T> collection, InsertOneOptions options)
-
setMapper
public void setMapper(Mapper mapper)
Sets the Mapper this Datastore uses- Parameters:
mapper
- the new Mapper
-
-