-
Notifications
You must be signed in to change notification settings - Fork 0
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
STNG-8 Use concurrent versions of HashMap, TreeMap, HashSet #193
Open
jkosternl
wants to merge
4
commits into
dev
Choose a base branch
from
STNG-8_ImproveConcurrentImpls
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+47
−63
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fdf63c8
Use concurrent versions of HashMap, TreeMap, HashSet. Remove 2 obsole…
jkosternl 976ea60
Merge branch 'dev' into STNG-8_ImproveConcurrentImpls
jkosternl 3fed434
STNG-8 Improve locking/synchronizing mechanism
jkosternl 5137a3b
Merge branch 'dev' into STNG-8_ImproveConcurrentImpls
jkosternl 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 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 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
48 changes: 25 additions & 23 deletions
48
core/src/main/java/org/dcsa/conformance/core/state/MemorySortedPartitionsNonLockingMap.java
This file contains 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 |
---|---|---|
@@ -1,51 +1,53 @@ | ||
package org.dcsa.conformance.core.state; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import lombok.SneakyThrows; | ||
import static org.dcsa.conformance.core.toolkit.JsonToolkit.OBJECT_MAPPER; | ||
|
||
import java.util.HashMap; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import java.util.List; | ||
import java.util.TreeMap; | ||
|
||
import static org.dcsa.conformance.core.toolkit.JsonToolkit.OBJECT_MAPPER; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentSkipListMap; | ||
import lombok.SneakyThrows; | ||
|
||
public class MemorySortedPartitionsNonLockingMap implements SortedPartitionsNonLockingMap { | ||
private final HashMap<String, TreeMap<String, JsonNode>> memoryMap = new HashMap<>(); | ||
|
||
private static final String VALUE = "value"; | ||
private final ConcurrentHashMap<String, ConcurrentSkipListMap<String, JsonNode>> memoryMap = new ConcurrentHashMap<>(); | ||
|
||
@SneakyThrows | ||
@Override | ||
public synchronized void setItemValue(String partitionKey, String sortKey, JsonNode value) { | ||
JsonNode valueCopy = OBJECT_MAPPER.readTree(value.toString()); | ||
memoryMap | ||
.computeIfAbsent(partitionKey, ignoredKey -> new TreeMap<>()) | ||
.put(sortKey, OBJECT_MAPPER.createObjectNode().set("value", valueCopy)); | ||
.computeIfAbsent(partitionKey, ignoredKey -> new ConcurrentSkipListMap<>()) | ||
.put(sortKey, OBJECT_MAPPER.createObjectNode().set(VALUE, valueCopy)); | ||
} | ||
|
||
@Override | ||
public synchronized JsonNode getItemValue(String partitionKey, String sortKey) { | ||
return memoryMap | ||
.getOrDefault(partitionKey, new TreeMap<>()) | ||
public JsonNode getItemValue(String partitionKey, String sortKey) { | ||
return getPartitionOrDefault(partitionKey) | ||
.getOrDefault(sortKey, OBJECT_MAPPER.createObjectNode()) | ||
.get("value"); | ||
.get(VALUE); | ||
} | ||
|
||
private synchronized ConcurrentSkipListMap<String, JsonNode> getPartitionOrDefault(String partitionKey) { | ||
return memoryMap.getOrDefault(partitionKey, new ConcurrentSkipListMap<>()); | ||
} | ||
|
||
@Override | ||
public synchronized JsonNode getFirstItemValue(String partitionKey) { | ||
TreeMap<String, JsonNode> partition = memoryMap.getOrDefault(partitionKey, new TreeMap<>()); | ||
return partition.isEmpty() ? null : partition.get(partition.firstKey()).get("value"); | ||
public JsonNode getFirstItemValue(String partitionKey) { | ||
ConcurrentSkipListMap<String, JsonNode> partition = getPartitionOrDefault(partitionKey); | ||
return partition.isEmpty() ? null : partition.get(partition.firstKey()).get(VALUE); | ||
} | ||
|
||
@Override | ||
public synchronized JsonNode getLastItemValue(String partitionKey) { | ||
TreeMap<String, JsonNode> partition = memoryMap.getOrDefault(partitionKey, new TreeMap<>()); | ||
return partition.isEmpty() ? null : partition.get(partition.lastKey()).get("value"); | ||
public JsonNode getLastItemValue(String partitionKey) { | ||
ConcurrentSkipListMap<String, JsonNode> partition = getPartitionOrDefault(partitionKey); | ||
return partition.isEmpty() ? null : partition.get(partition.lastKey()).get(VALUE); | ||
} | ||
|
||
@Override | ||
public synchronized List<JsonNode> getPartitionValues(String partitionKey) { | ||
return memoryMap.getOrDefault(partitionKey, new TreeMap<>()).values().stream() | ||
.map(item -> item.get("value")) | ||
public List<JsonNode> getPartitionValues(String partitionKey) { | ||
return getPartitionOrDefault(partitionKey).values().stream() | ||
.map(item -> item.get(VALUE)) | ||
.toList(); | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.
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.
Please let's discuss this before proceeding with the change, because "synchronized" was in place because most operations affect both collections in parallel.