JingsongLi commented on code in PR #1363: URL: https://github.com/apache/incubator-paimon/pull/1363#discussion_r1233632226
########## paimon-core/src/main/java/org/apache/paimon/operation/TagDeletion.java: ########## @@ -0,0 +1,172 @@ +/* + * 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.paimon.operation; + +import org.apache.paimon.Snapshot; +import org.apache.paimon.data.BinaryRow; +import org.apache.paimon.fs.FileIO; +import org.apache.paimon.fs.Path; +import org.apache.paimon.index.IndexFileHandler; +import org.apache.paimon.manifest.IndexManifestEntry; +import org.apache.paimon.manifest.ManifestEntry; +import org.apache.paimon.manifest.ManifestFile; +import org.apache.paimon.manifest.ManifestFileMeta; +import org.apache.paimon.manifest.ManifestList; +import org.apache.paimon.utils.FileStorePathFactory; +import org.apache.paimon.utils.SnapshotManager; +import org.apache.paimon.utils.TagManager; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.apache.paimon.operation.DeletionUtils.containsDataFile; +import static org.apache.paimon.operation.DeletionUtils.indexDataFiles; +import static org.apache.paimon.operation.DeletionUtils.readEntries; +import static org.apache.paimon.operation.DeletionUtils.tryDeleteDirectories; + +/** + * Delete tag files. This class doesn't check changelog files because they are handled by {@link + * SnapshotDeletion}. + */ +public class TagDeletion { + + private final FileIO fileIO; + private final FileStorePathFactory pathFactory; + private final ManifestList manifestList; + private final ManifestFile manifestFile; + private final IndexFileHandler indexFileHandler; + + private final SnapshotManager snapshotManager; + private final List<Snapshot> taggedSnapshots; + + private final Map<BinaryRow, Map<Integer, Set<String>>> skippedDataFiles; + private final Set<String> skippedManifests; + + public TagDeletion( + FileIO fileIO, + Path tablePath, + FileStorePathFactory pathFactory, + ManifestList manifestList, + ManifestFile manifestFile, + IndexFileHandler indexFileHandler) { + this.fileIO = fileIO; + this.pathFactory = pathFactory; + this.manifestList = manifestList; + this.manifestFile = manifestFile; + this.indexFileHandler = indexFileHandler; + + this.snapshotManager = new SnapshotManager(fileIO, tablePath); + this.taggedSnapshots = new TagManager(fileIO, tablePath).taggedSnapshots(); + + this.skippedDataFiles = new HashMap<>(); + this.skippedManifests = new HashSet<>(); + } + + /** Delete unused data files, manifest files and empty data file directories of tag. */ + public void delete(Snapshot taggedSnapshot) { + // collect from the earliest snapshot's base + Snapshot earliest = snapshotManager.snapshot(snapshotManager.earliestSnapshotId()); + collectSkip(earliest.baseManifests(manifestList)); + + // collect from the neighbor tags + int index = findIndex(taggedSnapshot); + if (index - 1 >= 0) { + collectSkip(taggedSnapshots.get(index - 1).dataManifests(manifestList)); + } + if (index + 1 < taggedSnapshots.size()) { + collectSkip(taggedSnapshots.get(index + 1).dataManifests(manifestList)); + } + + // delete data files and empty directories + deleteDataFiles(taggedSnapshot); + // delete manifests + deleteManifestFiles(taggedSnapshot); + } + + private void collectSkip(List<ManifestFileMeta> manifestFileMetas) { + Iterable<ManifestEntry> entries = readEntries(manifestFileMetas, manifestFile); + indexDataFiles(skippedDataFiles, ManifestEntry.mergeEntries(entries)); Review Comment: `ManifestEntry.mergeEntries` and `indexDataFiles` always together. You can introduce a method to do two things. -- 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]
