From d4d3d01405355cfe8d634b18160003a18c20a48b Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Sun, 4 Oct 2020 16:42:25 +0200 Subject: [PATCH] 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. --- .../java/org/lucares/pdb/map/PersistentMap.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/block-storage/src/main/java/org/lucares/pdb/map/PersistentMap.java b/block-storage/src/main/java/org/lucares/pdb/map/PersistentMap.java index ae8269d..c454e41 100644 --- a/block-storage/src/main/java/org/lucares/pdb/map/PersistentMap.java +++ b/block-storage/src/main/java/org/lucares/pdb/map/PersistentMap.java @@ -153,7 +153,10 @@ public class PersistentMap implements AutoCloseable { private final LRUCache nodeCache = new LRUCache<>(10_000); - private final LRUCache valueCache = new LRUCache<>(1_000); + private final LRUCache valueCache = new LRUCache<>(10_000); + + // guarded by: this + private volatile long nodeOffsetOfRootNode = -1; public PersistentMap(final Path path, final Path storageBasePath, final EncoderDecoder keyEncoder, final EncoderDecoder valueEncoder) { @@ -161,6 +164,9 @@ public class PersistentMap implements AutoCloseable { this.keyEncoder = keyEncoder; this.valueEncoder = valueEncoder; initIfNew(); + + final DiskBlock diskBlock = diskStore.getDiskBlock(NODE_OFFSET_TO_ROOT_NODE, diskStore.minAllocationSize()); + nodeOffsetOfRootNode = diskBlock.getByteBuffer().getLong(0); } @Override @@ -478,15 +484,14 @@ public class PersistentMap implements AutoCloseable { } private long readNodeOffsetOfRootNode() { - final DiskBlock diskBlock = diskStore.getDiskBlock(NODE_OFFSET_TO_ROOT_NODE, diskStore.minAllocationSize()); - - return diskBlock.getByteBuffer().getLong(0); + return nodeOffsetOfRootNode; } private void writeNodeOffsetOfRootNode(final long newNodeOffsetToRootNode) { final DiskBlock diskBlock = diskStore.getDiskBlock(NODE_OFFSET_TO_ROOT_NODE, diskStore.minAllocationSize()); diskBlock.getByteBuffer().putLong(0, newNodeOffsetToRootNode); diskBlock.force(); + nodeOffsetOfRootNode = newNodeOffsetToRootNode; } }