Search in sources :

Example 16 with BsonDocumentCodec

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

the class BsonDocumentTest method toJsonShouldRespectDefaultJsonWriterSettings.

@Test
public void toJsonShouldRespectDefaultJsonWriterSettings() {
    StringWriter writer = new StringWriter();
    new BsonDocumentCodec().encode(new JsonWriter(writer), document, EncoderContext.builder().build());
    assertEquals(writer.toString(), document.toJson());
}
Also used : StringWriter(java.io.StringWriter) JsonWriter(org.bson.json.JsonWriter) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) Test(org.junit.Test)

Example 17 with BsonDocumentCodec

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

the class BsonHelper method toBson.

public static ByteBuffer toBson(final BsonDocument document) {
    BasicOutputBuffer bsonOutput = new BasicOutputBuffer();
    new BsonDocumentCodec().encode(new BsonBinaryWriter(bsonOutput), document, EncoderContext.builder().build());
    return ByteBuffer.wrap(bsonOutput.toByteArray());
}
Also used : BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 18 with BsonDocumentCodec

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

the class GenericBsonDecimal128Test method encodeToHex.

private String encodeToHex(final BsonDocument decodedDocument) {
    BasicOutputBuffer outputBuffer = new BasicOutputBuffer();
    new BsonDocumentCodec().encode(new BsonBinaryWriter(outputBuffer), decodedDocument, EncoderContext.builder().build());
    return DatatypeConverter.printHexBinary(outputBuffer.toByteArray());
}
Also used : BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 19 with BsonDocumentCodec

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

the class MessageHelper method encodeJson.

private static ByteBuf encodeJson(final String json) {
    OutputBuffer outputBuffer = new BasicOutputBuffer();
    JsonReader jsonReader = new JsonReader(json);
    BsonDocumentCodec codec = new BsonDocumentCodec();
    BsonDocument document = codec.decode(jsonReader, DecoderContext.builder().build());
    BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer);
    codec.encode(writer, document, EncoderContext.builder().build());
    ByteBuffer documentByteBuffer = ByteBuffer.allocate(outputBuffer.size());
    documentByteBuffer.put(outputBuffer.toByteArray());
    return new ByteBufNIO(documentByteBuffer);
}
Also used : BsonDocument(org.bson.BsonDocument) JsonReader(org.bson.json.JsonReader) BsonBinaryWriter(org.bson.BsonBinaryWriter) ByteBufNIO(org.bson.ByteBufNIO) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) OutputBuffer(org.bson.io.OutputBuffer) ByteBuffer(java.nio.ByteBuffer) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 20 with BsonDocumentCodec

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

the class WriteProtocol method execute.

@Override
public WriteConcernResult execute(final InternalConnection connection) {
    WriteConcernResult writeConcernResult = null;
    RequestMessage requestMessage = null;
    do {
        long startTimeNanos = System.nanoTime();
        RequestMessage.EncodingMetadata encodingMetadata;
        int messageId;
        boolean sentCommandStartedEvent = false;
        ByteBufferBsonOutput bsonOutput = new ByteBufferBsonOutput(connection);
        try {
            if (requestMessage == null) {
                requestMessage = createRequestMessage(getMessageSettings(connection.getDescription()));
            }
            encodingMetadata = requestMessage.encodeWithMetadata(bsonOutput);
            sendStartedEvent(connection, requestMessage, encodingMetadata, bsonOutput);
            sentCommandStartedEvent = true;
            messageId = requestMessage.getId();
            if (shouldAcknowledge(encodingMetadata.getNextMessage())) {
                CommandMessage getLastErrorMessage = new CommandMessage(new MongoNamespace(getNamespace().getDatabaseName(), COMMAND_COLLECTION_NAME).getFullName(), createGetLastErrorCommandDocument(), false, getMessageSettings(connection.getDescription()));
                getLastErrorMessage.encode(bsonOutput);
                messageId = getLastErrorMessage.getId();
            }
            connection.sendMessage(bsonOutput.getByteBuffers(), messageId);
        } catch (RuntimeException e) {
            sendFailedEvent(connection, requestMessage, sentCommandStartedEvent, e, startTimeNanos);
            throw e;
        } finally {
            bsonOutput.close();
        }
        if (shouldAcknowledge(encodingMetadata.getNextMessage())) {
            ResponseBuffers responseBuffers = null;
            try {
                responseBuffers = connection.receiveMessage(messageId);
                ReplyMessage<BsonDocument> replyMessage = new ReplyMessage<BsonDocument>(responseBuffers, new BsonDocumentCodec(), messageId);
                writeConcernResult = getWriteResult(replyMessage.getDocuments().get(0), connection.getDescription().getServerAddress());
            } catch (WriteConcernException e) {
                sendSucceededEvent(connection, requestMessage, encodingMetadata.getNextMessage(), e, startTimeNanos);
                if (writeConcern.isAcknowledged()) {
                    throw e;
                } else if (ordered) {
                    break;
                }
            } catch (RuntimeException e) {
                sendFailedEvent(connection, requestMessage, sentCommandStartedEvent, e, startTimeNanos);
                throw e;
            } finally {
                if (responseBuffers != null) {
                    responseBuffers.close();
                }
            }
        }
        sendSucceededEvent(connection, requestMessage, encodingMetadata.getNextMessage(), writeConcernResult, startTimeNanos);
        requestMessage = encodingMetadata.getNextMessage();
    } while (requestMessage != null);
    return writeConcern.isAcknowledged() ? writeConcernResult : WriteConcernResult.unacknowledged();
}
Also used : WriteConcernResult(com.mongodb.WriteConcernResult) MongoNamespace(com.mongodb.MongoNamespace) BsonDocument(org.bson.BsonDocument) WriteConcernException(com.mongodb.WriteConcernException) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Aggregations

BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)26 BsonDocument (org.bson.BsonDocument)9 BasicOutputBuffer (org.bson.io.BasicOutputBuffer)5 StringWriter (java.io.StringWriter)4 JsonWriter (org.bson.json.JsonWriter)4 ByteBuffer (java.nio.ByteBuffer)3 BsonBinaryReader (org.bson.BsonBinaryReader)3 MongoNamespace (com.mongodb.MongoNamespace)2 WriteConcernResult (com.mongodb.WriteConcernResult)2 BsonBinaryWriter (org.bson.BsonBinaryWriter)2 BsonDocumentWriter (org.bson.BsonDocumentWriter)2 ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)2 Test (org.junit.Test)2 MongoCursorNotFoundException (com.mongodb.MongoCursorNotFoundException)1 WriteConcernException (com.mongodb.WriteConcernException)1 BulkWriteResult (com.mongodb.bulk.BulkWriteResult)1 WriteCommandResultHelper.getBulkWriteResult (com.mongodb.connection.WriteCommandResultHelper.getBulkWriteResult)1 BsonDocumentWrapper (org.bson.BsonDocumentWrapper)1 ByteBuf (org.bson.ByteBuf)1 ByteBufNIO (org.bson.ByteBufNIO)1