juntaozhang commented on code in PR #10226:
URL: https://github.com/apache/paimon/pull/10226#discussion_r4253637610


##########
paimon-common/src/main/java/org/apache/paimon/data/variant/VariantMetadata.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.paimon.data.variant;
+
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+import static org.apache.paimon.utils.BinaryRowDataUtil.byteBufferEquals;
+
+/**
+ * The string dictionary of a variant metadata buffer, with lazily decoded 
entries.
+ *
+ * <p>Rows of a batch usually share the same metadata bytes, so an instance 
can be shared across
+ * rows: {@link #adopt()} reparses the dictionary only when the bytes change. 
A shared instance is a
+ * mutable cache slot, not a stable dictionary; do not retain it across calls.
+ *
+ * <p>This class is not thread safe.
+ */
+public class VariantMetadata {
+    private static final int MAX_DICTIONARY_SIZE = 1 << 24;
+
+    private ByteBuffer reuse;
+    private ByteBuffer current;
+    private int dictSize;
+    private String[] dict;
+    private int offsetSize;
+    private int stringStart;
+
+    /** Creates an empty metadata that adopts the first buffer passed to 
{@link #adopt}. */
+    public static VariantMetadata empty() {
+        return new VariantMetadata();
+    }
+
+    private VariantMetadata() {
+        this.reuse = null;
+        this.current = null;
+        this.dictSize = 0;
+        this.dict = null;
+        this.offsetSize = 0;
+        this.stringStart = 0;
+    }
+
+    public void setCurrent(ByteBuffer metadata) {
+        this.current = metadata;
+    }
+
+    public void adopt() {
+        if (current == null) {
+            throw GenericVariantUtil.malformedVariant();
+        }
+
+        if (reuse != null
+                && reuse.remaining() == current.remaining()
+                && byteBufferEquals(reuse, current)) {
+            return;
+        }
+
+        reuse = snapshot(current);

Review Comment:
   Thanks for the detailed review. I've reworked the metadata-dictionary 
caching to address both points.



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

Reply via email to