rdblue commented on code in PR #19030:
URL: https://github.com/apache/iceberg/pull/19030#discussion_r4659305499


##########
core/src/main/java/org/apache/iceberg/RootManifestProducer.java:
##########
@@ -0,0 +1,188 @@
+/*
+ * 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.
+ */
+package org.apache.iceberg;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.iceberg.encryption.EncryptedOutputFile;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.apache.iceberg.types.Types;
+
+/** Responsible for producing v4 root manifest files for a commit. */
+class RootManifestProducer {
+  private final TableOperations ops;
+  private final FileIO io;
+  private final long snapshotId;
+  private final Map<String, String> writerProperties = Map.of();
+  private final List<TrackedFile> appendedDataFiles = Lists.newArrayList();
+
+  // internal state
+  private final String commitUUID = UUID.randomUUID().toString();
+  private final AtomicInteger attempt = new AtomicInteger(0);
+
+  // commit metadata
+  private final Set<String> manifestLocations = Sets.newHashSet();
+  private long assignedRowIds = 0;
+
+  RootManifestProducer(TableOperations ops, long snapshotId) {
+    this.ops = ops;
+    this.io = ops.io();
+    this.snapshotId = snapshotId;
+  }
+
+  void appendFile(DataFile dataFile) {
+    appendedDataFiles.add(TrackedFileAdapters.asTrackedFile(dataFile, 
ops.current().schema()));
+  }
+
+  TrackedFile apply(TableMetadata base, Snapshot parent, long 
attemptSequenceNumber) {
+    RootMetadata root = RootMetadata.fromSnapshot(base, parent, io);
+
+    manifestLocations.clear();
+    V4ManifestWriter rootWriter = newRootWriter(base, attemptSequenceNumber, 
base.nextRowId());
+    try (rootWriter) {
+      for (TrackedFile added : appendedDataFiles) {
+        rootWriter.append(added);
+      }
+
+      if (root != null) {
+        for (TrackedFile file : root.dataFiles()) {
+          if (file.tracking().isLive()) {
+            rootWriter.existing(file);
+          }
+        }
+
+        for (TrackedFile dataManifest : root.dataManifests()) {
+          if (dataManifest.tracking().isLive()) {
+            rootWriter.existing(dataManifest);

Review Comment:
   I think the logic is the same. Manifest list writer assigns missing first 
row IDs when they are missing:
   
   ```java
         if (manifest.content() != ManifestContent.DATA || 
manifest.firstRowId() != null) {
           return wrapper.wrap(manifest, null);
         } else {
           // assign first-row-id and update the next to assign
           wrapper.wrap(manifest, nextRowId);
           // leave space for existing and added rows, in case any of the 
existing data files do not
           // have an assigned first-row-id (this is the case with manifests 
from pre-v3 snapshots)
           this.nextRowId += manifest.existingRowsCount() + 
manifest.addedRowsCount();
           return wrapper;
         }
   ```
   
   And the `V4ManifestWriter` for root files does nearly the same thing:
   
   ```
         if (hasFirstRowId(tracking) || isDeletes(file.contentType())) {
           // no first-row-id assignment is needed
           return wrapper.wrap(file, tracking, null);
         } else {
           // assign first-row-id and update the next ID
           wrapper.wrap(file, tracking, nextRowId);
           this.nextRowId += rowsToAssign(file);
           return wrapper;
         }
   ```
   
   The `nextRowId` is updated by `manifest.addedRowsCount() + 
manifest.existingRowsCount() + manifest.modifiedRowsCount()` to account for 
entries that are modified in v4 instead of existing.



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