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

      • aggregate

        public Aggregation<org.bson.Document> aggregate​(String source)
        Description copied from interface: Datastore
        Returns a new query bound to the kind (a specific DBCollection)
        Specified by:
        aggregate in interface Datastore
        Parameters:
        source - The collection aggregation against
        Returns:
        the aggregation pipeline
      • aggregate

        public <T> Aggregation<T> aggregate​(Class<T> source)
        Description copied from interface: Datastore
        Returns a new query bound to the kind (a specific DBCollection)
        Specified by:
        aggregate in interface Datastore
        Type Parameters:
        T - the source type
        Parameters:
        source - The class to create aggregation against
        Returns:
        the aggregation pipeline
      • 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 class to create aggregation against
        Returns:
        the aggregation pipeline
      • 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
      • createQuery

        public <T> Query<T> createQuery​(Class<T> type,
                                        org.bson.Document q)
        Specified by:
        createQuery in interface AdvancedDatastore
        Type Parameters:
        T - The type of the entity
        Parameters:
        type - the class of objects to be returned
        q - the query which will be passed to a QueryFactory
        Returns:
        Query for the specified class type
      • find

        public <T> Query<T> find​(Class<T> type)
        Description copied from interface: Datastore
        Find all instances by type
        Specified by:
        find in interface Datastore
        Type Parameters:
        T - the type to query
        Parameters:
        type - the class to use for mapping the results
        Returns:
        the query
      • find

        public <T> Query<T> find​(String collection,
                                 Class<T> type)
        Description copied from interface: Datastore
        Find all instances by type from an alternate collection
        Specified by:
        find in interface Datastore
        Type Parameters:
        T - the type to query
        Parameters:
        collection - the actual collection to query. This overrides any mapped on collection on type.
        type - the class to use for mapping the results
        Returns:
        the query
      • findSession

        @Nullable
        public com.mongodb.client.ClientSession findSession​(SessionConfigurable<?> configurable)
        Specified by:
        findSession in interface Datastore
        Parameters:
        configurable - the configurable
        Returns:
        any session found first on the configurable then on this
      • 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
      • getLoggedQuery

        public String getLoggedQuery​(FindOptions options)
        Specified by:
        getLoggedQuery in interface Datastore
        Parameters:
        options - the options used when requesting logging
        Returns:
        the logged query
        Developer note.
        This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
      • getMapper

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

        public <T> void ensureIndexes​(Class<T> type)
        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:
        type - the class from which to get the index definitions
        See Also:
        Indexes, Indexed, Text
      • find

        public <T> Query<T> find​(String collection)
        Description copied from interface: Datastore
        Find all instances by type in a different collection than what is mapped on the class given.
        Specified by:
        find in interface Datastore
        Type Parameters:
        T - the type to query
        Parameters:
        collection - the collection to query against
        Returns:
        the query
      • getDatabase

        public com.mongodb.client.MongoDatabase getDatabase()
        Specified by:
        getDatabase in interface Datastore
        Returns:
        the MongoDatabase used by this DataStore
      • insert

        public <T> void insert​(T entity)
        Description copied from interface: Datastore
        Inserts an entity in to the mapped collection.
        Specified by:
        insert in interface Datastore
        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: Datastore
        Inserts an entity in to the mapped collection.
        Specified by:
        insert in interface Datastore
        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: Datastore
        Inserts entities in to the mapped collection.
        Specified by:
        insert in interface Datastore
        Type Parameters:
        T - the type of the entity
        Parameters:
        entities - the entities to insert
        options - the options to apply to the insert operation
      • 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.
      • 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
      • refresh

        public <T> void refresh​(T entity)
        Description copied from interface: Datastore
        Refreshes an existing entity to its current state in the database. Essentially, any existing mapped state is replaced by the latest persisted state while preserving the entity's reference and object identity.
        Specified by:
        refresh in interface Datastore
        Type Parameters:
        T - the entity type
        Parameters:
        entity - the entity to refresh
      • 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
      • 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​(MorphiaTransaction<T> body)
        Specified by:
        withTransaction in interface Datastore
        Type Parameters:
        T - the return type
        Parameters:
        body - the transaction wrapper
        Returns:
        the return value
      • 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
      • enableValidation

        public void enableValidation​(EntityModel model,
                                     Validation validation)
        Parameters:
        model - internal
        validation - internal
        Developer note.
        This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.