Skip to content

Commit efdb9a7

Browse files
committed
improved the set cache code - thenCompose was missing
1 parent d691f88 commit efdb9a7

File tree

1 file changed

+17
-34
lines changed

1 file changed

+17
-34
lines changed

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

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -362,21 +362,20 @@ CompletionStage<List<V>> invokeLoader(List<K> keys, List<Object> keyContexts, bo
362362
// and then fill in their values
363363
//
364364
CompletionStage<List<V>> batchLoad = invokeLoader(cacheMissedKeys, cacheMissedContexts);
365-
CompletionStage<CacheMissedData<K, V>> assembledValues = batchLoad.thenApply(batchedValues -> {
366-
assertResultSize(cacheMissedKeys, batchedValues);
365+
return batchLoad.thenCompose(missedValues -> {
366+
assertResultSize(cacheMissedKeys, missedValues);
367367

368-
for (int i = 0; i < batchedValues.size(); i++) {
368+
for (int i = 0; i < missedValues.size(); i++) {
369369
K missedKey = cacheMissedKeys.get(i);
370-
V v = batchedValues.get(i);
370+
V v = missedValues.get(i);
371371
valuesInKeyOrder.put(missedKey, v);
372372
}
373-
374-
return new CacheMissedData<>(cacheMissedKeys, batchedValues, new ArrayList<>(valuesInKeyOrder.values()));
373+
List<V> assembledValues = new ArrayList<>(valuesInKeyOrder.values());
374+
//
375+
// fire off a call to the ValueCache to allow it to set values into the
376+
// cache now that we have them
377+
return setToValueCache(assembledValues, cacheMissedKeys, missedValues);
375378
});
376-
//
377-
// fire off a call to the ValueCache to allow it to set values into the
378-
// cache now that we have them
379-
return setToValueCache(assembledValues);
380379
}
381380
});
382381
}
@@ -453,40 +452,24 @@ private CompletableFuture<List<Try<V>>> getFromValueCache(List<K> keys) {
453452
}
454453
}
455454

456-
static class CacheMissedData<K, V> {
457-
final List<K> missedKeys;
458-
final List<V> missedValues;
459-
final List<V> assembledValues;
460-
461-
CacheMissedData(List<K> missedKeys, List<V> missedValues, List<V> assembledValues) {
462-
this.missedKeys = missedKeys;
463-
this.missedValues = missedValues;
464-
this.assembledValues = assembledValues;
465-
}
466-
}
467-
468-
private CompletionStage<List<V>> setToValueCache(CompletionStage<CacheMissedData<K, V>> assembledValues) {
455+
private CompletionStage<List<V>> setToValueCache(List<V> assembledValues, List<K> missedKeys, List<V> missedValues) {
469456
try {
470457
boolean completeValueAfterCacheSet = loaderOptions.getValueCacheOptions().isCompleteValueAfterCacheSet();
471458
if (completeValueAfterCacheSet) {
472-
return assembledValues.thenCompose(cacheMissedData -> nonNull(valueCache
473-
.setValues(cacheMissedData.missedKeys, cacheMissedData.missedValues), () -> "Your ValueCache.setValues function MUST return a non null promise")
459+
return nonNull(valueCache
460+
.setValues(missedKeys, missedValues), () -> "Your ValueCache.setValues function MUST return a non null promise")
474461
// we dont trust the set cache to give us the values back - we have them - lets use them
475462
// if the cache set fails - then they wont be in cache and maybe next time they will
476-
.handle((ignored, setExIgnored) -> cacheMissedData.assembledValues));
463+
.handle((ignored, setExIgnored) -> assembledValues);
477464
} else {
478-
return assembledValues.thenApply(cacheMissedData -> {
479-
// no one is waiting for the set to happen here so if its truly async
480-
// it will happen eventually but no result will be dependant on it
481-
valueCache.setValues(cacheMissedData.missedKeys, cacheMissedData.missedValues);
482-
return cacheMissedData.assembledValues;
483-
});
465+
// no one is waiting for the set to happen here so if its truly async
466+
// it will happen eventually but no result will be dependant on it
467+
valueCache.setValues(missedKeys, missedValues);
484468
}
485469
} catch (RuntimeException ignored) {
486470
// if we cant set values back into the cache - so be it - this must be a faulty
487471
// ValueCache implementation
488-
return assembledValues.thenApply(cacheMissedData -> cacheMissedData.assembledValues);
489472
}
473+
return CompletableFuture.completedFuture(assembledValues);
490474
}
491-
492475
}

0 commit comments

Comments
 (0)