From b26c2a948e23c7ce1d69b8422f890c61baa4a9e1 Mon Sep 17 00:00:00 2001 From: Alexey Vinogradov Date: Thu, 27 Apr 2023 11:47:53 +0500 Subject: [PATCH] refactor(snapshots): Replace `Stream.toList` and the for each cycle to `Stream.forEachOrdered` (cherry picked from commit 499036719908a9487853a727c54e04c637119498) --- .../zeebe/snapshots/impl/SnapshotChecksum.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/snapshot/src/main/java/io/camunda/zeebe/snapshots/impl/SnapshotChecksum.java b/snapshot/src/main/java/io/camunda/zeebe/snapshots/impl/SnapshotChecksum.java index dfb6bc35cd35..29c42771226e 100644 --- a/snapshot/src/main/java/io/camunda/zeebe/snapshots/impl/SnapshotChecksum.java +++ b/snapshot/src/main/java/io/camunda/zeebe/snapshots/impl/SnapshotChecksum.java @@ -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 { @@ -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 @@ -70,11 +71,16 @@ public static void persist(final Path checksumPath, final SfvChecksum checksum) * * @return the SfvChecksum object */ - private static SfvChecksum createCombinedChecksum(final List files) throws IOException { + private static SfvChecksum createCombinedChecksum(final Stream 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; } }