DATAMONGO-1739 - Changed TerminatingFindOperation.stream() to return Stream.

TerminatingFindOperation.stream() now returns a Stream directly, leveraging Spring Data Commons' StreamUtils.createStreamFromIterator(…) to create a Stream and register a callback to forward calls to Stream.close() to the iterator.

Original pull request: #485.
This commit is contained in:
Oliver Gierke
2017-07-11 17:27:28 +02:00
committed by Mark Paluch
parent 566e69a825
commit e1f19f69bd
3 changed files with 14 additions and 13 deletions

View File

@@ -17,11 +17,11 @@ package org.springframework.data.mongodb.core;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.mongodb.core.query.NearQuery;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.util.CloseableIterator;
/**
* {@link ExecutableFindOperation} allows creation and execution of MongoDB find operations in a fluent API style.
@@ -111,10 +111,10 @@ public interface ExecutableFindOperation {
/**
* Stream all matching elements.
*
* @return a {@link CloseableIterator} that wraps the a Mongo DB {@link com.mongodb.Cursor} that needs to be closed.
* Never {@literal null}.
* @return a {@link Stream} that wraps the a Mongo DB {@link com.mongodb.Cursor} that needs to be closed. Never
* {@literal null}.
*/
CloseableIterator<T> stream();
Stream<T> stream();
/**
* Get the number of matching elements.

View File

@@ -19,6 +19,7 @@ import lombok.RequiredArgsConstructor;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import org.bson.Document;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
@@ -26,6 +27,7 @@ import org.springframework.data.mongodb.core.query.NearQuery;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.SerializationUtils;
import org.springframework.data.util.CloseableIterator;
import org.springframework.data.util.StreamUtils;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
@@ -135,8 +137,8 @@ class ExecutableFindOperationSupport implements ExecutableFindOperation {
}
@Override
public CloseableIterator<T> stream() {
return doStream();
public Stream<T> stream() {
return StreamUtils.createStreamFromIterator(doStream());
}
@Override

View File

@@ -22,6 +22,8 @@ import static org.springframework.data.mongodb.core.query.Query.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.stream.Stream;
import org.junit.Before;
import org.junit.Test;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
@@ -33,7 +35,6 @@ import org.springframework.data.mongodb.core.index.GeospatialIndex;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.NearQuery;
import org.springframework.data.util.CloseableIterator;
import com.mongodb.MongoClient;
@@ -168,7 +169,7 @@ public class ExecutableFindOperationSupportTests {
@Test // DATAMONGO-1563
public void streamAll() {
try (CloseableIterator<Person> stream = template.query(Person.class).stream()) {
try (Stream<Person> stream = template.query(Person.class).stream()) {
assertThat(stream).containsExactlyInAnyOrder(han, luke);
}
}
@@ -176,7 +177,7 @@ public class ExecutableFindOperationSupportTests {
@Test // DATAMONGO-1563
public void streamAllWithCollection() {
try (CloseableIterator<Human> stream = template.query(Human.class).inCollection(STAR_WARS).stream()) {
try (Stream<Human> stream = template.query(Human.class).inCollection(STAR_WARS).stream()) {
assertThat(stream).hasSize(2);
}
}
@@ -184,7 +185,7 @@ public class ExecutableFindOperationSupportTests {
@Test // DATAMONGO-1563
public void streamAllWithProjection() {
try (CloseableIterator<Jedi> stream = template.query(Person.class).as(Jedi.class).stream()) {
try (Stream<Jedi> stream = template.query(Person.class).as(Jedi.class).stream()) {
assertThat(stream).hasOnlyElementsOfType(Jedi.class).hasSize(2);
}
}
@@ -192,9 +193,7 @@ public class ExecutableFindOperationSupportTests {
@Test // DATAMONGO-1563
public void streamAllBy() {
try (CloseableIterator<Person> stream = template.query(Person.class).matching(query(where("firstname").is("luke")))
.stream()) {
try (Stream<Person> stream = template.query(Person.class).matching(query(where("firstname").is("luke"))).stream()) {
assertThat(stream).containsExactlyInAnyOrder(luke);
}
}