DATAMONGO-1431 - Added MongoOperations.stream(…) with explicit collection.

This commit is contained in:
Oliver Gierke
2016-07-13 15:48:11 +02:00
parent 9db2dde19b
commit 325bcd11b9
3 changed files with 57 additions and 5 deletions

View File

@@ -175,13 +175,28 @@ public interface MongoOperations {
* Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link Cursor} that needs to be closed.
*
* @param <T> element return type
* @param query
* @param entityType
* @return
* @param query must not be {@literal null}.
* @param entityType must not be {@literal null}.
* @return will never be {@literal null}.
* @since 1.7
*/
<T> CloseableIterator<T> stream(Query query, Class<T> entityType);
/**
* Executes the given {@link Query} on the entity collection of the specified {@code entityType} and collection backed
* by a Mongo DB {@link Cursor}.
* <p>
* Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link Cursor} that needs to be closed.
*
* @param <T> element return type
* @param query must not be {@literal null}.
* @param entityType must not be {@literal null}.
* @param collectionName must not be {@literal null} or empty.
* @return will never be {@literal null}.
* @since 1.10
*/
<T> CloseableIterator<T> stream(Query query, Class<T> entityType, String collectionName);
/**
* Create an uncapped collection with a name based on the provided entity class.
*
@@ -1031,5 +1046,4 @@ public interface MongoOperations {
* @return
*/
MongoConverter getConverter();
}

View File

@@ -326,7 +326,21 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@Override
public <T> CloseableIterator<T> stream(final Query query, final Class<T> entityType) {
return execute(entityType, new CollectionCallback<CloseableIterator<T>>() {
return stream(query, entityType, determineCollectionName(entityType));
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.MongoOperations#stream(org.springframework.data.mongodb.core.query.Query, java.lang.Class, java.lang.String)
*/
@Override
public <T> CloseableIterator<T> stream(final Query query, final Class<T> entityType, String collectionName) {
Assert.notNull(query, "Query must not be null!");
Assert.notNull(entityType, "Entity type must not be null!");
Assert.hasText(collectionName, "Collection name must not be null or empty!");
return execute(collectionName, new CollectionCallback<CloseableIterator<T>>() {
@Override
public CloseableIterator<T> doInCollection(DBCollection collection) throws MongoException, DataAccessException {

View File

@@ -3344,6 +3344,30 @@ public class MongoTemplateTests {
assertThat(loaded.bigDeciamVal, equalTo(new BigDecimal("800")));
}
/**
* @see DATAMONGO-1431
*/
@Test
public void streamExecutionUsesExplicitCollectionName() {
template.remove(new Query(), "some_special_collection");
template.remove(new Query(), Document.class);
Document document = new Document();
template.insert(document, "some_special_collection");
CloseableIterator<Document> stream = template.stream(new Query(), Document.class);
assertThat(stream.hasNext(), is(false));
stream = template.stream(new Query(), Document.class, "some_special_collection");
assertThat(stream.hasNext(), is(true));
assertThat(stream.next().id, is(document.id));
assertThat(stream.hasNext(), is(false));
}
static class TypeWithNumbers {
@Id String id;