From 6511c8acf99a77144fc63ab04bd7c8b80654ef39 Mon Sep 17 00:00:00 2001 From: Pavel Silin Date: Tue, 5 Nov 2024 12:07:22 +0100 Subject: [PATCH] issue 3758 rewrite calculateVersionSizes to fix problem with duplicated keys --- .../converter/storage/StorageFileMapper.java | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/elasticsearch-agent/src/main/java/com/epam/pipeline/elasticsearchagent/service/impl/converter/storage/StorageFileMapper.java b/elasticsearch-agent/src/main/java/com/epam/pipeline/elasticsearchagent/service/impl/converter/storage/StorageFileMapper.java index be89a161a6..18fe22eb00 100644 --- a/elasticsearch-agent/src/main/java/com/epam/pipeline/elasticsearchagent/service/impl/converter/storage/StorageFileMapper.java +++ b/elasticsearch-agent/src/main/java/com/epam/pipeline/elasticsearchagent/service/impl/converter/storage/StorageFileMapper.java @@ -27,13 +27,11 @@ import com.epam.pipeline.entity.search.SearchDocumentType; import com.epam.pipeline.entity.search.StorageFileSearchMask; import com.epam.pipeline.utils.FileContentUtils; -import com.epam.pipeline.utils.StreamUtils; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.ListUtils; import org.apache.commons.collections4.MapUtils; import org.apache.commons.collections4.SetUtils; import org.apache.commons.lang3.tuple.ImmutablePair; -import org.apache.commons.lang3.tuple.Pair; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.slf4j.Logger; @@ -42,12 +40,12 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; -import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; @@ -131,19 +129,24 @@ public XContentBuilder fileToDocument(final DataStorageFile dataStorageFile, private Map> calculateVersionSizes( final Map versions) { - return StreamUtils.grouped( - versions.values().stream().map(v -> (DataStorageFile) v), - Comparator.comparing(v -> MapUtils.emptyIfNull(v.getLabels()) - .getOrDefault(ESConstants.STORAGE_CLASS_LABEL, STANDARD_TIER) - ) - ).map(tierVersions -> { - final String storageClass = tierVersions.stream().findFirst() - .map(v -> MapUtils.emptyIfNull(v.getLabels()).get(ESConstants.STORAGE_CLASS_LABEL)) - .orElse(STANDARD_TIER); - final long totalSize = tierVersions.stream() - .collect(Collectors.summarizingLong(DataStorageFile::getSize)).getSum(); - return ImmutablePair.of(storageClass, ImmutablePair.of(totalSize, tierVersions.size())); - }).collect(Collectors.toMap(Pair::getKey, Pair::getValue)); + final HashMap> result = new HashMap<>(); + for (final AbstractDataStorageItem v : versions.values()) { + final DataStorageFile version = (DataStorageFile) v; + final String storageClass = Optional.ofNullable( + MapUtils.emptyIfNull(version.getLabels()).get(ESConstants.STORAGE_CLASS_LABEL) + ).orElse(STANDARD_TIER); + result.compute( + storageClass, + (tier, tierSize) -> { + if (tierSize == null) { + return ImmutablePair.of(version.getSize(), 1); + } else { + return ImmutablePair.of(tierSize.getLeft() + version.getSize(), tierSize.getRight() + 1); + } + } + ); + } + return result; } // This method construct mount path assuming that default mount point is /cloud-data/,