Search in sources :

Example 1 with FindPublisher

use of com.mongodb.reactivestreams.client.FindPublisher in project mongo-java-driver by mongodb.

the class GridFSUploadPublisherImpl method createCheckAndCreateIndexesMono.

private Mono<Void> createCheckAndCreateIndexesMono() {
    MongoCollection<Document> collection = filesCollection.withDocumentClass(Document.class).withReadPreference(primary());
    FindPublisher<Document> findPublisher;
    if (clientSession != null) {
        findPublisher = collection.find(clientSession);
    } else {
        findPublisher = collection.find();
    }
    AtomicBoolean collectionExists = new AtomicBoolean(false);
    return Mono.create(sink -> Mono.from(findPublisher.projection(PROJECTION).first()).subscribe(d -> collectionExists.set(true), sink::error, () -> {
        if (collectionExists.get()) {
            sink.success();
        } else {
            checkAndCreateIndex(filesCollection.withReadPreference(primary()), FILES_INDEX).doOnError(sink::error).doOnSuccess(i -> {
                checkAndCreateIndex(chunksCollection.withReadPreference(primary()), CHUNKS_INDEX).doOnError(sink::error).doOnSuccess(sink::success).subscribe();
            }).subscribe();
        }
    }));
}
Also used : Document(org.bson.Document) ListIndexesPublisher(com.mongodb.reactivestreams.client.ListIndexesPublisher) Date(java.util.Date) MongoGridFSException(com.mongodb.MongoGridFSException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) Function(java.util.function.Function) ByteBuffer(java.nio.ByteBuffer) BsonValue(org.bson.BsonValue) GridFSUploadPublisher(com.mongodb.reactivestreams.client.gridfs.GridFSUploadPublisher) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Subscriber(org.reactivestreams.Subscriber) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) Binary(org.bson.types.Binary) InsertOneResult(com.mongodb.client.result.InsertOneResult) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) ClientSession(com.mongodb.reactivestreams.client.ClientSession) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) AtomicLong(java.util.concurrent.atomic.AtomicLong) Assertions.notNull(com.mongodb.assertions.Assertions.notNull) FindPublisher(com.mongodb.reactivestreams.client.FindPublisher) DeleteResult(com.mongodb.client.result.DeleteResult) ObjectId(org.bson.types.ObjectId) ReadPreference.primary(com.mongodb.ReadPreference.primary) Nullable(com.mongodb.lang.Nullable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Document(org.bson.Document)

Example 2 with FindPublisher

use of com.mongodb.reactivestreams.client.FindPublisher in project mongo-java-driver by mongodb.

the class GridFSDownloadPublisherImpl method getChunkPublisher.

private Flux<ByteBuffer> getChunkPublisher(final GridFSFile gridFSFile) {
    Document filter = new Document("files_id", gridFSFile.getId());
    FindPublisher<Document> chunkPublisher;
    if (clientSession != null) {
        chunkPublisher = chunksCollection.find(clientSession, filter);
    } else {
        chunkPublisher = chunksCollection.find(filter);
    }
    AtomicInteger chunkCounter = new AtomicInteger(0);
    int numberOfChunks = (int) Math.ceil((double) gridFSFile.getLength() / gridFSFile.getChunkSize());
    Flux<ByteBuffer> byteBufferFlux = Flux.from(chunkPublisher.sort(new Document("n", 1))).map(chunk -> {
        int expectedChunkIndex = chunkCounter.getAndAdd(1);
        if (chunk == null || chunk.getInteger("n") != expectedChunkIndex) {
            throw new MongoGridFSException(format("Could not find file chunk for files_id: %s at chunk index %s.", gridFSFile.getId(), expectedChunkIndex));
        } else if (!(chunk.get("data") instanceof Binary)) {
            throw new MongoGridFSException("Unexpected data format for the chunk");
        }
        byte[] data = chunk.get("data", Binary.class).getData();
        long expectedDataLength = 0;
        if (numberOfChunks > 0) {
            expectedDataLength = expectedChunkIndex + 1 == numberOfChunks ? gridFSFile.getLength() - (expectedChunkIndex * (long) gridFSFile.getChunkSize()) : gridFSFile.getChunkSize();
        }
        if (data.length != expectedDataLength) {
            throw new MongoGridFSException(format("Chunk size data length is not the expected size. " + "The size was %s for file_id: %s chunk index %s it should be " + "%s bytes.", data.length, gridFSFile.getId(), expectedChunkIndex, expectedDataLength));
        }
        return ByteBuffer.wrap(data);
    }).doOnComplete(() -> {
        if (chunkCounter.get() < numberOfChunks) {
            throw new MongoGridFSException(format("Could not find file chunk for files_id: %s at chunk index %s.", gridFSFile.getId(), chunkCounter.get()));
        }
    });
    return bufferSizeBytes == null ? byteBufferFlux : new ResizingByteBufferFlux(byteBufferFlux, bufferSizeBytes);
}
Also used : Document(org.bson.Document) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) Binary(org.bson.types.Binary) Publisher(org.reactivestreams.Publisher) MongoGridFSException(com.mongodb.MongoGridFSException) Mono(reactor.core.publisher.Mono) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) ClientSession(com.mongodb.reactivestreams.client.ClientSession) Function(java.util.function.Function) ByteBuffer(java.nio.ByteBuffer) String.format(java.lang.String.format) GridFSDownloadPublisher(com.mongodb.reactivestreams.client.gridfs.GridFSDownloadPublisher) Flux(reactor.core.publisher.Flux) Assertions.notNull(com.mongodb.assertions.Assertions.notNull) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FindPublisher(com.mongodb.reactivestreams.client.FindPublisher) Nullable(com.mongodb.lang.Nullable) Subscriber(org.reactivestreams.Subscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MongoGridFSException(com.mongodb.MongoGridFSException) Binary(org.bson.types.Binary) Document(org.bson.Document) ByteBuffer(java.nio.ByteBuffer)

Example 3 with FindPublisher

use of com.mongodb.reactivestreams.client.FindPublisher 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)

