cache location of root node

The location of the root node is not cached in the nodeCache. So it had
to be read for every read or write access. We are still using a lock
when accessing the map. That makes it easy to cache the location.
This commit is contained in:
2020-10-04 16:42:25 +02:00
parent 598ee31c1d
commit d4d3d01405

View File

@@ -153,7 +153,10 @@ public class PersistentMap<K, V> implements AutoCloseable {
private final LRUCache<Long, PersistentMapDiskNode> nodeCache = new LRUCache<>(10_000); private final LRUCache<Long, PersistentMapDiskNode> nodeCache = new LRUCache<>(10_000);
private final LRUCache<K, V> valueCache = new LRUCache<>(1_000); private final LRUCache<K, V> valueCache = new LRUCache<>(10_000);
// guarded by: this
private volatile long nodeOffsetOfRootNode = -1;
public PersistentMap(final Path path, final Path storageBasePath, final EncoderDecoder<K> keyEncoder, public PersistentMap(final Path path, final Path storageBasePath, final EncoderDecoder<K> keyEncoder,
final EncoderDecoder<V> valueEncoder) { final EncoderDecoder<V> valueEncoder) {
@@ -161,6 +164,9 @@ public class PersistentMap<K, V> implements AutoCloseable {
this.keyEncoder = keyEncoder; this.keyEncoder = keyEncoder;
this.valueEncoder = valueEncoder; this.valueEncoder = valueEncoder;
initIfNew(); initIfNew();
final DiskBlock diskBlock = diskStore.getDiskBlock(NODE_OFFSET_TO_ROOT_NODE, diskStore.minAllocationSize());
nodeOffsetOfRootNode = diskBlock.getByteBuffer().getLong(0);
} }
@Override @Override
@@ -478,15 +484,14 @@ public class PersistentMap<K, V> implements AutoCloseable {
} }
private long readNodeOffsetOfRootNode() { private long readNodeOffsetOfRootNode() {
final DiskBlock diskBlock = diskStore.getDiskBlock(NODE_OFFSET_TO_ROOT_NODE, diskStore.minAllocationSize()); return nodeOffsetOfRootNode;
return diskBlock.getByteBuffer().getLong(0);
} }
private void writeNodeOffsetOfRootNode(final long newNodeOffsetToRootNode) { private void writeNodeOffsetOfRootNode(final long newNodeOffsetToRootNode) {
final DiskBlock diskBlock = diskStore.getDiskBlock(NODE_OFFSET_TO_ROOT_NODE, diskStore.minAllocationSize()); final DiskBlock diskBlock = diskStore.getDiskBlock(NODE_OFFSET_TO_ROOT_NODE, diskStore.minAllocationSize());
diskBlock.getByteBuffer().putLong(0, newNodeOffsetToRootNode); diskBlock.getByteBuffer().putLong(0, newNodeOffsetToRootNode);
diskBlock.force(); diskBlock.force();
nodeOffsetOfRootNode = newNodeOffsetToRootNode;
} }
} }