use of org.mongodb.morphia.query.QueryException in project morphia by mongodb.
the class DatastoreImpl method mapReduce.
@Override
@Deprecated
public <T> MapreduceResults<T> mapReduce(final MapreduceType type, final Query query, final Class<T> outputType, final MapReduceCommand baseCommand) {
Assert.parametersNotNull("map", baseCommand.getMap());
Assert.parameterNotEmpty("map", baseCommand.getMap());
Assert.parametersNotNull("reduce", baseCommand.getReduce());
Assert.parameterNotEmpty("reduce", baseCommand.getReduce());
if (query.getOffset() != 0 || query.getFieldsObject() != null) {
throw new QueryException("mapReduce does not allow the offset/retrievedFields query options.");
}
final OutputType outType = type.toOutputType();
final DBCollection dbColl = query.getCollection();
final MapReduceCommand cmd = new MapReduceCommand(dbColl, baseCommand.getMap(), baseCommand.getReduce(), baseCommand.getOutputTarget(), outType, query.getQueryObject());
cmd.setFinalize(baseCommand.getFinalize());
cmd.setScope(baseCommand.getScope());
if (query.getLimit() > 0) {
cmd.setLimit(query.getLimit());
}
if (query.getSortObject() != null) {
cmd.setSort(query.getSortObject());
}
if (LOG.isTraceEnabled()) {
LOG.info("Executing " + cmd.toString());
}
final EntityCache cache = createCache();
MapreduceResults<T> results = new MapreduceResults<T>(dbColl.mapReduce(baseCommand));
results.setType(type);
if (MapreduceType.INLINE.equals(type)) {
results.setInlineRequiredOptions(this, outputType, getMapper(), cache);
} else {
results.setQuery(newQuery(outputType, getDB().getCollection(results.getOutputCollectionName())));
}
return results;
}
use of org.mongodb.morphia.query.QueryException in project morphia by mongodb.
the class DatastoreImpl method update.
@SuppressWarnings("unchecked")
private <T> UpdateResults update(final Query<T> query, final DBObject update, final UpdateOptions options) {
DBCollection dbColl = query.getCollection();
// TODO remove this after testing.
if (dbColl == null) {
dbColl = getCollection(query.getEntityClass());
}
if (query.getSortObject() != null && query.getSortObject().keySet() != null && !query.getSortObject().keySet().isEmpty()) {
throw new QueryException("sorting is not allowed for updates.");
}
if (query.getOffset() > 0) {
throw new QueryException("a query offset is not allowed for updates.");
}
if (query.getLimit() > 0) {
throw new QueryException("a query limit is not allowed for updates.");
}
DBObject queryObject = query.getQueryObject();
final MappedClass mc = getMapper().getMappedClass(query.getEntityClass());
final List<MappedField> fields = mc.getFieldsAnnotatedWith(Version.class);
if (!fields.isEmpty()) {
final MappedField versionMF = fields.get(0);
if (update.get(versionMF.getNameToStore()) == null) {
if (!update.containsField("$inc")) {
update.put("$inc", new BasicDBObject(versionMF.getNameToStore(), 1));
} else {
((Map<String, Object>) (update.get("$inc"))).put(versionMF.getNameToStore(), 1);
}
}
}
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()));
}
use of org.mongodb.morphia.query.QueryException in project morphia by mongodb.
the class MapReduceOptions method toCommand.
@SuppressWarnings("deprecation")
MapReduceCommand toCommand(final Mapper mapper) {
if (query.getOffset() != 0 || query.getFieldsObject() != null) {
throw new QueryException("mapReduce does not allow the offset/retrievedFields query ");
}
final DBCollection dbColl = inputCollection != null ? getQuery().getCollection().getDB().getCollection(inputCollection) : query.getCollection();
final String target = outputCollection != null ? outputCollection : mapper.getMappedClass(resultType).getCollectionName();
final MapReduceCommand command = new MapReduceCommand(dbColl, map, reduce, target, outputType, query.getQueryObject());
command.setBypassDocumentValidation(bypassDocumentValidation);
command.setCollation(collation);
command.setFinalize(finalize);
command.setJsMode(jsMode);
command.setLimit(limit);
command.setMaxTime(maxTimeMS, TimeUnit.MILLISECONDS);
command.setOutputDB(outputDB);
command.setReadPreference(readPreference);
command.setScope(scope);
command.setSort(query.getSortObject());
command.setVerbose(verbose);
return command;
}
Aggregations