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