Skip to content

Commit

Permalink
feat: getters for futureCache and cacheMap
Browse files Browse the repository at this point in the history
  • Loading branch information
samvazquez committed May 11, 2022
1 parent 908a4a3 commit b04dfa0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
1 change: 0 additions & 1 deletion src/main/java/org/dataloader/CacheMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.dataloader.impl.DefaultCacheMap;

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

/**
Expand Down
26 changes: 17 additions & 9 deletions src/main/java/org/dataloader/DataLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -453,14 +452,6 @@ public Duration getTimeSinceDispatch() {
return Duration.between(helper.getLastDispatchTime(), helper.now());
}

/**
* This returns a read-only collection of CompletableFutures of the cache map.
* @return read-only collection of CompletableFutures
*/
public Collection<CompletableFuture<V>> getCacheFutures() {
return Collections.unmodifiableCollection(futureCache.getAll());
}

/**
* Requests to load the data with the specified key asynchronously, and returns a future of the resulting value.
* <p>
Expand Down Expand Up @@ -760,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;
}

}
15 changes: 2 additions & 13 deletions src/test/java/org/dataloader/DataLoaderCacheMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void should_provide_all_futures_from_cache() {
dataLoader.load(2);
dataLoader.load(1);

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

Expand All @@ -40,21 +40,10 @@ public void should_access_to_future_dependants() {
dataLoader.load(2).handle((v, t) -> t);
dataLoader.load(1).handle((v, t) -> t);

Collection<CompletableFuture<Integer>> futures = dataLoader.getCacheFutures();
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));
}

@Test(expected = UnsupportedOperationException.class)
public void should_throw_exception__on_mutation_attempt() {
DataLoader<Integer, Integer> dataLoader = newDataLoader(keysAsValues());

dataLoader.load(1).handle((v, t) -> t);

Collection<CompletableFuture<Integer>> futures = dataLoader.getCacheFutures();

futures.add(CompletableFuture.completedFuture(2));
}
}

0 comments on commit b04dfa0

Please sign in to comment.