All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.code.morphia.Datastore Maven / Gradle / Ivy

The newest version!
package com.google.code.morphia;


import java.util.List;
import java.util.Map;

import com.google.code.morphia.query.Query;
import com.google.code.morphia.query.QueryFactory;
import com.google.code.morphia.query.UpdateOperations;
import com.google.code.morphia.query.UpdateResults;
import com.google.code.morphia.utils.IndexDirection;
import com.google.code.morphia.utils.IndexFieldDef;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBRef;
import com.mongodb.MapReduceCommand;
import com.mongodb.Mongo;
import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;


/**
 * Datastore interface to get/delete/save objects
 *
 * @author Scott Hernandez
 */
public interface Datastore {
  /**
   * Creates a (type-safe) reference to the entity; if stored this will become a {@link DBRef}
   */
   Key getKey(T entity);

  /**
   * Does a query to check if the keyOrEntity exists in mongodb
   */
  Key exists(Object keyOrEntity);

  /**
   * Deletes the given entity (by id)
   */
   WriteResult delete(Class clazz, V id);

  /**
   * Deletes the given entities (by id)
   */
   WriteResult delete(Class clazz, Iterable ids);

  /**
   * Deletes the given entities based on the query
   */
   WriteResult delete(Query q);

  /**
   * Deletes the given entities based on the query, with the WriteConcern
   */
   WriteResult delete(Query q, WriteConcern wc);

  /**
   * Deletes the given entity (by @Id)
   */
   WriteResult delete(T entity);

  /**
   * Deletes the given entity (by @Id), with the WriteConcern
   */
   WriteResult delete(T entity, WriteConcern wc);

  /**
   * Find all instances by type
   */
   Query find(Class clazz);

