Search in sources :

Example 1 with WatchEvent

use of org.immutables.criteria.backend.WatchEvent 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 WatchEvent

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

the class ChangeStreamsTest method filter.

/**
 * Watch event with filter
 */
@Test
void filter() throws InterruptedException {
    PersonCriteria filter = person.age.atLeast(21);
    Flowable<WatchEvent<Person>> flowable = Flowable.fromPublisher(repository.watcher(filter).watch());
    TestSubscriber<WatchEvent<Person>> test = flowable.test();
    repository.insert(generator.next().withId("id1").withAge(18));
    test.await(200, TimeUnit.MILLISECONDS);
    test.assertEmpty();
    repository.insert(generator.next().withId("id2").withAge(19));
    test.await(200, TimeUnit.MILLISECONDS);
    test.assertEmpty();
    repository.insert(generator.next().withId("id3").withAge(21));
    test.awaitCount(1).assertValueCount(1);
    Person person = test.values().get(0).newValue().get();
    check(person.id()).is("id3");
    check(person.age()).is(21);
}
Also used : PersonCriteria(org.immutables.criteria.personmodel.PersonCriteria) WatchEvent(org.immutables.criteria.backend.WatchEvent) Person(org.immutables.criteria.personmodel.Person) Test(org.junit.jupiter.api.Test)

Example 3 with WatchEvent

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

the class GeodeCqTest method pubsub.

@Test
public void pubsub() throws Exception {
    PersonRepository repository = new PersonRepository(new GeodeBackend(GeodeSetup.of(x -> region)));
    TestSubscriber<WatchEvent<Person>> events = Flowable.fromPublisher(repository.watcher(PersonCriteria.person).watch()).test();
    final PersonGenerator generator = new PersonGenerator();
    final int count = 4;
    for (int i = 0; i < count; i++) {
        repository.insert(generator.next().withId("id" + i));
    }
    check(region.keySet()).notEmpty();
    // ensure (de)serialization is successful
    check(region.query("true")).hasSize(count);
    events.awaitCount(count);
    events.assertNoErrors();
    events.assertValueCount(count);
    check(events.values().stream().map(e -> e.newValue().get().id()).collect(Collectors.toList())).hasContentInAnyOrder("id0", "id1", "id2", "id3");
}
Also used : WatchEvent(org.immutables.criteria.backend.WatchEvent) PersonRepository(org.immutables.criteria.personmodel.PersonRepository) BeforeEach(org.junit.jupiter.api.BeforeEach) Person(org.immutables.criteria.personmodel.Person) ClientRegionShortcut(org.apache.geode.cache.client.ClientRegionShortcut) Disabled(org.junit.jupiter.api.Disabled) Collectors(java.util.stream.Collectors) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) ReflectionBasedAutoSerializer(org.apache.geode.pdx.ReflectionBasedAutoSerializer) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) Flowable(io.reactivex.Flowable) Checkers.check(org.immutables.check.Checkers.check) PersonGenerator(org.immutables.criteria.personmodel.PersonGenerator) Region(org.apache.geode.cache.Region) PersonCriteria(org.immutables.criteria.personmodel.PersonCriteria) TestSubscriber(io.reactivex.subscribers.TestSubscriber) ClientCache(org.apache.geode.cache.client.ClientCache) PersonGenerator(org.immutables.criteria.personmodel.PersonGenerator) PersonRepository(org.immutables.criteria.personmodel.PersonRepository) WatchEvent(org.immutables.criteria.backend.WatchEvent) Test(org.junit.jupiter.api.Test)

Example 4 with WatchEvent

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

the class ChangeStreamsTest method multiFilter.

@Test
void multiFilter() throws InterruptedException {
    PersonCriteria filter = person.age.atLeast(21).isActive.isTrue();
    Flowable<WatchEvent<Person>> flowable = Flowable.fromPublisher(repository.watcher(filter).watch());
    TestSubscriber<WatchEvent<Person>> test = flowable.test();
    repository.insert(generator.next().withId("id1").withAge(19));
    test.await(200, TimeUnit.MILLISECONDS);
    test.assertEmpty();
    repository.insert(generator.next().withId("id2").withAge(21).withIsActive(false));
    test.await(200, TimeUnit.MILLISECONDS);
    test.assertEmpty();
    repository.insert(generator.next().withId("id3").withAge(21).withIsActive(true));
    test.awaitCount(1).assertValueCount(1);
    Person person = test.values().get(0).newValue().get();
    check(person.id()).is("id3");
}
Also used : PersonCriteria(org.immutables.criteria.personmodel.PersonCriteria) WatchEvent(org.immutables.criteria.backend.WatchEvent) Person(org.immutables.criteria.personmodel.Person) Test(org.junit.jupiter.api.Test)

Aggregations

WatchEvent (org.immutables.criteria.backend.WatchEvent)4 Person (org.immutables.criteria.personmodel.Person)3 PersonCriteria (org.immutables.criteria.personmodel.PersonCriteria)3 Test (org.junit.jupiter.api.Test)3 Flowable (io.reactivex.Flowable)2 Collectors (java.util.stream.Collectors)2 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 FlowableTransformer (io.reactivex.FlowableTransformer)1 TestSubscriber (io.reactivex.subscribers.TestSubscriber)1 List (java.util.List)1