Search in sources :

Example 6 with MongoNamespace

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

the class MapReduceIterableImpl method execute.

MongoIterable<TResult> execute() {
    if (inline) {
        MapReduceWithInlineResultsOperation<TResult> operation = new MapReduceWithInlineResultsOperation<TResult>(namespace, new BsonJavaScript(mapFunction), new BsonJavaScript(reduceFunction), codecRegistry.get(resultClass)).filter(toBsonDocument(filter)).limit(limit).maxTime(maxTimeMS, MILLISECONDS).jsMode(jsMode).scope(toBsonDocument(scope)).sort(toBsonDocument(sort)).verbose(verbose).readConcern(readConcern).collation(collation);
        if (finalizeFunction != null) {
            operation.finalizeFunction(new BsonJavaScript(finalizeFunction));
        }
        return new OperationIterable<TResult>(operation, readPreference, executor);
    } else {
        MapReduceToCollectionOperation operation = createMapReduceToCollectionOperation();
        String dbName = databaseName != null ? databaseName : namespace.getDatabaseName();
        MongoIterable<TResult> delegated = new FindIterableImpl<TDocument, TResult>(new MongoNamespace(dbName, collectionName), documentClass, resultClass, codecRegistry, primary(), readConcern, executor, new BsonDocument(), new FindOptions().collation(collation)).batchSize(batchSize);
        return new AwaitingWriteOperationIterable<TResult, MapReduceStatistics>(operation, executor, delegated);
    }
}
Also used : FindOptions(com.mongodb.client.model.FindOptions) MongoNamespace(com.mongodb.MongoNamespace) BsonJavaScript(org.bson.BsonJavaScript) MapReduceToCollectionOperation(com.mongodb.operation.MapReduceToCollectionOperation) BsonDocument(org.bson.BsonDocument)

Example 7 with MongoNamespace

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

the class CollectionAcceptanceTest method shouldChangeACollectionNameWhenRenameIsCalled.

@Test
public void shouldChangeACollectionNameWhenRenameIsCalled() {
    //given
    collection.insertOne(new Document("someKey", "someValue"));
    assertThat(database.listCollectionNames().into(new ArrayList<String>()).contains(getCollectionName()), is(true));
    //when
    String newCollectionName = "TheNewCollectionName";
    collection.renameCollection(new MongoNamespace(getDatabaseName(), newCollectionName));
    //then
    assertThat(database.listCollectionNames().into(new ArrayList<String>()).contains(getCollectionName()), is(false));
    assertThat(database.listCollectionNames().into(new ArrayList<String>()).contains(newCollectionName), is(true));
    MongoCollection<Document> renamedCollection = database.getCollection(newCollectionName);
    assertThat("Renamed collection should have the same number of documents as original", renamedCollection.count(), is(1L));
}
Also used : BsonString(org.bson.BsonString) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) MongoNamespace(com.mongodb.MongoNamespace) Test(org.junit.Test)

Example 8 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 9 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 10 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)

Aggregations

MongoNamespace (com.mongodb.MongoNamespace)10 BsonDocument (org.bson.BsonDocument)8 BsonString (org.bson.BsonString)3 BsonValue (org.bson.BsonValue)3 Document (org.bson.Document)3 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)3 Before (org.junit.Before)3 WriteConcernResult (com.mongodb.WriteConcernResult)2 FindOptions (com.mongodb.client.model.FindOptions)2 ArrayList (java.util.ArrayList)2 BsonArray (org.bson.BsonArray)2 Test (org.junit.Test)2 WriteConcernException (com.mongodb.WriteConcernException)1 RenameCollectionOptions (com.mongodb.client.model.RenameCollectionOptions)1 CollectionHelper (com.mongodb.client.test.CollectionHelper)1 QueryResult (com.mongodb.connection.QueryResult)1 ServerVersion (com.mongodb.connection.ServerVersion)1 AggregateToCollectionOperation (com.mongodb.operation.AggregateToCollectionOperation)1 MapReduceToCollectionOperation (com.mongodb.operation.MapReduceToCollectionOperation)1 List (java.util.List)1