aihuaxu commented on code in PR #11857:
URL: https://github.com/apache/iceberg/pull/11857#discussion_r1938392850


##########
core/src/main/java/org/apache/iceberg/variants/VariantBuilderBase.java:
##########
@@ -0,0 +1,504 @@
+/*
+ * 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.variants;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+
+abstract class VariantBuilderBase {
+  protected static final int MAX_SHORT_STR_SIZE = 0x3F;
+
+  private final ByteBufferWrapper buffer;
+  private final Dictionary dict;
+  private int startPos;
+
+  VariantBuilderBase(ByteBufferWrapper buffer, Dictionary dict) {
+    this.buffer = buffer;
+    this.dict = dict;
+    startPos = buffer.pos;
+  }
+
+  /**
+   * Builds the variant metadata from `dictionaryKeys` and returns the 
resulting Variant object.
+   *
+   * @return The constructed Variant object.
+   */
+  public Variant build() {
+    int numKeys = dict.size();
+
+    // Calculate total size of dictionary strings
+    long numStringBytes = dict.totalBytes();
+    if (numStringBytes > VariantConstants.SIZE_LIMIT) {
+      throw new VariantSizeLimitException();
+    }
+
+    // Determine the number of bytes required for dictionary size and offset 
entry
+    int offsetSize = sizeOf(Math.max((int) numStringBytes, numKeys));
+
+    // metadata: header byte, dictionary size, offsets and string bytes
+    long metadataSize = 1 + offsetSize + (numKeys + 1) * offsetSize + 
numStringBytes;
+
+    // Ensure the metadata size is within limits
+    if (metadataSize > VariantConstants.SIZE_LIMIT) {
+      throw new VariantSizeLimitException();
+    }
+
+    ByteBufferWrapper metadataBuffer =
+        new ByteBufferWrapper((int) metadataSize, (int) metadataSize);
+
+    // Write header byte (version + offset size)
+    
metadataBuffer.addByte(VariantUtil.metadataHeader(VariantConstants.VERSION, 
offsetSize));
+
+    // Write number of keys
+    metadataBuffer.writeLittleEndianUnsigned(numKeys, offsetSize);
+
+    // Write offsets
+    int currentOffset = 0;
+    for (byte[] key : dict.getKeys()) {
+      metadataBuffer.writeLittleEndianUnsigned(currentOffset, offsetSize);
+      currentOffset += key.length;
+    }
+    metadataBuffer.writeLittleEndianUnsigned(numStringBytes, offsetSize);
+
+    // Write dictionary strings
+    dict.getKeys().forEach(metadataBuffer::addBytes);
+
+    return new VariantImpl(metadataBuffer.toByteArray(), buffer.toByteArray());
+  }
+
+  protected void writeNullInternal() {
+    buffer.addByte(VariantUtil.primitiveHeader(Variants.Primitives.TYPE_NULL));
+  }
+
+  protected void writeBooleanInternal(boolean value) {
+    buffer.addByte(
+        VariantUtil.primitiveHeader(
+            value ? Variants.Primitives.TYPE_TRUE : 
Variants.Primitives.TYPE_FALSE));
+  }
+
+  /**
+   * Writes a numeric value to the variant builder, automatically choosing the 
smallest type (INT8,
+   * INT16, INT32, or INT64) to store the value efficiently.
+   *
+   * @param value The numeric value to append.
+   */
+  protected void writeNumericInternal(long value) {

Review Comment:
   I added `writePrimitive`() in `ByteBufferWrapper` which will call the 
serialization in `PrimitiveWrapper`.



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