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


##########
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;
+  }

Review Comment:
   This is actually insufficient here if we really care because the underlying 
entries can still be changed here (they are not immutable) even if the 
collection is immutable. But probably safe enough



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