javrasya commented on code in PR #9464:
URL: https://github.com/apache/iceberg/pull/9464#discussion_r1568359596


##########
flink/v1.18/flink/src/main/java/org/apache/iceberg/flink/source/split/SerializerHelper.java:
##########
@@ -0,0 +1,186 @@
+/*
+ * 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.flink.source.split;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.io.UTFDataFormatException;
+import org.apache.flink.core.memory.DataInputDeserializer;
+import org.apache.flink.core.memory.DataOutputSerializer;
+
+/**
+ * Helper class to serialize and deserialize strings longer than 65K. The 
inspiration is mostly
+ * taken from the class 
org.apache.flink.core.memory.DataInputSerializer.readUTF and
+ * org.apache.flink.core.memory.DataOutputSerializer.writeUTF.
+ */
+public class SerializerHelper implements Serializable {
+
+  /**
+   * Similar to {@link DataOutputSerializer#writeUTF(String)}. The size is 
only limited by the
+   * maximum java array size of the buffer.
+   *
+   * @param out the output stream to write the string to.
+   * @param str the string value to be written.
+   * @deprecated This method is deprecated because there will be a method 
within the {@link
+   *     DataOutputSerializer} already which does the same thing, so use that 
one instead once that
+   *     is released on Flink version 1.20.
+   *     <p>See * <a 
href="https://issues.apache.org/jira/browse/FLINK-34228";>FLINK-34228</a> * <a
+   *     
href="https://github.com/apache/flink/pull/24191";>https://github.com/apache/flink/pull/24191</a>
+   */
+  @Deprecated
+  public static void writeLongUTF(DataOutputSerializer out, String str) throws 
IOException {
+    int strlen = str.length();
+    long utflen = 0;
+    int c;
+
+    /* use charAt instead of copying String to char array */
+    for (int i = 0; i < strlen; i++) {
+      c = str.charAt(i);
+      utflen += getUTFBytesSize(c);
+
+      if (utflen > Integer.MAX_VALUE) {
+        throw new UTFDataFormatException("Encoded string reached maximum 
length: " + utflen);
+      }
+    }
+    if (utflen > Integer.MAX_VALUE - 4) {
+      throw new UTFDataFormatException("Encoded string is too long: " + 
utflen);
+    }
+

Review Comment:
   With the current code, it is possible that it resizes twice due to [these 
two 
lines](https://github.com/apache/iceberg/pull/9464/files#diff-c3444af437f5a31f87967a363ee55e17f991c66057754fe117ff967f56a1a147R66-R67).
 I am not saying that it would run wrongly but two resize operations would 
cause two array copy operation (when the size is not big enough) which is worse 
than one resize. Or am I missing something 🤔 ?  
   
   [The early resize in the Flink 
PR](https://github.com/apache/flink/pull/24191/files#diff-fe4433a7c83db13761a692e0d9eb4e7e3be7a511b7aa938bb1bcbb7b70fa45ccR290)
 kind of prevents that. Now we don't have such behavior in this PR due to the 
reasons mentioned earlier. My suggestion above would reduce the number of 
resize operation to 1 since we will write both the utf length and the utf bytes 
all in one go. 



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