Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/iceberg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ set(ICEBERG_SOURCES
transform.cc
transform_function.cc
type.cc
update/expire_snapshots.cc
update/update_properties.cc
util/bucket_util.cc
util/conversions.cc
Expand Down
2 changes: 2 additions & 0 deletions src/iceberg/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ iceberg_sources = files(
'transform.cc',
'transform_function.cc',
'type.cc',
'update/expire_snapshots.cc',
'update/update_properties.cc',
'util/bucket_util.cc',
'util/conversions.cc',
Expand Down Expand Up @@ -202,6 +203,7 @@ subdir('catalog')
subdir('expression')
subdir('manifest')
subdir('row')
subdir('update')
subdir('util')

if get_option('tests').enabled()
Expand Down
40 changes: 40 additions & 0 deletions src/iceberg/pending_update.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,44 @@ class ICEBERG_EXPORT PendingUpdate : public ErrorCollector {
PendingUpdate() = default;
};

/// \brief Template class for type-safe table metadata changes using builder pattern
///
/// PendingUpdateTyped extends PendingUpdate with a type-safe Apply() method that
/// returns the specific result type for each operation. Subclasses implement
/// specific types of table updates such as schema changes, property updates, or
/// snapshot-producing operations like appends and deletes.
///
/// Apply() can be used to validate and inspect the uncommitted changes before
/// committing. Commit() applies the changes and commits them to the table.
///
/// \tparam T The type of result returned by Apply()
template <typename T>
class ICEBERG_EXPORT PendingUpdateTyped : public PendingUpdate {
public:
~PendingUpdateTyped() override = default;

/// \brief Apply the pending changes and return the uncommitted result (typed version)
///
/// This does not result in a permanent update.
///
/// \return the uncommitted changes that would be committed, or an error:
/// - ValidationFailed: if pending changes cannot be applied
/// - InvalidArgument: if pending changes are conflicting
virtual Result<T> ApplyTyped() = 0;

/// \brief Apply the pending changes (base class override)
///
/// This implementation calls ApplyTyped() and discards the result.
Status Apply() override {
auto result = ApplyTyped();
if (!result.has_value()) {
return std::unexpected(result.error());
}
return {};
}

protected:
PendingUpdateTyped() = default;
};

} // namespace iceberg
5 changes: 5 additions & 0 deletions src/iceberg/table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "iceberg/table_metadata.h"
#include "iceberg/table_properties.h"
#include "iceberg/table_scan.h"
#include "iceberg/update/expire_snapshots.h"
#include "iceberg/update/update_properties.h"
#include "iceberg/util/macros.h"

Expand Down Expand Up @@ -117,6 +118,10 @@ std::unique_ptr<Transaction> Table::NewTransaction() const {
throw NotImplemented("Table::NewTransaction is not implemented");
}

std::unique_ptr<iceberg::ExpireSnapshots> Table::NewExpireSnapshots() {
return std::make_unique<iceberg::ExpireSnapshots>(this);
}

const std::shared_ptr<FileIO>& Table::io() const { return io_; }

std::unique_ptr<TableScanBuilder> Table::NewScan() const {
Expand Down
5 changes: 5 additions & 0 deletions src/iceberg/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ class ICEBERG_EXPORT Table {
/// \return a pointer to the new Transaction
virtual std::unique_ptr<Transaction> NewTransaction() const;

/// \brief Create a new expire snapshots operation for this table
///
/// \return a unique pointer to the new ExpireSnapshots operation
virtual std::unique_ptr<ExpireSnapshots> NewExpireSnapshots();

/// \brief Returns a FileIO to read and write table data and metadata files
const std::shared_ptr<FileIO>& io() const;

Expand Down
2 changes: 2 additions & 0 deletions src/iceberg/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ add_iceberg_test(expression_test
predicate_test.cc
strict_metrics_evaluator_test.cc)

add_iceberg_test(update_test SOURCES update/expire_snapshots_test.cc)

add_iceberg_test(json_serde_test
SOURCES
json_internal_test.cc
Expand Down
158 changes: 158 additions & 0 deletions src/iceberg/test/update/expire_snapshots_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* 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 <memory>
#include <vector>

#include <gtest/gtest.h>

#include "iceberg/result.h"
#include "iceberg/snapshot.h"
#include "iceberg/test/matchers.h"

namespace iceberg {

// Basic API tests for ExpireSnapshots
// Full functional tests will be added when the implementation is complete

TEST(ExpireSnapshotsTest, FluentApiChaining) {
// Test that the fluent API works correctly with method chaining
ExpireSnapshots expire(nullptr);

auto& result =
expire.ExpireSnapshotId(123).ExpireOlderThan(1000000).RetainLast(5).SetCleanupLevel(
CleanupLevel::kMetadataOnly);

// Verify that chaining returns the same object
EXPECT_EQ(&result, &expire);
}

TEST(ExpireSnapshotsTest, ExpireSnapshotId) {
ExpireSnapshots expire(nullptr);
expire.ExpireSnapshotId(123);

// Currently returns NotImplemented - this test will be expanded
// when the actual implementation is added
auto result = expire.Apply();
EXPECT_THAT(result, IsError(ErrorKind::kNotImplemented));
}

TEST(ExpireSnapshotsTest, ExpireMultipleSnapshotIds) {
ExpireSnapshots expire(nullptr);
expire.ExpireSnapshotId(100).ExpireSnapshotId(200).ExpireSnapshotId(300);

// Currently returns NotImplemented
auto result = expire.Apply();
EXPECT_THAT(result, IsError(ErrorKind::kNotImplemented));
}

TEST(ExpireSnapshotsTest, ExpireOlderThan) {
ExpireSnapshots expire(nullptr);
int64_t timestamp = 1609459200000; // 2021-01-01 00:00:00 UTC
expire.ExpireOlderThan(timestamp);

// Currently returns NotImplemented
auto result = expire.Apply();
EXPECT_THAT(result, IsError(ErrorKind::kNotImplemented));
}

TEST(ExpireSnapshotsTest, RetainLast) {
ExpireSnapshots expire(nullptr);
expire.RetainLast(10);

// Currently returns NotImplemented
auto result = expire.Apply();
EXPECT_THAT(result, IsError(ErrorKind::kNotImplemented));
}

TEST(ExpireSnapshotsTest, DeleteWithCallback) {
ExpireSnapshots expire(nullptr);
std::vector<std::string> deleted_files;

expire.DeleteWith(
[&deleted_files](std::string_view file) { deleted_files.emplace_back(file); });

// Currently returns NotImplemented
auto result = expire.Commit();
EXPECT_THAT(result, IsError(ErrorKind::kNotImplemented));
}

TEST(ExpireSnapshotsTest, CleanupLevelNone) {
ExpireSnapshots expire(nullptr);
expire.SetCleanupLevel(CleanupLevel::kNone);

// Currently returns NotImplemented
auto result = expire.Apply();
EXPECT_THAT(result, IsError(ErrorKind::kNotImplemented));
}

TEST(ExpireSnapshotsTest, CleanupLevelMetadataOnly) {
ExpireSnapshots expire(nullptr);
expire.SetCleanupLevel(CleanupLevel::kMetadataOnly);

// Currently returns NotImplemented
auto result = expire.Apply();
EXPECT_THAT(result, IsError(ErrorKind::kNotImplemented));
}

TEST(ExpireSnapshotsTest, CleanupLevelAll) {
ExpireSnapshots expire(nullptr);
expire.SetCleanupLevel(CleanupLevel::kAll);

// Currently returns NotImplemented
auto result = expire.Apply();
EXPECT_THAT(result, IsError(ErrorKind::kNotImplemented));
}

TEST(ExpireSnapshotsTest, CombinedConfiguration) {
ExpireSnapshots expire(nullptr);
int64_t timestamp = 1609459200000;

expire.ExpireSnapshotId(100)
.ExpireSnapshotId(200)
.ExpireOlderThan(timestamp)
.RetainLast(5)
.SetCleanupLevel(CleanupLevel::kMetadataOnly);

// Currently returns NotImplemented
auto result = expire.Apply();
EXPECT_THAT(result, IsError(ErrorKind::kNotImplemented));
}

TEST(ExpireSnapshotsTest, CommitNotImplemented) {
ExpireSnapshots expire(nullptr);
expire.ExpireSnapshotId(123);

// Currently returns NotImplemented
auto result = expire.Commit();
EXPECT_THAT(result, IsError(ErrorKind::kNotImplemented));
}

TEST(ExpireSnapshotsTest, ApplyNotImplemented) {
ExpireSnapshots expire(nullptr);
expire.ExpireOlderThan(1000000);

// Currently returns NotImplemented
auto result = expire.Apply();
EXPECT_THAT(result, IsError(ErrorKind::kNotImplemented));
}

} // namespace iceberg
5 changes: 5 additions & 0 deletions src/iceberg/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class ICEBERG_EXPORT Transaction {
/// \return a new AppendFiles
virtual std::shared_ptr<AppendFiles> NewAppend() = 0;

/// \brief Create a new expire snapshots operation for this transaction
///
/// \return a unique pointer to the new ExpireSnapshots operation
virtual std::unique_ptr<ExpireSnapshots> NewExpireSnapshots() = 0;

/// \brief Apply the pending changes from all actions and commit
///
/// This method applies all pending data operations and metadata updates in the
Expand Down
3 changes: 3 additions & 0 deletions src/iceberg/type_fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ template <typename T>
class PendingUpdateTyped;
class UpdateProperties;

enum class CleanupLevel;
class ExpireSnapshots;

/// ----------------------------------------------------------------------------
/// TODO: Forward declarations below are not added yet.
/// ----------------------------------------------------------------------------
Expand Down
67 changes: 67 additions & 0 deletions src/iceberg/update/expire_snapshots.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 "iceberg/result.h"
#include "iceberg/snapshot.h"
#include "iceberg/table.h"
#include "iceberg/table_metadata.h"

namespace iceberg {

ExpireSnapshots::ExpireSnapshots(Table* table) : table_(table) {}

ExpireSnapshots& ExpireSnapshots::ExpireSnapshotId(int64_t snapshot_id) {
snapshot_ids_to_expire_.push_back(snapshot_id);
return *this;
}

ExpireSnapshots& ExpireSnapshots::ExpireOlderThan(int64_t timestamp_millis) {
expire_older_than_ms_ = timestamp_millis;
return *this;
}

ExpireSnapshots& ExpireSnapshots::RetainLast(int num_snapshots) {
retain_last_ = num_snapshots;
return *this;
}

ExpireSnapshots& ExpireSnapshots::DeleteWith(
std::function<void(std::string_view)> delete_func) {
delete_func_ = std::move(delete_func);
return *this;
}

ExpireSnapshots& ExpireSnapshots::SetCleanupLevel(CleanupLevel level) {
cleanup_level_ = level;
return *this;
}

Result<std::vector<std::shared_ptr<Snapshot>>> ExpireSnapshots::ApplyTyped() {
// Placeholder implementation - full snapshot expiration logic to be implemented
return NotImplemented("ExpireSnapshots::ApplyTyped() is not yet implemented");
}

Status ExpireSnapshots::Commit() {
// Placeholder implementation - full commit logic to be implemented
return NotImplemented("ExpireSnapshots::Commit() is not yet implemented");
}

} // namespace iceberg
Loading
Loading