jerqi commented on code in PR #7850: URL: https://github.com/apache/gravitino/pull/7850#discussion_r2269770257
########## core/src/main/java/org/apache/gravitino/stats/storage/MemoryPartitionStatsStorageFactory.java: ########## @@ -0,0 +1,226 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.gravitino.stats.storage; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Collectors; +import org.apache.gravitino.MetadataObject; +import org.apache.gravitino.stats.PartitionRange; +import org.apache.gravitino.stats.PartitionStatisticsDrop; +import org.apache.gravitino.stats.PartitionStatisticsUpdate; +import org.apache.gravitino.stats.StatisticValue; + +public class MemoryPartitionStatsStorageFactory implements PartitionStatisticStorageFactory { + + @Override + public PartitionStatisticStorage create(Map<String, String> properties) { + return new MemoryPartitionStatsStorage(); + } + + public static class MemoryPartitionStatsStorage implements PartitionStatisticStorage { + private static final Map<MetadataContainerKey, MetadataObjectStatisticsContainer> + totalStatistics = Maps.newConcurrentMap(); + + private MemoryPartitionStatsStorage() {} + + @Override + public List<PersistedPartitionStatistics> listStatistics( + String metalake, MetadataObject metadataObject, PartitionRange range) { + MetadataObjectStatisticsContainer tableStats = + totalStatistics.get(new MetadataContainerKey(metalake, metadataObject)); + + if (tableStats == null) { + return Lists.newArrayList(); + } + + Map<String, Map<String, StatisticValue<?>>> resultStats = Maps.newHashMap(); + for (PersistedPartitionStatistics partitionStat : tableStats.partitionStatistics().values()) { + String partitionName = partitionStat.partitionName(); + boolean lowerBoundSatisfied = + isBoundSatisfied( + range.lowerPartitionName(), + range.lowerBoundType(), + partitionName, + BoundDirection.LOWER); + + boolean upperBoundSatisfied = + isBoundSatisfied( + range.upperPartitionName(), + range.upperBoundType(), + partitionName, + BoundDirection.UPPER); + + if (lowerBoundSatisfied && upperBoundSatisfied) { + resultStats.put(partitionName, Maps.newHashMap(partitionStat.statistics())); + } + } + return resultStats.entrySet().stream() + .map(entry -> PersistedPartitionStatistics.of(entry.getKey(), entry.getValue())) + .collect(Collectors.toList()); + } + + private static boolean isBoundSatisfied( + Optional<String> boundPartitionName, + Optional<PartitionRange.BoundType> boundPartitionType, + String partitionName, + BoundDirection boundDirection) { + return boundPartitionName + .flatMap( + targetPartitionName -> + boundPartitionType.map( + type -> boundDirection.compare(targetPartitionName, partitionName, type))) + .orElse(true); + } + + @Override + public void updateStatistics(String metalake, List<MetadataObjectStatisticsUpdate> updates) { + for (MetadataObjectStatisticsUpdate update : updates) { + MetadataObject metadataObject = update.metadataObject(); + MetadataObjectStatisticsContainer tableStats = + totalStatistics.computeIfAbsent( + new MetadataContainerKey(metalake, metadataObject), + key -> new MetadataObjectStatisticsContainer(Maps.newHashMap())); + + List<PartitionStatisticsUpdate> stats = update.partitionUpdates(); + + for (PartitionStatisticsUpdate updatePartStat : stats) { + String partitionName = updatePartStat.partitionName(); + Map<String, StatisticValue<?>> partitionStats = updatePartStat.statistics(); + PersistedPartitionStatistics existedPartitionStats = + tableStats + .partitionStatistics() + .computeIfAbsent( + partitionName, + k -> PersistedPartitionStatistics.of(partitionName, new HashMap<>())); + for (Map.Entry<String, StatisticValue<?>> statEntry : partitionStats.entrySet()) { + String statName = statEntry.getKey(); + StatisticValue<?> statValue = statEntry.getValue(); + existedPartitionStats.statistics().put(statName, statValue); + } + } + } + } + + @Override + public List<PersistedPartitionStatistics> listStatistics( + String metalake, MetadataObject metadataObject, List<String> partitionNames) { + throw new UnsupportedOperationException( + "Don't support listing statistics by partition names"); + } + + @Override + public void dropStatistics(String metalake, List<MetadataObjectStatisticsDrop> drops) { + for (MetadataObjectStatisticsDrop drop : drops) { + MetadataObject metadataObject = drop.metadataObject(); + List<PartitionStatisticsDrop> partitionsToDrop = drop.drops(); + MetadataObjectStatisticsContainer tableStats = + totalStatistics.computeIfAbsent( + new MetadataContainerKey(metalake, metadataObject), + key -> new MetadataObjectStatisticsContainer(Maps.newHashMap())); + + for (PartitionStatisticsDrop partStats : partitionsToDrop) { + if (tableStats.partitionStatistics().containsKey(partStats.partitionName())) { + PersistedPartitionStatistics persistedPartitionStatistics = + tableStats.partitionStatistics().get(partStats.partitionName()); + for (String statName : partStats.statisticNames()) { + persistedPartitionStatistics.statistics().remove(statName); + } + if (persistedPartitionStatistics.statistics().isEmpty()) { + tableStats.partitionStatistics().remove(partStats.partitionName()); + } + } + } + + if (tableStats.partitionStatistics().isEmpty()) { + totalStatistics.remove(new MetadataContainerKey(metalake, metadataObject)); + } + } + } + + @Override + public void close() throws IOException {} Review Comment: Fixed. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
