28
28
import java .util .Optional ;
29
29
import java .util .function .Function ;
30
30
import java .util .function .UnaryOperator ;
31
- import java .util .stream .Collectors ;
32
31
33
32
import org .reactivestreams .Publisher ;
33
+
34
34
import org .springframework .dao .IncorrectResultSizeDataAccessException ;
35
35
import org .springframework .dao .OptimisticLockingFailureException ;
36
36
import org .springframework .data .domain .Example ;
47
47
import org .springframework .data .mongodb .repository .ReactiveMongoRepository ;
48
48
import org .springframework .data .mongodb .repository .query .MongoEntityInformation ;
49
49
import org .springframework .data .repository .query .FluentQuery ;
50
- import org .springframework .data .util .StreamUtils ;
51
50
import org .springframework .lang .Nullable ;
52
51
import org .springframework .util .Assert ;
53
52
@@ -264,8 +263,15 @@ public Mono<Void> deleteAllById(Iterable<? extends ID> ids) {
264
263
265
264
Assert .notNull (ids , "The given Iterable of Id's must not be null" );
266
265
266
+ return deleteAllById (ids , getReadPreference ());
267
+ }
268
+
269
+ @ SuppressWarnings ("OptionalUsedAsFieldOrParameterType" )
270
+ private Mono <Void > deleteAllById (Iterable <? extends ID > ids , Optional <ReadPreference > readPreference ) {
271
+
267
272
Query query = getIdQuery (ids );
268
- getReadPreference ().ifPresent (query ::withReadPreference );
273
+ readPreference .ifPresent (query ::withReadPreference );
274
+
269
275
return mongoOperations .remove (query , entityInformation .getJavaType (), entityInformation .getCollectionName ()).then ();
270
276
}
271
277
@@ -274,10 +280,9 @@ public Mono<Void> deleteAll(Iterable<? extends T> entities) {
274
280
275
281
Assert .notNull (entities , "The given Iterable of entities must not be null" );
276
282
277
- Collection <? extends ID > ids = StreamUtils .createStreamFromIterator (entities .iterator ())
278
- .map (entityInformation ::getId ).collect (Collectors .toList ());
279
-
280
- return deleteAllById (ids );
283
+ Optional <ReadPreference > readPreference = getReadPreference ();
284
+ return Flux .fromIterable (entities ).map (entityInformation ::getRequiredId ).collectList ()
285
+ .flatMap (ids -> deleteAllById (ids , readPreference ));
281
286
}
282
287
283
288
@ Override
@@ -464,10 +469,10 @@ private Query getIdQuery(Iterable<? extends ID> ids) {
464
469
465
470
/**
466
471
* Transform the elements emitted by this Flux into Publishers, then flatten these inner publishers into a single
467
- * Flux. The operation does not allow interleave between performing the map operation for the first and second source
468
- * element guaranteeing the mapping operation completed before subscribing to its following inners, that will then be
469
- * subscribed to eagerly emitting elements in order of their source.
470
- *
472
+ * Flux. The operation does not allow interleaving between performing the map operation for the first and second
473
+ * source element guaranteeing the mapping operation completed before subscribing to its following inners, that will
474
+ * then be subscribed to eagerly emitting elements in order of their source.
475
+ *
471
476
* <pre class="code">
472
477
* Flux.just(first-element).flatMap(...)
473
478
* .concatWith(Flux.fromIterable(remaining-elements).flatMapSequential(...))
@@ -481,42 +486,54 @@ private Query getIdQuery(Iterable<? extends ID> ids) {
481
486
static <T > Flux <T > concatMapSequentially (List <T > source ,
482
487
Function <? super T , ? extends Publisher <? extends T >> mapper ) {
483
488
484
- if (source .isEmpty ()) {
485
- return Flux .empty ();
486
- }
487
- if (source .size () == 1 ) {
488
- return Flux .just (source .iterator ().next ()).flatMap (mapper );
489
- }
490
- if (source .size () == 2 ) {
491
- return Flux .fromIterable (source ).concatMap (mapper );
492
- }
489
+ return switch (source .size ()) {
490
+ case 0 -> Flux .empty ();
491
+ case 1 -> Flux .just (source .get (0 )).flatMap (mapper );
492
+ case 2 -> Flux .fromIterable (source ).concatMap (mapper );
493
+ default -> {
493
494
494
- Flux <T > first = Flux .just (source .get (0 )).flatMap (mapper );
495
- Flux <T > theRest = Flux .fromIterable (source .subList (1 , source .size ())).flatMapSequential (mapper );
496
- return first .concatWith (theRest );
495
+ Flux <T > first = Flux .just (source .get (0 )).flatMap (mapper );
496
+ Flux <T > theRest = Flux .fromIterable (source .subList (1 , source .size ())).flatMapSequential (mapper );
497
+ yield first .concatWith (theRest );
498
+ }
499
+ };
497
500
}
498
501
499
502
static <T > Flux <T > concatMapSequentially (Publisher <T > publisher ,
500
503
Function <? super T , ? extends Publisher <? extends T >> mapper ) {
501
504
502
- return Flux .from (publisher ).switchOnFirst ((( signal , source ) -> {
505
+ return Flux .from (publisher ).switchOnFirst ((signal , source ) -> {
503
506
504
507
if (!signal .hasValue ()) {
505
508
return source .concatMap (mapper );
506
509
}
507
510
508
511
Mono <T > firstCall = Mono .from (mapper .apply (signal .get ()));
509
512
return firstCall .concatWith (source .skip (1 ).flatMapSequential (mapper ));
510
- })) ;
513
+ });
511
514
}
512
515
513
516
private static <E > List <E > toList (Iterable <E > source ) {
514
- return source instanceof List <E > list ? list : new ArrayList <>(toCollection (source ));
517
+
518
+ Collection <E > collection = toCollection (source );
519
+
520
+ if (collection instanceof List <E > list ) {
521
+ return list ;
522
+ }
523
+
524
+ return new ArrayList <>(collection );
515
525
}
516
526
517
527
private static <E > Collection <E > toCollection (Iterable <E > source ) {
518
- return source instanceof Collection <E > collection ? collection
519
- : StreamUtils .createStreamFromIterator (source .iterator ()).collect (Collectors .toList ());
528
+
529
+ if (source instanceof Collection <E > collection ) {
530
+ return collection ;
531
+ }
532
+
533
+ List <E > list = new ArrayList <>();
534
+ source .forEach (list ::add );
535
+
536
+ return list ;
520
537
}
521
538
522
539
/**
0 commit comments