Skip to content

Commit fa94732

Browse files
authored
Merge pull request #115 from samuelAndalon/feat/cache-map-get-values
feat: make cache map values accesible for read only purposes
2 parents 7bf46ea + b04dfa0 commit fa94732

File tree

7 files changed

+94
-2
lines changed

7 files changed

+94
-2
lines changed

src/main/java/org/dataloader/CacheMap.java

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.dataloader.annotations.PublicSpi;
2020
import org.dataloader.impl.DefaultCacheMap;
2121

22+
import java.util.Collection;
2223
import java.util.concurrent.CompletableFuture;
2324

2425
/**
@@ -73,6 +74,12 @@ static <K, V> CacheMap<K, V> simpleMap() {
7374
*/
7475
CompletableFuture<V> get(K key);
7576

77+
/**
78+
* Gets a collection of CompletableFutures from the cache map.
79+
* @return the collection of cached values
80+
*/
81+
Collection<CompletableFuture<V>> getAll();
82+
7683
/**
7784
* Creates a new cache map entry with the specified key and value, or updates the value if the key already exists.
7885
*

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,6 @@ public Duration getTimeSinceDispatch() {
452452
return Duration.between(helper.getLastDispatchTime(), helper.now());
453453
}
454454

455-
456455
/**
457456
* Requests to load the data with the specified key asynchronously, and returns a future of the resulting value.
458457
* <p>
@@ -752,4 +751,21 @@ public Statistics getStatistics() {
752751
return stats.getStatistics();
753752
}
754753

754+
/**
755+
* Gets the cacheMap associated with this data loader passed in via {@link DataLoaderOptions#cacheMap()}
756+
* @return the cacheMap of this data loader
757+
*/
758+
public CacheMap<Object, V> getCacheMap() {
759+
return futureCache;
760+
}
761+
762+
763+
/**
764+
* Gets the valueCache associated with this data loader passed in via {@link DataLoaderOptions#valueCache()}
765+
* @return the valueCache of this data loader
766+
*/
767+
public ValueCache<K, V> getValueCache() {
768+
return valueCache;
769+
}
770+
755771
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.dataloader.CacheMap;
2020
import org.dataloader.annotations.Internal;
2121

22+
import java.util.Collection;
2223
import java.util.HashMap;
2324
import java.util.Map;
2425
import java.util.concurrent.CompletableFuture;
@@ -60,6 +61,14 @@ public CompletableFuture<V> get(K key) {
6061
return cache.get(key);
6162
}
6263

64+
/**
65+
* {@inheritDoc}
66+
*/
67+
@Override
68+
public Collection<CompletableFuture<V>> getAll() {
69+
return cache.values();
70+
}
71+
6372
/**
6473
* {@inheritDoc}
6574
*/

src/test/java/ReadmeExamples.java

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.time.Duration;
1919
import java.util.ArrayList;
20+
import java.util.Collection;
2021
import java.util.List;
2122
import java.util.Map;
2223
import java.util.Set;
@@ -221,6 +222,11 @@ public CompletableFuture<Object> get(Object key) {
221222
return null;
222223
}
223224

225+
@Override
226+
public Collection<CompletableFuture<Object>> getAll() {
227+
return null;
228+
}
229+
224230
@Override
225231
public CacheMap set(Object key, CompletableFuture value) {
226232
return null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.dataloader;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.Collection;
7+
import java.util.List;
8+
import java.util.concurrent.CompletableFuture;
9+
10+
import static org.dataloader.DataLoaderFactory.newDataLoader;
11+
import static org.hamcrest.Matchers.equalTo;
12+
import static org.junit.Assert.assertThat;
13+
14+
/**
15+
* Tests for cacheMap functionality..
16+
*/
17+
public class DataLoaderCacheMapTest {
18+
19+
private <T> BatchLoader<T, T> keysAsValues() {
20+
return CompletableFuture::completedFuture;
21+
}
22+
23+
@Test
24+
public void should_provide_all_futures_from_cache() {
25+
DataLoader<Integer, Integer> dataLoader = newDataLoader(keysAsValues());
26+
27+
dataLoader.load(1);
28+
dataLoader.load(2);
29+
dataLoader.load(1);
30+
31+
Collection<CompletableFuture<Integer>> futures = dataLoader.getCacheMap().getAll();
32+
assertThat(futures.size(), equalTo(2));
33+
}
34+
35+
@Test
36+
public void should_access_to_future_dependants() {
37+
DataLoader<Integer, Integer> dataLoader = newDataLoader(keysAsValues());
38+
39+
dataLoader.load(1).handle((v, t) -> t);
40+
dataLoader.load(2).handle((v, t) -> t);
41+
dataLoader.load(1).handle((v, t) -> t);
42+
43+
Collection<CompletableFuture<Integer>> futures = dataLoader.getCacheMap().getAll();
44+
45+
List<CompletableFuture<Integer>> futuresList = new ArrayList<>(futures);
46+
assertThat(futuresList.get(0).getNumberOfDependents(), equalTo(2));
47+
assertThat(futuresList.get(1).getNumberOfDependents(), equalTo(1));
48+
}
49+
}

src/test/java/org/dataloader/DataLoaderIfPresentTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import static org.hamcrest.Matchers.sameInstance;
1212
import static org.junit.Assert.assertThat;
1313

14-
1514
/**
1615
* Tests for IfPresent and IfCompleted functionality.
1716
*/

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

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.dataloader.CacheMap;
44

5+
import java.util.Collection;
56
import java.util.LinkedHashMap;
67
import java.util.Map;
78
import java.util.concurrent.CompletableFuture;
@@ -24,6 +25,11 @@ public CompletableFuture<Object> get(String key) {
2425
return stash.get(key);
2526
}
2627

28+
@Override
29+
public Collection<CompletableFuture<Object>> getAll() {
30+
return stash.values();
31+
}
32+
2733
@Override
2834
public CacheMap<String, Object> set(String key, CompletableFuture<Object> value) {
2935
stash.put(key, value);

0 commit comments

Comments
 (0)