Search in sources :

Example 26 with MongoNamespace

use of com.mongodb.MongoNamespace 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 27 with MongoNamespace

use of com.mongodb.MongoNamespace 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 28 with MongoNamespace

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

the class OperationHelper method cursorDocumentToQueryResult.

private static <T> QueryResult<T> cursorDocumentToQueryResult(final BsonDocument cursorDocument, final ServerAddress serverAddress, final String fieldNameContainingBatch) {
    long cursorId = ((BsonInt64) cursorDocument.get("id")).getValue();
    MongoNamespace queryResultNamespace = new MongoNamespace(cursorDocument.getString("ns").getValue());
    return new QueryResult<T>(queryResultNamespace, BsonDocumentWrapperHelper.<T>toList(cursorDocument, fieldNameContainingBatch), cursorId, serverAddress);
}
Also used : BsonInt64(org.bson.BsonInt64) QueryResult(com.mongodb.connection.QueryResult) MongoNamespace(com.mongodb.MongoNamespace)

Example 29 with MongoNamespace

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

the class GridFSTest method setUp.

@Before
@Override
public void setUp() {
    super.setUp();
    gridFSBucket = GridFSBuckets.create(database);
    filesCollection = Fixture.initializeCollection(new MongoNamespace(getDefaultDatabaseName(), "fs.files")).withDocumentClass(BsonDocument.class);
    chunksCollection = Fixture.initializeCollection(new MongoNamespace(getDefaultDatabaseName(), "fs.chunks")).withDocumentClass(BsonDocument.class);
    final List<BsonDocument> filesDocuments = processFiles(data.getArray("files", new BsonArray()), new ArrayList<BsonDocument>());
    if (!filesDocuments.isEmpty()) {
        new MongoOperation<Void>() {

            @Override
            public void execute() {
                filesCollection.insertMany(filesDocuments, getCallback());
            }
        }.get();
    }
    final List<BsonDocument> chunksDocuments = processChunks(data.getArray("chunks", new BsonArray()), new ArrayList<BsonDocument>());
    if (!chunksDocuments.isEmpty()) {
        new MongoOperation<Void>() {

            @Override
            public void execute() {
                chunksCollection.insertMany(chunksDocuments, getCallback());
            }
        }.get();
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) MongoNamespace(com.mongodb.MongoNamespace) Before(org.junit.Before)

Example 30 with MongoNamespace

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

the class AggregateIterableImpl method execute.

@SuppressWarnings("deprecation")
private MongoIterable<TResult> execute() {
    List<BsonDocument> aggregateList = createBsonDocumentList();
    BsonValue outCollection = getAggregateOutCollection(aggregateList);
    if (outCollection != null) {
        AggregateToCollectionOperation operation = new AggregateToCollectionOperation(namespace, aggregateList, writeConcern).maxTime(maxTimeMS, MILLISECONDS).allowDiskUse(allowDiskUse).bypassDocumentValidation(bypassDocumentValidation).collation(collation);
        MongoIterable<TResult> delegated = new FindIterableImpl<TDocument, TResult>(new MongoNamespace(namespace.getDatabaseName(), outCollection.asString().getValue()), documentClass, resultClass, codecRegistry, primary(), readConcern, executor, new BsonDocument(), new FindOptions().collation(collation));
        if (batchSize != null) {
            delegated.batchSize(batchSize);
        }
        return new AwaitingWriteOperationIterable<TResult, Void>(operation, executor, delegated);
    } else {
        return new OperationIterable<TResult>(new AggregateOperation<TResult>(namespace, aggregateList, codecRegistry.get(resultClass)).maxTime(maxTimeMS, MILLISECONDS).allowDiskUse(allowDiskUse).batchSize(batchSize).useCursor(useCursor).readConcern(readConcern).collation(collation), readPreference, executor);
    }
}
Also used : AggregateToCollectionOperation(com.mongodb.operation.AggregateToCollectionOperation) FindOptions(com.mongodb.client.model.FindOptions) BsonDocument(org.bson.BsonDocument) MongoNamespace(com.mongodb.MongoNamespace) BsonValue(org.bson.BsonValue)

Aggregations

MongoNamespace (com.mongodb.MongoNamespace)55 BsonDocument (org.bson.BsonDocument)34 BsonString (org.bson.BsonString)21 Document (org.bson.Document)20 Before (org.junit.Before)17 MongoClientSettings (com.mongodb.MongoClientSettings)15 BsonValue (org.bson.BsonValue)13 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)10 Map (java.util.Map)10 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)10 DocumentCodec (org.bson.codecs.DocumentCodec)8 CollectionHelper (com.mongodb.client.test.CollectionHelper)7 Test (org.junit.jupiter.api.Test)7 ClientEncryptionSettings (com.mongodb.ClientEncryptionSettings)6 ConnectionString (com.mongodb.ConnectionString)6 Test (org.junit.Test)6 IndexOptions (com.mongodb.client.model.IndexOptions)5 AggregateToCollectionOperation (com.mongodb.internal.operation.AggregateToCollectionOperation)5 DisplayName (org.junit.jupiter.api.DisplayName)5