use of com.mongodb.lang.Nullable in project mongo-java-driver by mongodb.
the class CryptConnection method commandAsync.
@Override
public <T> void commandAsync(final String database, final BsonDocument command, final FieldNameValidator commandFieldNameValidator, final ReadPreference readPreference, final Decoder<T> commandResultDecoder, final SessionContext sessionContext, final ServerApi serverApi, final RequestContext requestContext, final boolean responseExpected, @Nullable final SplittablePayload payload, @Nullable final FieldNameValidator payloadFieldNameValidator, final SingleResultCallback<T> callback) {
if (serverIsLessThanVersionFourDotTwo(wrapped.getDescription())) {
callback.onResult(null, new MongoClientException("Auto-encryption requires a minimum MongoDB version of 4.2"));
return;
}
try {
BasicOutputBuffer bsonOutput = new BasicOutputBuffer();
BsonBinaryWriter bsonBinaryWriter = new BsonBinaryWriter(new BsonWriterSettings(), new BsonBinaryWriterSettings(getDescription().getMaxDocumentSize()), bsonOutput, getFieldNameValidator(payload, commandFieldNameValidator, payloadFieldNameValidator));
BsonWriter writer = payload == null ? bsonBinaryWriter : new SplittablePayloadBsonWriter(bsonBinaryWriter, bsonOutput, createSplittablePayloadMessageSettings(), payload, MAX_SPLITTABLE_DOCUMENT_SIZE);
getEncoder(command).encode(writer, command, EncoderContext.builder().build());
crypt.encrypt(database, new RawBsonDocument(bsonOutput.getInternalBuffer(), 0, bsonOutput.getSize())).flatMap((Function<RawBsonDocument, Mono<RawBsonDocument>>) encryptedCommand -> Mono.create(sink -> wrapped.commandAsync(database, encryptedCommand, commandFieldNameValidator, readPreference, new RawBsonDocumentCodec(), sessionContext, serverApi, requestContext, responseExpected, null, null, sinkToCallback(sink)))).flatMap(crypt::decrypt).map(decryptedResponse -> commandResultDecoder.decode(new BsonBinaryReader(decryptedResponse.getByteBuffer().asNIO()), DecoderContext.builder().build())).subscribe(decryptedResult -> callback.onResult(decryptedResult, null), e -> callback.onResult(null, e));
} catch (Throwable t) {
callback.onResult(null, t);
}
}
use of com.mongodb.lang.Nullable in project mongo-java-driver by mongodb.
the class OperationExecutorImpl method execute.
@Override
public <T> Mono<T> execute(final AsyncReadOperation<T> operation, final ReadPreference readPreference, final ReadConcern readConcern, @Nullable final ClientSession session) {
notNull("operation", operation);
notNull("readPreference", readPreference);
notNull("readConcern", readConcern);
if (session != null) {
session.notifyOperationInitiated(operation);
}
return Mono.from(subscriber -> clientSessionHelper.withClientSession(session, OperationExecutorImpl.this).map(clientSession -> getReadWriteBinding(getContext(subscriber), readPreference, readConcern, clientSession, session == null && clientSession != null)).switchIfEmpty(Mono.fromCallable(() -> getReadWriteBinding(getContext(subscriber), readPreference, readConcern, session, false))).flatMap(binding -> {
if (session != null && session.hasActiveTransaction() && !binding.getReadPreference().equals(primary())) {
binding.release();
return Mono.error(new MongoClientException("Read preference in a transaction must be primary"));
} else {
return Mono.<T>create(sink -> operation.executeAsync(binding, (result, t) -> {
try {
binding.release();
} finally {
sinkToCallback(sink).onResult(result, t);
}
})).doOnError((t) -> {
labelException(session, t);
unpinServerAddressOnTransientTransactionError(session, t);
});
}
}).subscribe(subscriber));
}
use of com.mongodb.lang.Nullable in project mongo-java-driver by mongodb.
the class GridFSPublisherCreator method createDeletePublisher.
public static Publisher<Void> createDeletePublisher(final MongoCollection<GridFSFile> filesCollection, final MongoCollection<Document> chunksCollection, @Nullable final ClientSession clientSession, final BsonValue id) {
notNull("filesCollection", filesCollection);
notNull("chunksCollection", chunksCollection);
notNull("id", id);
BsonDocument filter = new BsonDocument("_id", id);
Publisher<DeleteResult> fileDeletePublisher;
if (clientSession == null) {
fileDeletePublisher = filesCollection.deleteOne(filter);
} else {
fileDeletePublisher = filesCollection.deleteOne(clientSession, filter);
}
return Mono.from(fileDeletePublisher).flatMap(deleteResult -> {
if (deleteResult.wasAcknowledged() && deleteResult.getDeletedCount() == 0) {
throw new MongoGridFSException(format("No file found with the ObjectId: %s", id));
}
if (clientSession == null) {
return Mono.from(chunksCollection.deleteMany(new BsonDocument("files_id", id)));
} else {
return Mono.from(chunksCollection.deleteMany(clientSession, new BsonDocument("files_id", id)));
}
}).flatMap(i -> Mono.empty());
}
use of com.mongodb.lang.Nullable in project mongo-java-driver by mongodb.
the class GeometryDecoderHelper method decodeCoordinateReferenceSystem.
@Nullable
static CoordinateReferenceSystem decodeCoordinateReferenceSystem(final BsonReader reader) {
String crsName = null;
validateIsDocument(reader);
reader.readStartDocument();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
String name = reader.readName();
if (name.equals("type")) {
String type = reader.readString();
if (!type.equals("name")) {
throw new CodecConfigurationException(format("Unsupported CoordinateReferenceSystem '%s'.", type));
}
} else if (name.equals("properties")) {
crsName = decodeCoordinateReferenceSystemProperties(reader);
} else {
throw new CodecConfigurationException(format("Found invalid key '%s' in the CoordinateReferenceSystem.", name));
}
}
reader.readEndDocument();
return crsName != null ? new NamedCoordinateReferenceSystem(crsName) : null;
}
use of com.mongodb.lang.Nullable in project morphia by mongodb.
the class Modify method execute.
/**
* Performs the operation
*
* @param options the options to apply
* @return the operation result
*/
@Nullable
public T execute(ModifyOptions options) {
ClientSession session = getDatastore().findSession(options);
Document update = toDocument();
return session == null ? options.prepare(getCollection()).findOneAndUpdate(getQuery().toDocument(), update, options) : options.prepare(getCollection()).findOneAndUpdate(session, getQuery().toDocument(), update, options);
}
Aggregations