diff --git a/src/main/java/com/uid2/admin/store/writer/SaltStoreWriter.java b/src/main/java/com/uid2/admin/store/writer/SaltStoreWriter.java index 738ee64c..45e764b8 100644 --- a/src/main/java/com/uid2/admin/store/writer/SaltStoreWriter.java +++ b/src/main/java/com/uid2/admin/store/writer/SaltStoreWriter.java @@ -16,7 +16,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.time.Instant; -import java.util.Collections; +import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Map; @@ -48,23 +48,27 @@ protected List getSnapshots(RotatingSaltProvi throw new IllegalStateException("Snapshots cannot be null"); } final Instant now = Instant.now(); - List currentSnapshots = Stream.concat(provider.getSnapshots().stream(), Stream.of(data)) + List currentSnapshots = provider.getSnapshots(); + List snapshots = null; + snapshots = Stream.concat(currentSnapshots.stream(), Stream.of(data)) .sorted(Comparator.comparing(RotatingSaltProvider.SaltSnapshot::getEffective)) .collect(Collectors.toList()); - RotatingSaltProvider.SaltSnapshot newestEffectiveSnapshot = currentSnapshots.stream() + RotatingSaltProvider.SaltSnapshot newestEffectiveSnapshot = snapshots.stream() .filter(snapshot -> snapshot.isEffective(now)) .reduce((a, b) -> b).orElse(null); - return Stream.concat(provider.getSnapshots().stream(), Stream.of(data)) - .filter(snapshot -> { - boolean isValid = newestEffectiveSnapshot == null || snapshot == newestEffectiveSnapshot; - if (!isValid) { + + List filteredSnapshots = new ArrayList<>(); + + for (RotatingSaltProvider.SaltSnapshot snapshot : snapshots) { + if (newestEffectiveSnapshot != null && snapshot != newestEffectiveSnapshot) { LOGGER.info("Skipping effective snapshot, effective=" + snapshot.getEffective() + ", expires=" + snapshot.getExpires() + " in favour of newer snapshot, effective=" + newestEffectiveSnapshot.getEffective() + ", expires=" + newestEffectiveSnapshot.getExpires()); - } - return isValid; - }) - .sorted(Comparator.comparing(RotatingSaltProvider.SaltSnapshot::getEffective)) - .collect(Collectors.toList()); + continue; + } + filteredSnapshots.add(snapshot); + newestEffectiveSnapshot = null; + } + return filteredSnapshots; } public void upload(RotatingSaltProvider.SaltSnapshot data) throws Exception {