Skip to content

Commit ccbf15a

Browse files
committed
PR feedback - removed containsKey, updated a few method names and updated doc
1 parent d651b94 commit ccbf15a

File tree

7 files changed

+17
-97
lines changed

7 files changed

+17
-97
lines changed

src/main/java/org/dataloader/CachedValueStore.java

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.dataloader.annotations.PublicSpi;
44
import org.dataloader.impl.NoOpCachedValueStore;
55

6-
import java.util.List;
76
import java.util.concurrent.CompletableFuture;
87

98
/**
@@ -33,36 +32,21 @@ public interface CachedValueStore<K, V> {
3332
*
3433
* @return the cache store
3534
*/
36-
static <K, V> CachedValueStore<K, V> defaultStore() {
35+
static <K, V> CachedValueStore<K, V> defaultCachedValueStore() {
3736
//noinspection unchecked
3837
return (CachedValueStore<K, V>) NoOpCachedValueStore.NOOP;
3938
}
4039

4140
/**
42-
* Checks whether the specified key is contained in the store.
43-
* <p>
44-
* {@link DataLoader} first calls {@link #containsKey(Object)} and then calls {@link #get(Object)}. If the
45-
* backing cache implementation cannot answer the `containsKey` call then simply return true and the
46-
* following `get` call can complete exceptionally to cause the {@link DataLoader}
47-
* to enqueue the key to the {@link BatchLoader#load(List)} call since it is not present in cache.
48-
*
49-
* @param key the key to check if its present in the cache
50-
*
51-
* @return {@code true} if the cache contains the key, {@code false} otherwise
52-
*/
53-
CompletableFuture<Boolean> containsKey(K key);
54-
55-
/**
56-
* Gets the specified key from the store.
41+
* Gets the specified key from the store. if the key si not present, then the implementation MUST return an exceptionally completed future
42+
* and not null because null is a valid cacheable value. Any exception is will cause {@link DataLoader} to load the key via batch loading
43+
* instead.
5744
*
5845
* @param key the key to retrieve
5946
*
60-
* @return a future containing the cached value (which maybe null) or an exception if the key does
47+
* @return a future containing the cached value (which maybe null) or exceptionally completed future if the key does
6148
* not exist in the cache.
6249
*
63-
* IMPORTANT: The future may fail if the key does not exist depending on implementation. Implementations should
64-
* return an exceptional future if the key is not present in the cache, not null which is a valid value
65-
* for a key.
6650
*/
6751
CompletableFuture<V> get(K key);
6852

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,8 @@ public DataLoader(BatchLoader<K, V> batchLoadFunction, DataLoaderOptions options
415415
@VisibleForTesting
416416
DataLoader(Object batchLoadFunction, DataLoaderOptions options, Clock clock) {
417417
DataLoaderOptions loaderOptions = options == null ? new DataLoaderOptions() : options;
418-
this.futureCache = determineCacheMap(loaderOptions);
419-
this.cachedValueStore = determineCacheStore(loaderOptions);
418+
this.futureCache = determineFutureCache(loaderOptions);
419+
this.cachedValueStore = determineCachedValueStore(loaderOptions);
420420
// order of keys matter in data loader
421421
this.stats = nonNull(loaderOptions.getStatisticsCollector());
422422

@@ -425,13 +425,13 @@ public DataLoader(BatchLoader<K, V> batchLoadFunction, DataLoaderOptions options
425425

426426

427427
@SuppressWarnings("unchecked")
428-
private CacheMap<Object, V> determineCacheMap(DataLoaderOptions loaderOptions) {
428+
private CacheMap<Object, V> determineFutureCache(DataLoaderOptions loaderOptions) {
429429
return (CacheMap<Object, V>) loaderOptions.cacheMap().orElseGet(CacheMap::simpleMap);
430430
}
431431

432432
@SuppressWarnings("unchecked")
433-
private CachedValueStore<Object, V> determineCacheStore(DataLoaderOptions loaderOptions) {
434-
return (CachedValueStore<Object, V>) loaderOptions.cachedValueStore().orElseGet(CachedValueStore::defaultStore);
433+
private CachedValueStore<Object, V> determineCachedValueStore(DataLoaderOptions loaderOptions) {
434+
return (CachedValueStore<Object, V>) loaderOptions.cachedValueStore().orElseGet(CachedValueStore::defaultCachedValueStore);
435435
}
436436

437437
/**

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -307,17 +307,9 @@ private CompletableFuture<V> loadFromCache(K key, Object loadContext, boolean ba
307307
*/
308308
final CompletableFuture<V> future = new CompletableFuture<>();
309309

310-
cachedValueStore.containsKey(cacheKey).whenComplete((hasKey, containsCallEx) -> {
311-
boolean containsKey = containsCallEx == null && Boolean.TRUE.equals(hasKey);
312-
if (containsKey) {
313-
cachedValueStore.get(cacheKey).whenComplete((cachedValue, getCallEx) -> {
314-
if (getCallEx == null) {
315-
future.complete(cachedValue);
316-
} else {
317-
queueOrInvokeLoader(key, loadContext, batchingEnabled)
318-
.whenComplete(setValueIntoCacheAndCompleteFuture(cacheKey, future));
319-
}
320-
});
310+
cachedValueStore.get(cacheKey).whenComplete((cachedValue, getCallEx) -> {
311+
if (getCallEx == null) {
312+
future.complete(cachedValue);
321313
} else {
322314
queueOrInvokeLoader(key, loadContext, batchingEnabled)
323315
.whenComplete(setValueIntoCacheAndCompleteFuture(cacheKey, future));

src/main/java/org/dataloader/impl/NoOpCachedValueStore.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@ public class NoOpCachedValueStore<K, V> implements CachedValueStore<K, V> {
2222

2323
public static NoOpCachedValueStore<?, ?> NOOP = new NoOpCachedValueStore<>();
2424

25-
/**
26-
* {@inheritDoc}
27-
*/
28-
@Override
29-
public CompletableFuture<Boolean> containsKey(K key) {
30-
return CompletableFuture.completedFuture(false);
31-
}
32-
3325
/**
3426
* {@inheritDoc}
3527
*/

src/test/java/org/dataloader/DataLoaderCachedValueStoreTest.java

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -145,47 +145,6 @@ public void can_use_caffeine_for_caching() {
145145
assertArrayEquals(caffeineCache.asMap().keySet().toArray(), asList("a", "b", "c").toArray());
146146
}
147147

148-
@Test
149-
public void will_invoke_loader_if_CACHE_CONTAINS_call_throws_exception() {
150-
CustomCachedValueStore customStore = new CustomCachedValueStore() {
151-
@Override
152-
public CompletableFuture<Boolean> containsKey(String key) {
153-
if (key.equals("a")) {
154-
return failedFuture(new IllegalStateException("no A"));
155-
}
156-
if (key.equals("c")) {
157-
return completedFuture(false);
158-
}
159-
return super.containsKey(key);
160-
}
161-
};
162-
customStore.set("a", "Not From Cache A");
163-
customStore.set("b", "From Cache");
164-
customStore.set("c", "Not From Cache C");
165-
166-
List<List<String>> loadCalls = new ArrayList<>();
167-
DataLoaderOptions options = newOptions().setCachedValueStore(customStore);
168-
DataLoader<String, String> identityLoader = idLoader(options, loadCalls);
169-
170-
// Fetches as expected
171-
172-
CompletableFuture<String> fA = identityLoader.load("a");
173-
CompletableFuture<String> fB = identityLoader.load("b");
174-
CompletableFuture<String> fC = identityLoader.load("c");
175-
176-
await().until(identityLoader.dispatch()::isDone);
177-
assertThat(fA.join(), equalTo("a"));
178-
assertThat(fB.join(), equalTo("From Cache"));
179-
assertThat(fC.join(), equalTo("c"));
180-
181-
// a was not in cache (according to containsKey) and hence needed to be loaded
182-
assertThat(loadCalls, equalTo(singletonList(asList("a", "c"))));
183-
184-
// the failed containsKey calls will be SET back into the value cache after batch loading
185-
assertArrayEquals(customStore.store.keySet().toArray(), asList("a", "b", "c").toArray());
186-
assertArrayEquals(customStore.store.values().toArray(), asList("a", "From Cache", "c").toArray());
187-
}
188-
189148
@Test
190149
public void will_invoke_loader_if_CACHE_GET_call_throws_exception() {
191150
CustomCachedValueStore customStore = new CustomCachedValueStore() {

src/test/java/org/dataloader/fixtures/CaffeineCachedValueStore.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ public CaffeineCachedValueStore(Cache<String, Object> cache) {
1515
this.cache = cache;
1616
}
1717

18-
@Override
19-
public CompletableFuture<Boolean> containsKey(String key) {
20-
// caffeine cant answer this question efficiently so we rely on
21-
return CompletableFuture.completedFuture(true);
22-
}
23-
2418
@Override
2519
public CompletableFuture<Object> get(String key) {
2620
Object value = cache.getIfPresent(key);

src/test/java/org/dataloader/fixtures/CustomCachedValueStore.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import org.dataloader.CachedValueStore;
5+
import org.dataloader.impl.CompletableFutureKit;
56

67
import java.util.Map;
78
import java.util.concurrent.CompletableFuture;
@@ -11,13 +12,11 @@ public class CustomCachedValueStore implements CachedValueStore<String, Object>
1112

1213
public final Map<String, Object> store = new ConcurrentHashMap<>();
1314

14-
@Override
15-
public CompletableFuture<Boolean> containsKey(String key) {
16-
return CompletableFuture.completedFuture(store.containsKey(key));
17-
}
18-
1915
@Override
2016
public CompletableFuture<Object> get(String key) {
17+
if (!store.containsKey(key)) {
18+
return CompletableFutureKit.failedFuture(new RuntimeException("The key is missing"));
19+
}
2120
return CompletableFuture.completedFuture(store.get(key));
2221
}
2322

0 commit comments

Comments
 (0)