From 4eb1ed650b42a39d16bdd599ed2f137b80399a84 Mon Sep 17 00:00:00 2001 From: garyschulte Date: Tue, 19 Sep 2023 16:11:34 -0700 Subject: [PATCH] rwlock usage in getNearest and more javadoc errata Signed-off-by: garyschulte --- .../segmented/RocksDBSnapshotTransaction.java | 3 ++ .../SegmentedInMemoryKeyValueStorage.java | 30 +++++++++++-------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/plugins/rocksdb/src/main/java/org/hyperledger/besu/plugin/services/storage/rocksdb/segmented/RocksDBSnapshotTransaction.java b/plugins/rocksdb/src/main/java/org/hyperledger/besu/plugin/services/storage/rocksdb/segmented/RocksDBSnapshotTransaction.java index 6ca6c394e83..ad3438d381b 100644 --- a/plugins/rocksdb/src/main/java/org/hyperledger/besu/plugin/services/storage/rocksdb/segmented/RocksDBSnapshotTransaction.java +++ b/plugins/rocksdb/src/main/java/org/hyperledger/besu/plugin/services/storage/rocksdb/segmented/RocksDBSnapshotTransaction.java @@ -142,6 +142,9 @@ public void remove(final SegmentIdentifier segmentId, final byte[] key) { * *

be sure to close this iterator, like in a try-with-resources block, otherwise a native * memory leak might occur. + * + * @param segmentId id for the segment to iterate over. + * @return RocksIterator */ public RocksIterator getIterator(final SegmentIdentifier segmentId) { return snapTx.getIterator(readOptions, columnFamilyMapper.apply(segmentId)); diff --git a/services/kvstore/src/main/java/org/hyperledger/besu/services/kvstore/SegmentedInMemoryKeyValueStorage.java b/services/kvstore/src/main/java/org/hyperledger/besu/services/kvstore/SegmentedInMemoryKeyValueStorage.java index 053057b405e..e040d4233d4 100644 --- a/services/kvstore/src/main/java/org/hyperledger/besu/services/kvstore/SegmentedInMemoryKeyValueStorage.java +++ b/services/kvstore/src/main/java/org/hyperledger/besu/services/kvstore/SegmentedInMemoryKeyValueStorage.java @@ -116,18 +116,24 @@ public Optional get(final SegmentIdentifier segmentIdentifier, final byt public Optional getNearestTo( final SegmentIdentifier segmentIdentifier, final Bytes key) throws StorageException { - // TODO: revisit this for sort performance - Comparator>> comparing = - Comparator.comparing( - (Map.Entry> a) -> a.getKey().commonPrefixLength(key)) - .thenComparing((a, b) -> a.getKey().compareTo(b.getKey())); - return this.hashValueStore - .computeIfAbsent(segmentIdentifier, s -> new HashMap<>()) - .entrySet() - .stream() - .sorted(comparing.reversed()) - .findFirst() - .map(z -> new NearestKeyValue(z.getKey(), z.getValue())); + final Lock lock = rwLock.readLock(); + lock.lock(); + try { + // TODO: revisit this for sort performance + Comparator>> comparing = + Comparator.comparing( + (Map.Entry> a) -> a.getKey().commonPrefixLength(key)) + .thenComparing((a, b) -> a.getKey().compareTo(b.getKey())); + return this.hashValueStore + .computeIfAbsent(segmentIdentifier, s -> new HashMap<>()) + .entrySet() + .stream() + .sorted(comparing.reversed()) + .findFirst() + .map(z -> new NearestKeyValue(z.getKey(), z.getValue())); + } finally { + lock.unlock(); + } } @Override