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); }
/** * It is best to use a Mongo singleton instance here. * * @param mongoClient the representations of the connection to a MongoDB instance * @param dbName the name of the database * @return a Datastore that you can use to interact with MongoDB */ @SuppressWarnings("deprecation") public Datastore createDatastore(final MongoClient mongoClient, final String dbName) { return new DatastoreImpl(this, mongoClient, dbName); }
@Override public <T, V> WriteResult delete(final Class<T> clazz, final V id, final DeleteOptions options) { return delete(createQuery(clazz).filter("_id", id), options); }
@Override public <T> WriteResult delete(final T entity) { return delete(entity, getWriteConcern(entity)); }
final LinkedHashMap<Object, DBObject> involvedObjects = new LinkedHashMap<Object, DBObject>(); final DBObject dbObj = mapper.toDBObject(unwrapped, involvedObjects); final Key<T> key = getKey(unwrapped); unwrapped = ProxyHelper.unwrap(unwrapped); final Object id = mapper.getId(unwrapped); final DBCollection dbColl = getCollection(unwrapped); wr = tryVersionedUpdate(dbColl, unwrapped, dbObj, idValue, new InsertOptions().writeConcern(wc), mc); final Query<T> query = (Query<T>) createQuery(unwrapped.getClass()).filter("_id", id); wr = update(query, new BasicDBObject("$set", dbObj), new UpdateOptions().writeConcern(wc)).getWriteResult(); postSaveOperations(Collections.<Object>singletonList(entity), involvedObjects, false, dbColl.getName()); return key;
@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; }
@Override public <T> UpdateResults update(final Query<T> query, final UpdateOperations<T> operations, final UpdateOptions options) { DBCollection dbColl = query.getCollection(); // TODO remove this after testing. if (dbColl == null) { dbColl = getCollection(query.getEntityClass()); } final MappedClass mc = getMapper().getMappedClass(query.getEntityClass()); final List<MappedField> fields = mc.getFieldsAnnotatedWith(Version.class); DBObject queryObject = query.getQueryObject(); if (operations.isIsolated()) { queryObject.put("$isolated", true); } if (!fields.isEmpty()) { operations.inc(fields.get(0).getNameToStore(), 1); } final BasicDBObject update = (BasicDBObject) ((UpdateOpsImpl) operations).getOps(); if (LOG.isTraceEnabled()) { LOG.trace(format("Executing update(%s) for query: %s, ops: %s, multi: %s, upsert: %s", dbColl.getName(), queryObject, update, options.isMulti(), options.isUpsert())); } return new UpdateResults(dbColl.update(queryObject, update, enforceWriteConcern(options, query.getEntityClass()) .getOptions())); }
@Override public <T> Key<T> save(final T entity, final InsertOptions options) { if (entity == null) { throw new UpdateException("Can not persist a null entity"); } final T unwrapped = ProxyHelper.unwrap(entity); return save(getCollection(unwrapped), unwrapped, enforceWriteConcern(options, entity.getClass())); }
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); }
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> T findAndDelete(final Query<T> query, final FindAndModifyOptions options) { DBCollection dbColl = query.getCollection(); if (dbColl == null) { dbColl = getCollection(query.getEntityClass()); } if (LOG.isTraceEnabled()) { LOG.trace("Executing findAndModify(" + dbColl.getName() + ") with delete ..."); } FindAndModifyOptions copy = enforceWriteConcern(options, query.getEntityClass()) .copy() .projection(query.getFieldsObject()) .sort(query.getSortObject()) .returnNew(false) .upsert(false) .remove(true); final DBObject result = dbColl.findAndModify(query.getQueryObject(), copy.getOptions()); return result == null ? null : mapper.fromDBObject(this, query.getEntityClass(), result, createCache()); }
@SuppressWarnings("unchecked") private <T> MongoCollection<T> getMongoCollection(final String name, final Class<T> clazz, final WriteConcern concern) { final MongoCollection<T> collection = database.getCollection(name, clazz); return enforceWriteConcern(collection, clazz, concern); }
@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()); }
@Override public <T> WriteResult delete(final Query<T> query, final DeleteOptions options) { DBCollection dbColl = query.getCollection(); // TODO remove this after testing. if (dbColl == null) { dbColl = getCollection(query.getEntityClass()); } if (query.getSortObject() != null || query.getOffset() != 0 || query.getLimit() > 0) { throw new QueryException("Delete does not allow sort/offset/limit query options."); } return dbColl.remove(query.getQueryObject(), enforceWriteConcern(options, query.getEntityClass()).getOptions()); }
@Override @SuppressWarnings("unchecked") public <T> UpdateResults update(final Key<T> key, final UpdateOperations<T> operations) { Class<T> clazz = (Class<T>) key.getType(); if (clazz == null) { clazz = (Class<T>) mapper.getClassFromCollection(key.getCollection()); } return update(createQuery(clazz).disableValidation().filter("_id", key.getId()), operations, new UpdateOptions()); }
@Override public void enableDocumentValidation() { for (final MappedClass mc : mapper.getMappedClasses()) { process(mc, (Validation) mc.getAnnotation(Validation.class)); } }
@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 WriteResult deleteById(final K id) { return ds.delete(entityClazz, id); }
@Override public <T> T get(final Class<T> clazz, final DBRef ref) { DBObject object = getDB().getCollection(ref.getCollectionName()).findOne(new BasicDBObject("_id", ref.getId())); return mapper.fromDBObject(this, clazz, object, createCache()); }