@@ -592,6 +592,12 @@ class TableMetadataBuilder::Impl {
592592 Result<int32_t > AddSchema (const Schema& schema, int32_t new_last_column_id);
593593 void SetLocation (std::string_view location);
594594
595+ Status SetRef (const std::string& name, std::shared_ptr<SnapshotRef> ref);
596+ Status RemoveRef (const std::string& name);
597+ Status AddSnapshot (std::shared_ptr<Snapshot> snapshot);
598+ Status RemoveSnapshots (const std::vector<int64_t >& snapshot_ids);
599+ Status RemovePartitionSpecs (const std::vector<int32_t >& spec_ids);
600+
595601 Result<std::unique_ptr<TableMetadata>> Build ();
596602
597603 private:
@@ -1077,6 +1083,79 @@ int32_t TableMetadataBuilder::Impl::ReuseOrCreateNewSchemaId(
10771083 return new_schema_id;
10781084}
10791085
1086+ Status TableMetadataBuilder::Impl::SetRef (const std::string& name,
1087+ std::shared_ptr<SnapshotRef> ref) {
1088+ ICEBERG_PRECHECK (!metadata_.refs .contains (name),
1089+ " Cannot set ref: {}, which is already exist." , name);
1090+ metadata_.refs [name] = ref;
1091+ if (ref->type () == SnapshotRefType::kBranch ) {
1092+ auto retention = std::get<SnapshotRef::Branch>(ref->retention );
1093+ changes_.push_back (std::make_unique<table::SetSnapshotRef>(
1094+ name, ref->snapshot_id , ref->type (), retention.min_snapshots_to_keep ,
1095+ retention.max_snapshot_age_ms , retention.max_ref_age_ms ));
1096+ } else {
1097+ auto retention = std::get<SnapshotRef::Tag>(ref->retention );
1098+ changes_.push_back (std::make_unique<table::SetSnapshotRef>(
1099+ name, ref->snapshot_id , ref->type (), std::nullopt , std::nullopt ,
1100+ retention.max_ref_age_ms ));
1101+ }
1102+ return {};
1103+ }
1104+
1105+ Status TableMetadataBuilder::Impl::RemoveRef (const std::string& name) {
1106+ ICEBERG_PRECHECK (metadata_.refs .contains (name),
1107+ " Cannot remove ref: {}, which is not exist." , name);
1108+
1109+ metadata_.refs .erase (name);
1110+ changes_.push_back (std::make_unique<table::RemoveSnapshotRef>(name));
1111+
1112+ return {};
1113+ }
1114+
1115+ Status TableMetadataBuilder::Impl::AddSnapshot (std::shared_ptr<Snapshot> snapshot) {
1116+ // TODO(xiao.dong) this is only for test, not official complete implementation
1117+ metadata_.snapshots .emplace_back (std::move (snapshot));
1118+ return {};
1119+ }
1120+
1121+ Status TableMetadataBuilder::Impl::RemoveSnapshots (
1122+ const std::vector<int64_t >& snapshot_ids) {
1123+ auto current_snapshot_id = metadata_.current_snapshot_id ;
1124+ std::unordered_set<int64_t > snapshot_ids_set (snapshot_ids.begin (), snapshot_ids.end ());
1125+ ICEBERG_PRECHECK (!snapshot_ids_set.contains (current_snapshot_id),
1126+ " Cannot remove current snapshot: {}" , current_snapshot_id);
1127+
1128+ if (!snapshot_ids.empty ()) {
1129+ metadata_.snapshots =
1130+ metadata_.snapshots | std::views::filter ([&](const auto & snapshot) {
1131+ return !snapshot_ids_set.contains (snapshot->snapshot_id );
1132+ }) |
1133+ std::ranges::to<std::vector<std::shared_ptr<iceberg::Snapshot>>>();
1134+ changes_.push_back (std::make_unique<table::RemoveSnapshots>(snapshot_ids));
1135+ }
1136+
1137+ return {};
1138+ }
1139+
1140+ Status TableMetadataBuilder::Impl::RemovePartitionSpecs (
1141+ const std::vector<int32_t >& spec_ids) {
1142+ auto default_spec_id = metadata_.default_spec_id ;
1143+ std::unordered_set<int32_t > spec_ids_set (spec_ids.begin (), spec_ids.end ());
1144+ ICEBERG_PRECHECK (!spec_ids_set.contains (default_spec_id),
1145+ " Cannot remove default spec: {}" , default_spec_id);
1146+
1147+ if (!spec_ids.empty ()) {
1148+ metadata_.partition_specs =
1149+ metadata_.partition_specs | std::views::filter ([&](const auto & spec) {
1150+ return !spec_ids_set.contains (spec->spec_id ());
1151+ }) |
1152+ std::ranges::to<std::vector<std::shared_ptr<iceberg::PartitionSpec>>>();
1153+ changes_.push_back (std::make_unique<table::RemovePartitionSpecs>(spec_ids));
1154+ }
1155+
1156+ return {};
1157+ }
1158+
10801159TableMetadataBuilder::TableMetadataBuilder (int8_t format_version)
10811160 : impl_(std::make_unique<Impl>(format_version)) {}
10821161
@@ -1179,7 +1258,8 @@ TableMetadataBuilder& TableMetadataBuilder::AddPartitionSpec(
11791258
11801259TableMetadataBuilder& TableMetadataBuilder::RemovePartitionSpecs (
11811260 const std::vector<int32_t >& spec_ids) {
1182- throw IcebergError (std::format (" {} not implemented" , __FUNCTION__));
1261+ ICEBERG_BUILDER_RETURN_IF_ERROR (impl_->RemovePartitionSpecs (spec_ids));
1262+ return *this ;
11831263}
11841264
11851265TableMetadataBuilder& TableMetadataBuilder::RemoveSchemas (
@@ -1207,7 +1287,8 @@ TableMetadataBuilder& TableMetadataBuilder::AddSortOrder(
12071287
12081288TableMetadataBuilder& TableMetadataBuilder::AddSnapshot (
12091289 std::shared_ptr<Snapshot> snapshot) {
1210- throw IcebergError (std::format (" {} not implemented" , __FUNCTION__));
1290+ ICEBERG_BUILDER_RETURN_IF_ERROR (impl_->AddSnapshot (std::move (snapshot)));
1291+ return *this ;
12111292}
12121293
12131294TableMetadataBuilder& TableMetadataBuilder::SetBranchSnapshot (int64_t snapshot_id,
@@ -1217,11 +1298,13 @@ TableMetadataBuilder& TableMetadataBuilder::SetBranchSnapshot(int64_t snapshot_i
12171298
12181299TableMetadataBuilder& TableMetadataBuilder::SetRef (const std::string& name,
12191300 std::shared_ptr<SnapshotRef> ref) {
1220- throw IcebergError (std::format (" {} not implemented" , __FUNCTION__));
1301+ ICEBERG_BUILDER_RETURN_IF_ERROR (impl_->SetRef (name, std::move (ref)));
1302+ return *this ;
12211303}
12221304
12231305TableMetadataBuilder& TableMetadataBuilder::RemoveRef (const std::string& name) {
1224- throw IcebergError (std::format (" {} not implemented" , __FUNCTION__));
1306+ ICEBERG_BUILDER_RETURN_IF_ERROR (impl_->RemoveRef (name));
1307+ return *this ;
12251308}
12261309
12271310TableMetadataBuilder& TableMetadataBuilder::RemoveSnapshots (
@@ -1231,7 +1314,8 @@ TableMetadataBuilder& TableMetadataBuilder::RemoveSnapshots(
12311314
12321315TableMetadataBuilder& TableMetadataBuilder::RemoveSnapshots (
12331316 const std::vector<int64_t >& snapshot_ids) {
1234- throw IcebergError (std::format (" {} not implemented" , __FUNCTION__));
1317+ ICEBERG_BUILDER_RETURN_IF_ERROR (impl_->RemoveSnapshots (snapshot_ids));
1318+ return *this ;
12351319}
12361320
12371321TableMetadataBuilder& TableMetadataBuilder::SuppressHistoricalSnapshots () {
0 commit comments