55 * License, v. 2.0. If a copy of the MPL was not distributed with this
66 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
77 */
8+
89package org .seedstack .mongodb .morphia ;
910
1011import static com .google .common .base .Preconditions .checkArgument ;
11-
12+ import static java .util .Spliterators .spliteratorUnknownSize ;
13+
14+ import com .mongodb .DBCollection ;
15+ import dev .morphia .Datastore ;
16+ import dev .morphia .query .CountOptions ;
17+ import dev .morphia .query .CriteriaContainer ;
18+ import dev .morphia .query .FindOptions ;
19+ import dev .morphia .query .Query ;
20+ import dev .morphia .query .Sort ;
21+ import dev .morphia .query .internal .MorphiaCursor ;
1222import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
1323import java .util .ArrayList ;
1424import java .util .List ;
1525import java .util .Optional ;
26+ import java .util .Spliterator ;
1627import java .util .stream .Stream ;
28+ import java .util .stream .StreamSupport ;
1729import javax .inject .Inject ;
18- import org .mongodb .morphia .Datastore ;
19- import org .mongodb .morphia .mapping .Mapper ;
20- import org .mongodb .morphia .query .CountOptions ;
21- import org .mongodb .morphia .query .CriteriaContainer ;
22- import org .mongodb .morphia .query .FindOptions ;
23- import org .mongodb .morphia .query .Query ;
24- import org .mongodb .morphia .query .Sort ;
2530import org .seedstack .business .domain .AggregateExistsException ;
2631import org .seedstack .business .domain .AggregateNotFoundException ;
2732import org .seedstack .business .domain .AggregateRoot ;
4247 * @param <ID> Identifier class.
4348 */
4449public abstract class BaseMorphiaRepository <A extends AggregateRoot <ID >, ID > extends BaseRepository <A , ID > {
50+ public static final String ID_KEY = "_id" ;
4551 private Datastore datastore ;
4652 private SpecificationTranslator <MorphiaTranslationContext , CriteriaContainer > specificationTranslator ;
4753
@@ -77,12 +83,14 @@ public void add(A aggregate) throws AggregateExistsException {
7783
7884 @ Override
7985 public Stream <A > get (Specification <A > specification , Option ... options ) {
80- return buildQuery (specification , options ).asList (buildFindOptions (options )).stream ();
86+ final MorphiaCursor <A > cursor = buildQuery (specification , options ).find (buildFindOptions (options ));
87+ return StreamSupport .stream (spliteratorUnknownSize (cursor , Spliterator .ORDERED ), false )
88+ .onClose (cursor ::close );
8189 }
8290
8391 @ Override
8492 public Optional <A > get (ID id ) {
85- return Optional .ofNullable (datastore .get (getAggregateRootClass (), id ));
93+ return Optional .ofNullable (datastore .createQuery (getAggregateRootClass ()). first ( ));
8694 }
8795
8896 @ Override
@@ -92,7 +100,7 @@ public boolean contains(Specification<A> specification) {
92100
93101 @ Override
94102 public boolean contains (ID id ) {
95- return datastore .find (getAggregateRootClass ()).filter (Mapper . ID_KEY , id ).count (new CountOptions ().limit (1 )) > 0 ;
103+ return datastore .find (getAggregateRootClass ()).filter (ID_KEY , id ).count (new CountOptions ().limit (1 )) > 0 ;
96104 }
97105
98106 @ Override
@@ -102,7 +110,7 @@ public long count(Specification<A> specification) {
102110
103111 @ Override
104112 public long size () {
105- return datastore .getCount (getAggregateRootClass ());
113+ return datastore .createQuery (getAggregateRootClass ()). count ( );
106114 }
107115
108116 @ Override
@@ -112,7 +120,9 @@ public long remove(Specification<A> specification) throws AggregateNotFoundExcep
112120
113121 @ Override
114122 public void remove (ID id ) throws AggregateNotFoundException {
115- checkExactlyOneAggregateRemoved (datastore .delete (getAggregateRootClass (), id ).getN (), id );
123+ checkExactlyOneAggregateRemoved (
124+ datastore .delete (datastore .find (getAggregateRootClass ()).filter (ID_KEY , id )).getN (),
125+ id );
116126 }
117127
118128 private void checkExactlyOneAggregateRemoved (int n , ID id ) {
@@ -143,8 +153,9 @@ public A addOrUpdate(A aggregate) {
143153
144154 @ Override
145155 public void clear () {
146- datastore .getCollection (getAggregateRootClass ()).drop ();
147- datastore .getCollection (getAggregateRootClass ()).dropIndexes ();
156+ DBCollection collection = datastore .getCollection (getAggregateRootClass ());
157+ collection .drop ();
158+ collection .dropIndexes ();
148159 }
149160
150161 private Query <A > buildQuery (Specification <A > specification , Option ... options ) {
@@ -187,7 +198,7 @@ private void applyLimit(FindOptions findOptions, LimitOption limitOption) {
187198 findOptions .limit ((int ) limit );
188199 }
189200
190- private void applySort (Query query , SortOption sortOption ) {
201+ private void applySort (Query <?> query , SortOption sortOption ) {
191202 List <Sort > sorts = new ArrayList <>();
192203 for (SortOption .SortedAttribute sortedAttribute : sortOption .getSortedAttributes ()) {
193204 switch (sortedAttribute .getDirection ()) {
@@ -202,6 +213,6 @@ private void applySort(Query query, SortOption sortOption) {
202213 "Unsupported sort direction " + sortedAttribute .getDirection ());
203214 }
204215 }
205- query .order (sorts .toArray (new Sort [sorts . size () ]));
216+ query .order (sorts .toArray (new Sort [0 ]));
206217 }
207218}
0 commit comments