use of org.bson.BsonBinaryReader 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.BsonBinaryReader in project mongo-java-driver by mongodb.
the class ByteBufBsonDocument method toJson.
@Override
public String toJson(final JsonWriterSettings settings) {
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(stringWriter, settings);
ByteBuf duplicate = byteBuf.duplicate();
BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(duplicate));
try {
jsonWriter.pipe(reader);
return stringWriter.toString();
} finally {
duplicate.release();
reader.close();
}
}
use of org.bson.BsonBinaryReader in project mongo-java-driver by mongodb.
the class MessageHelper method decodeCommand.
public static BsonDocument decodeCommand(final BsonInput bsonInput) {
// length
bsonInput.readInt32();
//requestId
bsonInput.readInt32();
//responseTo
bsonInput.readInt32();
// opcode
bsonInput.readInt32();
// flags
bsonInput.readInt32();
//collectionName
bsonInput.readCString();
// numToSkip
bsonInput.readInt32();
// numToReturn
bsonInput.readInt32();
BsonBinaryReader reader = new BsonBinaryReader(bsonInput);
return new BsonDocumentCodec().decode(reader, DecoderContext.builder().build());
}
use of org.bson.BsonBinaryReader in project mongo-java-driver by mongodb.
the class CodecTestUtil method prepareReaderWithObjectToBeDecoded.
static BsonBinaryReader prepareReaderWithObjectToBeDecoded(final Object objectToDecode) {
// Need to encode it wrapped in a document to conform to the validation
Document document = new Document("wrapperDocument", objectToDecode);
BasicOutputBuffer outputBuffer = new BasicOutputBuffer();
BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer);
byte[] documentAsByteArrayForReader;
try {
new DocumentCodec().encode(writer, document, EncoderContext.builder().build());
documentAsByteArrayForReader = outputBuffer.toByteArray();
} finally {
writer.close();
}
BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(wrap(documentAsByteArrayForReader))));
// have to read off the wrapper document so the reader is in the correct position for the test
reader.readStartDocument();
reader.readName();
return reader;
}
use of org.bson.BsonBinaryReader in project mongo-java-driver by mongodb.
the class DocumentCodecTest method testIterableEncoding.
@Test
public void testIterableEncoding() throws IOException {
DocumentCodec documentCodec = new DocumentCodec();
Document doc = new Document().append("list", asList(1, 2, 3, 4, 5)).append("set", new HashSet<Integer>(asList(1, 2, 3, 4)));
documentCodec.encode(writer, doc, EncoderContext.builder().build());
BsonInput bsonInput = createInputBuffer();
Document decodedDocument = documentCodec.decode(new BsonBinaryReader(bsonInput), DecoderContext.builder().build());
assertEquals(new Document().append("list", asList(1, 2, 3, 4, 5)).append("set", asList(1, 2, 3, 4)), decodedDocument);
}
Aggregations