Search in sources :

Example 1 with FindResult

use of com.torodb.mongodb.commands.signatures.general.FindCommand.FindResult in project torodb by torodb.

the class OplogManager method loadState.

@Locked(exclusive = true)
private void loadState() throws OplogManagerPersistException {
    try {
        retrier.retry(() -> {
            try (ReadOnlyMongodTransaction transaction = connection.openReadOnlyTransaction()) {
                Status<FindResult> status = transaction.execute(new Request(OPLOG_DB, null, true, null), FindCommand.INSTANCE, new FindArgument.Builder().setCollection(OPLOG_COL).setSlaveOk(true).build());
                if (!status.isOk()) {
                    throw new RetrierAbortException(new MongoException(status));
                }
                Iterator<BsonDocument> batch = status.getResult().getCursor().getFirstBatch();
                if (!batch.hasNext()) {
                    lastAppliedHash = 0;
                    lastAppliedOpTime = OpTime.EPOCH;
                } else {
                    BsonDocument doc = batch.next();
                    BsonDocument subDoc = BsonReaderTool.getDocument(doc, KEY);
                    lastAppliedHash = BsonReaderTool.getLong(subDoc, "hash");
                    long optimeAsLong = BsonReaderTool.getLong(subDoc, "optime_i");
                    BsonDateTime optimeAsDateTime = DefaultBsonValues.newDateTime(optimeAsLong);
                    lastAppliedOpTime = new OpTime(TimestampToDateTime.toTimestamp(optimeAsDateTime, DefaultBsonValues::newTimestamp), BsonReaderTool.getLong(subDoc, "optime_t"));
                }
                notifyLastAppliedOpTimeChange();
                return Empty.getInstance();
            }
        }, Hint.INFREQUENT_ROLLBACK);
    } catch (RetrierGiveUpException ex) {
        throw new OplogManagerPersistException(ex);
    }
}
Also used : MongoException(com.eightkdata.mongowp.exceptions.MongoException) ReadOnlyMongodTransaction(com.torodb.mongodb.core.ReadOnlyMongodTransaction) BsonDocumentBuilder(com.eightkdata.mongowp.utils.BsonDocumentBuilder) Request(com.eightkdata.mongowp.server.api.Request) RetrierGiveUpException(com.torodb.core.retrier.RetrierGiveUpException) OpTime(com.eightkdata.mongowp.OpTime) DefaultBsonValues(com.eightkdata.mongowp.bson.utils.DefaultBsonValues) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) RetrierAbortException(com.torodb.core.retrier.RetrierAbortException) BsonDateTime(com.eightkdata.mongowp.bson.BsonDateTime) FindResult(com.torodb.mongodb.commands.signatures.general.FindCommand.FindResult) Locked(com.torodb.mongodb.annotations.Locked)

Example 2 with FindResult

use of com.torodb.mongodb.commands.signatures.general.FindCommand.FindResult 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 3 with FindResult

use of com.torodb.mongodb.commands.signatures.general.FindCommand.FindResult 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

FindResult (com.torodb.mongodb.commands.signatures.general.FindCommand.FindResult)3 BsonDocument (com.eightkdata.mongowp.bson.BsonDocument)2 MongoException (com.eightkdata.mongowp.exceptions.MongoException)2 Request (com.eightkdata.mongowp.server.api.Request)2 FindArgument (com.torodb.mongodb.commands.signatures.general.FindCommand.FindArgument)2 ErrorCode (com.eightkdata.mongowp.ErrorCode)1 OpTime (com.eightkdata.mongowp.OpTime)1 Status (com.eightkdata.mongowp.Status)1 BsonDateTime (com.eightkdata.mongowp.bson.BsonDateTime)1 DefaultBsonValues (com.eightkdata.mongowp.bson.utils.DefaultBsonValues)1 CommandFailed (com.eightkdata.mongowp.exceptions.CommandFailed)1 ReplyMessage (com.eightkdata.mongowp.messages.response.ReplyMessage)1 Command (com.eightkdata.mongowp.server.api.Command)1 BsonDocumentBuilder (com.eightkdata.mongowp.utils.BsonDocumentBuilder)1 Cursor (com.torodb.core.cursors.Cursor)1 AttributeReference (com.torodb.core.language.AttributeReference)1 Builder (com.torodb.core.language.AttributeReference.Builder)1 RetrierAbortException (com.torodb.core.retrier.RetrierAbortException)1 RetrierGiveUpException (com.torodb.core.retrier.RetrierGiveUpException)1 ToBsonDocumentTranslator (com.torodb.kvdocument.conversion.mongowp.ToBsonDocumentTranslator)1