Search in sources :

Example 1 with ReplaceOneModel

use of com.mongodb.client.model.ReplaceOneModel 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 ReplaceOneModel

use of com.mongodb.client.model.ReplaceOneModel 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 3 with ReplaceOneModel

use of com.mongodb.client.model.ReplaceOneModel in project mongo-java-driver by mongodb.

the class JsonPoweredCrudTestHelper method getBulkWriteResult.

BsonDocument getBulkWriteResult(final BsonDocument collectionOptions, final BsonDocument arguments, @Nullable final ClientSession clientSession) {
    List<WriteModel<BsonDocument>> writeModels = new ArrayList<WriteModel<BsonDocument>>();
    for (BsonValue bsonValue : arguments.getArray("requests")) {
        BsonDocument cur = bsonValue.asDocument();
        String name = cur.getString("name").getValue();
        BsonDocument requestArguments = cur.getDocument("arguments");
        if (name.equals("insertOne")) {
            writeModels.add(new InsertOneModel<BsonDocument>(requestArguments.getDocument("document")));
        } else if (name.equals("updateOne")) {
            if (requestArguments.isDocument("update")) {
                writeModels.add(new UpdateOneModel<BsonDocument>(requestArguments.getDocument("filter"), requestArguments.getDocument("update"), getUpdateOptions(requestArguments)));
            } else {
                // update is a pipeline
                writeModels.add(new UpdateOneModel<BsonDocument>(requestArguments.getDocument("filter"), getListOfDocuments(requestArguments.getArray("update")), getUpdateOptions(requestArguments)));
            }
        } else if (name.equals("updateMany")) {
            if (requestArguments.isDocument("update")) {
                writeModels.add(new UpdateManyModel<BsonDocument>(requestArguments.getDocument("filter"), requestArguments.getDocument("update"), getUpdateOptions(requestArguments)));
            } else {
                // update is a pipeline
                writeModels.add(new UpdateManyModel<BsonDocument>(requestArguments.getDocument("filter"), getListOfDocuments(requestArguments.getArray("update")), getUpdateOptions(requestArguments)));
            }
        } else if (name.equals("deleteOne")) {
            writeModels.add(new DeleteOneModel<BsonDocument>(requestArguments.getDocument("filter"), getDeleteOptions(requestArguments)));
        } else if (name.equals("deleteMany")) {
            writeModels.add(new DeleteManyModel<BsonDocument>(requestArguments.getDocument("filter"), getDeleteOptions(requestArguments)));
        } else if (name.equals("replaceOne")) {
            writeModels.add(new ReplaceOneModel<BsonDocument>(requestArguments.getDocument("filter"), requestArguments.getDocument("replacement"), getReplaceOptions(requestArguments)));
        } else {
            throw new UnsupportedOperationException(format("Unsupported write request type: %s", name));
        }
    }
    try {
        BulkWriteResult bulkWriteResult;
        BsonDocument optionsDocument = arguments.getDocument("options", new BsonDocument());
        BulkWriteOptions options = new BulkWriteOptions().ordered(optionsDocument.getBoolean("ordered", BsonBoolean.TRUE).getValue());
        if (optionsDocument.containsKey("bypassDocumentValidation")) {
            options.bypassDocumentValidation(optionsDocument.getBoolean("bypassDocumentValidation").getValue());
        }
        if (clientSession == null) {
            bulkWriteResult = getCollection(collectionOptions).bulkWrite(writeModels, options);
        } else {
            bulkWriteResult = getCollection(collectionOptions).bulkWrite(clientSession, writeModels, options);
        }
        return toResult(bulkWriteResult, writeModels, Collections.<BulkWriteError>emptyList());
    } catch (MongoBulkWriteException e) {
        BsonDocument result = toResult(e.getWriteResult(), writeModels, e.getWriteErrors());
        result.put("error", BsonBoolean.TRUE);
        return result;
    }
}
Also used : ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) ArrayList(java.util.ArrayList) BsonString(org.bson.BsonString) WriteModel(com.mongodb.client.model.WriteModel) DeleteOneModel(com.mongodb.client.model.DeleteOneModel) BulkWriteResult(com.mongodb.bulk.BulkWriteResult) UpdateOneModel(com.mongodb.client.model.UpdateOneModel) BsonDocument(org.bson.BsonDocument) BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) MongoBulkWriteException(com.mongodb.MongoBulkWriteException) BsonValue(org.bson.BsonValue)

Example 4 with ReplaceOneModel

use of com.mongodb.client.model.ReplaceOneModel in project immutables by immutables.

the class MongoSession method update.

/**
 * Uses <a href="https://docs.mongodb.com/manual/reference/method/db.collection.replaceOne/">replaceOne</a> operation
 * with <a href="https://docs.mongodb.com/manual/reference/method/db.collection.bulkWrite/">bulkWrite</a>. Right now has to convert
 * object to BsonDocument to extract {@code _id} attribute.
 */