Example 4 with FindPublisher

use of com.mongodb.reactivestreams.client.FindPublisher in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplate method doRemove.

protected <T> Mono<DeleteResult> doRemove(String collectionName, Query query, @Nullable Class<T> entityClass) {
    if (query == null) {
        throw new InvalidDataAccessApiUsageException("Query passed in to remove can't be null!");
    }
    Assert.hasText(collectionName, "Collection name must not be null or empty!");
    MongoPersistentEntity<?> entity = getPersistentEntity(entityClass);
    DeleteContext deleteContext = queryOperations.deleteQueryContext(query);
    Document queryObject = deleteContext.getMappedQuery(entity);
    DeleteOptions deleteOptions = deleteContext.getDeleteOptions(entityClass);
    Document removeQuery = deleteContext.getMappedQuery(entity);
    MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.REMOVE, collectionName, entityClass, null, removeQuery);
    WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
    return execute(collectionName, collection -> {
        maybeEmitEvent(new BeforeDeleteEvent<>(removeQuery, entityClass, collectionName));
        MongoCollection<Document> collectionToUse = prepareCollection(collection, writeConcernToUse);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(String.format("Remove using query: %s in collection: %s.", serializeToJsonSafely(removeQuery), collectionName));
        }
        if (query.getLimit() > 0 || query.getSkip() > 0) {
            FindPublisher<Document> cursor = new QueryFindPublisherPreparer(query, entityClass).prepare(// 
            collection.find(removeQuery)).projection(MappedDocument.getIdOnlyProjection());
            return // 
            Flux.from(cursor).map(// 
            MappedDocument::of).map(// 
            MappedDocument::getId).collectList().flatMapMany(val -> {
                return collectionToUse.deleteMany(MappedDocument.getIdIn(val), deleteOptions);
            });
        } else {
            return collectionToUse.deleteMany(removeQuery, deleteOptions);
        }
    }).doOnNext(// 
    it -> maybeEmitEvent(new AfterDeleteEvent<>(queryObject, entityClass, collectionName))).next();
}
Also used : Document(org.bson.Document) Arrays(java.util.Arrays) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) NumberUtils(org.springframework.util.NumberUtils) BsonUtils(org.springframework.data.mongodb.util.BsonUtils) Tuples(reactor.util.function.Tuples) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) ReactiveMongoDatabaseUtils(org.springframework.data.mongodb.ReactiveMongoDatabaseUtils) FullDocument(com.mongodb.client.model.changestream.FullDocument) QueryContext(org.springframework.data.mongodb.core.QueryOperations.QueryContext) Optionals(org.springframework.data.util.Optionals) UpdateResult(com.mongodb.client.result.UpdateResult) Map(java.util.Map) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) CursorOption(org.springframework.data.mongodb.core.query.Meta.CursorOption) MongoJsonSchemaMapper(org.springframework.data.mongodb.core.convert.MongoJsonSchemaMapper) AggregationOperationContext(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext) ClassUtils(org.springframework.util.ClassUtils) NearQuery(org.springframework.data.mongodb.core.query.NearQuery) MappingContextEvent(org.springframework.data.mapping.context.MappingContextEvent) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) CollectionUtils(org.springframework.util.CollectionUtils) FindPublisher(com.mongodb.reactivestreams.client.FindPublisher) LogFactory(org.apache.commons.logging.LogFactory) DistinctQueryContext(org.springframework.data.mongodb.core.QueryOperations.DistinctQueryContext) ApplicationContextAware(org.springframework.context.ApplicationContextAware) Validator(org.springframework.data.mongodb.core.validation.Validator) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) MappingContext(org.springframework.data.mapping.context.MappingContext) ArrayList(java.util.ArrayList) UpdateDefinition(org.springframework.data.mongodb.core.query.UpdateDefinition) MongoClient(com.mongodb.reactivestreams.client.MongoClient) Bson(org.bson.conversions.Bson) NoOpDbRefResolver(org.springframework.data.mongodb.core.convert.NoOpDbRefResolver) ReactiveMongoPersistentEntityIndexCreator(org.springframework.data.mongodb.core.index.ReactiveMongoPersistentEntityIndexCreator) DbRefResolver(org.springframework.data.mongodb.core.convert.DbRefResolver) MappingException(org.springframework.data.mapping.MappingException) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) Nullable(org.springframework.lang.Nullable) JsonSchemaMapper(org.springframework.data.mongodb.core.convert.JsonSchemaMapper) MongoSimpleTypes(org.springframework.data.mongodb.core.mapping.MongoSimpleTypes) com.mongodb.client.model(com.mongodb.client.model) PrefixingDelegatingAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.PrefixingDelegatingAggregationOperationContext) CountContext(org.springframework.data.mongodb.core.QueryOperations.CountContext) ReactiveIndexOperations(org.springframework.data.mongodb.core.index.ReactiveIndexOperations) org.springframework.data.mongodb.core.mapping.event(org.springframework.data.mongodb.core.mapping.event) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) Publisher(org.reactivestreams.Publisher) ObjectUtils(org.springframework.util.ObjectUtils) DeleteContext(org.springframework.data.mongodb.core.QueryOperations.DeleteContext) TypedAggregation(org.springframework.data.mongodb.core.aggregation.TypedAggregation) Mono(reactor.core.publisher.Mono) BeansException(org.springframework.beans.BeansException) MongoMappingEventPublisher(org.springframework.data.mongodb.core.index.MongoMappingEventPublisher) RelaxedTypeBasedAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.RelaxedTypeBasedAggregationOperationContext) ResourceUtils(org.springframework.util.ResourceUtils) Flux(reactor.core.publisher.Flux) ObjectId(org.bson.types.ObjectId) Collation(org.springframework.data.mongodb.core.query.Collation) EntityProjection(org.springframework.data.projection.EntityProjection) BsonValue(org.bson.BsonValue) PersistenceExceptionTranslator(org.springframework.dao.support.PersistenceExceptionTranslator) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) MapReduceOptions(org.springframework.data.mongodb.core.mapreduce.MapReduceOptions) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) AdaptibleEntity(org.springframework.data.mongodb.core.EntityOperations.AdaptibleEntity) CursorType(com.mongodb.CursorType) MongoWriter(org.springframework.data.mongodb.core.convert.MongoWriter) Meta(org.springframework.data.mongodb.core.query.Meta) ReactiveEntityCallbacks(org.springframework.data.mapping.callback.ReactiveEntityCallbacks) InsertOneResult(com.mongodb.client.result.InsertOneResult) SerializationUtils(org.springframework.data.mongodb.core.query.SerializationUtils) MongoException(com.mongodb.MongoException) Collection(java.util.Collection) ApplicationListener(org.springframework.context.ApplicationListener) Collectors(java.util.stream.Collectors) PersistentEntity(org.springframework.data.mapping.PersistentEntity) List(java.util.List) Optional(java.util.Optional) UpdateMapper(org.springframework.data.mongodb.core.convert.UpdateMapper) ReactiveMongoDatabaseFactory(org.springframework.data.mongodb.ReactiveMongoDatabaseFactory) TypeBasedAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext) UpdateContext(org.springframework.data.mongodb.core.QueryOperations.UpdateContext) Metric(org.springframework.data.geo.Metric) ReadPreference(com.mongodb.ReadPreference) DataAccessException(org.springframework.dao.DataAccessException) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) Tuple2(reactor.util.function.Tuple2) HashMap(java.util.HashMap) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) DistinctPublisher(com.mongodb.reactivestreams.client.DistinctPublisher) MongoDatabaseFactory(org.springframework.data.mongodb.MongoDatabaseFactory) MongoConverter(org.springframework.data.mongodb.core.convert.MongoConverter) Function(java.util.function.Function) Distance(org.springframework.data.geo.Distance) EntityReader(org.springframework.data.convert.EntityReader) ChangeStreamPublisher(com.mongodb.reactivestreams.client.ChangeStreamPublisher) ArrayFilter(org.springframework.data.mongodb.core.query.UpdateDefinition.ArrayFilter) Granularity(org.springframework.data.mongodb.core.timeseries.Granularity) MongoPersistentEntity(org.springframework.data.mongodb.core.mapping.MongoPersistentEntity) AggregationDefinition(org.springframework.data.mongodb.core.QueryOperations.AggregationDefinition) Subscriber(org.reactivestreams.Subscriber) ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) Iterator(java.util.Iterator) AggregationOptions(org.springframework.data.mongodb.core.aggregation.AggregationOptions) MapReducePublisher(com.mongodb.reactivestreams.client.MapReducePublisher) ClientSession(com.mongodb.reactivestreams.client.ClientSession) ApplicationContext(org.springframework.context.ApplicationContext) Query(org.springframework.data.mongodb.core.query.Query) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) AggregatePublisher(com.mongodb.reactivestreams.client.AggregatePublisher) ClientSessionOptions(com.mongodb.ClientSessionOptions) DeleteResult(com.mongodb.client.result.DeleteResult) Log(org.apache.commons.logging.Log) GeoResult(org.springframework.data.geo.GeoResult) SessionSynchronization(org.springframework.data.mongodb.SessionSynchronization) WriteConcern(com.mongodb.WriteConcern) Collections(java.util.Collections) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) DeleteContext(org.springframework.data.mongodb.core.QueryOperations.DeleteContext) Document(org.bson.Document) FullDocument(com.mongodb.client.model.changestream.FullDocument) WriteConcern(com.mongodb.WriteConcern) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException)

Aggregations

FindPublisher (com.mongodb.reactivestreams.client.FindPublisher)4 MongoCollection (com.mongodb.reactivestreams.client.MongoCollection)4 Function (java.util.function.Function)4 Document (org.bson.Document)4 Publisher (org.reactivestreams.Publisher)4 ClientSession (com.mongodb.reactivestreams.client.ClientSession)3 BsonValue (org.bson.BsonValue)3 Subscriber (org.reactivestreams.Subscriber)3 Flux (reactor.core.publisher.Flux)3 Mono (reactor.core.publisher.Mono)3 MongoException (com.mongodb.MongoException)2 MongoGridFSException (com.mongodb.MongoGridFSException)2 Assertions.notNull (com.mongodb.assertions.Assertions.notNull)2 GridFSFile (com.mongodb.client.gridfs.model.GridFSFile)2 FullDocument (com.mongodb.client.model.changestream.FullDocument)2 DeleteResult (com.mongodb.client.result.DeleteResult)2 InsertOneResult (com.mongodb.client.result.InsertOneResult)2 Nullable (com.mongodb.lang.Nullable)2 ChangeStreamPublisher (com.mongodb.reactivestreams.client.ChangeStreamPublisher)2 ByteBuffer (java.nio.ByteBuffer)2