use of com.mongodb.reactivestreams.client.ChangeStreamPublisher 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 com.mongodb.reactivestreams.client.ChangeStreamPublisher in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplate method changeStream.
@Override
public <T> Flux<ChangeStreamEvent<T>> changeStream(@Nullable String database, @Nullable String collectionName, ChangeStreamOptions options, Class<T> targetType) {
List<Document> filter = prepareFilter(options);
FullDocument fullDocument = ClassUtils.isAssignable(Document.class, targetType) ? FullDocument.DEFAULT : FullDocument.UPDATE_LOOKUP;
return //
ReactiveMongoDatabaseUtils.getDatabase(database, mongoDatabaseFactory).map(db -> {
ChangeStreamPublisher<Document> publisher;
if (StringUtils.hasText(collectionName)) {
publisher = filter.isEmpty() ? db.getCollection(collectionName).watch(Document.class) : db.getCollection(collectionName).watch(filter, Document.class);
} else {
publisher = filter.isEmpty() ? db.watch(Document.class) : db.watch(filter, Document.class);
}
publisher = options.getResumeToken().map(BsonValue::asDocument).map(publisher::resumeAfter).orElse(publisher);
publisher = options.getCollation().map(Collation::toMongoCollation).map(publisher::collation).orElse(publisher);
publisher = options.getResumeBsonTimestamp().map(publisher::startAtOperationTime).orElse(publisher);
return publisher.fullDocument(options.getFullDocumentLookup().orElse(fullDocument));
}).flatMapMany(publisher -> Flux.from(publisher).map(document -> new ChangeStreamEvent<>(document, targetType, getConverter())));
}
Aggregations