Search in sources :

Example 1 with QueryOptions

use of com.eightkdata.mongowp.messages.request.QueryMessage.QueryOptions in project torodb by torodb.

the class AbstractMongoOplogReader method getFirstOrLastOp.

private OplogOperation getFirstOrLastOp(boolean first) throws OplogStartMissingException, OplogOperationUnsupported, MongoException {
    Preconditions.checkState(!isClosed(), "You have to connect this client before");
    BsonDocument query = DefaultBsonValues.EMPTY_DOC;
    BsonDocument orderBy = first ? NATURAL_ORDER_SORT : INVERSE_ORDER_SORT;
    EnumSet<QueryOption> flags = EnumSet.of(QueryOption.SLAVE_OK);
    BsonDocument doc;
    MongoConnection connection = consumeConnection();
    try {
        MongoCursor<BsonDocument> cursor = connection.query(DATABASE, COLLECTION, query, 0, 1, new QueryOptions(flags), orderBy, null);
        try {
            Batch<BsonDocument> batch = cursor.fetchBatch();
            try {
                if (!batch.hasNext()) {
                    throw new OplogStartMissingException(getSyncSource());
                }
                doc = batch.next();
            } finally {
                batch.close();
            }
        } finally {
            cursor.close();
        }
        try {
            return OplogOperationParser.fromBson(doc);
        } catch (BadValueException | TypesMismatchException | NoSuchKeyException ex) {
            throw new OplogOperationUnsupported(doc, ex);
        }
    } finally {
        releaseConnection(connection);
    }
}
Also used : BadValueException(com.eightkdata.mongowp.exceptions.BadValueException) OplogOperationUnsupported(com.eightkdata.mongowp.exceptions.OplogOperationUnsupported) QueryOption(com.eightkdata.mongowp.messages.request.QueryMessage.QueryOption) OplogStartMissingException(com.eightkdata.mongowp.exceptions.OplogStartMissingException) QueryOptions(com.eightkdata.mongowp.messages.request.QueryMessage.QueryOptions) NoSuchKeyException(com.eightkdata.mongowp.exceptions.NoSuchKeyException) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) TypesMismatchException(com.eightkdata.mongowp.exceptions.TypesMismatchException) MongoConnection(com.eightkdata.mongowp.client.core.MongoConnection)

Example 2 with QueryOptions

use of com.eightkdata.mongowp.messages.request.QueryMessage.QueryOptions in project torodb by torodb.

the class TransactionalDbCloner method cloneCollection.

private void cloneCollection(String toDb, MongoConnection remoteConnection, WriteMongodTransaction transaction, CloneOptions opts, String collection, CollectionOptions collOptions) throws MongoException, CloningException {
    String fromDb = opts.getDbToClone();
    LOGGER.info("Cloning {}.{} into {}.{}", fromDb, collection, toDb, collection);
    //TODO: enable exhaust?
    EnumSet<QueryOption> queryFlags = EnumSet.of(QueryOption.NO_CURSOR_TIMEOUT);
    if (opts.isSlaveOk()) {
        queryFlags.add(QueryOption.SLAVE_OK);
    }
    MongoCursor<BsonDocument> cursor = remoteConnection.query(opts.getDbToClone(), collection, null, 0, 0, new QueryOptions(queryFlags), null, null);
    while (!cursor.hasNext()) {
        List<? extends BsonDocument> docsToInsert = cursor.fetchBatch().asList();
        Status<InsertResult> insertResult = transaction.execute(new Request(toDb, null, true, null), InsertCommand.INSTANCE, new InsertArgument.Builder(collection).addDocuments(docsToInsert).setWriteConcern(WriteConcern.fsync()).setOrdered(true).build());
        if (!insertResult.isOk() || insertResult.getResult().getN() != docsToInsert.size()) {
            throw new CloningException("Error while inserting a cloned document");
        }
    }
}
Also used : InsertResult(com.torodb.mongodb.commands.signatures.general.InsertCommand.InsertResult) Request(com.eightkdata.mongowp.server.api.Request) QueryOption(com.eightkdata.mongowp.messages.request.QueryMessage.QueryOption) QueryOptions(com.eightkdata.mongowp.messages.request.QueryMessage.QueryOptions) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) InsertArgument(com.torodb.mongodb.commands.signatures.general.InsertCommand.InsertArgument)

Example 3 with QueryOptions

use of com.eightkdata.mongowp.messages.request.QueryMessage.QueryOptions in project torodb by torodb.

the class AbstractMongoOplogReader method query.

public MongoCursor<OplogOperation> query(BsonDocument query, EnumSet<QueryOption> flags, BsonDocument sortBy) throws MongoException {
    Preconditions.checkState(!isClosed(), "You have to connect this client before");
    MongoConnection connection = consumeConnection();
    MongoCursor<BsonDocument> cursor = connection.query(DATABASE, COLLECTION, query, 0, 0, new QueryOptions(flags), sortBy, null);
    return new MyCursor<>(connection, TransformationMongoCursor.create(cursor, OplogOperationParser.asFunction()));
}
Also used : BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) MongoConnection(com.eightkdata.mongowp.client.core.MongoConnection) QueryOptions(com.eightkdata.mongowp.messages.request.QueryMessage.QueryOptions)

Aggregations

BsonDocument (com.eightkdata.mongowp.bson.BsonDocument)3 QueryOptions (com.eightkdata.mongowp.messages.request.QueryMessage.QueryOptions)3 MongoConnection (com.eightkdata.mongowp.client.core.MongoConnection)2 QueryOption (com.eightkdata.mongowp.messages.request.QueryMessage.QueryOption)2 BadValueException (com.eightkdata.mongowp.exceptions.BadValueException)1 NoSuchKeyException (com.eightkdata.mongowp.exceptions.NoSuchKeyException)1 OplogOperationUnsupported (com.eightkdata.mongowp.exceptions.OplogOperationUnsupported)1 OplogStartMissingException (com.eightkdata.mongowp.exceptions.OplogStartMissingException)1 TypesMismatchException (com.eightkdata.mongowp.exceptions.TypesMismatchException)1 Request (com.eightkdata.mongowp.server.api.Request)1 InsertArgument (com.torodb.mongodb.commands.signatures.general.InsertCommand.InsertArgument)1 InsertResult (com.torodb.mongodb.commands.signatures.general.InsertCommand.InsertResult)1