stevenzwu commented on code in PR #12774:
URL: https://github.com/apache/iceberg/pull/12774#discussion_r2350292523


##########
core/src/main/java/org/apache/iceberg/data/ContentFileWriteBuilderImpl.java:
##########
@@ -0,0 +1,336 @@
+/*
+ * 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.data;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.Metrics;
+import org.apache.iceberg.MetricsConfig;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.SortOrder;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.deletes.EqualityDeleteWriter;
+import org.apache.iceberg.deletes.PositionDelete;
+import org.apache.iceberg.deletes.PositionDeleteWriter;
+import org.apache.iceberg.encryption.EncryptionKeyMetadata;
+import org.apache.iceberg.io.DataWriter;
+import org.apache.iceberg.io.FileAppender;
+import org.apache.iceberg.io.WriteBuilder;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+
+/**
+ * An internal implementation that handles all {@link ContentFileWriteBuilder} 
interface variants.
+ *
+ * <p>This unified implementation serves as a backend for multiple specialized 
content writers:
+ *
+ * <ul>
+ *   <li>{@link DataWriteBuilder} for creating data files
+ *   <li>{@link EqualityDeleteWriteBuilder} for creating equality delete files
+ *   <li>{@link PositionDeleteWriteBuilder} for creating position delete files
+ * </ul>
+ *
+ * <p>The implementation delegates to a format-specific {@link WriteBuilder} 
while enriching it with
+ * content-specific functionality. When building a writer, the implementation 
configures the
+ * underlying builder and calls its {@link WriteBuilder#build()} method to 
create the appropriate
+ * specialized writer for the requested content type.
+ *
+ * @param <B> the concrete builder type for method chaining
+ * @param <S> the type of the schema for the input data
+ * @param <D> the type of data records the writer will accept
+ */
+@SuppressWarnings("unchecked")
+abstract class ContentFileWriteBuilderImpl<B extends 
ContentFileWriteBuilder<B, S>, D, S>
+    implements ContentFileWriteBuilder<B, S> {
+  private final WriteBuilder<D, S> writeBuilder;
+  private final String location;
+  private final FileFormat format;
+  private PartitionSpec spec = null;
+  private StructLike partition = null;
+  private EncryptionKeyMetadata keyMetadata = null;
+  private SortOrder sortOrder = null;
+
+  static <D, S> DataWriteBuilder<D, S> forDataFile(
+      WriteBuilder<D, S> writeBuilder, String location, FileFormat format) {
+    return new DataFileWriteBuilder<>(writeBuilder, location, format);
+  }
+
+  static <D, S> EqualityDeleteWriteBuilder<D, S> forEqualityDelete(
+      WriteBuilder<D, S> writeBuilder, String location, FileFormat format) {
+    return new EqualityDeleteFileWriteBuilder<>(writeBuilder, location, 
format);
+  }
+
+  static <D, S> PositionDeleteWriteBuilder<D, S> forPositionDelete(
+      WriteBuilder<PositionDelete<D>, S> writeBuilder, String location, 
FileFormat format) {
+    return new PositionDeleteFileWriteBuilder<>(writeBuilder, location, 
format);
+  }
+
+  private ContentFileWriteBuilderImpl(
+      WriteBuilder<D, S> writeBuilder, String location, FileFormat format) {
+    this.writeBuilder = writeBuilder;
+    this.location = location;
+    this.format = format;
+  }
+
+  @Override
+  public B set(String property, String value) {
+    writeBuilder.set(property, value);
+    return self();
+  }
+
+  @Override
+  public B meta(String property, String value) {
+    writeBuilder.meta(property, value);
+    return self();
+  }
+
+  @Override
+  public B metricsConfig(MetricsConfig metricsConfig) {
+    writeBuilder.metricsConfig(metricsConfig);
+    return self();
+  }
+
+  @Override
+  public B overwrite() {
+    writeBuilder.overwrite();
+    return self();
+  }
+
+  @Override
+  public B fileEncryptionKey(ByteBuffer encryptionKey) {
+    writeBuilder.fileEncryptionKey(encryptionKey);
+    return self();
+  }
+
+  @Override
+  public B fileAADPrefix(ByteBuffer aadPrefix) {
+    writeBuilder.fileAADPrefix(aadPrefix);
+    return self();
+  }
+
+  @Override
+  public B spec(PartitionSpec newSpec) {
+    this.spec = newSpec;
+    return self();
+  }
+
+  @Override
+  public B partition(StructLike newPartition) {
+    this.partition = newPartition;
+    return self();
+  }
+
+  @Override
+  public B keyMetadata(EncryptionKeyMetadata newKeyMetadata) {
+    this.keyMetadata = newKeyMetadata;
+    return self();
+  }
+
+  @Override
+  public B sortOrder(SortOrder newSortOrder) {
+    this.sortOrder = newSortOrder;
+    return self();
+  }
+
+  private static class DataFileWriteBuilder<D, S>

Review Comment:
   Both `ContentFileWriteBuilderImpl` and `DataWriteBuilder` extends from or 
implements `ContentFileWriteBuilderImpl` interface. wondering if it is a sign 
that if the hierarchy can be improved.
   
   E.g., one solution is to have `DataWriteBuilder` extends from the abstract 
class `ContentFileWriteBuilderImpl`. the down side is that  `DataWriteBuilder` 
couldn't be an interface anymore, which is also non ideal.



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to