Skip to content

Commit

Permalink
use synchronizeMap instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Neeharika-Sompalli committed Jan 12, 2024
1 parent 3f35eac commit fa478b2
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
* A base class for implementations of {@link ReadableKVState} and {@link WritableKVState}.
Expand All @@ -39,7 +38,7 @@ public abstract class ReadableKVStateBase<K, V> implements ReadableKVState<K, V>
* changed before we got to handle transaction. If the value is "null", this means it was NOT
* FOUND when we looked it up.
*/
private final Map<K, V> readCache = new ConcurrentHashMap<>();
private final Map<K, V> readCache = Collections.synchronizedMap(new HashMap<>());

private final Set<K> unmodifiableReadKeys = Collections.unmodifiableSet(readCache.keySet());

Expand Down Expand Up @@ -68,9 +67,7 @@ public V get(@NonNull K key) {
Objects.requireNonNull(key);
if (!hasBeenRead(key)) {
final var value = readFromDataSource(key);
if (value != null) {
markRead(key, value);
}
markRead(key, value);
}
return readCache.get(key);
}
Expand Down Expand Up @@ -123,7 +120,7 @@ public void reset() {
* @param key The key
* @param value The value
*/
protected final void markRead(@NonNull K key, @NonNull V value) {
protected final void markRead(@NonNull K key, @Nullable V value) {
readCache.put(key, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ public final V getForModify(@NonNull final K key) {
// We have not queried this key before, so let's look it up and store that we have
// read this key. And then return the value.
final var val = getForModifyFromDataSource(key);
if(val != null){
markRead(key, val);
}
markRead(key, val);
return val;
}

Expand Down

0 comments on commit fa478b2

Please sign in to comment.