zhjwpku commented on code in PR #408:
URL: https://github.com/apache/iceberg-cpp/pull/408#discussion_r2673018237


##########
src/iceberg/table_metadata.cc:
##########
@@ -982,6 +994,158 @@ void 
TableMetadataBuilder::Impl::SetLocation(std::string_view location) {
   
changes_.push_back(std::make_unique<table::SetLocation>(std::string(location)));
 }
 
+Status TableMetadataBuilder::Impl::AddSnapshot(std::shared_ptr<Snapshot> 
snapshot) {
+  if (snapshot == nullptr) {
+    // change is a noop
+    return {};
+  }
+  ICEBERG_CHECK(!metadata_.schemas.empty(),
+                "Attempting to add a snapshot before a schema is added");
+  ICEBERG_CHECK(!metadata_.partition_specs.empty(),
+                "Attempting to add a snapshot before a partition spec is 
added");
+  ICEBERG_CHECK(!metadata_.sort_orders.empty(),
+                "Attempting to add a snapshot before a sort order is added");
+
+  ICEBERG_CHECK(!snapshots_by_id_.contains(snapshot->snapshot_id),
+                "Snapshot already exists for id: {}", snapshot->snapshot_id);
+
+  ICEBERG_CHECK(
+      metadata_.format_version == 1 ||
+          snapshot->sequence_number > metadata_.last_sequence_number ||
+          !snapshot->parent_snapshot_id.has_value(),
+      "Cannot add snapshot with sequence number {} older than last sequence 
number {}",
+      snapshot->sequence_number, metadata_.last_sequence_number);
+
+  metadata_.last_updated_ms = snapshot->timestamp_ms;
+  metadata_.last_sequence_number = snapshot->sequence_number;
+
+  metadata_.snapshots.push_back(snapshot);
+  snapshots_by_id_.emplace(snapshot->snapshot_id, snapshot);
+
+  changes_.push_back(std::make_unique<table::AddSnapshot>(snapshot));
+
+  // Handle row lineage for format version >= 3
+  if (metadata_.format_version >= TableMetadata::kMinFormatVersionRowLineage) {
+    auto first_row_id = snapshot->FirstRowId();
+    ICEBERG_CHECK(first_row_id.has_value(),
+                  "Cannot add a snapshot: first-row-id is null");
+    ICEBERG_CHECK(
+        first_row_id.value() >= metadata_.next_row_id,
+        "Cannot add a snapshot, first-row-id is behind table next-row-id: {} < 
{}",
+        first_row_id.value(), metadata_.next_row_id);
+
+    metadata_.next_row_id += snapshot->AddedRows().value_or(0);
+  }
+
+  return {};
+}
+
+Status TableMetadataBuilder::Impl::SetBranchSnapshot(int64_t snapshot_id,

Review Comment:
   I think it's pretty much the same as AddSnapshot + SetBranchSnapshot(int64_t 
snapshot_id, const std::string& branch). We don't have 
TableMetadataBuilder::SetBranchSnapshot(std::shared_ptr<Snapshot> snapshot, 
const std::string& branch), so I guess we are good here.



-- 
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]

Reply via email to