Skip to content
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

fix: use concurrent hashmap instead of synchroniztion for recordcache #11112

Merged
merged 4 commits into from
Jan 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
* A base class for implementations of {@link ReadableKVState} and {@link WritableKVState}.
Expand All @@ -38,10 +40,12 @@ 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 = Collections.synchronizedMap(new HashMap<>());
private final ConcurrentMap<K, V> readCache = new ConcurrentHashMap<>();

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

private static final Object marker = new Object();

/**
* Create a new StateBase.
*
Expand Down Expand Up @@ -69,7 +73,8 @@ public V get(@NonNull K key) {
final var value = readFromDataSource(key);
markRead(key, value);
}
return readCache.get(key);
final var value = readCache.get(key);
return (value == marker) ? null : value;
}

/**
Expand Down Expand Up @@ -121,7 +126,11 @@ public void reset() {
* @param value The value
*/
protected final void markRead(@NonNull K key, @Nullable V value) {
readCache.put(key, value);
if (value == null) {
readCache.put(key, (V) marker);
} else {
readCache.put(key, value);
}
}

/**
Expand Down
Loading