private <T> Publisher<WriteResult> update(StandardOperations.Update operation) {
    ReplaceOptions options = new ReplaceOptions();
    if (operation.upsert()) {
        options.upsert(operation.upsert());
    }
    List<ReplaceOneModel<Object>> docs = operation.values().stream().map(value -> new ReplaceOneModel<>(new BsonDocument(Mongos.ID_FIELD_NAME, toBsonValue(keyExtractor.extract(value))), value, options)).collect(Collectors.toList());
    Publisher<BulkWriteResult> publisher = ((MongoCollection<Object>) collection).bulkWrite(docs);
    return Flowable.fromPublisher(publisher).map(x -> WriteResult.unknown());
}
Also used : Document(org.bson.Document) Iterables(com.google.common.collect.Iterables) ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) Path(org.immutables.criteria.expression.Path) PathNaming(org.immutables.criteria.backend.PathNaming) Projections(com.mongodb.client.model.Projections) StandardOperations(org.immutables.criteria.backend.StandardOperations) ReplaceOptions(com.mongodb.client.model.ReplaceOptions) BsonDocumentWriter(org.bson.BsonDocumentWriter) DefaultResult(org.immutables.criteria.backend.DefaultResult) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) ExpressionNaming(org.immutables.criteria.backend.ExpressionNaming) Backend(org.immutables.criteria.backend.Backend) Function(java.util.function.Function) FlowableTransformer(io.reactivex.FlowableTransformer) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue) Query(org.immutables.criteria.expression.Query) Level(java.util.logging.Level) FullDocument(com.mongodb.client.model.changestream.FullDocument) Bson(org.bson.conversions.Bson) ChangeStreamPublisher(com.mongodb.reactivestreams.client.ChangeStreamPublisher) Flowable(io.reactivex.Flowable) EncoderContext(org.bson.codecs.EncoderContext) WatchEvent(org.immutables.criteria.backend.WatchEvent) ProjectedTuple(org.immutables.criteria.backend.ProjectedTuple) Collation(org.immutables.criteria.expression.Collation) ExpressionConverter(org.immutables.criteria.expression.ExpressionConverter) MongoException(com.mongodb.MongoException) Publisher(org.reactivestreams.Publisher) CodecRegistries(org.bson.codecs.configuration.CodecRegistries) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) KeyExtractor(org.immutables.criteria.backend.KeyExtractor) Visitors(org.immutables.criteria.expression.Visitors) List(java.util.List) Sorts(com.mongodb.client.model.Sorts) BulkWriteResult(com.mongodb.bulk.BulkWriteResult) UniqueCachedNaming(org.immutables.criteria.backend.UniqueCachedNaming) FindPublisher(com.mongodb.reactivestreams.client.FindPublisher) Codec(org.bson.codecs.Codec) WriteResult(org.immutables.criteria.backend.WriteResult) Optional(java.util.Optional) BackendException(org.immutables.criteria.backend.BackendException) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) BsonDocument(org.bson.BsonDocument) ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) ReplaceOptions(com.mongodb.client.model.ReplaceOptions) BulkWriteResult(com.mongodb.bulk.BulkWriteResult)

Example 5 with ReplaceOneModel

use of com.mongodb.client.model.ReplaceOneModel in project oap by oaplatform.

the class MongoStorage method fsync.

@Override
public void fsync() {
    super.fsync();
    val count = new MutableInt();
    Stream.of(data.values().stream().filter(m -> m.modified >= lastFsync)).grouped(bulkSize).forEach(list -> {
        count.add(list.size());
        final List<? extends WriteModel<Metadata<T>>> bulk = Lists.map(list, metadata -> {
            val id = getIdentifier().get(metadata.object);
            return new ReplaceOneModel<>(eq("_id", new ObjectId(id)), metadata, UPDATE_OPTIONS_UPSERT);
        });
        collection.bulkWrite(bulk);
    });
    log.info("[{}] fsync total: {}, modified: {}", collection.getNamespace(), size(), count.intValue());
    lastFsync = System.currentTimeMillis();
}
Also used : lombok.val(lombok.val) ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) MutableInt(org.apache.commons.lang3.mutable.MutableInt) MongoCollection(com.mongodb.client.MongoCollection) DateTimeUtils(org.joda.time.DateTimeUtils) lombok.val(lombok.val) CodecRegistries(org.bson.codecs.configuration.CodecRegistries) WriteModel(com.mongodb.client.model.WriteModel) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) Consumer(java.util.function.Consumer) Slf4j(lombok.extern.slf4j.Slf4j) MemoryStorage(oap.storage.MemoryStorage) List(java.util.List) Lists(oap.util.Lists) IdentifierBuilder(oap.storage.IdentifierBuilder) Metadata(oap.storage.Metadata) ObjectId(org.bson.types.ObjectId) Optional(java.util.Optional) Filters.eq(com.mongodb.client.model.Filters.eq) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Stream(oap.util.Stream) UpdateOptions(com.mongodb.client.model.UpdateOptions) ObjectId(org.bson.types.ObjectId) MutableInt(org.apache.commons.lang3.mutable.MutableInt) ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) Metadata(oap.storage.Metadata)

Aggregations

ReplaceOneModel (com.mongodb.client.model.ReplaceOneModel)9 DeleteOneModel (com.mongodb.client.model.DeleteOneModel)5 ArrayList (java.util.ArrayList)5 DeleteManyModel (com.mongodb.client.model.DeleteManyModel)4 InsertOneModel (com.mongodb.client.model.InsertOneModel)4 UpdateOneModel (com.mongodb.client.model.UpdateOneModel)4 BsonDocument (org.bson.BsonDocument)4 UpdateManyModel (com.mongodb.client.model.UpdateManyModel)3 BsonValue (org.bson.BsonValue)3 CollectibleCodec (org.bson.codecs.CollectibleCodec)3 BulkWriteResult (com.mongodb.bulk.BulkWriteResult)2 DeleteRequest (com.mongodb.bulk.DeleteRequest)2 InsertRequest (com.mongodb.bulk.InsertRequest)2 UpdateRequest (com.mongodb.bulk.UpdateRequest)2 WriteRequest (com.mongodb.bulk.WriteRequest)2 BulkWriteOptions (com.mongodb.client.model.BulkWriteOptions)2 UpdateOptions (com.mongodb.client.model.UpdateOptions)2 WriteModel (com.mongodb.client.model.WriteModel)2 MixedBulkWriteOperation (com.mongodb.operation.MixedBulkWriteOperation)2 List (java.util.List)2