Skip to content

Commit

Permalink
merge: #12576
Browse files Browse the repository at this point in the history
12576: refactor(snapshots): Replace `Stream.toList` and the for each cycle to `Stream.forEachOrdered` r=deepthidevaki a=aivinog1

## Description

<!-- Please explain the changes you made here. -->
I replaced the collection with a list and the for each cycle to the `Stream.forEactOrdered` call. It should reduce memory footprint and possibly improve latency, especially on a large number of snapshot files.

## Related issues

<!-- Which issues are closed by this PR or are related -->

closes #12575 



Co-authored-by: Alexey Vinogradov <vinogradov.a.i.93@gmail.com>
  • Loading branch information
zeebe-bors-camunda[bot] and aivinog1 committed May 4, 2023
2 parents 4fa34c5 + 4990367 commit 8a8a58d
Showing 1 changed file with 12 additions and 6 deletions.
Expand Up @@ -9,9 +9,10 @@

import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Stream;

final class SnapshotChecksum {

Expand All @@ -38,7 +39,7 @@ public static SfvChecksum read(final Path checksumPath) throws IOException {
public static SfvChecksum calculate(final Path snapshotDirectory) throws IOException {
try (final var fileStream =
Files.list(snapshotDirectory).filter(SnapshotChecksum::isNotMetadataFile).sorted()) {
final SfvChecksum sfvChecksum = createCombinedChecksum(fileStream.toList());
final SfvChecksum sfvChecksum = createCombinedChecksum(fileStream);

// While persisting transient snapshot, the checksum of metadata file is added at the end.
// Hence when we recalculate the checksum, we must follow the same order. Otherwise base on
Expand Down Expand Up @@ -70,11 +71,16 @@ public static void persist(final Path checksumPath, final SfvChecksum checksum)
*
* @return the SfvChecksum object
*/
private static SfvChecksum createCombinedChecksum(final List<Path> files) throws IOException {
private static SfvChecksum createCombinedChecksum(final Stream<Path> files) {
final SfvChecksum checksum = new SfvChecksum();
for (final var f : files) {
checksum.updateFromFile(f);
}
files.forEachOrdered(
path -> {
try {
checksum.updateFromFile(path);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
return checksum;
}
}

0 comments on commit 8a8a58d

Please sign in to comment.