-
Notifications
You must be signed in to change notification settings - Fork 1
[BI-1339] Germplasm Details page #176
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bd85ecc
[BI-1339] Germplasm Details page
HMS17 0829e5c
Germplasm Retrieval and Display
HMS17 fee6a2d
Merge branch 'develop' of https://github.com/Breeding-Insight/bi-api …
HMS17 a2840df
Cleanup
HMS17 55a751a
Fix null key in unit test
HMS17 2e781eb
Unit test
HMS17 41518d7
Merge branch 'develop' into feature/BI-1339
HMS17 ceae257
Code review fixes
HMS17 10dd6d9
Merge remote-tracking branch 'origin/feature/BI-1339' into feature/BI…
HMS17 66a2e2e
Code review fixes 2
HMS17 f3f5e85
Code review cleanup
HMS17 f588f7c
Merge branch 'develop' into feature/BI-1339
HMS17 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,20 +30,25 @@ | |
| import java.util.concurrent.*; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * | ||
| * @param <K> key/id of object | ||
| * @param <R> object | ||
| */ | ||
| @Slf4j | ||
| public class ProgramCache<R> { | ||
| public class ProgramCache<K, R> { | ||
|
|
||
| private final FetchFunction<UUID, List<R>> fetchMethod; | ||
| private final FetchFunction<UUID, Map<K, R>> fetchMethod; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not clearly explained that K should be the object id, it took me a bit to figure out what K was supposed to be. Maybe a class header comment for explanation? |
||
| private final Map<UUID, Semaphore> programSemaphore = new HashMap<>(); | ||
| private final Cloner cloner; | ||
|
|
||
| private final Executor executor = Executors.newCachedThreadPool(); | ||
| private final LoadingCache<UUID, List<R>> cache = CacheBuilder.newBuilder() | ||
| private final LoadingCache<UUID, Map<K, R>> cache = CacheBuilder.newBuilder() | ||
| .build(new CacheLoader<>() { | ||
| @Override | ||
| public List<R> load(@NotNull UUID programId) throws Exception { | ||
| public Map<K, R> load(@NotNull UUID programId) throws Exception { | ||
| try { | ||
| List<R> values = fetchMethod.apply(programId); | ||
| Map<K, R> values = fetchMethod.apply(programId); | ||
| log.debug("cache loading complete.\nprogramId: " + programId); | ||
| return values; | ||
| } catch (Exception e) { | ||
|
|
@@ -54,7 +59,7 @@ public List<R> load(@NotNull UUID programId) throws Exception { | |
| } | ||
| }); | ||
|
|
||
| public ProgramCache(FetchFunction<UUID, List<R>> fetchMethod, List<UUID> keys) { | ||
| public ProgramCache(FetchFunction<UUID, Map<K, R>> fetchMethod, List<UUID> keys) { | ||
| this.fetchMethod = fetchMethod; | ||
| this.cloner = new Cloner(); | ||
| // Populate cache on start up | ||
|
|
@@ -63,27 +68,29 @@ public ProgramCache(FetchFunction<UUID, List<R>> fetchMethod, List<UUID> keys) { | |
| } | ||
| } | ||
|
|
||
| public ProgramCache(FetchFunction<UUID, List<R>> fetchMethod) { | ||
| public ProgramCache(FetchFunction<UUID, Map<K, R>> fetchMethod) { | ||
| this.fetchMethod = fetchMethod; | ||
| this.cloner = new Cloner(); | ||
| } | ||
|
|
||
| public List<R> get(UUID programId) throws ApiException { | ||
| public Map<K, R> get(UUID programId) throws ApiException { | ||
| try { | ||
| // This will get current cache data, or wait for the refresh to finish if there is no cache data. | ||
| // TODO: Do we want to wait for a refresh method if it is running? Returns current data right now, even if old | ||
| if (!programSemaphore.containsKey(programId) || cache.getIfPresent(programId) == null) { | ||
| // If the cache is missing, refresh and get | ||
| log.trace("cache miss, fetching from source.\nprogramId: " + programId); | ||
| updateCache(programId); | ||
| List<R> result = new ArrayList<>(cache.get(programId)); | ||
| result = result.stream().map(cloner::deepClone).collect(Collectors.toList()); | ||
| Map<K, R> result = new HashMap<>(cache.get(programId)); | ||
| result = result.entrySet().stream().map(cloner::deepClone) | ||
| .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
| return result; | ||
| } else { | ||
| log.trace("cache contains records for the program.\nprogramId: " + programId); | ||
| // Most cases where the cache is populated | ||
| List<R> result = new ArrayList<>(cache.get(programId)); | ||
| result = result.stream().map(cloner::deepClone).collect(Collectors.toList()); | ||
| Map<K, R> result = new HashMap<>(cache.get(programId)); | ||
| result = result.entrySet().stream().map(cloner::deepClone) | ||
| .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
| return result; | ||
| } | ||
| } catch (ExecutionException e) { | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.