Search in sources :

Example 36 with BsonDocument

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);
    }
}
Also used : IndexMap(com.mongodb.internal.connection.IndexMap) BsonDocument(org.bson.BsonDocument)

Example 37 with BsonDocument

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;
    }
}
Also used : BulkWriteUpsert(com.mongodb.bulk.BulkWriteUpsert) BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) ArrayList(java.util.ArrayList) BsonValue(org.bson.BsonValue)

Example 38 with BsonDocument

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;
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument)

Example 39 with BsonDocument

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;
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray)

Example 40 with BsonDocument

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;
}
Also used : BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) File(java.io.File)

Aggregations

BsonDocument (org.bson.BsonDocument)169 BsonString (org.bson.BsonString)53 BsonValue (org.bson.BsonValue)37 Test (org.junit.Test)36 BsonArray (org.bson.BsonArray)29 BsonInt32 (org.bson.BsonInt32)28 ArrayList (java.util.ArrayList)24 BsonDocumentReader (org.bson.BsonDocumentReader)17 SingleMapReaderImpl (org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl)14 BsonDocumentWriter (org.bson.BsonDocumentWriter)14 BsonInt64 (org.bson.BsonInt64)14 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)10 BsonDouble (org.bson.BsonDouble)8 Document (org.bson.Document)7 MongoNamespace (com.mongodb.MongoNamespace)6 Before (org.junit.Before)6 MongoGridFSException (com.mongodb.MongoGridFSException)5 BsonObjectId (org.bson.BsonObjectId)5 BsonWriter (org.bson.BsonWriter)5 ObjectId (org.bson.types.ObjectId)5