use of com.torodb.mongodb.commands.signatures.general.FindCommand.FindArgument in project torodb by torodb.
the class FindImplementation method apply.
@Override
public Status<FindResult> apply(Request req, Command<? super FindArgument, ? super FindResult> command, FindArgument arg, MongodTransaction context) {
logFindCommand(arg);
BsonDocument filter = arg.getFilter();
Cursor<BsonDocument> cursor;
switch(filter.size()) {
case 0:
{
cursor = context.getTorodTransaction().findAll(req.getDatabase(), arg.getCollection()).asDocCursor().transform(t -> t.getRoot()).transform(ToBsonDocumentTranslator.getInstance());
break;
}
case 1:
{
try {
cursor = getByAttributeCursor(context.getTorodTransaction(), req.getDatabase(), arg.getCollection(), filter).transform(ToBsonDocumentTranslator.getInstance());
} catch (CommandFailed ex) {
return Status.from(ex);
}
break;
}
default:
{
return Status.from(ErrorCode.COMMAND_FAILED, "The given query is not supported right now");
}
}
if (Long.valueOf(arg.getBatchSize()) > (long) Integer.MAX_VALUE) {
return Status.from(ErrorCode.COMMAND_FAILED, "Only batchSize equals or lower than " + Integer.MAX_VALUE + " is supported");
}
OptionalLong batchSize = arg.getEffectiveBatchSize();
List<BsonDocument> batch = cursor.getNextBatch(batchSize.isPresent() ? (int) batchSize.getAsLong() : 101);
cursor.close();
return Status.ok(new FindResult(CursorResult.createSingleBatchCursor(req.getDatabase(), arg.getCollection(), batch.iterator())));
}
use of com.torodb.mongodb.commands.signatures.general.FindCommand.FindArgument in project torodb by torodb.
the class TorodbSafeRequestProcessor method query.
@Override
public ReplyMessage query(MongodConnection connection, Request req, int requestId, QueryRequest queryRequest) throws MongoException {
FindArgument findArg = new FindArgument.Builder().setCollection(queryRequest.getCollection()).setFilter(queryRequest.getQuery() != null ? queryRequest.getQuery() : DefaultBsonValues.EMPTY_DOC).build();
Status<FindResult> status = execute(req, FindCommand.INSTANCE, findArg, connection);
if (!status.isOk()) {
throw new MongoException(status.getErrorCode(), status.getErrorMsg());
}
FindResult result = status.getResult();
assert result != null;
return new ReplyMessage(EmptyBsonContext.getInstance(), requestId, false, false, false, false, result.getCursor().getCursorId(), queryRequest.getNumberToSkip(), IterableDocumentProvider.of(Lists.newArrayList(result.getCursor().getFirstBatch())));
}
Aggregations