Search in sources :

Example 1 with ChangeStreamOptions

use of org.springframework.data.mongodb.core.ChangeStreamOptions in project spring-data-mongodb by spring-projects.

the class ChangeStreamTask method initCursor.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.mongodb.core.messaging.CursorReadingTask#initCursor(org.springframework.data.mongodb.core.MongoTemplate, org.springframework.data.mongodb.core.messaging.SubscriptionRequest.RequestOptions, java.lang.Class)
	 */
@Override
protected MongoCursor<ChangeStreamDocument<Document>> initCursor(MongoTemplate template, RequestOptions options, Class<?> targetType) {
    List<Document> filter = Collections.emptyList();
    BsonDocument resumeToken = new BsonDocument();
    Collation collation = null;
    FullDocument fullDocument = ClassUtils.isAssignable(Document.class, targetType) ? FullDocument.DEFAULT : FullDocument.UPDATE_LOOKUP;
    BsonTimestamp startAt = null;
    boolean resumeAfter = true;
    if (options instanceof ChangeStreamRequest.ChangeStreamRequestOptions) {
        ChangeStreamOptions changeStreamOptions = ((ChangeStreamRequestOptions) options).getChangeStreamOptions();
        filter = prepareFilter(template, changeStreamOptions);
        if (changeStreamOptions.getFilter().isPresent()) {
            Object val = changeStreamOptions.getFilter().get();
            if (val instanceof Aggregation) {
                collation = ((Aggregation) val).getOptions().getCollation().map(org.springframework.data.mongodb.core.query.Collation::toMongoCollation).orElse(null);
            }
        }
        if (changeStreamOptions.getResumeToken().isPresent()) {
            resumeToken = changeStreamOptions.getResumeToken().get().asDocument();
            resumeAfter = changeStreamOptions.isResumeAfter();
        }
        fullDocument = changeStreamOptions.getFullDocumentLookup().orElseGet(() -> ClassUtils.isAssignable(Document.class, targetType) ? FullDocument.DEFAULT : FullDocument.UPDATE_LOOKUP);
        startAt = changeStreamOptions.getResumeBsonTimestamp().orElse(null);
    }
    MongoDatabase db = StringUtils.hasText(options.getDatabaseName()) ? template.getMongoDbFactory().getMongoDatabase(options.getDatabaseName()) : template.getDb();
    ChangeStreamIterable<Document> iterable;
    if (StringUtils.hasText(options.getCollectionName())) {
        iterable = filter.isEmpty() ? db.getCollection(options.getCollectionName()).watch(Document.class) : db.getCollection(options.getCollectionName()).watch(filter, Document.class);
    } else {
        iterable = filter.isEmpty() ? db.watch(Document.class) : db.watch(filter, Document.class);
    }
    if (!options.maxAwaitTime().isZero()) {
        iterable = iterable.maxAwaitTime(options.maxAwaitTime().toMillis(), TimeUnit.MILLISECONDS);
    }
    if (!resumeToken.isEmpty()) {
        if (resumeAfter) {
            iterable = iterable.resumeAfter(resumeToken);
        } else {
            iterable = iterable.startAfter(resumeToken);
        }
    }
    if (startAt != null) {
        iterable.startAtOperationTime(startAt);
    }
    if (collation != null) {
        iterable = iterable.collation(collation);
    }
    iterable = iterable.fullDocument(fullDocument);
    return iterable.iterator();
}
Also used : FullDocument(com.mongodb.client.model.changestream.FullDocument) ChangeStreamOptions(org.springframework.data.mongodb.core.ChangeStreamOptions) Document(org.bson.Document) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) BsonDocument(org.bson.BsonDocument) FullDocument(com.mongodb.client.model.changestream.FullDocument) Collation(com.mongodb.client.model.Collation) ChangeStreamRequestOptions(org.springframework.data.mongodb.core.messaging.ChangeStreamRequest.ChangeStreamRequestOptions) BsonTimestamp(org.bson.BsonTimestamp) TypedAggregation(org.springframework.data.mongodb.core.aggregation.TypedAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) BsonDocument(org.bson.BsonDocument) MongoDatabase(com.mongodb.client.MongoDatabase)

Aggregations

MongoDatabase (com.mongodb.client.MongoDatabase)1 Collation (com.mongodb.client.model.Collation)1 ChangeStreamDocument (com.mongodb.client.model.changestream.ChangeStreamDocument)1 FullDocument (com.mongodb.client.model.changestream.FullDocument)1 BsonDocument (org.bson.BsonDocument)1 BsonTimestamp (org.bson.BsonTimestamp)1 Document (org.bson.Document)1 ChangeStreamOptions (org.springframework.data.mongodb.core.ChangeStreamOptions)1 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)1 TypedAggregation (org.springframework.data.mongodb.core.aggregation.TypedAggregation)1 ChangeStreamRequestOptions (org.springframework.data.mongodb.core.messaging.ChangeStreamRequest.ChangeStreamRequestOptions)1