Search in sources :

Example 21 with BsonDocumentCodec

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

the class WriteProtocol method executeAsync.

private void executeAsync(final RequestMessage requestMessage, final InternalConnection connection, final SingleResultCallback<WriteConcernResult> callback) {
    long startTimeNanos = System.nanoTime();
    boolean sentCommandStartedEvent = false;
    try {
        ByteBufferBsonOutput bsonOutput = new ByteBufferBsonOutput(connection);
        RequestMessage.EncodingMetadata encodingMetadata = encodeMessageWithMetadata(requestMessage, bsonOutput);
        sendStartedEvent(connection, requestMessage, encodingMetadata, bsonOutput);
        sentCommandStartedEvent = true;
        if (shouldAcknowledge(encodingMetadata.getNextMessage())) {
            CommandMessage getLastErrorMessage = new CommandMessage(new MongoNamespace(getNamespace().getDatabaseName(), COMMAND_COLLECTION_NAME).getFullName(), createGetLastErrorCommandDocument(), false, getMessageSettings(connection.getDescription()));
            encodeMessage(getLastErrorMessage, bsonOutput);
            SingleResultCallback<ResponseBuffers> receiveCallback = new WriteResultCallback(callback, new BsonDocumentCodec(), requestMessage, encodingMetadata.getNextMessage(), getLastErrorMessage.getId(), connection, startTimeNanos);
            connection.sendMessageAsync(bsonOutput.getByteBuffers(), getLastErrorMessage.getId(), new SendMessageCallback<WriteConcernResult>(connection, bsonOutput, requestMessage, getLastErrorMessage.getId(), getCommandName(requestMessage), startTimeNanos, commandListener, callback, receiveCallback));
        } else {
            connection.sendMessageAsync(bsonOutput.getByteBuffers(), requestMessage.getId(), new UnacknowledgedWriteResultCallback(callback, requestMessage, encodingMetadata.getNextMessage(), bsonOutput, connection, startTimeNanos));
        }
    } catch (Throwable t) {
        sendFailedEvent(connection, requestMessage, sentCommandStartedEvent, t, startTimeNanos);
        callback.onResult(null, t);
    }
}
Also used : WriteConcernResult(com.mongodb.WriteConcernResult) MongoNamespace(com.mongodb.MongoNamespace) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 22 with BsonDocumentCodec

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

the class QueryProtocol method execute.

@Override
public QueryResult<T> execute(final InternalConnection connection) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(format("Sending query of namespace %s on connection [%s] to server %s", namespace, connection.getDescription().getConnectionId(), connection.getDescription().getServerAddress()));
    }
    long startTimeNanos = System.nanoTime();
    QueryMessage message = null;
    try {
        boolean isExplain = false;
        ByteBufferBsonOutput bsonOutput = new ByteBufferBsonOutput(connection);
        try {
            message = createQueryMessage(connection.getDescription());
            RequestMessage.EncodingMetadata metadata = message.encodeWithMetadata(bsonOutput);
            isExplain = sendQueryStartedEvent(connection, message, bsonOutput, metadata);
            connection.sendMessage(bsonOutput.getByteBuffers(), message.getId());
        } finally {
            bsonOutput.close();
        }
        ResponseBuffers responseBuffers = connection.receiveMessage(message.getId());
        try {
            if (responseBuffers.getReplyHeader().isQueryFailure()) {
                BsonDocument errorDocument = new ReplyMessage<BsonDocument>(responseBuffers, new BsonDocumentCodec(), message.getId()).getDocuments().get(0);
                throw getQueryFailureException(errorDocument, connection.getDescription().getServerAddress());
            }
            ReplyMessage<T> replyMessage = new ReplyMessage<T>(responseBuffers, resultDecoder, message.getId());
            QueryResult<T> queryResult = new QueryResult<T>(namespace, replyMessage, connection.getDescription().getServerAddress());
            sendQuerySucceededEvent(connection.getDescription(), startTimeNanos, message, isExplain, responseBuffers, queryResult);
            LOGGER.debug("Query completed");
            return queryResult;
        } finally {
            responseBuffers.close();
        }
    } catch (RuntimeException e) {
        if (commandListener != null) {
            sendCommandFailedEvent(message, FIND_COMMAND_NAME, connection.getDescription(), startTimeNanos, e, commandListener);
        }
        throw e;
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 23 with BsonDocumentCodec

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

the class WriteCommandProtocol method receiveMessage.

private BsonDocument receiveMessage(final InternalConnection connection, final RequestMessage message) {
    ResponseBuffers responseBuffers = connection.receiveMessage(message.getId());
    try {
        ReplyMessage<BsonDocument> replyMessage = new ReplyMessage<BsonDocument>(responseBuffers, new BsonDocumentCodec(), message.getId());
        BsonDocument result = replyMessage.getDocuments().get(0);
        if (!ProtocolHelper.isCommandOk(result)) {
            throw getCommandFailureException(result, connection.getDescription().getServerAddress());
        }
        return result;
    } finally {
        responseBuffers.close();
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 24 with BsonDocumentCodec

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

the class WriteCommandProtocol method sendMessageAsync.

private void sendMessageAsync(final InternalConnection connection, final ByteBufferBsonOutput buffer, final BaseWriteCommandMessage message, final long startTimeNanos, final SingleResultCallback<BulkWriteResult> clientCallback, final SingleResultCallback<BsonDocument> callback) {
    SingleResultCallback<ResponseBuffers> receiveCallback = new CommandResultCallback<BsonDocument>(callback, new BsonDocumentCodec(), message.getId(), connection.getDescription().getServerAddress());
    connection.sendMessageAsync(buffer.getByteBuffers(), message.getId(), new SendMessageCallback<BulkWriteResult>(connection, buffer, message, message.getCommandName(), startTimeNanos, commandListener, clientCallback, receiveCallback));
}
Also used : BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BulkWriteResult(com.mongodb.bulk.BulkWriteResult) WriteCommandResultHelper.getBulkWriteResult(com.mongodb.connection.WriteCommandResultHelper.getBulkWriteResult)

Example 25 with BsonDocumentCodec

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

the class FindOperation method createExplainableQueryOperation.

private FindOperation<BsonDocument> createExplainableQueryOperation() {
    FindOperation<BsonDocument> explainFindOperation = new FindOperation<BsonDocument>(namespace, new BsonDocumentCodec());
    BsonDocument explainModifiers = new BsonDocument();
    if (modifiers != null) {
        explainModifiers.putAll(modifiers);
    }
    explainModifiers.append("$explain", BsonBoolean.TRUE);
    return explainFindOperation.filter(filter).projection(projection).sort(sort).skip(skip).limit(Math.abs(limit) * -1).modifiers(explainModifiers);
}
Also used : BsonDocument(org.bson.BsonDocument) 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