Search in sources :

Example 1 with IndexMap

use of com.mongodb.internal.connection.IndexMap in project mongo-java-driver by mongodb.

the class WriteCommandProtocol method executeBatchesAsync.

private void executeBatchesAsync(final InternalConnection connection, final BaseWriteCommandMessage message, final BulkWriteBatchCombiner bulkWriteBatchCombiner, final int batchNum, final int currentRangeStartIndex, final SingleResultCallback<BulkWriteResult> callback) {
    final long startTimeNanos = System.nanoTime();
    boolean sentStartedEvent = false;
    try {
        if (message != null && !bulkWriteBatchCombiner.shouldStopSendingMoreBatches()) {
            final ByteBufferBsonOutput bsonOutput = new ByteBufferBsonOutput(connection);
            RequestMessage.EncodingMetadata metadata = message.encodeWithMetadata(bsonOutput);
            sendStartedEvent(connection, message, bsonOutput, metadata);
            sentStartedEvent = true;
            final BaseWriteCommandMessage nextMessage = (BaseWriteCommandMessage) metadata.getNextMessage();
            final int itemCount = nextMessage != null ? message.getItemCount() - nextMessage.getItemCount() : message.getItemCount();
            final IndexMap indexMap = IndexMap.create(currentRangeStartIndex, itemCount);
            final int nextBatchNum = batchNum + 1;
            final int nextRangeStartIndex = currentRangeStartIndex + itemCount;
            if (nextBatchNum > 1) {
                if (getLogger().isDebugEnabled()) {
                    getLogger().debug(format("Asynchronously sending batch %d", batchNum));
                }
            }
            sendMessageAsync(connection, bsonOutput, message, startTimeNanos, callback, new SingleResultCallback<BsonDocument>() {

                @Override
                public void onResult(final BsonDocument result, final Throwable t) {
                    bsonOutput.close();
                    if (t != null) {
                        sendFailedEvent(connection, message, startTimeNanos, t);
                        callback.onResult(null, t);
                    } else {
                        sendSucceededEvent(connection, message, startTimeNanos, result);
                        if (nextBatchNum > 1) {
                            if (getLogger().isDebugEnabled()) {
                                getLogger().debug(format("Asynchronously received response for batch %d", batchNum));
                            }
                        }
                        if (WriteCommandResultHelper.hasError(result)) {
                            bulkWriteBatchCombiner.addErrorResult(getBulkWriteException(getType(), result, connection.getDescription().getServerAddress()), indexMap);
                        } else {
                            bulkWriteBatchCombiner.addResult(getBulkWriteResult(getType(), result), indexMap);
                        }
                        executeBatchesAsync(connection, nextMessage, bulkWriteBatchCombiner, nextBatchNum, nextRangeStartIndex, callback);
                    }
                }
            });
        } else {
            if (bulkWriteBatchCombiner.hasErrors()) {
                callback.onResult(null, bulkWriteBatchCombiner.getError());
            } else {
                callback.onResult(bulkWriteBatchCombiner.getResult(), null);
            }
        }
    } catch (Throwable t) {
        if (sentStartedEvent) {
            sendFailedEvent(connection, message, startTimeNanos, t);
        }
        callback.onResult(null, t);
    }
}
Also used : IndexMap(com.mongodb.internal.connection.IndexMap) BsonDocument(org.bson.BsonDocument)

Example 2 with IndexMap

use of com.mongodb.internal.connection.IndexMap in project mongo-java-driver by mongodb.

the class WriteCommandProtocol method execute.

@Override
public BulkWriteResult execute(final InternalConnection connection) {
    BaseWriteCommandMessage message = createRequestMessage(getMessageSettings(connection.getDescription()));
    long startTimeNanos = System.nanoTime();
    try {
        BulkWriteBatchCombiner bulkWriteBatchCombiner = new BulkWriteBatchCombiner(connection.getDescription().getServerAddress(), ordered, writeConcern);
        int batchNum = 0;
        int currentRangeStartIndex = 0;
        do {
            batchNum++;
            startTimeNanos = System.nanoTime();
            BaseWriteCommandMessage nextMessage = sendMessage(connection, message, batchNum);
            int itemCount = nextMessage != null ? message.getItemCount() - nextMessage.getItemCount() : message.getItemCount();
            IndexMap indexMap = IndexMap.create(currentRangeStartIndex, itemCount);
            BsonDocument result = receiveMessage(connection, message);
            if (nextMessage != null || batchNum > 1) {
                if (getLogger().isDebugEnabled()) {
                    getLogger().debug(format("Received response for batch %d", batchNum));
                }
            }
            if (WriteCommandResultHelper.hasError(result)) {
                MongoBulkWriteException bulkWriteException = getBulkWriteException(getType(), result, connection.getDescription().getServerAddress());
                bulkWriteBatchCombiner.addErrorResult(bulkWriteException, indexMap);
            } else {
                bulkWriteBatchCombiner.addResult(getBulkWriteResult(getType(), result), indexMap);
            }
            sendSucceededEvent(connection, message, startTimeNanos, result);
            currentRangeStartIndex += itemCount;
            message = nextMessage;
        } while (message != null && !bulkWriteBatchCombiner.shouldStopSendingMoreBatches());
        return bulkWriteBatchCombiner.getResult();
    } catch (MongoBulkWriteException e) {
        throw e;
    } catch (RuntimeException e) {
        sendFailedEvent(connection, message, startTimeNanos, e);
        throw e;
    }
}
Also used : IndexMap(com.mongodb.internal.connection.IndexMap) BsonDocument(org.bson.BsonDocument) MongoBulkWriteException(com.mongodb.MongoBulkWriteException)

Aggregations

IndexMap (com.mongodb.internal.connection.IndexMap)2 BsonDocument (org.bson.BsonDocument)2 MongoBulkWriteException (com.mongodb.MongoBulkWriteException)1