dramaticlly commented on code in PR #15590:
URL: https://github.com/apache/iceberg/pull/15590#discussion_r2943305950


##########
core/src/main/java/org/apache/iceberg/TableProperties.java:
##########
@@ -121,6 +121,10 @@ private TableProperties() {}
   public static final String MANIFEST_MERGE_ENABLED = 
"commit.manifest-merge.enabled";
   public static final boolean MANIFEST_MERGE_ENABLED_DEFAULT = true;
 
+  public static final String MANIFEST_FLUSH_FILE_COUNT_THRESHOLD =

Review Comment:
   After some offline discussion, I think we can keep the hardcoded default of 
100k for now, and there's room of improvement in RollingManifestWriter to 
support streaming write, which might keep the writer open in between of adding 
new entries. But this can follow up with a new PR with the mind to limit the 
scope of this PR.



##########
core/src/main/java/org/apache/iceberg/DataFileAccumulator.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.BiFunction;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import org.apache.iceberg.exceptions.RuntimeIOException;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.apache.iceberg.util.DataFileSet;
+
+/** Accumulates data files and flushes them to manifests when a count 
threshold is reached. */
+class DataFileAccumulator {
+
+  private final int flushThreshold;
+  private final BiFunction<Collection<DataFile>, Integer, List<ManifestFile>> 
writeManifests;
+  private final Function<ManifestFile, ManifestReader<DataFile>> readManifest;
+
+  private final Map<Integer, DataFileSet> pendingBySpec = Maps.newHashMap();
+  private final List<ManifestFile> flushedManifests = Lists.newLinkedList();
+  private final Set<Integer> allSpecIds = Sets.newHashSet();
+  private int pendingCount = 0;
+
+  DataFileAccumulator(
+      int flushThreshold,
+      BiFunction<Collection<DataFile>, Integer, List<ManifestFile>> 
writeManifests,
+      Function<ManifestFile, ManifestReader<DataFile>> readManifest) {
+    this.flushThreshold = flushThreshold;
+    this.writeManifests = writeManifests;
+    this.readManifest = readManifest;
+  }
+
+  /**
+   * Adds a data file. Returns true if the file was new. May flush to 
manifests if the threshold is
+   * reached.
+   */
+  boolean add(DataFile file, int specId) {
+    DataFileSet files = pendingBySpec.computeIfAbsent(specId, ignored -> 
DataFileSet.create());
+    if (files.add(file)) {
+      allSpecIds.add(specId);
+      pendingCount++;
+      if (pendingCount >= flushThreshold) {
+        flush();
+      }
+      return true;
+    }
+    return false;
+  }
+
+  boolean hasFiles() {
+    return !pendingBySpec.isEmpty() || !flushedManifests.isEmpty();
+  }
+
+  boolean hasPendingFiles() {
+    return !pendingBySpec.isEmpty();
+  }
+
+  /** All partition spec IDs seen across adds. */
+  Set<Integer> specIds() {
+    return Collections.unmodifiableSet(allSpecIds);
+  }
+
+  Map<Integer, DataFileSet> pendingBySpec() {
+    return pendingBySpec;
+  }
+
+  List<ManifestFile> flushedManifests() {
+    return flushedManifests;
+  }
+
+  /**
+   * All added data files (flushed + pending). Flushed files are lazily read 
back from their
+   * manifests to avoid holding them in memory during the add phase.
+   */
+  List<DataFile> allAddedFiles() {
+    ImmutableList.Builder<DataFile> builder = ImmutableList.builder();
+
+    for (ManifestFile manifest : flushedManifests) {
+      try (ManifestReader<DataFile> reader = readManifest.apply(manifest)) {
+        for (DataFile file : reader) {
+          builder.add(file);

Review Comment:
   Good point about lazy-materialization as it would defer when each manifest's 
files enter the heap (one batch at a time vs all at once) but I also want to 
share that.
   
   For the primary append path, `allAddedFiles()` is never called at all, The 
only caller is `BaseOverwriteFiles.validate()`, and only when 
`validateAddedFilesMatchOverwriteFilter` is true. Currently the 
`OverwriteFile::validateAddedFilesMatchOverwriteFilter` is not used anywhere 
other that unit test.
   
   I can maybe start a separate thread to see if we still need it.



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