23
23
import java .io .Serializable ;
24
24
import java .lang .reflect .Method ;
25
25
import java .util .ArrayList ;
26
+ import java .util .Collection ;
26
27
import java .util .Collections ;
27
- import java .util .Comparator ;
28
28
import java .util .List ;
29
+ import java .util .stream .Collectors ;
30
+ import java .util .stream .Stream ;
29
31
30
32
import org .aopalliance .intercept .MethodInterceptor ;
31
33
import org .aopalliance .intercept .MethodInvocation ;
@@ -142,8 +144,8 @@ public List<Document> bulkFetch(List<DBRef> refs) {
142
144
}
143
145
144
146
String collection = refs .iterator ().next ().getCollectionName ();
147
+ List <Object > ids = new ArrayList <>(refs .size ());
145
148
146
- List <Object > ids = new ArrayList <Object >(refs .size ());
147
149
for (DBRef ref : refs ) {
148
150
149
151
if (!collection .equals (ref .getCollectionName ())) {
@@ -155,10 +157,14 @@ public List<Document> bulkFetch(List<DBRef> refs) {
155
157
}
156
158
157
159
MongoDatabase db = mongoDbFactory .getDb ();
158
- List <Document > result = new ArrayList <>();
159
- db .getCollection (collection ).find (new Document ("_id" , new Document ("$in" , ids ))).into (result );
160
- result .sort (new DbRefByReferencePositionComparator (ids ));
161
- return result ;
160
+
161
+ List <Document > result = db .getCollection (collection ) //
162
+ .find (new Document ("_id" , new Document ("$in" , ids ))) //
163
+ .into (new ArrayList <>());
164
+
165
+ return ids .stream () //
166
+ .flatMap (id -> documentWithId (id , result )) //
167
+ .collect (Collectors .toList ());
162
168
}
163
169
164
170
/**
@@ -223,6 +229,20 @@ private boolean isLazyDbRef(MongoPersistentProperty property) {
223
229
return property .getDBRef () != null && property .getDBRef ().lazy ();
224
230
}
225
231
232
+ /**
233
+ * Returns document with the given identifier from the given list of {@link Document}s.
234
+ *
235
+ * @param identifier
236
+ * @param documents
237
+ * @return
238
+ */
239
+ private static Stream <Document > documentWithId (Object identifier , Collection <Document > documents ) {
240
+
241
+ return documents .stream () //
242
+ .filter (it -> it .get ("_id" ).equals (identifier )) //
243
+ .limit (1 );
244
+ }
245
+
226
246
/**
227
247
* A {@link MethodInterceptor} that is used within a lazy loading proxy. The property resolving is delegated to a
228
248
* {@link DbRefResolverCallback}. The resolving process is triggered by a method invocation on the proxy and is
@@ -449,37 +469,4 @@ private synchronized Object resolve() {
449
469
return result ;
450
470
}
451
471
}
452
-
453
- /**
454
- * {@link Comparator} for sorting {@link Document} that have been loaded in random order by a predefined list of
455
- * reference identifiers.
456
- *
457
- * @author Christoph Strobl
458
- * @author Oliver Gierke
459
- * @since 1.10
460
- */
461
- private static class DbRefByReferencePositionComparator implements Comparator <Document > {
462
-
463
- private final List <Object > reference ;
464
-
465
- /**
466
- * Creates a new {@link DbRefByReferencePositionComparator} for the given list of reference identifiers.
467
- *
468
- * @param referenceIds must not be {@literal null}.
469
- */
470
- public DbRefByReferencePositionComparator (List <Object > referenceIds ) {
471
-
472
- Assert .notNull (referenceIds , "Reference identifiers must not be null!" );
473
- this .reference = new ArrayList <Object >(referenceIds );
474
- }
475
-
476
- /*
477
- * (non-Javadoc)
478
- * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
479
- */
480
- @ Override
481
- public int compare (Document o1 , Document o2 ) {
482
- return Integer .compare (reference .indexOf (o1 .get ("_id" )), reference .indexOf (o2 .get ("_id" )));
483
- }
484
- }
485
472
}
0 commit comments