Skip to content

Commit d151a13

Browse files
domeniqueodrotbohm
authored andcommitted
DATAMONGO-1208 - Use QueryCursorPreparer for streaming in MongoTemplate.
We now use the QueryCursorPreparer honor skip, limit, sort, etc. for streaming. Original pull request: spring-projects#297. Polishing pull request: spring-projects#298.
1 parent 5e7e7d3 commit d151a13

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
* @author Thomas Darimont
137137
* @author Chuong Ngo
138138
* @author Christoph Strobl
139+
* @author Doménique Tilleuil
139140
*/
140141
@SuppressWarnings("deprecation")
141142
public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -335,9 +336,11 @@ public CloseableIterator<T> doInCollection(DBCollection collection) throws Mongo
335336
DBObject mappedQuery = queryMapper.getMappedObject(query.getQueryObject(), persistentEntity);
336337

337338
DBCursor cursor = collection.find(mappedQuery, mappedFields);
339+
QueryCursorPreparer cursorPreparer = new QueryCursorPreparer(query, entityType);
340+
338341
ReadDbObjectCallback<T> readCallback = new ReadDbObjectCallback<T>(mongoConverter, entityType);
339342

340-
return new CloseableIterableCusorAdapter<T>(cursor, exceptionTranslator, readCallback);
343+
return new CloseableIterableCusorAdapter<T>(cursorPreparer.prepare(cursor), exceptionTranslator, readCallback);
341344
}
342345
});
343346
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import org.springframework.data.mongodb.core.query.Criteria;
7474
import org.springframework.data.mongodb.core.query.Query;
7575
import org.springframework.data.mongodb.core.query.Update;
76+
import org.springframework.data.util.CloseableIterator;
7677
import org.springframework.test.context.ContextConfiguration;
7778
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
7879
import org.springframework.util.ObjectUtils;
@@ -2742,6 +2743,44 @@ public void ignoresNullElementsForInsertAll() {
27422743
assertThat(result, hasItems(newYork, washington));
27432744
}
27442745

2746+
/**
2747+
* @see DATAMONGO-1208
2748+
*/
2749+
@Test
2750+
public void takesSortIntoAccountWhenStreaming() {
2751+
2752+
Person youngestPerson = new Person("John", 20);
2753+
Person oldestPerson = new Person("Jane", 42);
2754+
2755+
template.insertAll(Arrays.asList(oldestPerson, youngestPerson));
2756+
2757+
Query q = new Query();
2758+
q.with(new Sort(Direction.ASC, "age"));
2759+
CloseableIterator<Person> stream = template.stream(q, Person.class);
2760+
2761+
assertThat(stream.next().getAge(), is(youngestPerson.getAge()));
2762+
assertThat(stream.next().getAge(), is(oldestPerson.getAge()));
2763+
}
2764+
2765+
/**
2766+
* @see DATAMONGO-1208
2767+
*/
2768+
@Test
2769+
public void takesLimitIntoAccountWhenStreaming() {
2770+
2771+
Person youngestPerson = new Person("John", 20);
2772+
Person oldestPerson = new Person("Jane", 42);
2773+
2774+
template.insertAll(Arrays.asList(oldestPerson, youngestPerson));
2775+
2776+
Query q = new Query();
2777+
q.with(new PageRequest(0, 1, new Sort(Direction.ASC, "age")));
2778+
CloseableIterator<Person> stream = template.stream(q, Person.class);
2779+
2780+
assertThat(stream.next().getAge(), is(youngestPerson.getAge()));
2781+
assertThat(stream.hasNext(), is(false));
2782+
}
2783+
27452784
static class DoucmentWithNamedIdField {
27462785

27472786
@Id String someIdKey;

0 commit comments

Comments
 (0)