-
Notifications
You must be signed in to change notification settings - Fork 94
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
bbakerman
merged 7 commits into
graphql-java:master
from
samuelAndalon:feat/cache-map-get-values
May 19, 2022
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
90f8364
feat: get all read-only values from cacheMap
e928732
feat: avoid importing all
b88f1ff
feat: avoid importing all
1bc485b
feat: avoid importing all
176f85f
feat: futureCache getAll in DataLoader
908a4a3
feat: update javadocs
b04dfa0
feat: getters for futureCache and cacheMap
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 DataLoaderis inside DataLoader.
Change this to
And for symmetry (since we are going to do this) we should expose the other atts
There was a problem hiding this comment.
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
andvalueCache
public ? i was trying to just expose theCacheMap
values as read-only withunmodifiableCollection
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-onlyCollection
from theDataLoader
There was a problem hiding this comment.
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 ofCacheMap
There was a problem hiding this comment.
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
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
There was a problem hiding this comment.
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
andvalueCache
,will think about the wrapper interface to remove the mutable methods
clear
,delete
andset
and probably open a separate PR, if we decide that we need it