Search in sources :

Example 1 with RawBsonDocumentCodec

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

the class CommandProtocol method execute.

@Override
public T execute(final InternalConnection connection) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(format("Sending command {%s : %s} to database %s on connection [%s] to server %s", getCommandName(), command.values().iterator().next(), namespace.getDatabaseName(), connection.getDescription().getConnectionId(), connection.getDescription().getServerAddress()));
    }
    long startTimeNanos = System.nanoTime();
    CommandMessage commandMessage = new CommandMessage(namespace.getFullName(), command, slaveOk, fieldNameValidator, ProtocolHelper.getMessageSettings(connection.getDescription()));
    ResponseBuffers responseBuffers = null;
    try {
        sendMessage(commandMessage, connection);
        responseBuffers = connection.receiveMessage(commandMessage.getId());
        if (!ProtocolHelper.isCommandOk(new BsonBinaryReader(new ByteBufferBsonInput(responseBuffers.getBodyByteBuffer())))) {
            throw getCommandFailureException(getResponseDocument(responseBuffers, commandMessage, new BsonDocumentCodec()), connection.getDescription().getServerAddress());
        }
        T retval = getResponseDocument(responseBuffers, commandMessage, commandResultDecoder);
        if (commandListener != null) {
            sendSucceededEvent(connection.getDescription(), startTimeNanos, commandMessage, getResponseDocument(responseBuffers, commandMessage, new RawBsonDocumentCodec()));
        }
        LOGGER.debug("Command execution completed");
        return retval;
    } catch (RuntimeException e) {
        sendFailedEvent(connection.getDescription(), startTimeNanos, commandMessage, e);
        throw e;
    } finally {
        if (responseBuffers != null) {
            responseBuffers.close();
        }
    }
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) RawBsonDocumentCodec(org.bson.codecs.RawBsonDocumentCodec) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) RawBsonDocumentCodec(org.bson.codecs.RawBsonDocumentCodec) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 2 with RawBsonDocumentCodec

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

the class CryptConnection method command.

@Override
public <T> T command(final String database, final BsonDocument command, final FieldNameValidator commandFieldNameValidator, final ReadPreference readPreference, final Decoder<T> commandResultDecoder, final SessionContext sessionContext, @Nullable final ServerApi serverApi, final RequestContext requestContext, final boolean responseExpected, @Nullable final SplittablePayload payload, @Nullable final FieldNameValidator payloadFieldNameValidator) {
    if (serverIsLessThanVersionFourDotTwo(wrapped.getDescription())) {
        throw new MongoClientException("Auto-encryption requires a minimum MongoDB version of 4.2");
    }
    BasicOutputBuffer bsonOutput = new BasicOutputBuffer();
    BsonBinaryWriter bsonBinaryWriter = new BsonBinaryWriter(new BsonWriterSettings(), new BsonBinaryWriterSettings(getDescription().getMaxDocumentSize()), bsonOutput, getFieldNameValidator(payload, commandFieldNameValidator, payloadFieldNameValidator));
    BsonWriter writer = payload == null ? bsonBinaryWriter : new SplittablePayloadBsonWriter(bsonBinaryWriter, bsonOutput, createSplittablePayloadMessageSettings(), payload, MAX_SPLITTABLE_DOCUMENT_SIZE);
    getEncoder(command).encode(writer, command, EncoderContext.builder().build());
    RawBsonDocument encryptedCommand = crypt.encrypt(database, new RawBsonDocument(bsonOutput.getInternalBuffer(), 0, bsonOutput.getSize()));
    RawBsonDocument encryptedResponse = wrapped.command(database, encryptedCommand, commandFieldNameValidator, readPreference, new RawBsonDocumentCodec(), sessionContext, serverApi, requestContext, responseExpected, null, null);
    RawBsonDocument decryptedResponse = crypt.decrypt(encryptedResponse);
    BsonBinaryReader reader = new BsonBinaryReader(decryptedResponse.getByteBuffer().asNIO());
    return commandResultDecoder.decode(reader, DecoderContext.builder().build());
}
Also used : MongoClientException(com.mongodb.MongoClientException) SplittablePayloadBsonWriter(com.mongodb.internal.connection.SplittablePayloadBsonWriter) RawBsonDocumentCodec(org.bson.codecs.RawBsonDocumentCodec) BsonBinaryReader(org.bson.BsonBinaryReader) SplittablePayloadBsonWriter(com.mongodb.internal.connection.SplittablePayloadBsonWriter) BsonWriter(org.bson.BsonWriter) RawBsonDocument(org.bson.RawBsonDocument) BsonBinaryWriter(org.bson.BsonBinaryWriter) BsonBinaryWriterSettings(org.bson.BsonBinaryWriterSettings) BsonWriterSettings(org.bson.BsonWriterSettings) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 3 with RawBsonDocumentCodec

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

