Skip to content

Commit cbc8e00

Browse files
committed
GH-5433 Use AtomicReferenceArray for value cache to ensure inter-thread visibility
1 parent 7bd99d5 commit cbc8e00

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/ValueStore.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import java.util.Optional;
5858
import java.util.Set;
5959
import java.util.concurrent.ConcurrentHashMap;
60+
import java.util.concurrent.atomic.AtomicReferenceArray;
6061
import java.util.concurrent.locks.ReadWriteLock;
6162
import java.util.concurrent.locks.ReentrantReadWriteLock;
6263
import java.util.concurrent.locks.StampedLock;
@@ -125,7 +126,7 @@ class ValueStore extends AbstractValueFactory {
125126
/**
126127
* A simple cache containing the [VALUE_CACHE_SIZE] most-recently used values stored by their ID.
127128
*/
128-
private final LmdbValue[] valueCache;
129+
private final AtomicReferenceArray<LmdbValue> valueCache;
129130
/**
130131
* A simple cache containing the [ID_CACHE_SIZE] most-recently used value-IDs stored by their value.
131132
*/
@@ -193,7 +194,7 @@ class ValueStore extends AbstractValueFactory {
193194
this.mapSize = config.getValueDBSize();
194195
open();
195196

196-
valueCache = new LmdbValue[config.getValueCacheSize()];
197+
valueCache = new AtomicReferenceArray<>(config.getValueCacheSize());
197198
valueIDCache = new ConcurrentCache<>(config.getValueIDCacheSize());
198199
namespaceCache = new ConcurrentCache<>(config.getNamespaceCacheSize());
199200
namespaceIDCache = new ConcurrentCache<>(config.getNamespaceIDCacheSize());
@@ -442,7 +443,7 @@ protected byte[] getData(long id) throws IOException {
442443
* @return the value object or <code>null</code> if not found
443444
*/
444445
LmdbValue cachedValue(long id) {
445-
LmdbValue value = valueCache[(int) (id % valueCache.length)];
446+
LmdbValue value = valueCache.get((int) (id % valueCache.length()));
446447
if (value != null && value.getInternalID() == id) {
447448
return value;
448449
}
@@ -459,7 +460,7 @@ LmdbValue cachedValue(long id) {
459460
* @return the value object or <code>null</code> if not found
460461
*/
461462
void cacheValue(long id, LmdbValue value) {
462-
valueCache[(int) (id % valueCache.length)] = value;
463+
valueCache.lazySet((int) (id % valueCache.length()), value);
463464
}
464465

465466
/**
@@ -1236,7 +1237,9 @@ public void clear() throws IOException {
12361237
}
12371238

12381239
protected void clearCaches() {
1239-
Arrays.fill(valueCache, null);
1240+
for (int i = 0; i < valueCache.length(); i++) {
1241+
valueCache.set(i, null);
1242+
}
12401243
valueIDCache.clear();
12411244
namespaceCache.clear();
12421245
namespaceIDCache.clear();

0 commit comments

Comments
 (0)