Skip to content

Commit 65f1a33

Browse files
committed
Updating the PR followup work
1 parent 0b54f86 commit 65f1a33

File tree

9 files changed

+73
-24
lines changed

9 files changed

+73
-24
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,5 @@ dependencies {
6161

6262
task wrapper(type: Wrapper) {
6363
gradleVersion = '4.0'
64-
distributionUrl = "http://services.gradle.org/distributions/gradle-4.0-all.zip"
64+
distributionUrl = "https://services.gradle.org/distributions/gradle-4.0-all.zip"
6565
}

gradle/publishing.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ apply plugin: 'com.jfrog.bintray'
44
publishing {
55
repositories {
66
maven {
7-
url 'http://dl.bintray.com/bbakerman/java-dataloader'
7+
url 'https://dl.bintray.com/bbakerman/java-dataloader'
88
}
99
}
1010
}
@@ -52,7 +52,7 @@ publishing {
5252
licenses {
5353
license {
5454
name 'The Apache Software License, Version 2.0'
55-
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
55+
url 'https://www.apache.org/licenses/LICENSE-2.0.txt'
5656
distribution 'repo'
5757
}
5858
}

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=http\://services.gradle.org/distributions/gradle-4.0-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-all.zip

src/main/java/org/dataloader/DataLoader.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ public CompletableFuture<List<V>> loadMany(List<K> keys, List<Object> keyContext
470470
* @return the promise of the queued load requests
471471
*/
472472
public CompletableFuture<List<V>> dispatch() {
473-
return helper.dispatch().futureList;
473+
return helper.dispatch().getPromisedResults();
474474
}
475475

476476
/**
@@ -480,9 +480,9 @@ public CompletableFuture<List<V>> dispatch() {
480480
* If batching is disabled, or there are no queued requests, then a succeeded promise with no entries dispatched is
481481
* returned.
482482
*
483-
* @return the promise of the queued load requests and the number of entries dispatched.
483+
* @return the promise of the queued load requests and the number of keys dispatched.
484484
*/
485-
public DataLoaderHelper.DispatchResult<V> dispatchWithCounts() {
485+
public DispatchResult<V> dispatchWithCounts() {
486486
return helper.dispatch();
487487
}
488488

src/main/java/org/dataloader/DataLoaderHelper.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,6 @@ Object getCacheKey(K key) {
134134
loaderOptions.cacheKeyFunction().get().getKey(key) : key;
135135
}
136136

137-
public static class DispatchResult<X> {
138-
public final CompletableFuture<List<X>> futureList;
139-
public final int totalEntriesHandled;
140-
public DispatchResult(CompletableFuture<List<X>> futureList, int totalEntriesHandled) {
141-
this.futureList = futureList;
142-
this.totalEntriesHandled = totalEntriesHandled;
143-
}
144-
}
145-
146137
DispatchResult<V> dispatch() {
147138
boolean batchingEnabled = loaderOptions.batchingEnabled();
148139
//

src/main/java/org/dataloader/DataLoaderRegistry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ public void dispatchAll() {
118118
*/
119119
public int dispatchAllWithCount() {
120120
int sum = 0;
121-
for (DataLoader dataLoader : getDataLoaders()) {
122-
sum += dataLoader.dispatchWithCounts().totalEntriesHandled;
121+
for (DataLoader<?,?> dataLoader : getDataLoaders()) {
122+
sum += dataLoader.dispatchWithCounts().getKeysCount();
123123
}
124124
return sum;
125125
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.dataloader;
2+
3+
import java.util.List;
4+
import java.util.concurrent.CompletableFuture;
5+
6+
/**
7+
* When a DataLoader is dispatched this object holds the promised results and also the count of key asked for
8+
* via methods like {@link org.dataloader.DataLoader#load(Object)} or {@link org.dataloader.DataLoader#loadMany(java.util.List)}
9+
*
10+
* @param <T> for two
11+
*/
12+
13+
public class DispatchResult<T> {
14+
private final CompletableFuture<List<T>> futureList;
15+
private final int keysCount;
16+
17+
public DispatchResult(CompletableFuture<List<T>> futureList, int keysCount) {
18+
this.futureList = futureList;
19+
this.keysCount = keysCount;
20+
}
21+
22+
public CompletableFuture<List<T>> getPromisedResults() {
23+
return futureList;
24+
}
25+
26+
public int getKeysCount() {
27+
return keysCount;
28+
}
29+
}

src/test/java/org/dataloader/DataLoaderRegistryTest.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package org.dataloader;
22

3-
import java.util.concurrent.CompletableFuture;
43
import org.dataloader.stats.Statistics;
54
import org.junit.Test;
65

6+
import java.util.concurrent.CompletableFuture;
7+
78
import static java.util.Arrays.asList;
89
import static org.hamcrest.Matchers.equalTo;
910
import static org.hamcrest.Matchers.hasItems;
@@ -131,4 +132,28 @@ public void computeIfAbsent_returns_an_existing_data_loader_if_there_was_a_value
131132
assertThat(registry.getDataLoaders(), hasItems(dlA));
132133
}
133134

135+
@Test
136+
public void dispatch_counts_are_maintained() {
137+
138+
DataLoaderRegistry registry = new DataLoaderRegistry();
139+
140+
DataLoader<Object, Object> dlA = new DataLoader<>(identityBatchLoader);
141+
DataLoader<Object, Object> dlB = new DataLoader<>(identityBatchLoader);
142+
143+
registry.register("a", dlA);
144+
registry.register("b", dlB);
145+
146+
dlA.load("av1");
147+
dlA.load("av2");
148+
dlB.load("bv1");
149+
dlB.load("bv2");
150+
151+
int dispatchDepth = registry.dispatchDepth();
152+
assertThat(dispatchDepth, equalTo(4));
153+
154+
int dispatchedCount = registry.dispatchAllWithCount();
155+
dispatchDepth = registry.dispatchDepth();
156+
assertThat(dispatchedCount, equalTo(4));
157+
assertThat(dispatchDepth, equalTo(0));
158+
}
134159
}

src/test/java/org/dataloader/DataLoaderTest.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,11 @@ public void should_Return_zero_entries_dispatched_when_no_keys_supplied() {
110110
assertThat(promisedValues.size(), is(0));
111111
success.set(true);
112112
});
113-
DataLoaderHelper.DispatchResult dispatchResult = identityLoader.dispatchWithCounts();
113+
DispatchResult dispatchResult = identityLoader.dispatchWithCounts();
114114
await().untilAtomic(success, is(true));
115-
assertThat(dispatchResult.totalEntriesHandled, equalTo(0));
115+
assertThat(dispatchResult.getKeysCount(), equalTo(0));
116116
}
117+
117118
@Test
118119
public void should_Batch_multiple_requests() throws ExecutionException, InterruptedException {
119120
List<Collection<Integer>> loadCalls = new ArrayList<>();
@@ -126,19 +127,22 @@ public void should_Batch_multiple_requests() throws ExecutionException, Interrup
126127
await().until(() -> future1.isDone() && future2.isDone());
127128
assertThat(future1.get(), equalTo(1));
128129
assertThat(future2.get(), equalTo(2));
129-
assertThat(loadCalls, equalTo(singletonList(asList(1, 2)))); }
130+
assertThat(loadCalls, equalTo(singletonList(asList(1, 2))));
131+
}
130132

131133
@Test
132134
public void should_Return_number_of_batched_entries() throws ExecutionException, InterruptedException {
133135
List<Collection<Integer>> loadCalls = new ArrayList<>();
134136
DataLoader<Integer, Integer> identityLoader = idLoader(new DataLoaderOptions(), loadCalls);
135137

136138
CompletableFuture<Integer> future1 = identityLoader.load(1);
139+
CompletableFuture<Integer> future1a = identityLoader.load(1);
137140
CompletableFuture<Integer> future2 = identityLoader.load(2);
138-
DataLoaderHelper.DispatchResult dispatchResult = identityLoader.dispatchWithCounts();
141+
DispatchResult<?> dispatchResult = identityLoader.dispatchWithCounts();
139142

140143
await().until(() -> future1.isDone() && future2.isDone());
141-
assertThat(dispatchResult.totalEntriesHandled, equalTo(2));
144+
assertThat(dispatchResult.getKeysCount(), equalTo(2)); // its two because its the number dispatched (by key) not the load calls
145+
assertThat(dispatchResult.getPromisedResults().isDone(), equalTo(true));
142146
}
143147

144148
@Test

0 commit comments

Comments
 (0)