Skip to content

Commit

Permalink
filter inMemory key value storage rather than takeWhile to avoid sort
Browse files Browse the repository at this point in the history
Signed-off-by: garyschulte <garyschulte@gmail.com>
  • Loading branch information
garyschulte committed Sep 1, 2023
1 parent 65b6cac commit 2a03e64
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ public Map<Bytes32, Bytes> streamAccountFlatDatabase(
final long max) {
final Stream<Pair<Bytes32, Bytes>> pairStream =
storage
.streamFromKey(ACCOUNT_INFO_STATE, startKeyHash.toArrayUnsafe())
.streamFromKey(
ACCOUNT_INFO_STATE, startKeyHash.toArrayUnsafe(), endKeyHash.toArrayUnsafe())
.limit(max)
.map(pair -> new Pair<>(Bytes32.wrap(pair.getKey()), Bytes.wrap(pair.getValue())))
.takeWhile(pair -> pair.getFirst().compareTo(endKeyHash) <= 0);
.map(pair -> new Pair<>(Bytes32.wrap(pair.getKey()), Bytes.wrap(pair.getValue())));

final TreeMap<Bytes32, Bytes> collected =
pairStream.collect(
Expand All @@ -164,8 +164,7 @@ public Map<Bytes32, Bytes> streamStorageFlatDatabase(
pair ->
new Pair<>(
Bytes32.wrap(Bytes.wrap(pair.getKey()).slice(Hash.SIZE)),
RLP.encodeValue(Bytes.wrap(pair.getValue()).trimLeadingZeros())))
.takeWhile(pair -> pair.getFirst().compareTo(endKeyHash) <= 0);
RLP.encodeValue(Bytes.wrap(pair.getValue()).trimLeadingZeros())));

final TreeMap<Bytes32, Bytes> collected =
pairStream.collect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public Stream<Pair<byte[], byte[]>> streamFromKey(
final Bytes endKeyBytes = Bytes.wrap(endKey);
return stream(segmentId)
.filter(e -> startKeyBytes.compareTo(Bytes.wrap(e.getKey())) <= 0)
.takeWhile(e -> endKeyBytes.compareTo(Bytes.wrap(e.getKey())) >= 0);
.filter(e -> endKeyBytes.compareTo(Bytes.wrap(e.getKey())) >= 0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public Stream<Pair<byte[], byte[]>> streamFromKey(
final Bytes endKeyHash = Bytes.wrap(endKey);
return stream(segmentIdentifier)
.filter(e -> startKeyHash.compareTo(Bytes.wrap(e.getKey())) <= 0)
.takeWhile(e -> endKeyHash.compareTo(Bytes.wrap(e.getKey())) >= 0);
.filter(e -> endKeyHash.compareTo(Bytes.wrap(e.getKey())) >= 0);
}

@Override
Expand Down

0 comments on commit 2a03e64

Please sign in to comment.