Skip to content

feat: make cache map values accesible for read only purposes #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than expose the getAll down in the helper and then in the data loader,

I think we should expose the CacheMap itself on DataLoader

    private final CacheMap<Object, V> futureCache;

is inside DataLoader.

Change this to

    public  CacheMap<Object, V> getCacheMap() 

And for symmetry (since we are going to do this) we should expose the other atts

    private final StatisticsCollector stats;
    private final CacheMap<Object, V> futureCache;
    private final ValueCache<K, V> valueCache;

Copy link
Author

@samuelAndalon samuelAndalon May 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we worry about making futureCache and valueCache public ? i was trying to just expose the CacheMap values as read-only with unmodifiableCollection to avoid allowing the mutation of their state -- Let me know what you think.

For now i removed the method in the DataLoaderHelper and directly exposed the read-only Collection from the DataLoader

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also was wondering, given that we are adding a new method in the CacheMap interface, should we define a default behavior to return maybe empty collection or null ? otherwise this could be a breaking change if ppl is using a custom implementation of CacheMap

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get your point on trying to reduce danger but the fact is the DataLoaderOptions passed in the CachMap / ValueMap or it get defaulted.

I guess we could make a UnmodifiableCacheMap / ValueCache wrapper say so gain write safety

Should we worry about making futureCache and valueCache public ?

My view is sure it could be a foot gun... so dont shoot yourself with it. You needed access to the future cache so lets give it out. After all it was nominally passed in.

But UnmodifiableCacheMap is not a crazy idea

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the getters for cacheMap and valueCache,
will think about the wrapper interface to remove the mutable methods clear, delete and set and probably open a separate PR, if we decide that we need it

}

/**
* {@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