Package dev.morphia

Class DatastoreImpl

  • All Implemented Interfaces:
    AdvancedDatastore, Datastore
    Direct Known Subclasses:
    BaseMorphiaSession

    public class DatastoreImpl
    extends Object
    implements AdvancedDatastore
    A generic (type-safe) wrapper around mongodb collections
    Developer note.
    This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
    • 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 database
        mongoClient - the client
        mapper - the mapper
        queryFactory - 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

      • createQuery

        public <T> Query<T> createQuery​(String collection,
                                        Class<T> type)
        Specified by:
        createQuery in interface AdvancedDatastore
        Type Parameters:
        T - The type of the entity
        Parameters:
        collection - the collection to query
        type - 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 interface AdvancedDatastore
        Type Parameters:
        T - The type of the entity
        Parameters:
        clazz - the class of objects to be returned
        q - the query which will be passed to a QueryFactory
        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 interface AdvancedDatastore
        Type Parameters:
        T - The type of the entity
        V - The type of the ID value
        Parameters:
        clazz - The type of the entity
        id - 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 interface AdvancedDatastore
        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 interface AdvancedDatastore
        Type Parameters:
        T - The type of the entity
        Parameters:
        type - The type of the entity
        ops - 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 interface AdvancedDatastore
        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 interface AdvancedDatastore
        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 interface AdvancedDatastore
        Type Parameters:
        T - the type of the entity
        Parameters:
        entity - the entity to insert
        options - 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 interface AdvancedDatastore
        Type Parameters:
        T - the type of the entity
        Parameters:
        entities - the entities to insert
        options - 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 interface AdvancedDatastore
        Type Parameters:
        T - the type of the entity
        Parameters:
        collection - the collection to query
        ex - 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.
      • newQuery

        protected <T> Query<T> newQuery​(Class<T> type,
                                        org.bson.Document query)
      • newQuery

        protected <T> Query<T> newQuery​(Class<T> type)
      • withTransaction

        public <T> T withTransaction​(MorphiaTransaction<T> body)
        Specified by:
        withTransaction in interface Datastore
        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 interface Datastore
        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 interface Datastore
        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 interface Datastore
        Type Parameters:
        T - the return type
        Parameters:
        options - the session options to apply
        transaction - 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 specific DBCollection)
        Specified by:
        createAggregation in interface Datastore
        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
        Specified by:
        find in interface Datastore
        Type Parameters:
        T - the type to query
        Parameters:
        clazz - the class to use for mapping the results
        Returns:
        the query
      • getMapper

        public Mapper getMapper()
        Specified by:
        getMapper in interface Datastore
        Returns:
        the Mapper used by this Datastore
      • delete

        public <T> com.mongodb.client.result.DeleteResult delete​(T entity)
        Description copied from interface: Datastore
        Deletes the given entity (by @Id)
        Specified by:
        delete in interface Datastore
        Type Parameters:
        T - the type to delete
        Parameters:
        entity - the entity to delete
        Returns:
        results of the delete
      • delete

        public <T> com.mongodb.client.result.DeleteResult delete​(T entity,
                                                                 DeleteOptions options)
        Deletes the given entity (by @Id), with the WriteConcern
        Specified by:
        delete in interface Datastore
        Type Parameters:
        T - the type to delete
        Parameters:
        entity - the entity to delete
        options - the options to use when deleting
        Returns:
        results of the delete
      • ensureCaps

        public void ensureCaps()
        Description copied from interface: Datastore
        ensure capped collections for Entity(s)
        Specified by:
        ensureCaps in interface Datastore
      • ensureIndexes

        public void ensureIndexes()
        Description copied from interface: Datastore
        Ensures (creating if necessary) the indexes found during class mapping
        Specified by:
        ensureIndexes in interface Datastore
        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 interface Datastore
        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 for find("_id in", ids)
        Specified by:
        get in interface Datastore
        Type Parameters:
        T - the type to fetch
        V - the type of the ID
        Parameters:
        clazz - the class to use for mapping
        ids - the IDs to query
        Returns:
        the query to find the entities
      • getByKey

        public <T> T getByKey​(Class<T> clazz,
                              Key<T> key)
        Description copied from interface: Datastore
        Find the given entity (by collectionName/id);
        Specified by:
        getByKey in interface Datastore
        Type Parameters:
        T - the type to fetch
        Parameters:
        clazz - the class to use for mapping
        key - the key search with
        Returns:
        the matched entity. may be null.
      • 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 for find("_id in", ids)
        Specified by:
        getByKeys in interface Datastore
        Type Parameters:
        T - the type to fetch
        Parameters:
        clazz - the class to use for mapping
        keys - the keys to search with
        Returns:
        the matched entities. may be null.
      • getByKeys

        public <T> List<T> getByKeys​(Iterable<Key<T>> keys)
        Description copied from interface: Datastore
        Find the given entities (by id); shorthand for find("_id in", ids)
        Specified by:
        getByKeys in interface Datastore
        Type Parameters:
        T - the type to fetch
        Parameters:
        keys - the keys to search with
        Returns:
        the matched entities. may be null.
      • getCollection

        public <T> com.mongodb.client.MongoCollection<T> getCollection​(Class<T> type)
        Specified by:
        getCollection in interface Datastore
        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 interface Datastore
        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 a DBRef
        Specified by:
        getKey in interface Datastore
        Type Parameters:
        T - the type of the entity
        Parameters:
        entity - the entity whose key is to be returned
        Returns:
        the Key
      • 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.
        Specified by:
        merge in interface Datastore
        Type Parameters:
        T - the type of the entity
        Parameters:
        entity - the entity to merge back in to the database
        Returns:
        the new merged entity. NOTE: this is a database fetch.
      • 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.
        Specified by:
        merge in interface Datastore
        Type Parameters:
        T - the type of the entity
        Parameters:
        entity - the entity to merge back in to the database
        options - the options to apply
        Returns:
        the new merged entity. NOTE: this is a database fetch.
      • 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.
        Specified by:
        merge in interface Datastore
        Type Parameters:
        T - the type of the entity
        Parameters:
        entity - the entity to merge back in to the database
        wc - the WriteConcern to use
      • 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 interface Datastore
        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
        Specified by:
        save in interface Datastore
        Type Parameters:
        T - the type of the entity
        Parameters:
        entities - the entities to save
        options - the options to apply to the save operation
        Returns:
        the saved entities
      • save

        public <T> T save​(T entity)
        Description copied from interface: Datastore
        Saves an entity (Object) and updates the @Id field
        Specified by:
        save in interface Datastore
        Type Parameters:
        T - the type of the entity
        Parameters:
        entity - the entity to save
        Returns:
        the saved entity
      • save

        public <T> T save​(T entity,
                          InsertOneOptions options)
        Description copied from interface: Datastore
        Saves an entity (Object) and updates the @Id field
        Specified by:
        save in interface Datastore
        Type Parameters:
        T - the type of the entity
        Parameters:
        entity - the entity to save
        options - the options to apply to the save operation
        Returns:
        the saved entity
      • 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