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);
}
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);
}
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");
}
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");
}
Aggregations