Search in sources :

Example 6 with BsonDocumentWriter

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

the class SimpleSessionContext method createNewServerSessionIdentifier.

private static BsonDocument createNewServerSessionIdentifier() {
    UuidCodec uuidCodec = new UuidCodec(UuidRepresentation.STANDARD);
    BsonDocument holder = new BsonDocument();
    BsonDocumentWriter bsonDocumentWriter = new BsonDocumentWriter(holder);
    bsonDocumentWriter.writeStartDocument();
    bsonDocumentWriter.writeName("id");
    uuidCodec.encode(bsonDocumentWriter, UUID.randomUUID(), EncoderContext.builder().build());
    bsonDocumentWriter.writeEndDocument();
    return holder;
}
Also used : UuidCodec(org.bson.codecs.UuidCodec) BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter)

Example 7 with BsonDocumentWriter

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

the class TestCommandListener method getWritableClone.

private BsonDocument getWritableClone(final BsonDocument original) {
    BsonDocument clone = new BsonDocument();
    BsonDocumentWriter writer = new BsonDocumentWriter(clone);
    new BsonDocumentCodec(CODEC_REGISTRY_HACK).encode(writer, original, EncoderContext.builder().build());
    return clone;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 8 with BsonDocumentWriter

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

the class UnifiedCrudHelper method executeIterateUntilDocumentOrError.

public OperationResult executeIterateUntilDocumentOrError(final BsonDocument operation) {
    String id = operation.getString("object").getValue();
    if (entities.hasCursor(id)) {
        MongoCursor<BsonDocument> cursor = entities.getCursor(id);
        if (operation.containsKey("arguments")) {
            throw new UnsupportedOperationException("Unexpected arguments");
        }
        return resultOf(cursor::next);
    } else {
        MongoCursor<ChangeStreamDocument<BsonDocument>> cursor = entities.getChangeStreamCursor(id);
        if (operation.containsKey("arguments")) {
            throw new UnsupportedOperationException("Unexpected arguments");
        }
        return resultOf(() -> {
            BsonDocumentWriter bsonDocumentWriter = new BsonDocumentWriter(new BsonDocument());
            changeStreamDocumentCodec.encode(bsonDocumentWriter, cursor.next(), EncoderContext.builder().build());
            return bsonDocumentWriter.getDocument();
        });
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) BsonString(org.bson.BsonString)

Example 9 with BsonDocumentWriter

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

the class SimpleExpression method toBsonDocument.

@Override
public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
    BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
    writer.writeStartDocument();
    writer.writeName(name);
    BuildersHelper.encodeValue(writer, expression, codecRegistry);
    writer.writeEndDocument();
    return writer.getDocument();
}
Also used : BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument)

Example 10 with BsonDocumentWriter

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

the class DocumentCodec method getDocumentId.

@Override
public BsonValue getDocumentId(final Document document) {
    if (!documentHasId(document)) {
        throw new IllegalStateException("The document does not contain an _id");
    }
    Object id = document.get(ID_FIELD_NAME);
    if (id instanceof BsonValue) {
        return (BsonValue) id;
    }
    BsonDocument idHoldingDocument = new BsonDocument();
    BsonWriter writer = new BsonDocumentWriter(idHoldingDocument);
    writer.writeStartDocument();
    writer.writeName(ID_FIELD_NAME);
    writeValue(writer, EncoderContext.builder().build(), id);
    writer.writeEndDocument();
    return idHoldingDocument.get(ID_FIELD_NAME);
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonWriter(org.bson.BsonWriter) BsonValue(org.bson.BsonValue)

Aggregations

BsonDocument (org.bson.BsonDocument)34 BsonDocumentWriter (org.bson.BsonDocumentWriter)34 Test (org.junit.Test)13 BsonWriter (org.bson.BsonWriter)9 SingleMapReaderImpl (org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl)6 BsonDocumentReader (org.bson.BsonDocumentReader)6 FieldReader (org.apache.drill.exec.vector.complex.reader.FieldReader)4 BsonValue (org.bson.BsonValue)4 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)4 BsonValueCodecProvider (org.bson.codecs.BsonValueCodecProvider)4 ValueCodecProvider (org.bson.codecs.ValueCodecProvider)4 Test (org.junit.jupiter.api.Test)4 BaseTest (org.apache.drill.test.BaseTest)3 BsonInt32 (org.bson.BsonInt32)2 Codec (org.bson.codecs.Codec)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 ChangeStreamDocument (com.mongodb.client.model.changestream.ChangeStreamDocument)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1