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:
committed by
Mark Paluch
parent
566e69a825
commit
e1f19f69bd
@@ -17,11 +17,11 @@ package org.springframework.data.mongodb.core;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import org.springframework.data.geo.GeoResults;
|
import org.springframework.data.geo.GeoResults;
|
||||||
import org.springframework.data.mongodb.core.query.NearQuery;
|
import org.springframework.data.mongodb.core.query.NearQuery;
|
||||||
import org.springframework.data.mongodb.core.query.Query;
|
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.
|
* {@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.
|
* Stream all matching elements.
|
||||||
*
|
*
|
||||||
* @return a {@link CloseableIterator} that wraps the a Mongo DB {@link com.mongodb.Cursor} that needs to be closed.
|
* @return a {@link Stream} that wraps the a Mongo DB {@link com.mongodb.Cursor} that needs to be closed. Never
|
||||||
* Never {@literal null}.
|
* {@literal null}.
|
||||||
*/
|
*/
|
||||||
CloseableIterator<T> stream();
|
Stream<T> stream();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the number of matching elements.
|
* Get the number of matching elements.
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import lombok.RequiredArgsConstructor;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
import org.springframework.dao.IncorrectResultSizeDataAccessException;
|
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.Query;
|
||||||
import org.springframework.data.mongodb.core.query.SerializationUtils;
|
import org.springframework.data.mongodb.core.query.SerializationUtils;
|
||||||
import org.springframework.data.util.CloseableIterator;
|
import org.springframework.data.util.CloseableIterator;
|
||||||
|
import org.springframework.data.util.StreamUtils;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
@@ -135,8 +137,8 @@ class ExecutableFindOperationSupport implements ExecutableFindOperation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CloseableIterator<T> stream() {
|
public Stream<T> stream() {
|
||||||
return doStream();
|
return StreamUtils.createStreamFromIterator(doStream());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ import static org.springframework.data.mongodb.core.query.Query.*;
|
|||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.dao.IncorrectResultSizeDataAccessException;
|
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.mapping.Field;
|
||||||
import org.springframework.data.mongodb.core.query.BasicQuery;
|
import org.springframework.data.mongodb.core.query.BasicQuery;
|
||||||
import org.springframework.data.mongodb.core.query.NearQuery;
|
import org.springframework.data.mongodb.core.query.NearQuery;
|
||||||
import org.springframework.data.util.CloseableIterator;
|
|
||||||
|
|
||||||
import com.mongodb.MongoClient;
|
import com.mongodb.MongoClient;
|
||||||
|
|
||||||
@@ -168,7 +169,7 @@ public class ExecutableFindOperationSupportTests {
|
|||||||
@Test // DATAMONGO-1563
|
@Test // DATAMONGO-1563
|
||||||
public void streamAll() {
|
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);
|
assertThat(stream).containsExactlyInAnyOrder(han, luke);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -176,7 +177,7 @@ public class ExecutableFindOperationSupportTests {
|
|||||||
@Test // DATAMONGO-1563
|
@Test // DATAMONGO-1563
|
||||||
public void streamAllWithCollection() {
|
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);
|
assertThat(stream).hasSize(2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -184,7 +185,7 @@ public class ExecutableFindOperationSupportTests {
|
|||||||
@Test // DATAMONGO-1563
|
@Test // DATAMONGO-1563
|
||||||
public void streamAllWithProjection() {
|
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);
|
assertThat(stream).hasOnlyElementsOfType(Jedi.class).hasSize(2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -192,9 +193,7 @@ public class ExecutableFindOperationSupportTests {
|
|||||||
@Test // DATAMONGO-1563
|
@Test // DATAMONGO-1563
|
||||||
public void streamAllBy() {
|
public void streamAllBy() {
|
||||||
|
|
||||||
try (CloseableIterator<Person> stream = template.query(Person.class).matching(query(where("firstname").is("luke")))
|
try (Stream<Person> stream = template.query(Person.class).matching(query(where("firstname").is("luke"))).stream()) {
|
||||||
.stream()) {
|
|
||||||
|
|
||||||
assertThat(stream).containsExactlyInAnyOrder(luke);
|
assertThat(stream).containsExactlyInAnyOrder(luke);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user