RussellSpitzer commented on code in PR #15590: URL: https://github.com/apache/iceberg/pull/15590#discussion_r3034314571
########## core/src/main/java/org/apache/iceberg/DataFileAccumulator.java: ########## @@ -0,0 +1,134 @@ +/* + * 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.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.io.CloseableIterable; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.Iterables; +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 { + + static final int DEFAULT_FLUSH_THRESHOLD = 100_000; + + private final int flushThreshold; + private final BiFunction<Collection<DataFile>, Integer, List<ManifestFile>> writeManifests; + private final Function<ManifestFile, CloseableIterable<DataFile>> readManifest; + + private final Map<Integer, DataFileSet> pendingBySpec = Maps.newHashMap(); + private final List<ManifestFile> flushedManifests = Lists.newArrayList(); + private final Set<Integer> allSpecIds = Sets.newHashSet(); + private int pendingCount = 0; + + DataFileAccumulator( + int flushThreshold, + BiFunction<Collection<DataFile>, Integer, List<ManifestFile>> writeManifests, + Function<ManifestFile, CloseableIterable<DataFile>> readManifest) { + Preconditions.checkArgument( + flushThreshold > 0, "Flush threshold must be positive: %s", flushThreshold); + 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()); Review Comment: We used to hold a global set here and that made it impossible to add duplicate entries for the same file. Now we can theoretically add the same file to the same spec multiple times as long as it's between flushes. We didn't really "rely" on this before (we wouldn't break we would just silently ignore dupes). I think it's probably fine to not have that check anymore -- 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]
