Skip to content

Commit

Permalink
issue 3758 rewrite calculateVersionSizes to fix problem with duplicat…
Browse files Browse the repository at this point in the history
…ed keys (#3759)
  • Loading branch information
SilinPavel authored Nov 5, 2024
1 parent 4b137c3 commit 6ce63a5
Showing 1 changed file with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -131,19 +129,24 @@ public XContentBuilder fileToDocument(final DataStorageFile dataStorageFile,

private Map<String, ImmutablePair<Long, Integer>> calculateVersionSizes(
final Map<String, AbstractDataStorageItem> 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<String, ImmutablePair<Long, Integer>> 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/,
Expand Down

0 comments on commit 6ce63a5

Please sign in to comment.