Search in sources :

Example 1 with UpdateRequest

use of com.mongodb.bulk.UpdateRequest in project mongo-java-driver by mongodb.

the class MongoCollectionImpl method bulkWrite.

@SuppressWarnings("unchecked")
@Override
public void bulkWrite(final List<? extends WriteModel<? extends TDocument>> requests, final BulkWriteOptions options, final SingleResultCallback<BulkWriteResult> callback) {
    notNull("requests", requests);
    List<WriteRequest> writeRequests = new ArrayList<WriteRequest>(requests.size());
    for (WriteModel<? extends TDocument> writeModel : requests) {
        WriteRequest writeRequest;
        if (writeModel == null) {
            throw new IllegalArgumentException("requests can not contain a null value");
        } else if (writeModel instanceof InsertOneModel) {
            TDocument document = ((InsertOneModel<TDocument>) writeModel).getDocument();
            if (getCodec() instanceof CollectibleCodec) {
                document = ((CollectibleCodec<TDocument>) getCodec()).generateIdIfAbsentFromDocument(document);
            }
            writeRequest = new InsertRequest(documentToBsonDocument(document));
        } else if (writeModel instanceof ReplaceOneModel) {
            ReplaceOneModel<TDocument> replaceOneModel = (ReplaceOneModel<TDocument>) writeModel;
            writeRequest = new UpdateRequest(toBsonDocument(replaceOneModel.getFilter()), documentToBsonDocument(replaceOneModel.getReplacement()), WriteRequest.Type.REPLACE).upsert(replaceOneModel.getOptions().isUpsert()).collation(replaceOneModel.getOptions().getCollation());
        } else if (writeModel instanceof UpdateOneModel) {
            UpdateOneModel<TDocument> updateOneModel = (UpdateOneModel<TDocument>) writeModel;
            writeRequest = new UpdateRequest(toBsonDocument(updateOneModel.getFilter()), toBsonDocument(updateOneModel.getUpdate()), WriteRequest.Type.UPDATE).multi(false).upsert(updateOneModel.getOptions().isUpsert()).collation(updateOneModel.getOptions().getCollation());
        } else if (writeModel instanceof UpdateManyModel) {
            UpdateManyModel<TDocument> updateManyModel = (UpdateManyModel<TDocument>) writeModel;
            writeRequest = new UpdateRequest(toBsonDocument(updateManyModel.getFilter()), toBsonDocument(updateManyModel.getUpdate()), WriteRequest.Type.UPDATE).multi(true).upsert(updateManyModel.getOptions().isUpsert()).collation(updateManyModel.getOptions().getCollation());
        } else if (writeModel instanceof DeleteOneModel) {
            DeleteOneModel<TDocument> deleteOneModel = (DeleteOneModel<TDocument>) writeModel;
            writeRequest = new DeleteRequest(toBsonDocument(deleteOneModel.getFilter())).multi(false).collation(deleteOneModel.getOptions().getCollation());
        } else if (writeModel instanceof DeleteManyModel) {
            DeleteManyModel<TDocument> deleteManyModel = (DeleteManyModel<TDocument>) writeModel;
            writeRequest = new DeleteRequest(toBsonDocument(deleteManyModel.getFilter())).multi(true).collation(deleteManyModel.getOptions().getCollation());
        } else {
            throw new UnsupportedOperationException(format("WriteModel of type %s is not supported", writeModel.getClass()));
        }
        writeRequests.add(writeRequest);
    }
    executor.execute(new MixedBulkWriteOperation(namespace, writeRequests, options.isOrdered(), writeConcern).bypassDocumentValidation(options.getBypassDocumentValidation()), callback);
}
Also used : InsertOneModel(com.mongodb.client.model.InsertOneModel) CollectibleCodec(org.bson.codecs.CollectibleCodec) UpdateRequest(com.mongodb.bulk.UpdateRequest) WriteRequest(com.mongodb.bulk.WriteRequest) InsertRequest(com.mongodb.bulk.InsertRequest) ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) ArrayList(java.util.ArrayList) DeleteOneModel(com.mongodb.client.model.DeleteOneModel) MixedBulkWriteOperation(com.mongodb.operation.MixedBulkWriteOperation) UpdateOneModel(com.mongodb.client.model.UpdateOneModel) UpdateManyModel(com.mongodb.client.model.UpdateManyModel) DeleteRequest(com.mongodb.bulk.DeleteRequest) DeleteManyModel(com.mongodb.client.model.DeleteManyModel)

