RussellSpitzer commented on a change in pull request #1264: URL: https://github.com/apache/iceberg/pull/1264#discussion_r466012991
########## File path: spark/src/main/java/org/apache/iceberg/actions/ExpireSnapshotsAction.java ########## @@ -0,0 +1,156 @@ +/* + * 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.iceberg.actions; + +import java.util.List; +import java.util.Set; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; +import java.util.stream.Collectors; +import org.apache.iceberg.ExpireSnapshots; +import org.apache.iceberg.HasTableOperations; +import org.apache.iceberg.Table; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.TableOperations; +import org.apache.iceberg.exceptions.NotFoundException; +import org.apache.iceberg.relocated.com.google.common.base.Joiner; +import org.apache.iceberg.util.Tasks; +import org.apache.iceberg.util.ThreadPools; +import org.apache.spark.api.java.JavaSparkContext; +import org.apache.spark.sql.Column; +import org.apache.spark.sql.Dataset; +import org.apache.spark.sql.Row; +import org.apache.spark.sql.SparkSession; +import org.apache.spark.sql.functions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ExpireSnapshotsAction extends BaseAction<ExpireSnapshotActionResult> { + private static final Logger LOG = LoggerFactory.getLogger(ExpireSnapshotsAction.class); + + private final SparkSession spark; + private final JavaSparkContext sparkContext; + private final Table table; + private final TableOperations ops; + private final ExpireSnapshots localExpireSnapshots; + private final TableMetadata base; + private static final String DATAFILE = "Data File"; + private static final String MANIFEST = "Manifest"; + private static final String MANIFESTLIST = "Manifest List"; + private static final String OTHER = "Other"; + + private final Consumer<String> defaultDelete = new Consumer<String>() { + @Override + public void accept(String file) { + ops.io().deleteFile(file); + } + }; + private Consumer<String> deleteFunc = defaultDelete; + + + ExpireSnapshotsAction(SparkSession spark, Table table) { + this.spark = spark; + this.sparkContext = JavaSparkContext.fromSparkContext(spark.sparkContext()); + this.table = table; + this.ops = ((HasTableOperations) table).operations(); + this.base = ops.current(); + this.localExpireSnapshots = table.expireSnapshots().cleanExpiredFiles(false); + } + + public ExpireSnapshotsAction expireSnapshotId(long expireSnapshotId) { + localExpireSnapshots.expireSnapshotId(expireSnapshotId); + return this; + } + + public ExpireSnapshotsAction expireOlderThan(long timestampMillis) { + localExpireSnapshots.expireOlderThan(timestampMillis); + return this; + } + + public ExpireSnapshotsAction retainLast(int numSnapshots) { + localExpireSnapshots.retainLast(numSnapshots); + return this; + } + + public ExpireSnapshotsAction deleteWith(Consumer<String> newDeleteFunc) { + deleteFunc = newDeleteFunc; + return this; + } + + + @Override + protected Table table() { + return table; + } + + private Dataset<Row> appendTypeString(Dataset<Row> ds, String type) { + return ds.select(new Column("file_path"), functions.lit(type).as("DataFile")); + } + + private Dataset<Row> getValidFileDF() { Review comment: sgtm ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
