synchronize access to the PerstistentMap

The map is not (yet) thread-safe. Eventually we'll replace the
synchronized blocks with read/write locks on the nodes.
This commit is contained in:
2018-11-17 10:02:29 +01:00
parent fce0f6a04d
commit b2107acf4e

View File

@@ -141,20 +141,20 @@ public class PersistentMap<K, V> implements AutoCloseable {
} }
} }
public void putAllValues(final Map<K, V> map) throws IOException { public synchronized void putAllValues(final Map<K, V> map) throws IOException {
for (final Entry<K, V> e : map.entrySet()) { for (final Entry<K, V> e : map.entrySet()) {
putValue(e.getKey(), e.getValue()); putValue(e.getKey(), e.getValue());
} }
} }
public V putValue(final K key, final V value) throws IOException { public synchronized V putValue(final K key, final V value) throws IOException {
final byte[] encodedKey = keyEncoder.encode(key); final byte[] encodedKey = keyEncoder.encode(key);
final byte[] encodedValue = valueEncoder.encode(value); final byte[] encodedValue = valueEncoder.encode(value);
final byte[] oldValue = putValue(encodedKey, encodedValue); final byte[] oldValue = putValue(encodedKey, encodedValue);
return oldValue == null ? null : valueEncoder.decode(oldValue); return oldValue == null ? null : valueEncoder.decode(oldValue);
} }
public V getValue(final K key) throws IOException { public synchronized V getValue(final K key) throws IOException {
final byte[] encodedKey = keyEncoder.encode(key); final byte[] encodedKey = keyEncoder.encode(key);
final byte[] foundValue = getValue(encodedKey); final byte[] foundValue = getValue(encodedKey);
return foundValue == null ? null : valueEncoder.decode(foundValue); return foundValue == null ? null : valueEncoder.decode(foundValue);
@@ -311,7 +311,7 @@ public class PersistentMap<K, V> implements AutoCloseable {
diskBlock.force(); diskBlock.force();
} }
public void print() throws IOException { public synchronized void print() throws IOException {
visitNodeEntriesPreOrder((node, parentNode, nodeEntry, depth) -> { visitNodeEntriesPreOrder((node, parentNode, nodeEntry, depth) -> {
@@ -324,7 +324,7 @@ public class PersistentMap<K, V> implements AutoCloseable {
}); });
} }
public void visitNodeEntriesPreOrder(final VisitorCallback visitor) throws IOException { public synchronized void visitNodeEntriesPreOrder(final VisitorCallback visitor) throws IOException {
final long rootNodeOffset = readNodeOffsetOfRootNode(); final long rootNodeOffset = readNodeOffsetOfRootNode();
visitNodeEntriesPreOrderRecursively(rootNodeOffset, null, visitor, 0); visitNodeEntriesPreOrderRecursively(rootNodeOffset, null, visitor, 0);
} }
@@ -347,7 +347,7 @@ public class PersistentMap<K, V> implements AutoCloseable {
FIND, ITERATE FIND, ITERATE
} }
public void visitValues(final K keyPrefix, final Visitor<K, V> visitor) throws IOException { public synchronized void visitValues(final K keyPrefix, final Visitor<K, V> visitor) throws IOException {
final byte[] encodedKeyPrefix = keyEncoder.encode(keyPrefix); final byte[] encodedKeyPrefix = keyEncoder.encode(keyPrefix);
final long rootNodeOffset = readNodeOffsetOfRootNode(); final long rootNodeOffset = readNodeOffsetOfRootNode();