Search in sources :

Example 1 with FindArgument

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())));
}
Also used : Request(com.eightkdata.mongowp.server.api.Request) FindResult(com.torodb.mongodb.commands.signatures.general.FindCommand.FindResult) AttributeReference(com.torodb.core.language.AttributeReference) Cursor(com.torodb.core.cursors.Cursor) KvDocument(com.torodb.kvdocument.values.KvDocument) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) Command(com.eightkdata.mongowp.server.api.Command) ToBsonDocumentTranslator(com.torodb.kvdocument.conversion.mongowp.ToBsonDocumentTranslator) Singleton(javax.inject.Singleton) CursorResult(com.torodb.mongodb.commands.pojos.CursorResult) MongodTransaction(com.torodb.mongodb.core.MongodTransaction) OptionalLong(java.util.OptionalLong) KvValue(com.torodb.kvdocument.values.KvValue) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Status(com.eightkdata.mongowp.Status) FindArgument(com.torodb.mongodb.commands.signatures.general.FindCommand.FindArgument) TorodTransaction(com.torodb.torod.TorodTransaction) Builder(com.torodb.core.language.AttributeReference.Builder) ErrorCode(com.eightkdata.mongowp.ErrorCode) ReadTorodbCommandImpl(com.torodb.mongodb.commands.impl.ReadTorodbCommandImpl) CommandFailed(com.eightkdata.mongowp.exceptions.CommandFailed) LogManager(org.apache.logging.log4j.LogManager) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) CommandFailed(com.eightkdata.mongowp.exceptions.CommandFailed) OptionalLong(java.util.OptionalLong) FindResult(com.torodb.mongodb.commands.signatures.general.FindCommand.FindResult)

Example 2 with FindArgument

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())));
}
Also used : FindArgument(com.torodb.mongodb.commands.signatures.general.FindCommand.FindArgument) MongoException(com.eightkdata.mongowp.exceptions.MongoException) ReplyMessage(com.eightkdata.mongowp.messages.response.ReplyMessage) FindResult(com.torodb.mongodb.commands.signatures.general.FindCommand.FindResult)

Aggregations

FindArgument (com.torodb.mongodb.commands.signatures.general.FindCommand.FindArgument)2 FindResult (com.torodb.mongodb.commands.signatures.general.FindCommand.FindResult)2 ErrorCode (com.eightkdata.mongowp.ErrorCode)1 Status (com.eightkdata.mongowp.Status)1 BsonDocument (com.eightkdata.mongowp.bson.BsonDocument)1 CommandFailed (com.eightkdata.mongowp.exceptions.CommandFailed)1 MongoException (com.eightkdata.mongowp.exceptions.MongoException)1 ReplyMessage (com.eightkdata.mongowp.messages.response.ReplyMessage)1 Command (com.eightkdata.mongowp.server.api.Command)1 Request (com.eightkdata.mongowp.server.api.Request)1 Cursor (com.torodb.core.cursors.Cursor)1 AttributeReference (com.torodb.core.language.AttributeReference)1 Builder (com.torodb.core.language.AttributeReference.Builder)1 ToBsonDocumentTranslator (com.torodb.kvdocument.conversion.mongowp.ToBsonDocumentTranslator)1 KvDocument (com.torodb.kvdocument.values.KvDocument)1 KvValue (com.torodb.kvdocument.values.KvValue)1 ReadTorodbCommandImpl (com.torodb.mongodb.commands.impl.ReadTorodbCommandImpl)1 CursorResult (com.torodb.mongodb.commands.pojos.CursorResult)1 MongodTransaction (com.torodb.mongodb.core.MongodTransaction)1 TorodTransaction (com.torodb.torod.TorodTransaction)1