Search in sources :

Example 1 with PathNaming

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

the class MongoSession method watch.

private <X> Publisher<WatchEvent<X>> watch(StandardOperations.Watch operation) {
    final MongoCollection<X> collection = (MongoCollection<X>) this.collection;
    if (operation.query().hasProjections()) {
        return Flowable.error(new UnsupportedOperationException("Projections are not yet supported with watch operation"));
    }
    ChangeStreamPublisher<X> watch;
    if (!operation.query().filter().isPresent()) {
        // watch without filter
        watch = collection.watch(collection.getDocumentClass());
    } else {
        // prefix all attributes with 'fullDocument.'
        PathNaming naming = path -> "fullDocument." + this.pathNaming.name(path);
        // reuse aggregation pipeline
        AggregationQuery agg = new AggregationQuery(operation.query(), naming);
        watch = collection.watch(agg.toPipeline(), collection.getDocumentClass());
    }
    return Flowable.fromPublisher(watch.fullDocument(FullDocument.UPDATE_LOOKUP)).map(MongoWatchEvent::fromChangeStream);
}
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) PathNaming(org.immutables.criteria.backend.PathNaming)

Example 2 with PathNaming

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

the class GeodeQueryVisitorTest method oqlChecker.

private static StringChecker oqlChecker(Criterion<?> criteria, boolean useBindVariables) {
    final PathNaming pathNaming = ReservedWordNaming.of(PathNaming.defaultNaming());
    final GeodeQueryVisitor queryVisitor = new GeodeQueryVisitor(useBindVariables, pathNaming);
    Expression filter = Criterias.toQuery(criteria).filter().orElseThrow(() -> new IllegalStateException("no filter"));
    return check(filter.accept(queryVisitor).oql());
}
Also used : Expression(org.immutables.criteria.expression.Expression) PathNaming(org.immutables.criteria.backend.PathNaming)

Example 3 with PathNaming

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

the class MongoSession method query.

private Publisher<?> query(StandardOperations.Select select) {
    final Query query = select.query();
    final boolean hasProjections = query.hasProjections();
    boolean useAggregationPipeline = query.hasAggregations() || query.distinct() || query.count() && query.limit().isPresent();
    ExpressionNaming expressionNaming = useAggregationPipeline ? ExpressionNaming.from(UniqueCachedNaming.of(query.projections())) : expression -> pathNaming.name((Path) expression);
    MongoCollection<?> collection = this.collection;
    if (hasProjections) {
        // add special TupleCodecProvider for projections
        CodecRegistry newRegistry = CodecRegistries.fromRegistries(this.collection.getCodecRegistry(), CodecRegistries.fromProviders(new TupleCodecProvider(query, expressionNaming)));
        collection = this.collection.withDocumentClass(ProjectedTuple.class).withCodecRegistry(newRegistry);
    }
    if (useAggregationPipeline) {
        // aggregations
        AggregationQuery agg = new AggregationQuery(query, pathNaming);
        if (query.count()) {
            // also for empty result-set mongo does not return single(0) but empty publisher
            return Flowable.fromPublisher(collection.aggregate(agg.toPipeline(), BsonDocument.class)).map(d -> d.get("count").asNumber().longValue()).defaultIfEmpty(// return Single.just(0) for empty publisher
            0L);
        }
        return collection.aggregate(agg.toPipeline(), ProjectedTuple.class);
    }
    Bson filter = toBsonFilter(query);
    if (query.count()) {
        // simple form of count all (without distinct, aggregations etc.) : count(*)
        return Flowable.fromPublisher(collection.countDocuments(filter));
    }
    final FindPublisher<?> find = collection.find(filter);
    if (!query.collations().isEmpty()) {
        // add sorting
        final Function<Collation, Bson> toSortFn = col -> {
            final String path = col.path().toStringPath();
            return col.direction().isAscending() ? Sorts.ascending(path) : Sorts.descending(path);
        };
        final List<Bson> sorts = query.collations().stream().map(toSortFn).collect(Collectors.toList());
        find.sort(Sorts.orderBy(sorts));
    }
    query.limit().ifPresent(limit -> find.limit((int) limit));
    query.offset().ifPresent(offset -> find.skip((int) offset));
    if (hasProjections) {
        List<String> fields = query.projections().stream().map(p -> pathNaming.name((Path) p)).collect(Collectors.toList());
        find.projection(Projections.include(fields));
        return find;
    }
    // post-process result with projections
    return find;
}
Also used : Path(org.immutables.criteria.expression.Path) 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) ExpressionNaming(org.immutables.criteria.backend.ExpressionNaming) Query(org.immutables.criteria.expression.Query) Collation(org.immutables.criteria.expression.Collation) Bson(org.bson.conversions.Bson) BsonDocument(org.bson.BsonDocument) CodecRegistry(org.bson.codecs.configuration.CodecRegistry)

Aggregations

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