yihua commented on code in PR #12983: URL: https://github.com/apache/hudi/pull/12983#discussion_r2061057774
########## hudi-client/hudi-client-common/src/main/java/org/apache/hudi/metadata/index/record/RecordIndexer.java: ########## @@ -0,0 +1,199 @@ +/* + * 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.hudi.metadata.index.record; + +import org.apache.hudi.common.data.HoodieData; +import org.apache.hudi.common.engine.HoodieEngineContext; +import org.apache.hudi.common.model.FileSlice; +import org.apache.hudi.common.model.HoodieBaseFile; +import org.apache.hudi.common.model.HoodieRecord; +import org.apache.hudi.common.model.HoodieTableType; +import org.apache.hudi.common.table.HoodieTableMetaClient; +import org.apache.hudi.common.table.timeline.HoodieInstant; +import org.apache.hudi.common.table.view.HoodieTableFileSystemView; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.common.util.collection.Pair; +import org.apache.hudi.config.HoodieWriteConfig; +import org.apache.hudi.io.HoodieMergedReadHandle; +import org.apache.hudi.metadata.HoodieBackedTableMetadata; +import org.apache.hudi.metadata.HoodieMetadataPayload; +import org.apache.hudi.metadata.HoodieTableMetadataUtil; +import org.apache.hudi.metadata.index.Indexer; +import org.apache.hudi.table.HoodieTable; +import org.apache.hudi.util.Lazy; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import static org.apache.hudi.metadata.HoodieTableMetadata.SOLO_COMMIT_TIMESTAMP; +import static org.apache.hudi.metadata.HoodieTableMetadataUtil.readRecordKeysFromBaseFiles; +import static org.apache.hudi.metadata.MetadataPartitionType.RECORD_INDEX; + +public class RecordIndexer implements Indexer { + + private static final Logger LOG = LoggerFactory.getLogger(RecordIndexer.class); + // Average size of a record saved within the record index. + // Record index has a fixed size schema. This has been calculated based on experiments with default settings + // for block size (1MB), compression (GZ) and disabling the hudi metadata fields. + public static final int RECORD_INDEX_AVERAGE_RECORD_SIZE = 48; + private final HoodieEngineContext engineContext; + private final HoodieWriteConfig dataTableWriteConfig; + private final HoodieTableMetaClient dataTableMetaClient; + private final HoodieTable table; + + public RecordIndexer(HoodieEngineContext engineContext, + HoodieWriteConfig dataTableWriteConfig, + HoodieTableMetaClient dataTableMetaClient, + HoodieTable table) { + this.engineContext = engineContext; + this.dataTableWriteConfig = dataTableWriteConfig; + this.dataTableMetaClient = dataTableMetaClient; + this.table = table; + } + + @Override + public String getPartitionName() { + return RECORD_INDEX.getPartitionPath(); + } + + @Override + public Pair<Integer, HoodieData<HoodieRecord>> build( + List<HoodieTableMetadataUtil.DirectoryInfo> partitionInfoList, + Map<String, Map<String, Long>> partitionToFilesMap, + String createInstantTime, + Lazy<HoodieTableFileSystemView> fsView, + HoodieBackedTableMetadata metadata, + String instantTimeForPartition) throws IOException { + // TODO(yihua): could we unify the file listing across indexes? + // Collect the list of latest base files present in each partition + List<String> partitions = metadata.getAllPartitionPaths(); + fsView.get().loadAllPartitions(); + HoodieData<HoodieRecord> records = null; + if (dataTableMetaClient.getTableConfig().getTableType() == HoodieTableType.COPY_ON_WRITE) { + // for COW, we can only consider base files to initialize. + final List<Pair<String, HoodieBaseFile>> partitionBaseFilePairs = new ArrayList<>(); + for (String partition : partitions) { + partitionBaseFilePairs.addAll(fsView.get().getLatestBaseFiles(partition) + .map(basefile -> Pair.of(partition, basefile)).collect(Collectors.toList())); + } + + LOG.info("Initializing record index from " + partitionBaseFilePairs.size() + " base files in " Review Comment: Good catch. 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]
