10
10
import org .dataloader .stats .context .IncrementCacheHitCountStatisticsContext ;
11
11
import org .dataloader .stats .context .IncrementLoadCountStatisticsContext ;
12
12
import org .dataloader .stats .context .IncrementLoadErrorCountStatisticsContext ;
13
+ import org .reactivestreams .Subscriber ;
14
+ import org .reactivestreams .Subscription ;
13
15
14
16
import java .time .Clock ;
15
17
import java .time .Instant ;
@@ -246,7 +248,7 @@ private CompletableFuture<List<V>> dispatchQueueBatch(List<K> keys, List<Object>
246
248
return batchLoad
247
249
.thenApply (values -> {
248
250
assertResultSize (keys , values );
249
- if (isObserverLoader () || isMapObserverLoader ()) {
251
+ if (isPublisherLoader () || isMappedPublisherLoader ()) {
250
252
// We have already completed the queued futures by the time the overall batchLoad future has completed.
251
253
return values ;
252
254
}
@@ -428,10 +430,10 @@ CompletableFuture<List<V>> invokeLoader(List<K> keys, List<Object> keyContexts,
428
430
.context (context ).keyContexts (keys , keyContexts ).build ();
429
431
if (isMapLoader ()) {
430
432
batchLoad = invokeMapBatchLoader (keys , environment );
431
- } else if (isObserverLoader ()) {
432
- batchLoad = invokeObserverBatchLoader (keys , keyContexts , queuedFutures , environment );
433
- } else if (isMapObserverLoader ()) {
434
- batchLoad = invokeMappedObserverBatchLoader (keys , keyContexts , queuedFutures , environment );
433
+ } else if (isPublisherLoader ()) {
434
+ batchLoad = invokePublisherBatchLoader (keys , keyContexts , queuedFutures , environment );
435
+ } else if (isMappedPublisherLoader ()) {
436
+ batchLoad = invokeMappedPublisherBatchLoader (keys , keyContexts , queuedFutures , environment );
435
437
} else {
436
438
batchLoad = invokeListBatchLoader (keys , environment );
437
439
}
@@ -503,46 +505,46 @@ private CompletableFuture<List<V>> invokeMapBatchLoader(List<K> keys, BatchLoade
503
505
});
504
506
}
505
507
506
- private CompletableFuture <List <V >> invokeObserverBatchLoader (List <K > keys , List <Object > keyContexts , List <CompletableFuture <V >> queuedFutures , BatchLoaderEnvironment environment ) {
508
+ private CompletableFuture <List <V >> invokePublisherBatchLoader (List <K > keys , List <Object > keyContexts , List <CompletableFuture <V >> queuedFutures , BatchLoaderEnvironment environment ) {
507
509
CompletableFuture <List <V >> loadResult = new CompletableFuture <>();
508
- BatchObserver <V > observer = new BatchObserverImpl (loadResult , keys , keyContexts , queuedFutures );
510
+ Subscriber <V > subscriber = new DataLoaderSubscriber (loadResult , keys , keyContexts , queuedFutures );
509
511
510
512
BatchLoaderScheduler batchLoaderScheduler = loaderOptions .getBatchLoaderScheduler ();
511
- if (batchLoadFunction instanceof ObserverBatchLoaderWithContext ) {
512
- ObserverBatchLoaderWithContext <K , V > loadFunction = (ObserverBatchLoaderWithContext <K , V >) batchLoadFunction ;
513
+ if (batchLoadFunction instanceof PublisherBatchLoaderWithContext ) {
514
+ PublisherBatchLoaderWithContext <K , V > loadFunction = (PublisherBatchLoaderWithContext <K , V >) batchLoadFunction ;
513
515
if (batchLoaderScheduler != null ) {
514
- BatchLoaderScheduler .ScheduledObserverBatchLoaderCall loadCall = () -> loadFunction .load (keys , observer , environment );
516
+ BatchLoaderScheduler .ScheduledObserverBatchLoaderCall loadCall = () -> loadFunction .load (keys , subscriber , environment );
515
517
batchLoaderScheduler .scheduleObserverBatchLoader (loadCall , keys , environment );
516
518
} else {
517
- loadFunction .load (keys , observer , environment );
519
+ loadFunction .load (keys , subscriber , environment );
518
520
}
519
521
} else {
520
- ObserverBatchLoader <K , V > loadFunction = (ObserverBatchLoader <K , V >) batchLoadFunction ;
522
+ PublisherBatchLoader <K , V > loadFunction = (PublisherBatchLoader <K , V >) batchLoadFunction ;
521
523
if (batchLoaderScheduler != null ) {
522
- BatchLoaderScheduler .ScheduledObserverBatchLoaderCall loadCall = () -> loadFunction .load (keys , observer );
524
+ BatchLoaderScheduler .ScheduledObserverBatchLoaderCall loadCall = () -> loadFunction .load (keys , subscriber );
523
525
batchLoaderScheduler .scheduleObserverBatchLoader (loadCall , keys , null );
524
526
} else {
525
- loadFunction .load (keys , observer );
527
+ loadFunction .load (keys , subscriber );
526
528
}
527
529
}
528
530
return loadResult ;
529
531
}
530
532
531
- private CompletableFuture <List <V >> invokeMappedObserverBatchLoader (List <K > keys , List <Object > keyContexts , List <CompletableFuture <V >> queuedFutures , BatchLoaderEnvironment environment ) {
533
+ private CompletableFuture <List <V >> invokeMappedPublisherBatchLoader (List <K > keys , List <Object > keyContexts , List <CompletableFuture <V >> queuedFutures , BatchLoaderEnvironment environment ) {
532
534
CompletableFuture <List <V >> loadResult = new CompletableFuture <>();
533
- MappedBatchObserver < K , V > observer = new MappedBatchObserverImpl (loadResult , keys , keyContexts , queuedFutures );
535
+ Subscriber < Map . Entry < K , V >> observer = new DataLoaderMapEntrySubscriber (loadResult , keys , keyContexts , queuedFutures );
534
536
535
537
BatchLoaderScheduler batchLoaderScheduler = loaderOptions .getBatchLoaderScheduler ();
536
- if (batchLoadFunction instanceof MappedObserverBatchLoaderWithContext ) {
537
- MappedObserverBatchLoaderWithContext <K , V > loadFunction = (MappedObserverBatchLoaderWithContext <K , V >) batchLoadFunction ;
538
+ if (batchLoadFunction instanceof MappedPublisherBatchLoaderWithContext ) {
539
+ MappedPublisherBatchLoaderWithContext <K , V > loadFunction = (MappedPublisherBatchLoaderWithContext <K , V >) batchLoadFunction ;
538
540
if (batchLoaderScheduler != null ) {
539
541
BatchLoaderScheduler .ScheduledObserverBatchLoaderCall loadCall = () -> loadFunction .load (keys , observer , environment );
540
542
batchLoaderScheduler .scheduleObserverBatchLoader (loadCall , keys , environment );
541
543
} else {
542
544
loadFunction .load (keys , observer , environment );
543
545
}
544
546
} else {
545
- MappedObserverBatchLoader <K , V > loadFunction = (MappedObserverBatchLoader <K , V >) batchLoadFunction ;
547
+ MappedPublisherBatchLoader <K , V > loadFunction = (MappedPublisherBatchLoader <K , V >) batchLoadFunction ;
546
548
if (batchLoaderScheduler != null ) {
547
549
BatchLoaderScheduler .ScheduledObserverBatchLoaderCall loadCall = () -> loadFunction .load (keys , observer );
548
550
batchLoaderScheduler .scheduleObserverBatchLoader (loadCall , keys , null );
@@ -557,12 +559,12 @@ private boolean isMapLoader() {
557
559
return batchLoadFunction instanceof MappedBatchLoader || batchLoadFunction instanceof MappedBatchLoaderWithContext ;
558
560
}
559
561
560
- private boolean isObserverLoader () {
561
- return batchLoadFunction instanceof ObserverBatchLoader ;
562
+ private boolean isPublisherLoader () {
563
+ return batchLoadFunction instanceof PublisherBatchLoader ;
562
564
}
563
565
564
- private boolean isMapObserverLoader () {
565
- return batchLoadFunction instanceof MappedObserverBatchLoader ;
566
+ private boolean isMappedPublisherLoader () {
567
+ return batchLoadFunction instanceof MappedPublisherBatchLoader ;
566
568
}
567
569
568
570
int dispatchDepth () {
@@ -616,7 +618,8 @@ private static <T> DispatchResult<T> emptyDispatchResult() {
616
618
return (DispatchResult <T >) EMPTY_DISPATCH_RESULT ;
617
619
}
618
620
619
- private class BatchObserverImpl implements BatchObserver <V > {
621
+ private class DataLoaderSubscriber implements Subscriber <V > {
622
+
620
623
private final CompletableFuture <List <V >> valuesFuture ;
621
624
private final List <K > keys ;
622
625
private final List <Object > callContexts ;
@@ -628,7 +631,7 @@ private class BatchObserverImpl implements BatchObserver<V> {
628
631
private boolean onErrorCalled = false ;
629
632
private boolean onCompletedCalled = false ;
630
633
631
- private BatchObserverImpl (
634
+ private DataLoaderSubscriber (
632
635
CompletableFuture <List <V >> valuesFuture ,
633
636
List <K > keys ,
634
637
List <Object > callContexts ,
@@ -640,6 +643,11 @@ private BatchObserverImpl(
640
643
this .queuedFutures = queuedFutures ;
641
644
}
642
645
646
+ @ Override
647
+ public void onSubscribe (Subscription subscription ) {
648
+ subscription .request (keys .size ());
649
+ }
650
+
643
651
@ Override
644
652
public void onNext (V value ) {
645
653
assert !onErrorCalled && !onCompletedCalled ;
@@ -671,7 +679,7 @@ public void onNext(V value) {
671
679
}
672
680
673
681
@ Override
674
- public void onCompleted () {
682
+ public void onComplete () {
675
683
assert !onErrorCalled ;
676
684
onCompletedCalled = true ;
677
685
@@ -701,7 +709,7 @@ public void onError(Throwable ex) {
701
709
}
702
710
}
703
711
704
- private class MappedBatchObserverImpl implements MappedBatchObserver < K , V > {
712
+ private class DataLoaderMapEntrySubscriber implements Subscriber < Map . Entry < K , V > > {
705
713
private final CompletableFuture <List <V >> valuesFuture ;
706
714
private final List <K > keys ;
707
715
private final List <Object > callContexts ;
@@ -714,7 +722,7 @@ private class MappedBatchObserverImpl implements MappedBatchObserver<K, V> {
714
722
private boolean onErrorCalled = false ;
715
723
private boolean onCompletedCalled = false ;
716
724
717
- private MappedBatchObserverImpl (
725
+ private DataLoaderMapEntrySubscriber (
718
726
CompletableFuture <List <V >> valuesFuture ,
719
727
List <K > keys ,
720
728
List <Object > callContexts ,
@@ -737,8 +745,15 @@ private MappedBatchObserverImpl(
737
745
}
738
746
739
747
@ Override
740
- public void onNext (K key , V value ) {
748
+ public void onSubscribe (Subscription subscription ) {
749
+ subscription .request (keys .size ());
750
+ }
751
+
752
+ @ Override
753
+ public void onNext (Map .Entry <K , V > entry ) {
741
754
assert !onErrorCalled && !onCompletedCalled ;
755
+ K key = entry .getKey ();
756
+ V value = entry .getValue ();
742
757
743
758
Object callContext = callContextByKey .get (key );
744
759
CompletableFuture <V > future = queuedFutureByKey .get (key );
@@ -765,7 +780,7 @@ public void onNext(K key, V value) {
765
780
}
766
781
767
782
@ Override
768
- public void onCompleted () {
783
+ public void onComplete () {
769
784
assert !onErrorCalled ;
770
785
onCompletedCalled = true ;
771
786
0 commit comments