dongxiao1198 commented on code in PR #490: URL: https://github.com/apache/iceberg-cpp/pull/490#discussion_r2667399561
########## src/iceberg/update/expire_snapshots.cc: ########## @@ -0,0 +1,297 @@ +/* + * 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. + */ + +#include "iceberg/update/expire_snapshots.h" + +#include <cstdint> +#include <iostream> +#include <memory> +#include <unordered_set> +#include <vector> + +#include "iceberg/result.h" +#include "iceberg/schema.h" +#include "iceberg/snapshot.h" +#include "iceberg/table.h" +#include "iceberg/table_metadata.h" +#include "iceberg/transaction.h" +#include "iceberg/util/error_collector.h" +#include "iceberg/util/macros.h" +#include "iceberg/util/snapshot_util_internal.h" + +namespace iceberg { + +Result<std::shared_ptr<ExpireSnapshots>> ExpireSnapshots::Make( + std::shared_ptr<Transaction> transaction) { + ICEBERG_PRECHECK(transaction != nullptr, + "Cannot create ExpireSnapshots without a transaction"); + return std::shared_ptr<ExpireSnapshots>(new ExpireSnapshots(std::move(transaction))); +} + +ExpireSnapshots::ExpireSnapshots( + [[maybe_unused]] std::shared_ptr<Transaction> transaction) + : PendingUpdate(std::move(transaction)), + default_min_num_snapshots_( + transaction_->current().properties.Get(TableProperties::kMinSnapshotsToKeep)), + default_max_ref_age_ms_( + transaction_->current().properties.Get(TableProperties::kMaxRefAgeMs)), + current_time_ms_(std::chrono::time_point_cast<std::chrono::milliseconds>( + std::chrono::system_clock::now())) { + auto max_snapshot_age_ms = + transaction_->current().properties.Get(TableProperties::kMaxSnapshotAgeMs); + default_expire_older_than_ = + current_time_ms_.time_since_epoch().count() - max_snapshot_age_ms; +} + +ExpireSnapshots::~ExpireSnapshots() = default; + +ExpireSnapshots& ExpireSnapshots::ExpireSnapshotId(int64_t snapshot_id) { + const auto& current_metadata = transaction_->current(); + auto iter = std::ranges::find_if(current_metadata.snapshots, + [&](const std::shared_ptr<Snapshot>& snapshot) { + return snapshot->snapshot_id == snapshot_id; + }); + + ICEBERG_BUILDER_CHECK(iter != current_metadata.snapshots.end(), + "Snapshot:{} not exist.", snapshot_id); + snapshot_ids_to_expire_.push_back(snapshot_id); + return *this; +} + +ExpireSnapshots& ExpireSnapshots::ExpireOlderThan(int64_t timestamp_millis) { + ICEBERG_BUILDER_CHECK(timestamp_millis > 0, "Timestamp must be positive: {}", + timestamp_millis); + default_expire_older_than_ = timestamp_millis; + return *this; +} + +ExpireSnapshots& ExpireSnapshots::RetainLast(int num_snapshots) { + ICEBERG_BUILDER_CHECK(num_snapshots > 0, + "Number of snapshots to retain must be positive: {}", + num_snapshots); + default_min_num_snapshots_ = num_snapshots; + return *this; +} + +ExpireSnapshots& ExpireSnapshots::DeleteWith( + std::function<void(const std::string&)> delete_func) { + ICEBERG_BUILDER_CHECK(delete_func != nullptr, "Delete function cannot be null"); + delete_func_ = std::move(delete_func); + return *this; +} + +ExpireSnapshots& ExpireSnapshots::CleanupLevel(enum CleanupLevel level) { + cleanup_level_ = level; Review Comment: cleanExpiredFiles was deprecated in lastest version -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
