Skip to content

Commit

Permalink
Fixed memory map data corruption
Browse files Browse the repository at this point in the history
Now writing deep copy of JSON value to prevent future JSON changes from corrupting previously written JSON
  • Loading branch information
gj0dcsa committed Nov 20, 2023
1 parent 2fbede8 commit 107e01e
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;

import java.util.HashMap;
import java.util.List;
import java.util.TreeMap;

public class MemorySortedPartitionsNonLockingMap implements SortedPartitionsNonLockingMap {
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());
memoryMap
.computeIfAbsent(partitionKey, (ignoredKey) -> new TreeMap<>())
.put(sortKey, new ObjectMapper().createObjectNode().set("value", value));
.put(sortKey, new ObjectMapper().createObjectNode().set("value", valueCopy));
}

@Override
Expand Down

0 comments on commit 107e01e

Please sign in to comment.