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


##########
src/iceberg/update/snapshot_update.cc:
##########
@@ -0,0 +1,438 @@
+/*
+ * 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/snapshot_update.h"
+
+#include <charconv>
+#include <chrono>
+#include <format>
+
+#include "iceberg/file_io.h"
+#include "iceberg/manifest/manifest_entry.h"
+#include "iceberg/manifest/manifest_list.h"
+#include "iceberg/manifest/manifest_writer.h"
+#include "iceberg/manifest/rolling_manifest_writer.h"
+#include "iceberg/snapshot.h"
+#include "iceberg/table.h"
+#include "iceberg/util/macros.h"
+#include "iceberg/util/snapshot_util_internal.h"
+#include "iceberg/util/string_util.h"
+#include "iceberg/util/uuid.h"
+
+namespace iceberg {
+
+SnapshotUpdate::SnapshotUpdate(std::shared_ptr<Transaction> transaction)
+    : PendingUpdate(std::move(transaction)) {
+  target_manifest_size_bytes_ =
+      
transaction_->current().properties.Get(TableProperties::kManifestTargetSizeBytes);
+
+  // For format version 1, check if snapshot ID inheritance is enabled
+  if (transaction_->current().format_version == 1) {
+    can_inherit_snapshot_id_ = transaction_->current().properties.Get(
+        TableProperties::kSnapshotIdInheritanceEnabled);
+  }
+
+  // Generate commit UUID
+  commit_uuid_ = Uuid::GenerateV7().ToString();
+
+  // Initialize delete function if not set
+  if (!delete_func_) {
+    delete_func_ = [this](const std::string& path) {
+      return transaction_->table()->io()->DeleteFile(path);
+    };
+  }
+}
+
+Result<std::vector<ManifestFile>> SnapshotUpdate::WriteDataManifests(
+    const std::vector<DataFile>& data_files, const 
std::shared_ptr<PartitionSpec>& spec) {
+  if (data_files.empty()) {
+    return std::vector<ManifestFile>{};
+  }
+
+  ICEBERG_ASSIGN_OR_RAISE(auto current_schema, 
transaction_->current().Schema());
+
+  int8_t format_version = transaction_->current().format_version;
+  std::optional<int64_t> snapshot_id =
+      snapshot_id_ ? std::make_optional(*snapshot_id_) : std::nullopt;
+
+  // Create factory function for rolling manifest writer
+  RollingManifestWriter::ManifestWriterFactory factory =
+      [this, spec, current_schema, format_version,
+       snapshot_id]() -> Result<std::unique_ptr<ManifestWriter>> {
+    std::string manifest_path = ManifestPath();
+
+    if (format_version == 1) {
+      return ManifestWriter::MakeV1Writer(
+          snapshot_id, manifest_path, transaction_->table()->io(), spec, 
current_schema);
+    } else if (format_version == 2) {
+      return ManifestWriter::MakeV2Writer(snapshot_id, manifest_path,
+                                          transaction_->table()->io(), spec,
+                                          current_schema, 
ManifestContent::kData);
+    } else {  // format_version == 3
+      std::optional<int64_t> first_row_id =
+          transaction_->table()->metadata()->next_row_id;
+      return ManifestWriter::MakeV3Writer(snapshot_id, first_row_id, 
manifest_path,
+                                          transaction_->table()->io(), spec,
+                                          current_schema, 
ManifestContent::kData);
+    }
+  };
+
+  // Create rolling manifest writer
+  RollingManifestWriter rolling_writer(factory, target_manifest_size_bytes_);
+
+  // Write all files
+  for (const auto& file : data_files) {
+    ICEBERG_RETURN_UNEXPECTED(
+        rolling_writer.WriteAddedEntry(std::make_shared<DataFile>(file)));
+  }
+
+  // Close the rolling writer
+  ICEBERG_RETURN_UNEXPECTED(rolling_writer.Close());
+
+  // Get all manifest files
+  ICEBERG_ASSIGN_OR_RAISE(auto manifest_files, 
rolling_writer.ToManifestFiles());
+
+  return manifest_files;
+}
+
+Result<std::vector<ManifestFile>> SnapshotUpdate::WriteDeleteManifests(
+    const std::vector<DataFile>& delete_files,
+    const std::shared_ptr<PartitionSpec>& spec) {
+  if (delete_files.empty()) {
+    return std::vector<ManifestFile>{};
+  }
+
+  int8_t format_version = transaction_->current().format_version;
+  if (format_version < 2) {
+    // Delete manifests are only supported in format version 2+
+    return std::vector<ManifestFile>{};
+  }
+
+  ICEBERG_ASSIGN_OR_RAISE(auto current_schema, 
transaction_->current().Schema());
+
+  std::optional<int64_t> snapshot_id =
+      snapshot_id_ ? std::make_optional(*snapshot_id_) : std::nullopt;
+
+  // Create factory function for rolling manifest writer
+  RollingManifestWriter::ManifestWriterFactory factory =
+      [this, spec, current_schema, format_version,
+       snapshot_id]() -> Result<std::unique_ptr<ManifestWriter>> {
+    std::string manifest_path = ManifestPath();
+
+    if (format_version == 2) {
+      return ManifestWriter::MakeV2Writer(snapshot_id, manifest_path,
+                                          transaction_->table()->io(), spec,
+                                          current_schema, 
ManifestContent::kDeletes);
+    } else {  // format_version == 3
+      std::optional<int64_t> first_row_id =
+          transaction_->table()->metadata()->next_row_id;
+      return ManifestWriter::MakeV3Writer(snapshot_id, first_row_id, 
manifest_path,
+                                          transaction_->table()->io(), spec,
+                                          current_schema, 
ManifestContent::kDeletes);
+    }
+  };
+
+  // Create rolling manifest writer
+  RollingManifestWriter rolling_writer(factory, target_manifest_size_bytes_);
+
+  // Write all delete files
+  for (const auto& file : delete_files) {
+    ICEBERG_RETURN_UNEXPECTED(
+        rolling_writer.WriteAddedEntry(std::make_shared<DataFile>(file)));
+  }
+
+  // Close the rolling writer
+  ICEBERG_RETURN_UNEXPECTED(rolling_writer.Close());
+
+  // Get all manifest files
+  ICEBERG_ASSIGN_OR_RAISE(auto manifest_files, 
rolling_writer.ToManifestFiles());
+
+  return manifest_files;
+}
+
+int64_t SnapshotUpdate::SnapshotId() {
+  if (snapshot_id_.has_value()) {
+    return *snapshot_id_;
+  }
+  snapshot_id_ = SnapshotUtil::GenerateSnapshotId(transaction_->current());
+  return *snapshot_id_;
+}
+
+Result<SnapshotUpdate::ApplyResult> SnapshotUpdate::Apply() {
+  ICEBERG_RETURN_UNEXPECTED(CheckErrors());
+
+  // Get the latest snapshot for the target branch
+  std::shared_ptr<Snapshot> parent_snapshot;
+  std::optional<int64_t> parent_snapshot_id;
+  auto parent_snapshot_result =
+      SnapshotUtil::LatestSnapshot(transaction_->current(), target_branch_);
+  if (!parent_snapshot_result.has_value()) [[unlikely]] {
+    if (parent_snapshot_result.error().kind == ErrorKind::kNotFound) {
+      parent_snapshot_id = std::nullopt;
+    }
+    return std::unexpected<Error>(parent_snapshot_result.error());
+  } else {
+    parent_snapshot = *parent_snapshot_result;
+    parent_snapshot_id = parent_snapshot->snapshot_id;
+  }
+  int64_t sequence_number = transaction_->current().NextSequenceNumber();
+
+  ICEBERG_RETURN_UNEXPECTED(Validate(transaction_->current(), 
parent_snapshot));
+
+  std::vector<ManifestFile> manifests = Apply(transaction_->current(), 
parent_snapshot);
+
+  std::string manifest_list_path = ManifestListPath();
+  manifest_lists_.push_back(manifest_list_path);
+
+  // Create manifest list writer based on format version
+  int8_t format_version = transaction_->current().format_version;
+  int64_t snapshot_id = SnapshotId();
+  std::unique_ptr<ManifestListWriter> writer;
+
+  if (format_version == 1) {

Review Comment:
   Yeah, I think this deserves a separate PR, so #493 



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