use of org.bson.BsonDocument 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);
}
}
use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class WriteCommandResultHelper method getUpsertedItems.
@SuppressWarnings("unchecked")
private static List<BulkWriteUpsert> getUpsertedItems(final BsonDocument result) {
BsonValue upsertedValue = result.get("upserted");
if (upsertedValue == null) {
return Collections.emptyList();
} else {
List<BulkWriteUpsert> bulkWriteUpsertList = new ArrayList<BulkWriteUpsert>();
for (BsonValue upsertedItem : (BsonArray) upsertedValue) {
BsonDocument upsertedItemDocument = (BsonDocument) upsertedItem;
bulkWriteUpsertList.add(new BulkWriteUpsert(upsertedItemDocument.getNumber("index").intValue(), upsertedItemDocument.get("_id")));
}
return bulkWriteUpsertList;
}
}
use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class WriteProtocol method createGetLastErrorCommandDocument.
private BsonDocument createGetLastErrorCommandDocument() {
BsonDocument command = new BsonDocument("getlasterror", new BsonInt32(1));
command.putAll(writeConcern.asDocument());
return command;
}
use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class WriteProtocol method getResponseDocument.
private BsonDocument getResponseDocument(final RequestMessage curMessage, final RequestMessage nextMessage, final WriteConcernResult writeConcernResult, final WriteConcernException writeConcernException) {
BsonDocument response = new BsonDocument("ok", new BsonInt32(1));
if (writeConcern.isAcknowledged()) {
if (writeConcernException == null) {
appendToWriteCommandResponseDocument(curMessage, nextMessage, writeConcernResult, response);
} else {
response.put("n", new BsonInt32(0));
BsonDocument writeErrorDocument = new BsonDocument("index", new BsonInt32(0)).append("code", new BsonInt32(writeConcernException.getErrorCode()));
if (writeConcernException.getErrorMessage() != null) {
writeErrorDocument.append("errmsg", new BsonString(writeConcernException.getErrorMessage()));
}
response.put("writeErrors", new BsonArray(singletonList(writeErrorDocument)));
}
}
return response;
}
use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class AbstractServerDiscoveryAndMonitoringTest method data.
public static Collection<Object[]> data(final String root) throws URISyntaxException, IOException {
List<Object[]> data = new ArrayList<Object[]>();
for (File file : JsonPoweredTestHelper.getTestFiles(root)) {
BsonDocument testDocument = JsonPoweredTestHelper.getTestDocument(file);
data.add(new Object[] { testDocument.getString("description").getValue(), testDocument });
}
return data;
}
Aggregations