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


##########
core/src/main/java/org/apache/iceberg/V4ManifestWriter.java:
##########
@@ -0,0 +1,393 @@
+/*
+ * 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.Closeable;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.Map;
+import org.apache.iceberg.encryption.EncryptedOutputFile;
+import org.apache.iceberg.encryption.EncryptionKeyMetadata;
+import org.apache.iceberg.encryption.NativeEncryptionOutputFile;
+import org.apache.iceberg.io.FileAppender;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.types.Types;
+
+abstract class V4ManifestWriter implements Closeable {
+  private static final int FORMAT_VERSION = 4;
+  private static final long UNASSIGNED_SEQ = -1L;
+
+  // TODO:
+  // - Relativize paths
+
+  static V4ManifestWriter createRoot(
+      EncryptedOutputFile file,
+      Map<Integer, PartitionSpec> specsById,
+      Types.StructType contentStatsType,
+      Map<String, String> writerProperties,
+      int tableSchemaId,
+      long snapshotId,
+      long sequenceNumber,
+      long firstRowId) {
+    Types.StructType partitionType = 
Partitioning.unionPartitionTypes(specsById.values());
+    return new V4ManifestWriter.RootWriter(
+        file,
+        specsById,
+        partitionType,
+        contentStatsType,
+        writerProperties,
+        tableSchemaId,
+        snapshotId,
+        sequenceNumber,
+        firstRowId);
+  }
+
+  static V4ManifestWriter createLeaf(
+      EncryptedOutputFile file,
+      Map<Integer, PartitionSpec> specsById,
+      Types.StructType contentStatsType,
+      Map<String, String> writerProperties,
+      int tableSchemaId,
+      Long snapshotId) {
+    Types.StructType partitionType = 
Partitioning.unionPartitionTypes(specsById.values());
+    return new V4ManifestWriter.LeafWriter(
+        file,
+        specsById,
+        partitionType,
+        contentStatsType,
+        writerProperties,
+        tableSchemaId,
+        snapshotId);
+  }
+
+  private final OutputFile output;
+  private final EncryptionKeyMetadata keyMetadata;
+  private final Schema manifestSchema;
+  private final Long snapshotId;
+  private final FileAppender<StructLike> writer;
+
+  private boolean closed = false;
+  private int recordCount = 0;
+  private int addedFiles = 0;
+  private int existingFiles = 0;
+  private int deletedFiles = 0;
+  private int replacedFiles = 0;
+  private int modifiedFiles = 0;
+  private long addedRows = 0L;
+  private long existingRows = 0L;
+  private long deletedRows = 0L;
+  private long replacedRows = 0L;
+  private long modifiedRows = 0L;
+  private long minDataSequenceNumber = UNASSIGNED_SEQ;
+  private long unassignedRows = 0L;
+
+  private V4ManifestWriter(
+      EncryptedOutputFile output,
+      Schema manifestSchema,
+      Map<String, String> fileMetadata,
+      Map<String, String> writerProperties,
+      Long snapshotId) {
+    this.output = outputFile(output);
+    this.manifestSchema = manifestSchema;
+    this.keyMetadata = output.keyMetadata();
+    this.snapshotId = snapshotId;
+    this.writer = newAppender(this.output, fileMetadata, writerProperties);
+  }
+
+  protected abstract StructLike prepare(TrackedFile file, Tracking tracking);
+
+  // TODO: add row ID space to spec and ManifestInfo
+  public long assignedRowIds() {
+    return unassignedRows;
+  }
+
+  private FileAppender<StructLike> newAppender(
+      OutputFile file, Map<String, String> fileMetadata, Map<String, String> 
writerProperties) {
+    try {
+      return InternalData.write(FileFormat.PARQUET, file)
+          .schema(manifestSchema)
+          .named("tracked_file")
+          .meta(fileMetadata)
+          .set(writerProperties)
+          .overwrite()
+          .build();
+    } catch (IOException e) {
+      throw new UncheckedIOException(
+          "Failed to create manifest writer for location: " + file.location(), 
e);
+    }
+  }
+
+  private ManifestInfo summary() {
+    return new ManifestInfoStruct(
+        addedFiles,
+        existingFiles,
+        deletedFiles,
+        replacedFiles,
+        modifiedFiles,
+        addedRows,
+        existingRows,
+        deletedRows,
+        replacedRows,
+        modifiedRows,
+        minDataSequenceNumber,
+        null, // no manifest DV
+        FORMAT_VERSION);
+  }
+
+  public void append(TrackedFile file) {
+    TrackingBuilder added = TrackingBuilder.added(snapshotId);
+    if (file.deletionVector() != null) {
+      added.dvUpdated();
+    }
+
+    write(file, added.build());
+  }
+
+  public void appendAtSeq(TrackedFile file, long dataSequenceNumber) {
+    TrackingBuilder added = TrackingBuilder.added(snapshotId, 
dataSequenceNumber);
+    if (file.deletionVector() != null) {
+      added.dvUpdated();
+    }
+
+    write(file, added.build());
+  }
+
+  public void modify(TrackedFile file, DeletionVector dv) {
+    TrackingBuilder modified = TrackingBuilder.from(file.tracking(), 
snapshotId).dvUpdated();
+    TrackedFile withNewDV = Delegates.withDV(file, dv);
+    write(withNewDV, modified.build());
+  }
+
+  public void existing(TrackedFile file) {
+    Tracking existing = TrackingBuilder.from(file.tracking(), 
snapshotId).build();
+    write(file, existing);
+  }
+
+  public void replace(TrackedFile file) {
+    Tracking replaced = TrackingBuilder.replaced(file.tracking(), snapshotId);
+    write(file, replaced);
+  }
+
+  public void delete(TrackedFile file) {
+    Tracking deleted = TrackingBuilder.deleted(file.tracking(), snapshotId);
+    write(file, deleted);
+  }
+
+  private void write(TrackedFile file, Tracking tracking) {
+    this.recordCount += 1;
+    updateManifestInfo(file, tracking);
+    writer.add(prepare(file, tracking));
+  }
+
+  public long length() {
+    return writer.length();
+  }
+
+  @Override
+  public void close() throws IOException {
+    this.closed = true;
+    writer.close();
+  }
+
+  private void updateManifestInfo(TrackedFile file, Tracking tracking) {

Review Comment:
   This was originally part of the leaf writer, but I moved it in case 
`ManifestInfo` is useful to the caller producing a root manifest. For instance, 
we could use this in snapshot properties.
   
   This was correct to identify that `file.recordCount()` isn't right for leaf 
manifests. I'll update this to use `rowsToAssign(file)`. I intended to come 
back to this in the next iteration for writing manifest files to the root, but 
it's a simple fix for now.
   
   For the min sequence number, I'll update this to only aggregate sequence 
numbers from data files. I don't think it is useful to aggregate the min 
sequence number across the entire table. It probably isn't useful at the root 
level at all, but at least it will be consistent that way.



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