diff --git a/src/main/java/org/dataloader/CacheMap.java b/src/main/java/org/dataloader/CacheMap.java index 0db31b8..6d27a47 100644 --- a/src/main/java/org/dataloader/CacheMap.java +++ b/src/main/java/org/dataloader/CacheMap.java @@ -19,6 +19,7 @@ import org.dataloader.annotations.PublicSpi; import org.dataloader.impl.DefaultCacheMap; +import java.util.Collection; import java.util.concurrent.CompletableFuture; /** @@ -73,6 +74,12 @@ static CacheMap simpleMap() { */ CompletableFuture get(K key); + /** + * Gets a collection of CompletableFutures from the cache map. + * @return the collection of cached values + */ + Collection> getAll(); + /** * Creates a new cache map entry with the specified key and value, or updates the value if the key already exists. * diff --git a/src/main/java/org/dataloader/DataLoader.java b/src/main/java/org/dataloader/DataLoader.java index 05abf42..e800a55 100644 --- a/src/main/java/org/dataloader/DataLoader.java +++ b/src/main/java/org/dataloader/DataLoader.java @@ -452,7 +452,6 @@ public Duration getTimeSinceDispatch() { return Duration.between(helper.getLastDispatchTime(), helper.now()); } - /** * Requests to load the data with the specified key asynchronously, and returns a future of the resulting value. *

@@ -752,4 +751,21 @@ public Statistics getStatistics() { return stats.getStatistics(); } + /** + * Gets the cacheMap associated with this data loader passed in via {@link DataLoaderOptions#cacheMap()} + * @return the cacheMap of this data loader + */ + public CacheMap getCacheMap() { + return futureCache; + } + + + /** + * Gets the valueCache associated with this data loader passed in via {@link DataLoaderOptions#valueCache()} + * @return the valueCache of this data loader + */ + public ValueCache getValueCache() { + return valueCache; + } + } diff --git a/src/main/java/org/dataloader/impl/DefaultCacheMap.java b/src/main/java/org/dataloader/impl/DefaultCacheMap.java index 9346ad8..fa89bb0 100644 --- a/src/main/java/org/dataloader/impl/DefaultCacheMap.java +++ b/src/main/java/org/dataloader/impl/DefaultCacheMap.java @@ -19,6 +19,7 @@ import org.dataloader.CacheMap; import org.dataloader.annotations.Internal; +import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.concurrent.CompletableFuture; @@ -60,6 +61,14 @@ public CompletableFuture get(K key) { return cache.get(key); } + /** + * {@inheritDoc} + */ + @Override + public Collection> getAll() { + return cache.values(); + } + /** * {@inheritDoc} */ diff --git a/src/test/java/ReadmeExamples.java b/src/test/java/ReadmeExamples.java index 33b1607..e37550e 100644 --- a/src/test/java/ReadmeExamples.java +++ b/src/test/java/ReadmeExamples.java @@ -17,6 +17,7 @@ import java.time.Duration; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; @@ -221,6 +222,11 @@ public CompletableFuture get(Object key) { return null; } + @Override + public Collection> getAll() { + return null; + } + @Override public CacheMap set(Object key, CompletableFuture value) { return null; diff --git a/src/test/java/org/dataloader/DataLoaderCacheMapTest.java b/src/test/java/org/dataloader/DataLoaderCacheMapTest.java new file mode 100644 index 0000000..abfc8d3 --- /dev/null +++ b/src/test/java/org/dataloader/DataLoaderCacheMapTest.java @@ -0,0 +1,49 @@ +package org.dataloader; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +import static org.dataloader.DataLoaderFactory.newDataLoader; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +/** + * Tests for cacheMap functionality.. + */ +public class DataLoaderCacheMapTest { + + private BatchLoader keysAsValues() { + return CompletableFuture::completedFuture; + } + + @Test + public void should_provide_all_futures_from_cache() { + DataLoader dataLoader = newDataLoader(keysAsValues()); + + dataLoader.load(1); + dataLoader.load(2); + dataLoader.load(1); + + Collection> futures = dataLoader.getCacheMap().getAll(); + assertThat(futures.size(), equalTo(2)); + } + + @Test + public void should_access_to_future_dependants() { + DataLoader dataLoader = newDataLoader(keysAsValues()); + + dataLoader.load(1).handle((v, t) -> t); + dataLoader.load(2).handle((v, t) -> t); + dataLoader.load(1).handle((v, t) -> t); + + Collection> futures = dataLoader.getCacheMap().getAll(); + + List> futuresList = new ArrayList<>(futures); + assertThat(futuresList.get(0).getNumberOfDependents(), equalTo(2)); + assertThat(futuresList.get(1).getNumberOfDependents(), equalTo(1)); + } +} diff --git a/src/test/java/org/dataloader/DataLoaderIfPresentTest.java b/src/test/java/org/dataloader/DataLoaderIfPresentTest.java index 5e29d54..916fdef 100644 --- a/src/test/java/org/dataloader/DataLoaderIfPresentTest.java +++ b/src/test/java/org/dataloader/DataLoaderIfPresentTest.java @@ -11,7 +11,6 @@ import static org.hamcrest.Matchers.sameInstance; import static org.junit.Assert.assertThat; - /** * Tests for IfPresent and IfCompleted functionality. */ diff --git a/src/test/java/org/dataloader/fixtures/CustomCacheMap.java b/src/test/java/org/dataloader/fixtures/CustomCacheMap.java index 51e6687..695da5e 100644 --- a/src/test/java/org/dataloader/fixtures/CustomCacheMap.java +++ b/src/test/java/org/dataloader/fixtures/CustomCacheMap.java @@ -2,6 +2,7 @@ import org.dataloader.CacheMap; +import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; import java.util.concurrent.CompletableFuture; @@ -24,6 +25,11 @@ public CompletableFuture get(String key) { return stash.get(key); } + @Override + public Collection> getAll() { + return stash.values(); + } + @Override public CacheMap set(String key, CompletableFuture value) { stash.put(key, value);