/** * Get the name of the collection that the results of the map reduce were saved into. If the map reduce was an inline operation (i.e . * the results were returned directly from calling the map reduce) this will return null. * * @return the name of the collection that the map reduce results are stored in */ @Nullable public final String getCollectionName() { return collection == null ? null : collection.getName(); }
@Override @Deprecated public long countAll() { final DBObject query = getQueryObject(); if (LOG.isTraceEnabled()) { LOG.trace("Executing count(" + dbColl.getName() + ") for query: " + query); } return dbColl.getCount(query); }
@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 public <U> Iterator<U> aggregate(final Class<U> target, final AggregationOptions options, final ReadPreference readPreference) { return aggregate(datastore.getCollection(target).getName(), target, options, readPreference); }
@Override public <U> Iterator<U> out(final Class<U> target) { return out(datastore.getCollection(target).getName(), target); }
@Override public <U> Iterator<U> out(final Class<U> target, final AggregationOptions options) { return out(datastore.getCollection(target).getName(), target, options); }
protected <T> Key<T> insert(final DBCollection dbColl, final T entity, final InsertOptions options) { final LinkedHashMap<Object, DBObject> involvedObjects = new LinkedHashMap<Object, DBObject>(); dbColl.insert(singletonList(entityToDBObj(entity, involvedObjects)), enforceWriteConcern(options, entity.getClass()) .getOptions()); return postSaveOperations(singletonList(entity), involvedObjects, dbColl.getName()).get(0); }
@Override public <T, V> DBRef createRef(final Class<T> clazz, final V id) { if (id == null) { throw new MappingException("Could not get id for " + clazz.getName()); } return new DBRef(getCollection(clazz).getName(), id); }
private <T> Iterable<Key<T>> insert(final DBCollection dbColl, final Iterable<T> entities, final InsertOptions options) { if (!entities.iterator().hasNext()) { return emptyList(); } final Map<Object, DBObject> involvedObjects = new LinkedHashMap<Object, DBObject>(); final List<DBObject> list = new ArrayList<DBObject>(); com.mongodb.InsertOptions insertOptions = options.getOptions(); for (final T entity : entities) { if (options.getWriteConcern() == null) { insertOptions = enforceWriteConcern(options, entity.getClass()).getOptions(); } list.add(toDbObject(entity, involvedObjects)); } dbColl.insert(list, insertOptions); return postSaveOperations(entities, involvedObjects, dbColl.getName()); } /*
@Override public <T, V> T get(final Class<T> clazz, final V id) { return find(getCollection(clazz).getName(), clazz, "_id", id, 0, 1, true).get(); }
protected <T> Key<T> save(final DBCollection dbColl, final T entity, final InsertOptions options) { final MappedClass mc = validateSave(entity); // involvedObjects is used not only as a cache but also as a list of what needs to be called for life-cycle methods at the end. final LinkedHashMap<Object, DBObject> involvedObjects = new LinkedHashMap<Object, DBObject>(); final DBObject document = entityToDBObj(entity, involvedObjects); // try to do an update if there is a @Version field final Object idValue = document.get("_id"); WriteResult wr = tryVersionedUpdate(dbColl, entity, document, idValue, enforceWriteConcern(options, entity.getClass()), mc); if (wr == null) { saveDocument(dbColl, document, options); } return postSaveOperations(singletonList(entity), involvedObjects, dbColl.getName()).get(0); }
MongoNamespace getNamespace() { return new MongoNamespace(getDB().getName(), getName()); }
@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()); }
private DBCursor prepareCursor(final FindOptions findOptions) { final DBObject query = getQueryObject(); if (LOG.isTraceEnabled()) { LOG.trace(String.format("Running query(%s) : %s, options: %s,", dbColl.getName(), query, findOptions)); } if (findOptions.isSnapshot() && (findOptions.getSortDBObject() != null || findOptions.hasHint())) { LOG.warn("Snapshotted query should not have hint/sort."); } if (findOptions.getCursorType() != NonTailable && (findOptions.getSortDBObject() != null)) { LOG.warn("Sorting on tail is not allowed."); } return dbColl.find(query, findOptions.getOptions() .copy() .sort(getSortObject()) .projection(getFieldsObject())) .setDecoderFactory(ds.getDecoderFact()); }
/** * The collStats command returns a variety of storage statistics for a given collection * * @return a CommandResult containing the statistics about this collection * @mongodb.driver.manual reference/command/collStats/ collStats Command */ public CommandResult getStats() { return getDB().executeCommand(new BsonDocument("collStats", new BsonString(getName())), getReadPreference()); }
@Override public <T> T findAndModify(final Query<T> query, final UpdateOperations<T> operations, final FindAndModifyOptions options) { DBCollection dbColl = query.getCollection(); // TODO remove this after testing. if (dbColl == null) { dbColl = getCollection(query.getEntityClass()); } if (LOG.isTraceEnabled()) { LOG.info("Executing findAndModify(" + dbColl.getName() + ") with update "); } updateForVersioning(query, operations); DBObject res = dbColl.findAndModify(query.getQueryObject(), options.copy() .sort(query.getSortObject()) .projection(query.getFieldsObject()) .update(((UpdateOpsImpl<T>) operations).getOps()) .getOptions()); return res == null ? null : mapper.fromDBObject(this, query.getEntityClass(), res, createCache()); }
@PrePersist void prePersist() { if (myLongId == null) { final String collName = ds.getCollection(getClass()).getName(); final Query<StoredId> q = ds.find(StoredId.class).filter("_id", collName); final UpdateOperations<StoredId> uOps = ds.createUpdateOperations(StoredId.class).inc("value"); StoredId newId = ds.findAndModify(q, uOps); if (newId == null) { newId = new StoredId(collName); ds.save(newId); } myLongId = newId.getValue(); } } }
@Test public void testOtherDelete() throws Exception { final E e = new E(); e.id = new CId("test"); getDs().save(e); getAds().delete(getDs().getCollection(E.class).getName(), E.class, e.id); }
@Test public void testOtherDelete() { final CompoundIdEntity entity = new CompoundIdEntity(); entity.id = new CompoundId("test"); getDs().save(entity); ((AdvancedDatastore) getDs()).delete(getDs().getCollection(CompoundIdEntity.class).getName(), CompoundIdEntity.class, entity.id); }