use of com.mongodb.bulk.UpdateRequest in project mongo-java-driver by mongodb.
the class OperationHelper method validateWriteRequestCollations.
static void validateWriteRequestCollations(final AsyncConnection connection, final List<? extends WriteRequest> requests, final WriteConcern writeConcern, final AsyncCallableWithConnection callable) {
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(connection, collation, writeConcern, new AsyncCallableWithConnection() {
@Override
public void call(final AsyncConnection connection, final Throwable t) {
callable.call(connection, t);
}
});
}
use of com.mongodb.bulk.UpdateRequest in project mongo-java-driver by mongodb.
the class OperationHelper method validateWriteRequestCollations.
static void validateWriteRequestCollations(final Connection connection, 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(connection, collation, writeConcern);
}
use of com.mongodb.bulk.UpdateRequest in project mongo-java-driver by mongodb.
the class UpdateCommandMessage method writeTheWrites.
@Override
protected UpdateCommandMessage writeTheWrites(final BsonOutput bsonOutput, final int commandStartPosition, final BsonBinaryWriter writer) {
UpdateCommandMessage nextMessage = null;
writer.writeStartArray("updates");
for (int i = 0; i < updates.size(); i++) {
writer.mark();
UpdateRequest update = updates.get(i);
writer.writeStartDocument();
writer.pushMaxDocumentSize(getSettings().getMaxDocumentSize());
writer.writeName("q");
getCodec(update.getFilter()).encode(writer, update.getFilter(), EncoderContext.builder().build());
writer.writeName("u");
int bufferPosition = bsonOutput.getPosition();
getCodec(update.getUpdate()).encode(writer, update.getUpdate(), EncoderContext.builder().build());
if (update.getType() == WriteRequest.Type.UPDATE && bsonOutput.getPosition() == bufferPosition + 8) {
throw new IllegalArgumentException("Invalid BSON document for an update");
}
if (update.isMulti()) {
writer.writeBoolean("multi", update.isMulti());
}
if (update.isUpsert()) {
writer.writeBoolean("upsert", update.isUpsert());
}
if (update.getCollation() != null) {
writer.writeName("collation");
BsonDocument collation = update.getCollation().asDocument();
getCodec(collation).encode(writer, collation, EncoderContext.builder().build());
}
writer.popMaxDocumentSize();
writer.writeEndDocument();
if (exceedsLimits(bsonOutput.getPosition() - commandStartPosition, i + 1)) {
writer.reset();
nextMessage = new UpdateCommandMessage(getWriteNamespace(), isOrdered(), getWriteConcern(), getBypassDocumentValidation(), getSettings(), updates.subList(i, updates.size()));
break;
}
}
writer.writeEndArray();
return nextMessage;
}
use of com.mongodb.bulk.UpdateRequest in project mongo-java-driver by mongodb.
the class UpdateProtocol method appendToWriteCommandResponseDocument.
@Override
protected void appendToWriteCommandResponseDocument(final RequestMessage curMessage, final RequestMessage nextMessage, final WriteConcernResult writeConcernResult, final BsonDocument response) {
response.append("n", new BsonInt32(writeConcernResult.getCount()));
UpdateMessage updateMessage = (UpdateMessage) curMessage;
UpdateRequest updateRequest = updateMessage.getUpdateRequests().get(0);
BsonValue upsertedId = null;
if (writeConcernResult.getUpsertedId() != null) {
upsertedId = writeConcernResult.getUpsertedId();
} else if (!writeConcernResult.isUpdateOfExisting() && updateRequest.isUpsert()) {
if (updateRequest.getUpdate().containsKey("_id")) {
upsertedId = updateRequest.getUpdate().get("_id");
} else if (updateRequest.getFilter().containsKey("_id")) {
upsertedId = updateRequest.getFilter().get("_id");
}
}
if (upsertedId != null) {
response.append("upserted", new BsonArray(singletonList(new BsonDocument("index", new BsonInt32(0)).append("_id", upsertedId))));
}
}
Aggregations