Search in sources :

Example 6 with BsonDocumentCodec

use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.

the class CommandProtocol method execute.

@Override
public T execute(final InternalConnection connection) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(format("Sending command {%s : %s} to database %s on connection [%s] to server %s", getCommandName(), command.values().iterator().next(), namespace.getDatabaseName(), connection.getDescription().getConnectionId(), connection.getDescription().getServerAddress()));
    }
    long startTimeNanos = System.nanoTime();
    CommandMessage commandMessage = new CommandMessage(namespace.getFullName(), command, slaveOk, fieldNameValidator, ProtocolHelper.getMessageSettings(connection.getDescription()));
    ResponseBuffers responseBuffers = null;
    try {
        sendMessage(commandMessage, connection);
        responseBuffers = connection.receiveMessage(commandMessage.getId());
        if (!ProtocolHelper.isCommandOk(new BsonBinaryReader(new ByteBufferBsonInput(responseBuffers.getBodyByteBuffer())))) {
            throw getCommandFailureException(getResponseDocument(responseBuffers, commandMessage, new BsonDocumentCodec()), connection.getDescription().getServerAddress());
        }
        T retval = getResponseDocument(responseBuffers, commandMessage, commandResultDecoder);
        if (commandListener != null) {
            sendSucceededEvent(connection.getDescription(), startTimeNanos, commandMessage, getResponseDocument(responseBuffers, commandMessage, new RawBsonDocumentCodec()));
        }
        LOGGER.debug("Command execution completed");
        return retval;
    } catch (RuntimeException e) {
        sendFailedEvent(connection.getDescription(), startTimeNanos, commandMessage, e);
        throw e;
    } finally {
        if (responseBuffers != null) {
            responseBuffers.close();
        }
    }
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) RawBsonDocumentCodec(org.bson.codecs.RawBsonDocumentCodec) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) RawBsonDocumentCodec(org.bson.codecs.RawBsonDocumentCodec) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 7 with BsonDocumentCodec

use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.

the class GetMoreProtocol method execute.

@Override
public QueryResult<T> execute(final InternalConnection connection) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(format("Getting more documents from namespace %s with cursor %d on connection [%s] to server %s", namespace, cursorId, connection.getDescription().getConnectionId(), connection.getDescription().getServerAddress()));
    }
    long startTimeNanos = System.nanoTime();
    GetMoreMessage message = new GetMoreMessage(namespace.getFullName(), cursorId, numberToReturn);
    QueryResult<T> queryResult = null;
    try {
        sendMessage(message, connection);
        ResponseBuffers responseBuffers = connection.receiveMessage(message.getId());
        try {
            if (responseBuffers.getReplyHeader().isCursorNotFound()) {
                throw new MongoCursorNotFoundException(message.getCursorId(), connection.getDescription().getServerAddress());
            }
            if (responseBuffers.getReplyHeader().isQueryFailure()) {
                BsonDocument errorDocument = new ReplyMessage<BsonDocument>(responseBuffers, new BsonDocumentCodec(), message.getId()).getDocuments().get(0);
                throw getQueryFailureException(errorDocument, connection.getDescription().getServerAddress());
            }
            queryResult = new QueryResult<T>(namespace, new ReplyMessage<T>(responseBuffers, resultDecoder, message.getId()), connection.getDescription().getServerAddress());
            if (commandListener != null) {
                sendCommandSucceededEvent(message, COMMAND_NAME, asGetMoreCommandResponseDocument(queryResult, responseBuffers), connection.getDescription(), startTimeNanos, commandListener);
            }
        } finally {
            responseBuffers.close();
        }
        LOGGER.debug("Get-more completed");
        return queryResult;
    } catch (RuntimeException e) {
        if (commandListener != null) {
            sendCommandFailedEvent(message, COMMAND_NAME, connection.getDescription(), startTimeNanos, e, commandListener);
        }
        throw e;
    }
}
Also used : BsonDocument(org.bson.BsonDocument) MongoCursorNotFoundException(com.mongodb.MongoCursorNotFoundException) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 8 with BsonDocumentCodec

use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.

the class TestCommandListener method getWritableClone.

private BsonDocument getWritableClone(final BsonDocument original) {
    BsonDocument clone = new BsonDocument();
    BsonDocumentWriter writer = new BsonDocumentWriter(clone);
    new BsonDocumentCodec(CODEC_REGISTRY_HACK).encode(writer, original, EncoderContext.builder().build());
    return clone;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 9 with BsonDocumentCodec

use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.

the class ClusterFixture method enableMaxTimeFailPoint.

public static void enableMaxTimeFailPoint() {
    assumeThat(isSharded(), is(false));
    new CommandWriteOperation<BsonDocument>("admin", new BsonDocumentWrapper<Document>(new Document("configureFailPoint", "maxTimeAlwaysTimeOut").append("mode", "alwaysOn"), new DocumentCodec()), new BsonDocumentCodec()).execute(getBinding());
}
Also used : BsonDocument(org.bson.BsonDocument) DocumentCodec(org.bson.codecs.DocumentCodec) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BsonDocumentWrapper(org.bson.BsonDocumentWrapper) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 10 with BsonDocumentCodec

use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.

the class MessageHelper method decodeCommand.

public static BsonDocument decodeCommand(final BsonInput bsonInput) {
    // length
    bsonInput.readInt32();
    //requestId
    bsonInput.readInt32();
    //responseTo
    bsonInput.readInt32();
    // opcode
    bsonInput.readInt32();
    // flags
    bsonInput.readInt32();
    //collectionName
    bsonInput.readCString();
    // numToSkip
    bsonInput.readInt32();
    // numToReturn
    bsonInput.readInt32();
    BsonBinaryReader reader = new BsonBinaryReader(bsonInput);
    return new BsonDocumentCodec().decode(reader, DecoderContext.builder().build());
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Aggregations

BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)26 BsonDocument (org.bson.BsonDocument)9 BasicOutputBuffer (org.bson.io.BasicOutputBuffer)5 StringWriter (java.io.StringWriter)4 JsonWriter (org.bson.json.JsonWriter)4 ByteBuffer (java.nio.ByteBuffer)3 BsonBinaryReader (org.bson.BsonBinaryReader)3 MongoNamespace (com.mongodb.MongoNamespace)2 WriteConcernResult (com.mongodb.WriteConcernResult)2 BsonBinaryWriter (org.bson.BsonBinaryWriter)2 BsonDocumentWriter (org.bson.BsonDocumentWriter)2 ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)2 Test (org.junit.Test)2 MongoCursorNotFoundException (com.mongodb.MongoCursorNotFoundException)1 WriteConcernException (com.mongodb.WriteConcernException)1 BulkWriteResult (com.mongodb.bulk.BulkWriteResult)1 WriteCommandResultHelper.getBulkWriteResult (com.mongodb.connection.WriteCommandResultHelper.getBulkWriteResult)1 BsonDocumentWrapper (org.bson.BsonDocumentWrapper)1 ByteBuf (org.bson.ByteBuf)1 ByteBufNIO (org.bson.ByteBufNIO)1