Browse Source

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

pull/380/head
Oliver Gierke 10 years ago
parent
commit
325bcd11b9
  1. 22
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java
  2. 16
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java
  3. 24
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

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

@ -175,13 +175,28 @@ public interface MongoOperations {
* Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link Cursor} that needs to be closed. * Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link Cursor} that needs to be closed.
* *
* @param <T> element return type * @param <T> element return type
* @param query * @param query must not be {@literal null}.
* @param entityType * @param entityType must not be {@literal null}.
* @return * @return will never be {@literal null}.
* @since 1.7 * @since 1.7
*/ */
<T> CloseableIterator<T> stream(Query query, Class<T> entityType); <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. * Create an uncapped collection with a name based on the provided entity class.
* *
@ -1031,5 +1046,4 @@ public interface MongoOperations {
* @return * @return
*/ */
MongoConverter getConverter(); MongoConverter getConverter();
} }

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

@ -326,7 +326,21 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@Override @Override
public <T> CloseableIterator<T> stream(final Query query, final Class<T> entityType) { 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 @Override
public CloseableIterator<T> doInCollection(DBCollection collection) throws MongoException, DataAccessException { public CloseableIterator<T> doInCollection(DBCollection collection) throws MongoException, DataAccessException {

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

@ -3344,6 +3344,30 @@ public class MongoTemplateTests {
assertThat(loaded.bigDeciamVal, equalTo(new BigDecimal("800"))); 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 { static class TypeWithNumbers {
@Id String id; @Id String id;

Loading…
Cancel
Save