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();
}
}
}
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());
}
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);
}
}
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);
}
}
};
}
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();
}
Aggregations