Search in sources :

Example 6 with WriteConcernResult

use of com.mongodb.WriteConcernResult in project mongo-java-driver by mongodb.

the class WriteProtocol method execute.

@Override
public WriteConcernResult execute(final InternalConnection connection) {
    WriteConcernResult writeConcernResult = null;
    RequestMessage requestMessage = null;
    do {
        long startTimeNanos = System.nanoTime();
        RequestMessage.EncodingMetadata encodingMetadata;
        int messageId;
        boolean sentCommandStartedEvent = false;
        ByteBufferBsonOutput bsonOutput = new ByteBufferBsonOutput(connection);
        try {
            if (requestMessage == null) {
                requestMessage = createRequestMessage(getMessageSettings(connection.getDescription()));
            }
            encodingMetadata = requestMessage.encodeWithMetadata(bsonOutput);
            sendStartedEvent(connection, requestMessage, encodingMetadata, bsonOutput);
            sentCommandStartedEvent = true;
            messageId = requestMessage.getId();
            if (shouldAcknowledge(encodingMetadata.getNextMessage())) {
                CommandMessage getLastErrorMessage = new CommandMessage(new MongoNamespace(getNamespace().getDatabaseName(), COMMAND_COLLECTION_NAME).getFullName(), createGetLastErrorCommandDocument(), false, getMessageSettings(connection.getDescription()));
                getLastErrorMessage.encode(bsonOutput);
                messageId = getLastErrorMessage.getId();
            }
            connection.sendMessage(bsonOutput.getByteBuffers(), messageId);
        } catch (RuntimeException e) {
            sendFailedEvent(connection, requestMessage, sentCommandStartedEvent, e, startTimeNanos);
            throw e;
        } finally {
            bsonOutput.close();
        }
        if (shouldAcknowledge(encodingMetadata.getNextMessage())) {
            ResponseBuffers responseBuffers = null;
            try {
                responseBuffers = connection.receiveMessage(messageId);
                ReplyMessage<BsonDocument> replyMessage = new ReplyMessage<BsonDocument>(responseBuffers, new BsonDocumentCodec(), messageId);
                writeConcernResult = getWriteResult(replyMessage.getDocuments().get(0), connection.getDescription().getServerAddress());
            } catch (WriteConcernException e) {
                sendSucceededEvent(connection, requestMessage, encodingMetadata.getNextMessage(), e, startTimeNanos);
                if (writeConcern.isAcknowledged()) {
                    throw e;
                } else if (ordered) {
                    break;
                }
            } catch (RuntimeException e) {
                sendFailedEvent(connection, requestMessage, sentCommandStartedEvent, e, startTimeNanos);
                throw e;
            } finally {
                if (responseBuffers != null) {
                    responseBuffers.close();
                }
            }
        }
        sendSucceededEvent(connection, requestMessage, encodingMetadata.getNextMessage(), writeConcernResult, startTimeNanos);
        requestMessage = encodingMetadata.getNextMessage();
    } while (requestMessage != null);
    return writeConcern.isAcknowledged() ? writeConcernResult : WriteConcernResult.unacknowledged();
}
Also used : WriteConcernResult(com.mongodb.WriteConcernResult) MongoNamespace(com.mongodb.MongoNamespace) BsonDocument(org.bson.BsonDocument) WriteConcernException(com.mongodb.WriteConcernException) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 7 with WriteConcernResult

use of com.mongodb.WriteConcernResult 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 8 with WriteConcernResult

use of com.mongodb.WriteConcernResult in project mongo-java-driver by mongodb.

the class InsertProtocol method execute.

@Override
public WriteConcernResult execute(final InternalConnection connection) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(format("Inserting %d documents into namespace %s on connection [%s] to server %s", insertRequestList.size(), getNamespace(), connection.getDescription().getConnectionId(), connection.getDescription().getServerAddress()));
    }
    WriteConcernResult writeConcernResult = super.execute(connection);
    LOGGER.debug("Insert completed");
    return writeConcernResult;
}
Also used : WriteConcernResult(com.mongodb.WriteConcernResult)

Example 9 with WriteConcernResult

use of com.mongodb.WriteConcernResult in project mongo-java-driver by mongodb.

the class DeleteProtocol method execute.

@Override
public WriteConcernResult execute(final InternalConnection connection) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(format("Deleting documents from namespace %s on connection [%s] to server %s", getNamespace(), connection.getDescription().getConnectionId(), connection.getDescription().getServerAddress()));
    }
    WriteConcernResult writeConcernResult = super.execute(connection);
    LOGGER.debug("Delete completed");
    return writeConcernResult;
}
Also used : WriteConcernResult(com.mongodb.WriteConcernResult)

Example 10 with WriteConcernResult

use of com.mongodb.WriteConcernResult in project mongo-java-driver by mongodb.

the class InsertProtocol method execute.

@Override
public WriteConcernResult execute(final InternalConnection connection) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(format("Inserting 1 document into namespace %s on connection [%s] to server %s", getNamespace(), connection.getDescription().getConnectionId(), connection.getDescription().getServerAddress()));
    }
    WriteConcernResult writeConcernResult = super.execute(connection);
    LOGGER.debug("Insert completed");
    return writeConcernResult;
}
Also used : WriteConcernResult(com.mongodb.WriteConcernResult)

Aggregations

WriteConcernResult (com.mongodb.WriteConcernResult)10 MongoNamespace (com.mongodb.MongoNamespace)3 BsonDocument (org.bson.BsonDocument)3 MongoException (com.mongodb.MongoException)2 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)2 MongoBulkWriteException (com.mongodb.MongoBulkWriteException)1 MongoWriteConcernException (com.mongodb.MongoWriteConcernException)1 MongoWriteException (com.mongodb.MongoWriteException)1 WriteConcern (com.mongodb.WriteConcern)1 WriteConcernException (com.mongodb.WriteConcernException)1 WriteError (com.mongodb.WriteError)1 Assertions (com.mongodb.assertions.Assertions)1 Assertions.assertTrue (com.mongodb.assertions.Assertions.assertTrue)1 Assertions.isTrueArgument (com.mongodb.assertions.Assertions.isTrueArgument)1 Assertions.notNull (com.mongodb.assertions.Assertions.notNull)1 BulkWriteResult (com.mongodb.bulk.BulkWriteResult)1 WriteConcernError (com.mongodb.bulk.WriteConcernError)1 ConnectionDescription (com.mongodb.connection.ConnectionDescription)1 ErrorHandlingResultCallback.errorHandlingCallback (com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback)1 SingleResultCallback (com.mongodb.internal.async.SingleResultCallback)1