Skip to content

Commit 90f8364

Browse files
author
samvazquez
committed
feat: get all read-only values from cacheMap
1 parent 7bf46ea commit 90f8364

File tree

8 files changed

+101
-18
lines changed

8 files changed

+101
-18
lines changed

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

Lines changed: 7 additions & 0 deletions
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 of 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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@
2525
import java.time.Clock;
2626
import java.time.Duration;
2727
import java.time.Instant;
28-
import java.util.ArrayList;
29-
import java.util.Collections;
30-
import java.util.List;
31-
import java.util.Optional;
28+
import java.util.*;
3229
import java.util.concurrent.CompletableFuture;
3330
import java.util.function.BiConsumer;
3431

@@ -452,6 +449,13 @@ public Duration getTimeSinceDispatch() {
452449
return Duration.between(helper.getLastDispatchTime(), helper.now());
453450
}
454451

452+
/**
453+
* This returns a read-only collection of CompletableFutures of the cache map.
454+
* @return read-only collection of CompletableFutures
455+
*/
456+
public Collection<CompletableFuture<V>> getCacheFutures() {
457+
return helper.getCacheFutures();
458+
}
455459

456460
/**
457461
* Requests to load the data with the specified key asynchronously, and returns a future of the resulting value.

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,7 @@
77

88
import java.time.Clock;
99
import java.time.Instant;
10-
import java.util.ArrayList;
11-
import java.util.Collection;
12-
import java.util.LinkedHashSet;
13-
import java.util.List;
14-
import java.util.Map;
15-
import java.util.Optional;
16-
import java.util.Set;
10+
import java.util.*;
1711
import java.util.concurrent.CompletableFuture;
1812
import java.util.concurrent.CompletionException;
1913
import java.util.concurrent.CompletionStage;
@@ -99,6 +93,10 @@ public Instant getLastDispatchTime() {
9993
return lastDispatchTime.get();
10094
}
10195

96+
public Collection<CompletableFuture<V>> getCacheFutures() {
97+
return Collections.unmodifiableCollection(futureCache.getAll());
98+
}
99+
102100
Optional<CompletableFuture<V>> getIfPresent(K key) {
103101
synchronized (dataLoader) {
104102
boolean cachingEnabled = loaderOptions.cachingEnabled();

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
import org.dataloader.CacheMap;
2020
import org.dataloader.annotations.Internal;
2121

22-
import java.util.HashMap;
23-
import java.util.Map;
22+
import java.util.*;
2423
import java.util.concurrent.CompletableFuture;
2524

2625
/**
@@ -60,6 +59,14 @@ public CompletableFuture<V> get(K key) {
6059
return cache.get(key);
6160
}
6261

62+
/**
63+
* {@inheritDoc}
64+
*/
65+
@Override
66+
public Collection<CompletableFuture<V>> getAll() {
67+
return cache.values();
68+
}
69+
6370
/**
6471
* {@inheritDoc}
6572
*/

src/test/java/ReadmeExamples.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
import org.dataloader.stats.ThreadLocalStatisticsCollector;
1717

1818
import java.time.Duration;
19-
import java.util.ArrayList;
20-
import java.util.List;
21-
import java.util.Map;
22-
import java.util.Set;
19+
import java.util.*;
2320
import java.util.concurrent.CompletableFuture;
2421
import java.util.concurrent.CompletionStage;
2522
import java.util.stream.Collectors;
@@ -221,6 +218,11 @@ public CompletableFuture<Object> get(Object key) {
221218
return null;
222219
}
223220

221+
@Override
222+
public Collection<CompletableFuture<Object>> getAll() {
223+
return null;
224+
}
225+
224226
@Override
225227
public CacheMap set(Object key, CompletableFuture value) {
226228
return null;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.getCacheFutures();
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.getCacheFutures();
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+
50+
@Test(expected = UnsupportedOperationException.class)
51+
public void should_throw_exception__on_mutation_attempt() {
52+
DataLoader<Integer, Integer> dataLoader = newDataLoader(keysAsValues());
53+
54+
dataLoader.load(1).handle((v, t) -> t);
55+
56+
Collection<CompletableFuture<Integer>> futures = dataLoader.getCacheFutures();
57+
58+
futures.add(CompletableFuture.completedFuture(2));
59+
}
60+
}

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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 6 additions & 0 deletions
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)