use of com.mongodb.internal.bulk.UpdateRequest in project mongo-java-driver by mongodb.
the class MixedBulkWriteOperation method executeLegacyBatchesAsync.
private void executeLegacyBatchesAsync(final AsyncWriteBinding binding, final AsyncConnection connection, final SingleResultCallback<BulkWriteResult> callback) {
List<? extends WriteRequest> writeRequests = getWriteRequests();
LoopState loopState = new LoopState();
AsyncCallbackRunnable loop = new AsyncCallbackLoop(loopState, iterationCallback -> {
int i = loopState.iteration();
if (loopState.breakAndCompleteIf(() -> i == writeRequests.size(), iterationCallback)) {
return;
}
WriteRequest writeRequest = writeRequests.get(i);
SingleResultCallback<WriteConcernResult> commandCallback = (ignored, t) -> iterationCallback.onResult(null, t);
if (writeRequest.getType() == INSERT) {
connection.insertAsync(getNamespace(), isOrdered(), (InsertRequest) writeRequest, binding.getRequestContext(), commandCallback);
} else if (writeRequest.getType() == UPDATE || writeRequest.getType() == REPLACE) {
connection.updateAsync(getNamespace(), isOrdered(), (UpdateRequest) writeRequest, binding.getRequestContext(), commandCallback);
} else {
connection.deleteAsync(getNamespace(), isOrdered(), (DeleteRequest) writeRequest, binding.getRequestContext(), commandCallback);
}
});
loop.run((voidResult, t) -> {
if (t != null) {
callback.onResult(null, t);
} else {
callback.onResult(BulkWriteResult.unacknowledged(), null);
}
});
}
use of com.mongodb.internal.bulk.UpdateRequest in project mongo-java-driver by mongodb.
the class OperationHelper method validateWriteRequestCollations.
static void validateWriteRequestCollations(final ConnectionDescription connectionDescription, final List<? extends WriteRequest> requests, final WriteConcern writeConcern) {
Collation collation = null;
for (WriteRequest request : requests) {
if (request instanceof UpdateRequest) {
collation = ((UpdateRequest) request).getCollation();
} else if (request instanceof DeleteRequest) {
collation = ((DeleteRequest) request).getCollation();
}
if (collation != null) {
break;
}
}
validateCollationAndWriteConcern(connectionDescription, collation, writeConcern);
}
use of com.mongodb.internal.bulk.UpdateRequest in project mongo-java-driver by mongodb.
the class DBCollection method update.
/**
* Modify an existing document or documents in collection.
*
* @param query the selection criteria for the update
* @param update the modifications to apply
* @param options the options to apply to the update operation
* @return the result of the operation
* @throws com.mongodb.DuplicateKeyException if the write failed to a duplicate unique key
* @throws com.mongodb.WriteConcernException if the write failed due some other failure specific to the update command
* @throws com.mongodb.MongoCommandException if the write failed due to a specific command exception
* @throws MongoException if the operation failed for some other reason
* @mongodb.driver.manual tutorial/modify-documents/ Modify
* @since 3.4
*/
public WriteResult update(final DBObject query, final DBObject update, final DBCollectionUpdateOptions options) {
notNull("query", query);
notNull("update", update);
notNull("options", options);
WriteConcern writeConcern = options.getWriteConcern() != null ? options.getWriteConcern() : getWriteConcern();
Type updateType = (!update.keySet().isEmpty() && update.keySet().iterator().next().startsWith("$")) ? UPDATE : Type.REPLACE;
UpdateRequest updateRequest = new UpdateRequest(wrap(query), wrap(update, options.getEncoder()), updateType).upsert(options.isUpsert()).multi(options.isMulti()).collation(options.getCollation()).arrayFilters(wrapAllowNull(options.getArrayFilters(), options.getEncoder()));
return executeWriteOperation(new UpdateOperation(getNamespace(), true, writeConcern, retryWrites, singletonList(updateRequest)).bypassDocumentValidation(options.getBypassDocumentValidation()));
}
use of com.mongodb.internal.bulk.UpdateRequest in project mongo-java-driver by mongodb.
the class DBCollection method replaceOrInsert.
@SuppressWarnings("unchecked")
private WriteResult replaceOrInsert(final DBObject obj, final Object id, final WriteConcern writeConcern) {
DBObject filter = new BasicDBObject(ID_FIELD_NAME, id);
UpdateRequest replaceRequest = new UpdateRequest(wrap(filter), wrap(obj, objectCodec), Type.REPLACE).upsert(true);
return executeWriteOperation(new UpdateOperation(getNamespace(), false, writeConcern, retryWrites, singletonList(replaceRequest)));
}
use of com.mongodb.internal.bulk.UpdateRequest in project mongo-java-driver by mongodb.
the class OperationHelper method validateWriteRequestHints.
static void validateWriteRequestHints(final ConnectionDescription connectionDescription, final List<? extends WriteRequest> requests, final WriteConcern writeConcern) {
for (WriteRequest request : requests) {
Bson hint = null;
String hintString = null;
if (request instanceof UpdateRequest) {
hint = ((UpdateRequest) request).getHint();
hintString = ((UpdateRequest) request).getHintString();
} else if (request instanceof DeleteRequest) {
hint = ((DeleteRequest) request).getHint();
hintString = ((DeleteRequest) request).getHintString();
}
if (hint != null || hintString != null) {
validateWriteRequestHint(connectionDescription, writeConcern, request);
break;
}
}
}
Aggregations