  /**
   * 

Find all instances by collectionName, and filter property.

This is the same as: {@code find(clazzOrEntity).filter(property, *value); }

*/ Query find(Class clazz, String property, V value); /** *

Find all instances by collectionName, and filter property.

This is the same as: {@code find(clazzOrEntity).filter(property, *value).offset(offset).limit(size); }

*/ Query find(Class clazz, String property, V value, int offset, int size); /** * Find the given entities (by id); shorthand for {@code find("_id in", ids)} */ Query get(Class clazz, Iterable ids); /** * Find the given entity (by id); shorthand for {@code find("_id ", id)} */ T get(Class clazz, V id); /** * Find the given entity (by collectionName/id); think of this as refresh */ T get(T entity); /** * Find the given entities (by id), verifying they are of the correct type; shorthand for {@code find("_id in", ids)} */ List getByKeys(Class clazz, Iterable> keys); /** * Find the given entities (by id); shorthand for {@code find("_id in", ids)} */ List getByKeys(Iterable> keys); /** * Find the given entity (by collectionName/id); */ T getByKey(Class clazz, Key key); /** * Gets the count this kind ({@link DBCollection}) */ long getCount(T entity); /** * Gets the count this kind ({@link DBCollection}) */ long getCount(Class clazz); /** * Gets the count of items returned by this query; same as {@code query.countAll()} */ long getCount(Query query); /** * Saves the entities (Objects) and updates the @Id field */ Iterable> save(Iterable entities); /** * Saves the entities (Objects) and updates the @Id field, with the WriteConcern */ Iterable> save(Iterable entities, WriteConcern wc); /** * Saves the entities (Objects) and updates the @Id field */ Iterable> save(T... entities); /** * Saves the entity (Object) and updates the @Id field */ Key save(T entity); /** * Saves the entity (Object) and updates the @Id field, with the WriteConcern */ Key save(T entity, 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. */ Key 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. */ Key merge(T entity, WriteConcern wc); /** * updates the entity with the operations; this is an atomic operation */ UpdateResults update(T ent, UpdateOperations ops); /** * updates the entity with the operations; this is an atomic operation */ UpdateResults update(Key key, UpdateOperations ops); /** * updates all entities found with the operations; this is an atomic operation per entity */ UpdateResults update(Query query, UpdateOperations ops); /** * updates all entities found with the operations, if nothing is found insert the update as an entity if "createIfMissing" is true; this * is an atomic operation per entity */ UpdateResults update(Query query, UpdateOperations ops, boolean createIfMissing); UpdateResults update(Query query, UpdateOperations ops, boolean createIfMissing, WriteConcern wc); /** * updates the first entity found with the operations; this is an atomic operation */ UpdateResults updateFirst(Query query, UpdateOperations ops); /** * updates the first entity found with the operations, if nothing is found insert the update as an entity if "createIfMissing" is true; * this is an atomic operation per entity */ UpdateResults updateFirst(Query query, UpdateOperations ops, boolean createIfMissing); UpdateResults updateFirst(Query query, UpdateOperations ops, boolean createIfMissing, WriteConcern wc); /** * updates the first entity found with the operations, if nothing is found insert the update as an entity if "createIfMissing" is true; * this is an atomic operation per entity */ UpdateResults updateFirst(Query query, T entity, boolean createIfMissing); /** * Deletes the given entities based on the query (first item only). * * @return the deleted Entity */ T findAndDelete(Query q); /** * Find the first Entity from the Query, and modify it. * * @return The modified Entity (the result of the update) */ T findAndModify(Query q, UpdateOperations ops); /** * Find the first Entity from the Query, and modify it. * * @param q the query to find the Entity with; You are not allowed to offset/skip in the query. * @param oldVersion indicated the old version of the Entity should be returned * @return The Entity (the result of the update if oldVersion is false) */ T findAndModify(Query q, UpdateOperations ops, boolean oldVersion); /** * Find the first Entity from the Query, and modify it. * * @param q the query to find the Entity with; You are not allowed to offset/skip in the query. * @param oldVersion indicated the old version of the Entity should be returned * @param createIfMissing if the query returns no results, then a new object will be created (sets upsert=true) * @return The Entity (the result of the update if oldVersion is false) */ T findAndModify(Query q, UpdateOperations ops, boolean oldVersion, boolean createIfMissing); /** * Runs a map/reduce job at the server; this should be used with a server version 1.7.4 or higher * * @param The type of resulting data * @param outputType The type of resulting data; inline is not working yet * @param type MapreduceType * @param q The query (only the criteria, limit and sort will be used) * @param map The map function, in javascript, as a string * @param reduce The reduce function, in javascript, as a string * @param finalize The finalize function, in javascript, as a string; can be null * @param scopeFields Each map entry will be a global variable in all the functions; can be null * @return counts and stuff */ MapreduceResults mapReduce(MapreduceType type, Query q, String map, String reduce, String finalize, Map scopeFields, Class outputType); /** * Runs a map/reduce job at the server; this should be used with a server version 1.7.4 or higher * * @param The type of resulting data * @param type MapreduceType * @param q The query (only the criteria, limit and sort will be used) * @param outputType The type of resulting data; inline is not working yet * @param baseCommand The base command to fill in and send to the server * @return counts and stuff */ MapreduceResults mapReduce(MapreduceType type, Query q, Class outputType, MapReduceCommand baseCommand); /** * The builder for all update operations */ UpdateOperations createUpdateOperations(Class kind); /** * Returns a new query bound to the kind (a specific {@link DBCollection}) */ Query createQuery(Class kind); /** * Returns a new query based on the example object */ Query queryByExample(T example); /** * Ensures (creating if necessary) the index and direction */ void ensureIndex(Class clazz, String field, IndexDirection dir); /** * Ensures (creating if necessary) the index including the field(s) + directions */ @Deprecated void ensureIndex(Class clazz, IndexFieldDef... fields); /** * Ensures (creating if necessary) the index including the field(s) + directions */ @Deprecated void ensureIndex(Class clazz, String name, IndexFieldDef[] fields, boolean unique, boolean dropDupsOnCreate); /** * Ensures (creating if necessary) the index including the field(s) + directions; eg fields = "field1, -field2" ({field1:1, field2:-1}) */ void ensureIndex(Class clazz, String fields); /** * Ensures (creating if necessary) the index including the field(s) + directions; eg fields = "field1, -field2" ({field1:1, field2:-1}) */ void ensureIndex(Class clazz, String name, String fields, boolean unique, boolean dropDupsOnCreate); /** * Ensures (creating if necessary) the indexes found during class mapping (using {@code @Indexed, @Indexes)} */ void ensureIndexes(); /** * Ensures (creating if necessary) the indexes found during class mapping (using {@code @Indexed, @Indexes)}, possibly in the background */ void ensureIndexes(boolean background); /** * Ensures (creating if necessary) the indexes found during class mapping (using {@code @Indexed, @Indexes)} */ void ensureIndexes(Class clazz); /** * Ensures (creating if necessary) the indexes found during class mapping (using {@code @Indexed, @Indexes)}, possibly in the background */ void ensureIndexes(Class clazz, boolean background); /** * ensure capped DBCollections for {@code Entity}(s) */ void ensureCaps(); DB getDB(); Mongo getMongo(); DBCollection getCollection(Class c); WriteConcern getDefaultWriteConcern(); void setDefaultWriteConcern(WriteConcern wc); /** * Replaces the current {@link QueryFactory} with the given value. * * @see QueryFactory */ void setQueryFactory(QueryFactory queryFactory); /** * Returns the current {@link QueryFactory}. * * @see QueryFactory */ QueryFactory getQueryFactory(); }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy