DATAMONGO-765 - Support skip and limit parameters in GridFsTemplate and ReactiveGridFsTemplate.
Leverage `skip` and `limit` methods exposed in `GridFSFindIterable` to support limiting and skipping results. In particular these changes allow the `find(Query)` method to correctly consider parameters of a page request. Original pull request: #806.
This commit is contained in:
committed by
Mark Paluch
parent
cd4e4065ff
commit
1d98b77f3d
@@ -23,7 +23,6 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.bson.BsonObjectId;
|
||||
import org.bson.Document;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.springframework.core.io.support.ResourcePatternResolver;
|
||||
@@ -51,6 +50,7 @@ import com.mongodb.client.gridfs.model.GridFSFile;
|
||||
* @author Mark Paluch
|
||||
* @author Hartmut Lang
|
||||
* @author Niklas Helge Hanft
|
||||
* @author Denis Zavedeev
|
||||
*/
|
||||
public class GridFsTemplate extends GridFsOperationsSupport implements GridFsOperations, ResourcePatternResolver {
|
||||
|
||||
@@ -166,7 +166,14 @@ public class GridFsTemplate extends GridFsOperationsSupport implements GridFsOpe
|
||||
Document queryObject = getMappedQuery(query.getQueryObject());
|
||||
Document sortObject = getMappedQuery(query.getSortObject());
|
||||
|
||||
return getGridFs().find(queryObject).sort(sortObject);
|
||||
GridFSFindIterable iterable = getGridFs().find(queryObject).sort(sortObject);
|
||||
if (query.getSkip() > 0) {
|
||||
iterable = iterable.skip(Math.toIntExact(query.getSkip()));
|
||||
}
|
||||
if (query.getLimit() > 0) {
|
||||
iterable = iterable.limit(query.getLimit());
|
||||
}
|
||||
return iterable;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -50,6 +50,7 @@ import com.mongodb.reactivestreams.client.gridfs.GridFSFindPublisher;
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Nick Stolwijk
|
||||
* @author Denis Zavedeev
|
||||
* @since 2.2
|
||||
*/
|
||||
public class ReactiveGridFsTemplate extends GridFsOperationsSupport implements ReactiveGridFsOperations {
|
||||
@@ -261,7 +262,12 @@ public class ReactiveGridFsTemplate extends GridFsOperationsSupport implements R
|
||||
Document sortObject = getMappedQuery(query.getSortObject());
|
||||
|
||||
GridFSFindPublisher publisherToUse = getGridFs().find(queryObject).sort(sortObject);
|
||||
|
||||
if (query.getLimit() > 0) {
|
||||
publisherToUse = publisherToUse.limit(query.getLimit());
|
||||
}
|
||||
if (query.getSkip() > 0) {
|
||||
publisherToUse = publisherToUse.skip(Math.toIntExact(query.getSkip()));
|
||||
}
|
||||
Integer cursorBatchSize = query.getMeta().getCursorBatchSize();
|
||||
if (cursorBatchSize != null) {
|
||||
publisherToUse = publisherToUse.batchSize(cursorBatchSize);
|
||||
|
||||
@@ -20,6 +20,7 @@ import static org.springframework.data.mongodb.core.query.Criteria.*;
|
||||
import static org.springframework.data.mongodb.core.query.Query.*;
|
||||
import static org.springframework.data.mongodb.gridfs.GridFsCriteria.*;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -27,6 +28,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.bson.BsonObjectId;
|
||||
import org.bson.Document;
|
||||
@@ -38,6 +40,7 @@ import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.domain.Sort.Direction;
|
||||
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
|
||||
@@ -61,6 +64,7 @@ import com.mongodb.gridfs.GridFSInputFile;
|
||||
* @author Martin Baumgartner
|
||||
* @author Hartmut Lang
|
||||
* @author Mark Paluch
|
||||
* @author Denis Zavedeev
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:gridfs/gridfs.xml")
|
||||
@@ -289,6 +293,23 @@ public class GridFsTemplateIntegrationTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2411
|
||||
public void considersSkipLimitWhenQueryingFiles() {
|
||||
Stream.of( //
|
||||
"a", "aa", "aaa", //
|
||||
"b", "bb", "bb", //
|
||||
"c", "cc", "ccc", //
|
||||
"d", "dd", "ddd") //
|
||||
.forEach(filename -> operations.store(new ByteArrayInputStream(new byte[0]), filename));
|
||||
|
||||
PageRequest pageRequest = PageRequest.of(2, 3, Direction.ASC, "filename");
|
||||
List<String> filenames = operations.find(new Query().with(pageRequest)) //
|
||||
.map(GridFSFile::getFilename) //
|
||||
.into(new ArrayList<>());
|
||||
|
||||
assertThat(filenames).containsExactly("c", "cc", "ccc");
|
||||
}
|
||||
|
||||
class Metadata {
|
||||
String version;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import static org.springframework.data.mongodb.core.query.Query.*;
|
||||
import static org.springframework.data.mongodb.gridfs.GridFsCriteria.*;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -36,10 +37,14 @@ import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferFactory;
|
||||
import org.springframework.core.io.buffer.DataBufferUtils;
|
||||
import org.springframework.core.io.buffer.DefaultDataBuffer;
|
||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
import org.springframework.dao.IncorrectResultSizeDataAccessException;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.mongodb.ReactiveMongoDatabaseFactory;
|
||||
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
|
||||
import org.springframework.data.mongodb.core.convert.MongoConverter;
|
||||
@@ -48,6 +53,7 @@ import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
import com.mongodb.client.gridfs.model.GridFSFile;
|
||||
import com.mongodb.gridfs.GridFS;
|
||||
import com.mongodb.gridfs.GridFSInputFile;
|
||||
import com.mongodb.internal.HexUtils;
|
||||
@@ -60,6 +66,7 @@ import com.mongodb.reactivestreams.client.gridfs.helpers.AsyncStreamHelper;
|
||||
* @author Mark Paluch
|
||||
* @author Christoph Strobl
|
||||
* @author Nick Stolwijk
|
||||
* @author Denis Zavedeev
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration("classpath:gridfs/reactive-gridfs.xml")
|
||||
@@ -282,6 +289,26 @@ public class ReactiveGridFsTemplateTests {
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2411
|
||||
public void considersSkipLimitWhenQueryingFiles() {
|
||||
DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
|
||||
DataBuffer buffer = bufferFactory.allocateBuffer(0);
|
||||
Flux.just( //
|
||||
"a", "aa", "aaa", //
|
||||
"b", "bb", "bbb", //
|
||||
"c", "cc", "ccc", //
|
||||
"d", "dd", "ddd") //
|
||||
.flatMap(fileName -> operations.store(Mono.just(buffer), fileName)) //
|
||||
.blockLast();
|
||||
|
||||
PageRequest pageRequest = PageRequest.of(2, 3, Sort.Direction.ASC, "filename");
|
||||
operations.find(new Query().with(pageRequest)) //
|
||||
.map(GridFSFile::getFilename) //
|
||||
.as(StepVerifier::create) //
|
||||
.expectNext("c", "cc", "ccc") //
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
static class Metadata {
|
||||
String version;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user