Search in sources :

Example 6 with BsonDocument

use of org.bson.BsonDocument in project mongo-java-driver by mongodb.

the class CommandProtocol method sendSucceededEvent.

private void sendSucceededEvent(final ConnectionDescription connectionDescription, final long startTimeNanos, final CommandMessage commandMessage, final BsonDocument response) {
    if (commandListener != null) {
        BsonDocument responseDocumentForEvent = (SECURITY_SENSITIVE_COMMANDS.contains(getCommandName())) ? new BsonDocument() : response;
        sendCommandSucceededEvent(commandMessage, getCommandName(), responseDocumentForEvent, connectionDescription, startTimeNanos, commandListener);
    }
}
Also used : BsonDocument(org.bson.BsonDocument)

Example 7 with BsonDocument

use of org.bson.BsonDocument in project mongo-java-driver by mongodb.

the class CommandProtocol method sendFailedEvent.

private void sendFailedEvent(final ConnectionDescription connectionDescription, final long startTimeNanos, final CommandMessage commandMessage, final Throwable t) {
    if (commandListener != null) {
        Throwable commandEventException = t;
        if (t instanceof MongoCommandException && (SECURITY_SENSITIVE_COMMANDS.contains(getCommandName()))) {
            commandEventException = new MongoCommandException(new BsonDocument(), connectionDescription.getServerAddress());
        }
        sendCommandFailedEvent(commandMessage, getCommandName(), connectionDescription, startTimeNanos, commandEventException, commandListener);
    }
}
Also used : MongoCommandException(com.mongodb.MongoCommandException) BsonDocument(org.bson.BsonDocument)

Example 8 with BsonDocument

use of org.bson.BsonDocument in project mongo-java-driver by mongodb.

the class GetMoreProtocol method asGetMoreCommandResponseDocument.

private BsonDocument asGetMoreCommandResponseDocument(final QueryResult<T> queryResult, final ResponseBuffers responseBuffers) {
    List<ByteBufBsonDocument> rawResultDocuments = Collections.emptyList();
    if (responseBuffers.getReplyHeader().getNumberReturned() != 0) {
        responseBuffers.getBodyByteBuffer().position(0);
        rawResultDocuments = ByteBufBsonDocument.create(responseBuffers);
    }
    BsonDocument cursorDocument = new BsonDocument("id", queryResult.getCursor() == null ? new BsonInt64(0) : new BsonInt64(queryResult.getCursor().getId())).append("ns", new BsonString(namespace.getFullName())).append("nextBatch", new BsonArray(rawResultDocuments));
    return new BsonDocument("cursor", cursorDocument).append("ok", new BsonDouble(1));
}
Also used : BsonInt64(org.bson.BsonInt64) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonDouble(org.bson.BsonDouble)

Example 9 with BsonDocument

use of org.bson.BsonDocument in project mongo-java-driver by mongodb.

the class GetMoreProtocol method execute.

@Override
public QueryResult<T> execute(final InternalConnection connection) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(format("Getting more documents from namespace %s with cursor %d on connection [%s] to server %s", namespace, cursorId, connection.getDescription().getConnectionId(), connection.getDescription().getServerAddress()));
    }
    long startTimeNanos = System.nanoTime();
    GetMoreMessage message = new GetMoreMessage(namespace.getFullName(), cursorId, numberToReturn);
    QueryResult<T> queryResult = null;
    try {
        sendMessage(message, connection);
        ResponseBuffers responseBuffers = connection.receiveMessage(message.getId());
        try {
            if (responseBuffers.getReplyHeader().isCursorNotFound()) {
                throw new MongoCursorNotFoundException(message.getCursorId(), connection.getDescription().getServerAddress());
            }
            if (responseBuffers.getReplyHeader().isQueryFailure()) {
                BsonDocument errorDocument = new ReplyMessage<BsonDocument>(responseBuffers, new BsonDocumentCodec(), message.getId()).getDocuments().get(0);
                throw getQueryFailureException(errorDocument, connection.getDescription().getServerAddress());
            }
            queryResult = new QueryResult<T>(namespace, new ReplyMessage<T>(responseBuffers, resultDecoder, message.getId()), connection.getDescription().getServerAddress());
            if (commandListener != null) {
                sendCommandSucceededEvent(message, COMMAND_NAME, asGetMoreCommandResponseDocument(queryResult, responseBuffers), connection.getDescription(), startTimeNanos, commandListener);
            }
        } finally {
            responseBuffers.close();
        }
        LOGGER.debug("Get-more completed");
        return queryResult;
    } catch (RuntimeException e) {
        if (commandListener != null) {
            sendCommandFailedEvent(message, COMMAND_NAME, connection.getDescription(), startTimeNanos, e, commandListener);
        }
        throw e;
    }
}
Also used : BsonDocument(org.bson.BsonDocument) MongoCursorNotFoundException(com.mongodb.MongoCursorNotFoundException) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 10 with BsonDocument

use of org.bson.BsonDocument in project mongo-java-driver by mongodb.

the class InsertCommandMessage method writeTheWrites.

protected InsertCommandMessage writeTheWrites(final BsonOutput bsonOutput, final int commandStartPosition, final BsonBinaryWriter writer) {
    InsertCommandMessage nextMessage = null;
    writer.writeStartArray("documents");
    writer.pushMaxDocumentSize(getSettings().getMaxDocumentSize());
    for (int i = 0; i < insertRequestList.size(); i++) {
        writer.mark();
        BsonDocument document = insertRequestList.get(i).getDocument();
        getCodec(document).encode(writer, document, EncoderContext.builder().isEncodingCollectibleDocument(true).build());
        if (exceedsLimits(bsonOutput.getPosition() - commandStartPosition, i + 1)) {
            writer.reset();
            nextMessage = new InsertCommandMessage(getWriteNamespace(), isOrdered(), getWriteConcern(), getBypassDocumentValidation(), getSettings(), insertRequestList.subList(i, insertRequestList.size()));
            break;
        }
    }
    writer.popMaxDocumentSize();
    writer.writeEndArray();
    return nextMessage;
}
Also used : BsonDocument(org.bson.BsonDocument)

Aggregations

BsonDocument (org.bson.BsonDocument)166 BsonString (org.bson.BsonString)53 BsonValue (org.bson.BsonValue)37 Test (org.junit.Test)34 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 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 Document (org.bson.Document)5 ObjectId (org.bson.types.ObjectId)5