/** * Creates an AggregationPipeline * * @param datastore the datastore to use * @param collection the database collection on which to operate * @param source the source type to aggregate */ public AggregationPipelineImpl(final xyz.morphia.DatastoreImpl datastore, final DBCollection collection, final Class source) { this.datastore = datastore; this.collection = collection; mapper = datastore.getMapper(); this.source = source; }
@Override public <T> UpdateOperations<T> createUpdateOperations(final Class<T> clazz) { return new UpdateOpsImpl<T>(clazz, getMapper()); }
protected void initType(final Class<T> type) { entityClazz = type; ds.getMapper().addMappedClass(type); }
@Override public MongoCursor<T> find(final FindOptions options) { return new MorphiaCursor<T>(ds, prepareCursor(options), ds.getMapper(), clazz, cache); }
@Override public Query<T> retrieveKnownFields() { final MappedClass mc = ds.getMapper().getMappedClass(clazz); final List<String> fields = new ArrayList<String>(mc.getPersistenceFields().size() + 1); for (final MappedField mf : mc.getPersistenceFields()) { fields.add(mf.getNameToStore()); } retrievedFields(true, fields.toArray(new String[0])); return this; }
@Override @Deprecated public <T> void ensureIndex(final Class<T> clazz, final String name, final String fields, final boolean unique, final boolean dropDupsOnCreate) { MappedClass mappedClass = getMapper().getMappedClass(clazz); ensureIndex(mappedClass.getCollectionName(), clazz, name, fields, unique, dropDupsOnCreate); }
@Override public Query<T> order(final String sort) { getOptions().sort(parseFieldsString(sort, clazz, ds.getMapper(), validateName)); return this; }
@Override @SuppressWarnings("unchecked") public <T> T get(final T entity) { return (T) find(entity.getClass()).filter("_id", getMapper().getId(entity)).get(); }
@Override public MorphiaIterator<T, T> fetch(final FindOptions options) { final DBCursor cursor = prepareCursor(options); if (LOG.isTraceEnabled()) { LOG.trace("Getting cursor(" + dbColl.getName() + ") for query:" + cursor.getQuery()); } return new MorphiaIterator<T, T>(ds, prepareCursor(options), ds.getMapper(), clazz, dbColl.getName(), cache); }
@Override @Deprecated public DBObject getFieldsObject() { DBObject projection = getOptions().getProjection(); if (projection == null || projection.keySet().isEmpty()) { return null; } final MappedClass mc = ds.getMapper().getMappedClass(clazz); Entity entityAnnotation = mc.getEntityAnnotation(); final BasicDBObject fieldsFilter = copy(projection); if (includeFields && entityAnnotation != null && !entityAnnotation.noClassnameStored()) { fieldsFilter.put(ds.getMapper().getOptions().getDiscriminatorField(), 1); } return fieldsFilter; }
@Override @Deprecated public <T> void ensureIndex(final String collection, final Class<T> clazz, final String name, final String fields, final boolean unique, final boolean dropDupsOnCreate) { if (dropDupsOnCreate) { LOG.warn("Support for dropDups has been removed from the server. Please remove this setting."); } indexHelper.createIndex(getMongoCollection(collection, clazz, null), getMapper().getMappedClass(clazz), new IndexBuilder() .fields(fields) .name(name) .unique(unique), false); }
/** * Gets the write concern for entity or returns the default write concern for this datastore * * @param clazzOrEntity the class or entity to use when looking up the WriteConcern */ private WriteConcern getWriteConcern(final Object clazzOrEntity) { WriteConcern wc = defConcern; if (clazzOrEntity != null) { final Entity entityAnn = getMapper().getMappedClass(clazzOrEntity).getEntityAnnotation(); if (entityAnn != null && !entityAnn.concern().isEmpty()) { wc = WriteConcern.valueOf(entityAnn.concern()); } } return wc; } }
@Override public MongoCursor<Key<T>> keys(final FindOptions options) { QueryImpl<T> cloned = cloneQuery(); cloned.getOptions().projection(new BasicDBObject("_id", 1)); cloned.includeFields = true; return new MorphiaKeyCursor<T>(ds, cloned.prepareCursor(options), ds.getMapper(), clazz, dbColl.getName()); }
@Override public MorphiaKeyIterator<T> fetchKeys(final FindOptions options) { QueryImpl<T> cloned = cloneQuery(); cloned.getOptions().projection(new BasicDBObject("_id", 1)); cloned.includeFields = true; return new MorphiaKeyIterator<T>(ds, cloned.prepareCursor(options), ds.getMapper(), clazz, dbColl.getName()); }
@Override public Query<T> project(final String field, final boolean include) { final Mapper mapper = ds.getMapper(); String fieldName = new PathTarget(mapper, mapper.getMappedClass(clazz), field, validateName).translatedPath(); validateProjections(fieldName, include); project(fieldName, include ? 1 : 0); return this; }
@Override public Query<T> order(final Sort... sorts) { BasicDBObject sortList = new BasicDBObject(); for (Sort sort : sorts) { String s = sort.getField(); if (validateName) { s = new PathTarget(ds.getMapper(), clazz, s).translatedPath(); } sortList.put(s, sort.getOrder()); } getOptions().sort(sortList); return this; }
@Override public Query<T> project(final String field, final ArraySlice slice) { final Mapper mapper = ds.getMapper(); String fieldName = new PathTarget(mapper, mapper.getMappedClass(clazz), field, validateName).translatedPath(); validateProjections(fieldName, true); project(fieldName, slice.toDatabase()); return this; }
@Override public Query<T> project(final Meta meta) { final Mapper mapper = ds.getMapper(); String fieldName = new PathTarget(mapper, clazz, meta.getField(), false).translatedPath(); validateProjections(fieldName, true); project(meta.toDatabase()); return this; }
@Override public QueryImpl<T> cloneQuery() { final QueryImpl<T> n = new QueryImpl<T>(clazz, dbColl, ds); n.cache = ds.getMapper().createEntityCache(); // fresh cache n.includeFields = includeFields; n.setQuery(n); // feels weird, correct? n.validateName = validateName; n.validateType = validateType; n.baseQuery = copy(baseQuery); n.options = options != null ? options.copy() : null; // fields from superclass n.setAttachedTo(getAttachedTo()); n.setChildren(getChildren() == null ? null : new ArrayList<Criteria>(getChildren())); return n; }
@Override public <T> MapreduceResults<T> mapReduce(final MapReduceOptions<T> options) { DBCollection collection = options.getQuery().getCollection(); final EntityCache cache = createCache(); MapreduceResults<T> results = new MapreduceResults<T>(collection.mapReduce(options.toCommand(getMapper()))); results.setOutputType(options.getOutputType()); if (OutputType.INLINE.equals(options.getOutputType())) { results.setInlineRequiredOptions(this, options.getResultType(), getMapper(), cache); } else { results.setQuery(newQuery(options.getResultType(), getDB().getCollection(results.getOutputCollectionName()))); } return results; }