Search in sources :

Example 1 with BsonBinaryReader

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

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();
    }
}
Also used : StringWriter(java.io.StringWriter) BsonBinaryReader(org.bson.BsonBinaryReader) ByteBuf(org.bson.ByteBuf) JsonWriter(org.bson.json.JsonWriter) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 3 with BsonBinaryReader

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());
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 4 with BsonBinaryReader

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;
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) BsonBinaryWriter(org.bson.BsonBinaryWriter) Document(org.bson.Document) ByteBufNIO(org.bson.ByteBufNIO) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 5 with BsonBinaryReader

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);
}
Also used : BsonInput(org.bson.io.BsonInput) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput) BsonBinaryReader(org.bson.BsonBinaryReader) Document(org.bson.Document) Test(org.junit.Test)

Aggregations

BsonBinaryReader (org.bson.BsonBinaryReader)30 ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)17 BasicOutputBuffer (org.bson.io.BasicOutputBuffer)13 BsonBinaryWriter (org.bson.BsonBinaryWriter)12 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)8 BsonWriter (org.bson.BsonWriter)7 Document (org.bson.Document)7 ByteBuf (org.bson.ByteBuf)6 Test (org.junit.Test)6 IOContext (com.fasterxml.jackson.core.io.IOContext)5 BufferRecycler (com.fasterxml.jackson.core.util.BufferRecycler)5 BsonInput (org.bson.io.BsonInput)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 BsonDocument (org.bson.BsonDocument)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 MongoClientException (com.mongodb.MongoClientException)2 SingleResultCallback (com.mongodb.internal.async.SingleResultCallback)2 SplittablePayloadBsonWriter (com.mongodb.internal.connection.SplittablePayloadBsonWriter)2 StringWriter (java.io.StringWriter)2 List (java.util.List)2