the class LoggingCommandEventSender method sendSucceededEvent.

@Override
public void sendSucceededEvent(final ResponseBuffers responseBuffers) {
    long elapsedTimeNanos = System.nanoTime() - startTimeNanos;
    if (loggingRequired()) {
        logger.debug(format("Execution of command with request id %d completed successfully in %s ms on connection [%s] to server %s", message.getId(), getElapsedTimeFormattedInMilliseconds(elapsedTimeNanos), description.getConnectionId(), description.getServerAddress()));
    }
    if (eventRequired()) {
        BsonDocument responseDocumentForEvent = redactionRequired ? new BsonDocument() : responseBuffers.getResponseDocument(message.getId(), new RawBsonDocumentCodec());
        sendCommandSucceededEvent(message, commandName, responseDocumentForEvent, description, elapsedTimeNanos, commandListener, requestContext);
    }
}
Also used : BsonDocument(org.bson.BsonDocument) RawBsonDocumentCodec(org.bson.codecs.RawBsonDocumentCodec)

Example 4 with RawBsonDocumentCodec

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

the class MultiFileImportBenchmark method importJsonFile.

private Runnable importJsonFile(final CountDownLatch latch, final int fileId) {
    final RawBsonDocumentCodec codec = new RawBsonDocumentCodec();
    return new Runnable() {

        @Override
        public void run() {
            try {
                String resourcePath = "parallel/ldjson_multi/ldjson" + String.format("%03d", fileId) + ".txt";
                BufferedReader reader = new BufferedReader(readFromRelativePath(resourcePath), 1024 * 64);
                try {
                    String json;
                    List<RawBsonDocument> documents = new ArrayList<RawBsonDocument>(1000);
                    while ((json = reader.readLine()) != null) {
                        RawBsonDocument document = codec.decode(new JsonReader(json), DecoderContext.builder().build());
                        documents.add(document);
                        if (documents.size() == 1000) {
                            final List<RawBsonDocument> documentsToInsert = documents;
                            documentWritingService.submit(new Runnable() {

                                @Override
                                public void run() {
                                    collection.insertMany(documentsToInsert, new InsertManyOptions().ordered(false));
                                    latch.countDown();
                                }
                            });
                            documents = new ArrayList<RawBsonDocument>(1000);
                        }
                    }
                    if (!documents.isEmpty()) {
                        throw new IllegalStateException("Document count not a multiple of 1000");
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                } finally {
                    reader.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    };
}
Also used : RawBsonDocumentCodec(org.bson.codecs.RawBsonDocumentCodec) ArrayList(java.util.ArrayList) RawBsonDocument(org.bson.RawBsonDocument) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader) JsonReader(org.bson.json.JsonReader) InsertManyOptions(com.mongodb.client.model.InsertManyOptions)

Example 5 with RawBsonDocumentCodec

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

the class RawBsonDocument method toJson.

@Override
public String toJson(final JsonWriterSettings settings) {
    StringWriter writer = new StringWriter();
    new RawBsonDocumentCodec().encode(new JsonWriter(writer, settings), this, EncoderContext.builder().build());
    return writer.toString();
}
Also used : StringWriter(java.io.StringWriter) RawBsonDocumentCodec(org.bson.codecs.RawBsonDocumentCodec) JsonWriter(org.bson.json.JsonWriter)

Aggregations

RawBsonDocumentCodec (org.bson.codecs.RawBsonDocumentCodec)7 RawBsonDocument (org.bson.RawBsonDocument)4 BsonBinaryReader (org.bson.BsonBinaryReader)3 MongoClientException (com.mongodb.MongoClientException)2 SplittablePayloadBsonWriter (com.mongodb.internal.connection.SplittablePayloadBsonWriter)2 IOException (java.io.IOException)2 BsonBinaryWriter (org.bson.BsonBinaryWriter)2 BsonBinaryWriterSettings (org.bson.BsonBinaryWriterSettings)2 BsonDocument (org.bson.BsonDocument)2 BsonWriter (org.bson.BsonWriter)2 BsonWriterSettings (org.bson.BsonWriterSettings)2 MongoNamespace (com.mongodb.MongoNamespace)1 ReadPreference (com.mongodb.ReadPreference)1 RequestContext (com.mongodb.RequestContext)1 ServerApi (com.mongodb.ServerApi)1 WriteConcernResult (com.mongodb.WriteConcernResult)1 TextBasedBenchmarkResultWriter (com.mongodb.benchmark.framework.TextBasedBenchmarkResultWriter)1 InsertManyOptions (com.mongodb.client.model.InsertManyOptions)1 ConnectionDescription (com.mongodb.connection.ConnectionDescription)1 SingleResultCallback (com.mongodb.internal.async.SingleResultCallback)1