Skip to content

Commit

Permalink
make layered worldstate streaming by segment cheaper by copying only …
Browse files Browse the repository at this point in the history
…the segment in question

Signed-off-by: garyschulte <garyschulte@gmail.com>
  • Loading branch information
garyschulte committed Jul 13, 2023
1 parent e26a189 commit 8f1125c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
*/
package org.hyperledger.besu.plugin.services.storage;

/** The interface Snappable key value storage.
*
*/
/** The interface Snappable key value storage.*/
public interface SnappableKeyValueStorage extends SegmentedKeyValueStorage {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,11 @@ public Stream<Pair<byte[], byte[]>> stream(final SegmentIdentifier segmentId) {
lock.lock();
try {
// copy of our in memory store to use for streaming and filtering:
var ourLayerState = new HashMap<>(hashValueStore);
var ourLayerState = Optional.ofNullable(hashValueStore.get(segmentId)).map(HashMap::new)
.orElse(new HashMap<>());

return Streams.concat(
ourLayerState.computeIfAbsent(segmentId, __ -> new HashMap<>()).entrySet().stream()
ourLayerState.entrySet().stream()
.filter(entry -> entry.getValue().isPresent())
.map(
bytesEntry ->
Expand All @@ -127,10 +128,11 @@ public Stream<byte[]> streamKeys(final SegmentIdentifier segmentId) {
lock.lock();
try {
// copy of our in memory store to use for streaming and filtering:
var ourLayerState = new HashMap<>(hashValueStore);
var ourLayerState = Optional.ofNullable(hashValueStore.get(segmentId)).map(HashMap::new)
.orElse(new HashMap<>());

return Streams.concat(
ourLayerState.computeIfAbsent(segmentId, __ -> new HashMap<>()).entrySet().stream()
ourLayerState.entrySet().stream()
.filter(entry -> entry.getValue().isPresent())
.map(bytesEntry -> bytesEntry.getKey().toArrayUnsafe())
// since we are layered, concat a parent stream filtered by our map entries:
Expand Down

0 comments on commit 8f1125c

Please sign in to comment.