Skip to content

Commit

Permalink
Avoid creating an ObjectMapper for each set/getItemValue
Browse files Browse the repository at this point in the history
Signed-off-by: Niels Thykier <[email protected]>
  • Loading branch information
nt-gt committed Nov 21, 2023
1 parent faf5f01 commit aa50cb5
Showing 1 changed file with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,25 @@
import java.util.TreeMap;

public class MemorySortedPartitionsNonLockingMap implements SortedPartitionsNonLockingMap {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

private final HashMap<String, TreeMap<String, JsonNode>> memoryMap = new HashMap<>();


@SneakyThrows
@Override
public synchronized void setItemValue(String partitionKey, String sortKey, JsonNode value) {
JsonNode valueCopy = new ObjectMapper().readTree(value.toString());
JsonNode valueCopy = OBJECT_MAPPER.readTree(value.toString());
memoryMap
.computeIfAbsent(partitionKey, (ignoredKey) -> new TreeMap<>())
.put(sortKey, new ObjectMapper().createObjectNode().set("value", valueCopy));
.put(sortKey, OBJECT_MAPPER.createObjectNode().set("value", valueCopy));
}

@Override
public synchronized JsonNode getItemValue(String partitionKey, String sortKey) {
return memoryMap
.getOrDefault(partitionKey, new TreeMap<>())
.getOrDefault(sortKey, new ObjectMapper().createObjectNode())
.getOrDefault(sortKey, OBJECT_MAPPER.createObjectNode())
.get("value");
}

Expand Down

0 comments on commit aa50cb5

Please sign in to comment.