Search in sources :

Example 1 with WriteResult

use of org.immutables.criteria.backend.WriteResult 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 2 with WriteResult

use of org.immutables.criteria.backend.WriteResult in project immutables by immutables.

the class ScrollingTest method elasticseachInit.

@BeforeEach
void elasticseachInit() throws IOException {
    Map<String, String> model = ImmutableMap.<String, String>builder().put("string", "keyword").put("optionalString", "keyword").put("bool", "boolean").put("intNumber", "integer").build();
    new IndexOps(restClient, MAPPER, "test").create(model).blockingAwait();
    ElasticsearchBackend backend = backend();
    ElasticsearchOps ops = new ElasticsearchOps(restClient, "test", MAPPER, 1024);
    for (int i = 0; i < SIZE; i++) {
        ObjectNode doc = MAPPER.createObjectNode().put("string", "s" + i).put("bool", true).put("intNumber", i);
        WriteResult result = ops.insertDocument(doc).blockingGet();
    }
}
Also used : WriteResult(org.immutables.criteria.backend.WriteResult) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

WriteResult (org.immutables.criteria.backend.WriteResult)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Iterables (com.google.common.collect.Iterables)1 MongoException (com.mongodb.MongoException)1 BulkWriteResult (com.mongodb.bulk.BulkWriteResult)1 Projections (com.mongodb.client.model.Projections)1 ReplaceOneModel (com.mongodb.client.model.ReplaceOneModel)1 ReplaceOptions (com.mongodb.client.model.ReplaceOptions)1 Sorts (com.mongodb.client.model.Sorts)1 FullDocument (com.mongodb.client.model.changestream.FullDocument)1 ChangeStreamPublisher (com.mongodb.reactivestreams.client.ChangeStreamPublisher)1 FindPublisher (com.mongodb.reactivestreams.client.FindPublisher)1 MongoCollection (com.mongodb.reactivestreams.client.MongoCollection)1 Flowable (io.reactivex.Flowable)1 FlowableTransformer (io.reactivex.FlowableTransformer)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Function (java.util.function.Function)1 Level (java.util.logging.Level)1