Skip to content

Commit

Permalink
Merge pull request #115 from samuelAndalon/feat/cache-map-get-values
Browse files Browse the repository at this point in the history
feat: make cache map values accesible for read only purposes
  • Loading branch information
bbakerman committed May 19, 2022
2 parents 7bf46ea + b04dfa0 commit fa94732
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/main/java/org/dataloader/CacheMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.dataloader.annotations.PublicSpi;
import org.dataloader.impl.DefaultCacheMap;

import java.util.Collection;
import java.util.concurrent.CompletableFuture;

/**
Expand Down Expand Up @@ -73,6 +74,12 @@ static <K, V> CacheMap<K, V> simpleMap() {
*/
CompletableFuture<V> get(K key);

/**
* Gets a collection of CompletableFutures from the cache map.
* @return the collection of cached values
*/
Collection<CompletableFuture<V>> getAll();

/**
* Creates a new cache map entry with the specified key and value, or updates the value if the key already exists.
*
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/org/dataloader/DataLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
Expand Down Expand Up @@ -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<Object, V> 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<K, V> getValueCache() {
return valueCache;
}

}
9 changes: 9 additions & 0 deletions src/main/java/org/dataloader/impl/DefaultCacheMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,6 +61,14 @@ public CompletableFuture<V> get(K key) {
return cache.get(key);
}

/**
* {@inheritDoc}
*/
@Override
public Collection<CompletableFuture<V>> getAll() {
return cache.values();
}

/**
* {@inheritDoc}
*/
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/ReadmeExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -221,6 +222,11 @@ public CompletableFuture<Object> get(Object key) {
return null;
}

@Override
public Collection<CompletableFuture<Object>> getAll() {
return null;
}

@Override
public CacheMap set(Object key, CompletableFuture value) {
return null;
Expand Down
49 changes: 49 additions & 0 deletions src/test/java/org/dataloader/DataLoaderCacheMapTest.java
Original file line number Diff line number Diff line change
@@ -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 <T> BatchLoader<T, T> keysAsValues() {
return CompletableFuture::completedFuture;
}

@Test
public void should_provide_all_futures_from_cache() {
DataLoader<Integer, Integer> dataLoader = newDataLoader(keysAsValues());

dataLoader.load(1);
dataLoader.load(2);
dataLoader.load(1);

Collection<CompletableFuture<Integer>> futures = dataLoader.getCacheMap().getAll();
assertThat(futures.size(), equalTo(2));
}

@Test
public void should_access_to_future_dependants() {
DataLoader<Integer, Integer> 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<CompletableFuture<Integer>> futures = dataLoader.getCacheMap().getAll();

List<CompletableFuture<Integer>> futuresList = new ArrayList<>(futures);
assertThat(futuresList.get(0).getNumberOfDependents(), equalTo(2));
assertThat(futuresList.get(1).getNumberOfDependents(), equalTo(1));
}
}
1 change: 0 additions & 1 deletion src/test/java/org/dataloader/DataLoaderIfPresentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertThat;


/**
* Tests for IfPresent and IfCompleted functionality.
*/
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/dataloader/fixtures/CustomCacheMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,6 +25,11 @@ public CompletableFuture<Object> get(String key) {
return stash.get(key);
}

@Override
public Collection<CompletableFuture<Object>> getAll() {
return stash.values();
}

@Override
public CacheMap<String, Object> set(String key, CompletableFuture<Object> value) {
stash.put(key, value);
Expand Down

0 comments on commit fa94732

Please sign in to comment.