You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -88,6 +86,7 @@ You can then use it to load values which will be `CompleteableFuture` promises t
88
86
89
87
or you can use it to compose future computations as follows. The key requirement is that you call
90
88
`dataloader.dispatch()` or its variant `dataloader.dispatchAndJoin()` at some point in order to make the underlying calls happen to the batch loader.
89
+
91
90
In this version of data loader, this does not happen automatically. More on this in [Manual dispatching](#manual-dispatching) .
92
91
93
92
```java
@@ -126,12 +125,47 @@ maintain minimal outgoing data requests.
126
125
127
126
In the example above, the first call to dispatch will cause the batched user keys (1 and 2) to be fired at the BatchLoader function to load 2 users.
128
127
129
-
Since each `thenAccept` callback made more calls to `userLoader` to get the "user they they invited", another 2 user keys are fired at the BatchLoader function for them.
128
+
Since each `thenAccept` callback made more calls to `userLoader` to get the "user they they invited", another 2 user keys are given at the `BatchLoader`
129
+
function for them.
130
130
131
-
In this case the `userLoader.dispatchAndJoin()` is used to fire a dispatch call, wait for it (aka join it), see if the data loader has more batched entries, (which is does)
132
-
and then it repeats this until the data loader internal queue of keys is empty. At this point we have made 2 batched calls instead of the niave 4 calls we might have made if
131
+
In this case the `userLoader.dispatchAndJoin()` is used to make a dispatch call, wait for it (aka join it), see if the data loader has more batched entries, (which is does)
132
+
and then it repeats this until the data loader internal queue of keys is empty. At this point we have made 2 batched calls instead of the naive 4 calls we might have made if
133
133
we did not "batch" the calls to load data.
134
134
135
+
## Batching requires batch backing APIs
136
+
137
+
You will notice in our BatchLoader example that the backing service had the ability to get a list of users give a list of user ids in one call.
Copy file name to clipboardExpand all lines: src/main/java/org/dataloader/BatchLoader.java
+20-7Lines changed: 20 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -17,18 +17,27 @@
17
17
packageorg.dataloader;
18
18
19
19
importjava.util.List;
20
+
importjava.util.concurrent.CompletionStage;
20
21
21
22
/**
22
23
* A function that is invoked for batch loading a list of data values indicated by the provided list of keys. The
23
-
* function returns a {@link PromisedValues} to aggregate results of individual load requests.
24
+
* function returns a promise of a list of results of individual load requests.
24
25
*
25
26
* There are a few constraints that must be upheld:
26
27
* <ul>
27
28
* <li>The list of values must be the same size as the list of keys.</li>
28
29
* <li>Each index in the list of values must correspond to the same index in the list of keys.</li>
29
30
* </ul>
30
31
*
31
-
* For example, if your batch function was provided the list of keys: [ 2, 9, 6, 1 ], and loading from a back-end service returned the values:
32
+
* For example, if your batch function was provided the list of keys:
33
+
*
34
+
* <pre>
35
+
* [
36
+
* 2, 9, 6, 1
37
+
* ]
38
+
* </pre>
39
+
*
40
+
* and loading from a back-end service returned this list of values:
32
41
*
33
42
* <pre>
34
43
* [
@@ -38,10 +47,13 @@
38
47
* ]
39
48
* </pre>
40
49
*
41
-
* The back-end service returned results in a different order than we requested, likely because it was more efficient for it to do so. Also, it omitted a result for key 6, which we can interpret as no value
42
-
* existing for that key.
50
+
* then the batch loader function contract has been broken.
51
+
*
52
+
* The back-end service returned results in a different order than we requested, likely because it was more efficient for it to
53
+
* do so. Also, it omitted a result for key 6, which we may interpret as no value existing for that key.
43
54
*
44
-
* To uphold the constraints of the batch function, it must return an List of values the same length as the List of keys, and re-order them to ensure each index aligns with the original keys [ 2, 9, 6, 1 ]:
55
+
* To uphold the constraints of the batch function, it must return an List of values the same length as
56
+
* the List of keys, and re-order them to ensure each index aligns with the original keys [ 2, 9, 6, 1 ]:
45
57
*
46
58
* <pre>
47
59
* [
@@ -56,16 +68,17 @@
56
68
* @param <V> type parameter indicating the type of values returned
0 commit comments