Skip to content

Extend GridFsTemplate and its reactive variant to accept a provided GridFSBucket instance #4389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-1627-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-1627-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-1627-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-1627-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

import org.bson.Document;
import org.bson.types.ObjectId;
Expand All @@ -30,6 +31,7 @@
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.util.BsonUtils;
import org.springframework.data.util.Lazy;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
Expand All @@ -56,9 +58,7 @@
*/
public class GridFsTemplate extends GridFsOperationsSupport implements GridFsOperations, ResourcePatternResolver {

private final MongoDatabaseFactory dbFactory;

private final @Nullable String bucket;
private Supplier<GridFSBucket> bucketSupplier;

/**
* Creates a new {@link GridFsTemplate} using the given {@link MongoDatabaseFactory} and {@link MongoConverter}.
Expand All @@ -78,13 +78,22 @@ public GridFsTemplate(MongoDatabaseFactory dbFactory, MongoConverter converter)
* @param bucket can be {@literal null}.
*/
public GridFsTemplate(MongoDatabaseFactory dbFactory, MongoConverter converter, @Nullable String bucket) {
this(converter, Lazy.of(() -> getGridFs(dbFactory, bucket)));
}

super(converter);
/**
* Creates a new {@link GridFsTemplate} using the given {@link MongoConverter} and {@link Supplier} providing the
* required {@link GridFSBucket}.
*
* @param converter must not be {@literal null}.
* @param gridFSBucket must not be {@literal null}.
* @since 4.2
*/
public GridFsTemplate(MongoConverter converter, Supplier<GridFSBucket> gridFSBucket) {

Assert.notNull(dbFactory, "MongoDbFactory must not be null");
super(converter);

this.dbFactory = dbFactory;
this.bucket = bucket;
this.bucketSupplier = gridFSBucket;
}

public ObjectId store(InputStream content, @Nullable String filename, @Nullable String contentType,
Expand Down Expand Up @@ -142,7 +151,7 @@ public void delete(Query query) {
}

public ClassLoader getClassLoader() {
return dbFactory.getClass().getClassLoader();
return this.getClassLoader();
}

public GridFsResource getResource(String location) {
Expand Down Expand Up @@ -182,7 +191,11 @@ public GridFsResource[] getResources(String locationPattern) {
return new GridFsResource[] { getResource(locationPattern) };
}

private GridFSBucket getGridFs() {
GridFSBucket getGridFs() {
return this.bucketSupplier.get();
}

private static GridFSBucket getGridFs(MongoDatabaseFactory dbFactory, @Nullable String bucket) {

MongoDatabase db = dbFactory.getMongoDatabase();
return bucket == null ? GridFSBuckets.create(db) : GridFSBuckets.create(db, bucket);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.bson.Document;
import org.bson.types.ObjectId;
import org.reactivestreams.Publisher;

import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
Expand All @@ -37,6 +36,7 @@
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.SerializationUtils;
import org.springframework.data.mongodb.util.BsonUtils;
import org.springframework.data.util.Lazy;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
Expand All @@ -61,9 +61,8 @@
*/
public class ReactiveGridFsTemplate extends GridFsOperationsSupport implements ReactiveGridFsOperations {

private final ReactiveMongoDatabaseFactory dbFactory;
private final DataBufferFactory dataBufferFactory;
private final @Nullable String bucket;
private Mono<GridFSBucket> bucketSupplier;

/**
* Creates a new {@link ReactiveGridFsTemplate} using the given {@link ReactiveMongoDatabaseFactory} and
Expand Down Expand Up @@ -100,15 +99,27 @@ public ReactiveGridFsTemplate(ReactiveMongoDatabaseFactory dbFactory, MongoConve
*/
public ReactiveGridFsTemplate(DataBufferFactory dataBufferFactory, ReactiveMongoDatabaseFactory dbFactory,
MongoConverter converter, @Nullable String bucket) {
this(converter, Mono.defer(Lazy.of(() -> doGetBucket(dbFactory, bucket))), dataBufferFactory);
}

/**
* Creates a new {@link ReactiveGridFsTemplate} using the given {@link MongoConverter}, {@link Mono} emitting a
* {@link ReactiveMongoDatabaseFactory} and {@link DataBufferFactory}.
*
* @param converter must not be {@literal null}.
* @param gridFSBucket must not be {@literal null}.
* @param dataBufferFactory must not be {@literal null}.
* @since 4.2
*/
public ReactiveGridFsTemplate(MongoConverter converter, Mono<GridFSBucket> gridFSBucket,
DataBufferFactory dataBufferFactory) {

super(converter);

Assert.notNull(dataBufferFactory, "DataBufferFactory must not be null");
Assert.notNull(dbFactory, "ReactiveMongoDatabaseFactory must not be null");

this.bucketSupplier = gridFSBucket;
this.dataBufferFactory = dataBufferFactory;
this.dbFactory = dbFactory;
this.bucket = bucket;
}

@Override
Expand Down Expand Up @@ -247,11 +258,15 @@ public <T> Flux<T> createFlux(ReactiveBucketCallback<T> callback) {
return doGetBucket().flatMapMany(callback::doInBucket);
}

protected Mono<GridFSBucket> doGetBucket() {
static Mono<GridFSBucket> doGetBucket(ReactiveMongoDatabaseFactory dbFactory, @Nullable String bucket) {
return dbFactory.getMongoDatabase()
.map(db -> bucket == null ? GridFSBuckets.create(db) : GridFSBuckets.create(db, bucket));
}

protected Mono<GridFSBucket> doGetBucket() {
return bucketSupplier;
}

/**
* @param <T>
* @author Mathieu Ouellet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

import javax.net.ssl.SSLSocketFactory;

import java.util.function.Supplier;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.model.GridFSFile;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -168,8 +173,12 @@ public void testGridFsTemplateFactory() {
assertThat(ctx.containsBean("gridFsTemplate")).isTrue();
GridFsOperations operations = (GridFsOperations) ctx.getBean("gridFsTemplate");

MongoDatabaseFactory dbf = (MongoDatabaseFactory) getField(operations, "dbFactory");
assertThat(getField(dbf, "databaseName")).isEqualTo("database");
Supplier<GridFSBucket> gridFSBucketSupplier = (Supplier<GridFSBucket>) getField(operations, "bucketSupplier");
GridFSBucket gfsBucket = gridFSBucketSupplier.get();
assertThat(gfsBucket.getBucketName()).isEqualTo("fs"); // fs is the default

MongoCollection<GridFSFile> filesCollection = (MongoCollection<GridFSFile>) getField(gfsBucket, "filesCollection");
assertThat(filesCollection.getNamespace().getDatabaseName()).isEqualTo("database");

MongoConverter converter = (MongoConverter) getField(operations, "converter");
assertThat(converter).isNotNull();
Expand All @@ -181,9 +190,12 @@ public void testSecondGridFsTemplateFactory() {
assertThat(ctx.containsBean("secondGridFsTemplate")).isTrue();
GridFsOperations operations = (GridFsOperations) ctx.getBean("secondGridFsTemplate");

MongoDatabaseFactory dbf = (MongoDatabaseFactory) getField(operations, "dbFactory");
assertThat(getField(dbf, "databaseName")).isEqualTo("database");
assertThat(getField(operations, "bucket")).isEqualTo(null);
Supplier<GridFSBucket> gridFSBucketSupplier = (Supplier<GridFSBucket>) getField(operations, "bucketSupplier");
GridFSBucket gfsBucket = gridFSBucketSupplier.get();
assertThat(gfsBucket.getBucketName()).isEqualTo("fs"); // fs is the default

MongoCollection<GridFSFile> filesCollection = (MongoCollection<GridFSFile>) getField(gfsBucket, "filesCollection");
assertThat(filesCollection.getNamespace().getDatabaseName()).isEqualTo("database");

MongoConverter converter = (MongoConverter) getField(operations, "converter");
assertThat(converter).isNotNull();
Expand All @@ -195,9 +207,12 @@ public void testThirdGridFsTemplateFactory() {
assertThat(ctx.containsBean("thirdGridFsTemplate")).isTrue();
GridFsOperations operations = (GridFsOperations) ctx.getBean("thirdGridFsTemplate");

MongoDatabaseFactory dbf = (MongoDatabaseFactory) getField(operations, "dbFactory");
assertThat(getField(dbf, "databaseName")).isEqualTo("database");
assertThat(getField(operations, "bucket")).isEqualTo("bucketString");
Supplier<GridFSBucket> gridFSBucketSupplier = (Supplier<GridFSBucket>) getField(operations, "bucketSupplier");
GridFSBucket gfsBucket = gridFSBucketSupplier.get();
assertThat(gfsBucket.getBucketName()).isEqualTo("bucketString"); // fs is the default

MongoCollection<GridFSFile> filesCollection = (MongoCollection<GridFSFile>) getField(gfsBucket, "filesCollection");
assertThat(filesCollection.getNamespace().getDatabaseName()).isEqualTo("database");

MongoConverter converter = (MongoConverter) getField(operations, "converter");
assertThat(converter).isNotNull();
Expand Down