Example 2 with UpdateRequest

use of com.mongodb.bulk.UpdateRequest in project mongo-java-driver by mongodb.

the class UpdateMessage method encodeMessageBodyWithMetadata.

@Override
protected EncodingMetadata encodeMessageBodyWithMetadata(final BsonOutput bsonOutput, final int messageStartPosition) {
    // reserved
    bsonOutput.writeInt32(0);
    bsonOutput.writeCString(getCollectionName());
    UpdateRequest updateRequest = updates.get(0);
    int flags = 0;
    if (updateRequest.isUpsert()) {
        flags |= 1;
    }
    if (updateRequest.isMulti()) {
        flags |= 2;
    }
    bsonOutput.writeInt32(flags);
    int firstDocumentStartPosition = bsonOutput.getPosition();
    addDocument(updateRequest.getFilter(), bsonOutput, new NoOpFieldNameValidator());
    if (updateRequest.getType() == REPLACE) {
        addCollectibleDocument(updateRequest.getUpdate(), bsonOutput, new CollectibleDocumentFieldNameValidator());
    } else {
        int bufferPosition = bsonOutput.getPosition();
        addDocument(updateRequest.getUpdate(), bsonOutput, new UpdateFieldNameValidator());
        if (bsonOutput.getPosition() == bufferPosition + 5) {
            throw new IllegalArgumentException("Invalid BSON document for an update");
        }
    }
    if (updates.size() == 1) {
        return new EncodingMetadata(null, firstDocumentStartPosition);
    } else {
        return new EncodingMetadata(new UpdateMessage(getCollectionName(), updates.subList(1, updates.size()), getSettings()), firstDocumentStartPosition);
    }
}
Also used : NoOpFieldNameValidator(com.mongodb.internal.validator.NoOpFieldNameValidator) UpdateRequest(com.mongodb.bulk.UpdateRequest) CollectibleDocumentFieldNameValidator(com.mongodb.internal.validator.CollectibleDocumentFieldNameValidator) UpdateFieldNameValidator(com.mongodb.internal.validator.UpdateFieldNameValidator)

Example 3 with UpdateRequest

use of com.mongodb.bulk.UpdateRequest in project mongo-java-driver by mongodb.

the class MongoCollectionImpl method bulkWrite.

@Override
@SuppressWarnings("unchecked")
public BulkWriteResult bulkWrite(final List<? extends WriteModel<? extends TDocument>> requests, final BulkWriteOptions options) {
    notNull("requests", requests);
    List<WriteRequest> writeRequests = new ArrayList<WriteRequest>(requests.size());
    for (WriteModel<? extends TDocument> writeModel : requests) {
        WriteRequest writeRequest;
        if (writeModel == null) {
            throw new IllegalArgumentException("requests can not contain a null value");
        } else if (writeModel instanceof InsertOneModel) {
            TDocument document = ((InsertOneModel<TDocument>) writeModel).getDocument();
            if (getCodec() instanceof CollectibleCodec) {
                document = ((CollectibleCodec<TDocument>) getCodec()).generateIdIfAbsentFromDocument(document);
            }
            writeRequest = new InsertRequest(documentToBsonDocument(document));
        } else if (writeModel instanceof ReplaceOneModel) {
            ReplaceOneModel<TDocument> replaceOneModel = (ReplaceOneModel<TDocument>) writeModel;
            writeRequest = new UpdateRequest(toBsonDocument(replaceOneModel.getFilter()), documentToBsonDocument(replaceOneModel.getReplacement()), WriteRequest.Type.REPLACE).upsert(replaceOneModel.getOptions().isUpsert()).collation(replaceOneModel.getOptions().getCollation());
        } else if (writeModel instanceof UpdateOneModel) {
            UpdateOneModel<TDocument> updateOneModel = (UpdateOneModel<TDocument>) writeModel;
            writeRequest = new UpdateRequest(toBsonDocument(updateOneModel.getFilter()), toBsonDocument(updateOneModel.getUpdate()), WriteRequest.Type.UPDATE).multi(false).upsert(updateOneModel.getOptions().isUpsert()).collation(updateOneModel.getOptions().getCollation());
        } else if (writeModel instanceof UpdateManyModel) {
            UpdateManyModel<TDocument> updateManyModel = (UpdateManyModel<TDocument>) writeModel;
            writeRequest = new UpdateRequest(toBsonDocument(updateManyModel.getFilter()), toBsonDocument(updateManyModel.getUpdate()), WriteRequest.Type.UPDATE).multi(true).upsert(updateManyModel.getOptions().isUpsert()).collation(updateManyModel.getOptions().getCollation());
        } else if (writeModel instanceof DeleteOneModel) {
            DeleteOneModel<TDocument> deleteOneModel = (DeleteOneModel<TDocument>) writeModel;
            writeRequest = new DeleteRequest(toBsonDocument(deleteOneModel.getFilter())).multi(false).collation(deleteOneModel.getOptions().getCollation());
        } else if (writeModel instanceof DeleteManyModel) {
            DeleteManyModel<TDocument> deleteManyModel = (DeleteManyModel<TDocument>) writeModel;
            writeRequest = new DeleteRequest(toBsonDocument(deleteManyModel.getFilter())).multi(true).collation(deleteManyModel.getOptions().getCollation());
        } else {
            throw new UnsupportedOperationException(format("WriteModel of type %s is not supported", writeModel.getClass()));
        }
        writeRequests.add(writeRequest);
    }
    return executor.execute(new MixedBulkWriteOperation(namespace, writeRequests, options.isOrdered(), writeConcern).bypassDocumentValidation(options.getBypassDocumentValidation()));
}
Also used : InsertOneModel(com.mongodb.client.model.InsertOneModel) CollectibleCodec(org.bson.codecs.CollectibleCodec) UpdateRequest(com.mongodb.bulk.UpdateRequest) WriteRequest(com.mongodb.bulk.WriteRequest) InsertRequest(com.mongodb.bulk.InsertRequest) ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) ArrayList(java.util.ArrayList) DeleteOneModel(com.mongodb.client.model.DeleteOneModel) MixedBulkWriteOperation(com.mongodb.operation.MixedBulkWriteOperation) UpdateOneModel(com.mongodb.client.model.UpdateOneModel) UpdateManyModel(com.mongodb.client.model.UpdateManyModel) DeleteRequest(com.mongodb.bulk.DeleteRequest) DeleteManyModel(com.mongodb.client.model.DeleteManyModel)

Example 4 with UpdateRequest

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);
        }
    });
}
Also used : UpdateRequest(com.mongodb.bulk.UpdateRequest) WriteRequest(com.mongodb.bulk.WriteRequest) AsyncConnection(com.mongodb.connection.AsyncConnection) Collation(com.mongodb.client.model.Collation) DeleteRequest(com.mongodb.bulk.DeleteRequest)

Example 5 with UpdateRequest

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);
}
Also used : UpdateRequest(com.mongodb.bulk.UpdateRequest) WriteRequest(com.mongodb.bulk.WriteRequest) Collation(com.mongodb.client.model.Collation) DeleteRequest(com.mongodb.bulk.DeleteRequest)

Aggregations

UpdateRequest (com.mongodb.bulk.UpdateRequest)7 DeleteRequest (com.mongodb.bulk.DeleteRequest)4 WriteRequest (com.mongodb.bulk.WriteRequest)4 InsertRequest (com.mongodb.bulk.InsertRequest)2 Collation (com.mongodb.client.model.Collation)2 DeleteManyModel (com.mongodb.client.model.DeleteManyModel)2 DeleteOneModel (com.mongodb.client.model.DeleteOneModel)2 InsertOneModel (com.mongodb.client.model.InsertOneModel)2 ReplaceOneModel (com.mongodb.client.model.ReplaceOneModel)2 UpdateManyModel (com.mongodb.client.model.UpdateManyModel)2 UpdateOneModel (com.mongodb.client.model.UpdateOneModel)2 MixedBulkWriteOperation (com.mongodb.operation.MixedBulkWriteOperation)2 ArrayList (java.util.ArrayList)2 BsonDocument (org.bson.BsonDocument)2 CollectibleCodec (org.bson.codecs.CollectibleCodec)2 AsyncConnection (com.mongodb.connection.AsyncConnection)1 CollectibleDocumentFieldNameValidator (com.mongodb.internal.validator.CollectibleDocumentFieldNameValidator)1 NoOpFieldNameValidator (com.mongodb.internal.validator.NoOpFieldNameValidator)1 UpdateFieldNameValidator (com.mongodb.internal.validator.UpdateFieldNameValidator)1 BsonArray (org.bson.BsonArray)1