ademakov commented on code in PR #898:
URL: https://github.com/apache/ignite-3/pull/898#discussion_r906026422


##########
modules/schema/src/main/java/org/apache/ignite/internal/schema/BinaryTupleBuilder.java:
##########
@@ -0,0 +1,966 @@
+/*
+ * 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.ignite.internal.schema;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.CharBuffer;
+import java.nio.charset.CharacterCodingException;
+import java.nio.charset.CharsetEncoder;
+import java.nio.charset.CoderResult;
+import java.nio.charset.StandardCharsets;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.util.BitSet;
+import java.util.UUID;
+import org.apache.ignite.lang.IgniteInternalException;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Utility to construct a binary tuple.
+ */
+public class BinaryTupleBuilder {
+    /** The buffer size allocated for values when we do not know anything 
better. */
+    private static final int DEFAULT_BUFFER_SIZE = 4000;
+
+    /** Current element. */
+    private int elementIndex = 0;
+
+    /** Number of elements in the tuple. */
+    private final int numElements;
+
+    /** Size of an offset table entry. */
+    private final int entrySize;
+
+    /** Position of the varlen offset table. */
+    private final int entryBase;
+
+    /** Starting position of variable-length values. */
+    private final int valueBase;
+
+    /** Buffer for tuple content. */
+    private ByteBuffer buffer;
+
+    /** Charset encoder for strings. Initialized lazily. */
+    private CharsetEncoder cachedEncoder;
+
+    /** Flag indicating if any NULL values were really put here. */
+    private boolean hasNullValues = false;
+
+    /**
+     * Constructor.
+     *
+     * @param numElements Number of tuple elements.
+     * @param allowNulls True if NULL values are possible, false otherwise.
+     * @param totalValueSize Total estimated length of non-NULL values, -1 if 
not known.
+     */
+    private BinaryTupleBuilder(int numElements, boolean allowNulls, int 
totalValueSize) {
+        this.numElements = numElements;
+
+        int base = BinaryTupleSchema.HEADER_SIZE;
+        if (allowNulls) {
+            base += BinaryTupleSchema.nullMapSize(numElements);
+        }
+
+        entryBase = base;
+
+        if (totalValueSize < 0) {
+            entrySize = 4;
+        } else {
+            entrySize = 
BinaryTupleSchema.flagsToEntrySize(BinaryTupleSchema.valueSizeToFlags(totalValueSize));
+        }
+
+        valueBase = base + entrySize * numElements;
+    }
+
+    /**
+     * Creates a builder.
+     *
+     * @param schema Tuple schema.
+     * @return Tuple builder.
+     */
+    public BinaryTupleBuilder create(BinaryTupleSchema schema) {
+        return create(schema.elementCount(), schema.hasNullableElements());
+    }
+
+    /**
+     * Creates a builder.
+     *
+     * @param numElements Number of tuple elements.
+     * @param allowNulls True if NULL values are possible, false otherwise.
+     * @return Tuple builder.
+     */
+    public BinaryTupleBuilder create(int numElements, boolean allowNulls) {
+        return create(numElements, allowNulls, -1);
+    }
+
+    /**
+     * Creates a builder.
+     *
+     * @param schema Tuple schema.
+     * @param totalValueSize Total estimated length of non-NULL values, -1 if 
not known.
+     * @return Tuple builder.
+     */
+    public BinaryTupleBuilder create(BinaryTupleSchema schema, int 
totalValueSize) {
+        return create(schema.elementCount(), schema.hasNullableElements(), 
totalValueSize);
+    }
+
+    /**
+     * Creates a builder.
+     *
+     * @param numElements Number of tuple elements.
+     * @param allowNulls True if NULL values are possible, false otherwise.
+     * @param totalValueSize Total estimated length of non-NULL values, -1 if 
not known.
+     * @return Tuple builder.
+     */
+    public BinaryTupleBuilder create(int numElements, boolean allowNulls, int 
totalValueSize) {
+        var builder = new BinaryTupleBuilder(numElements, allowNulls, 
totalValueSize);
+        builder.allocate(totalValueSize);
+        return builder;
+    }
+
+    /**
+     * Creates a builder with direct buffer.
+     *
+     * @param schema Tuple schema.
+     * @return Tuple builder.
+     */
+    public BinaryTupleBuilder createWithDirectBuffer(BinaryTupleSchema schema) 
{
+        return createWithDirectBuffer(schema.elementCount(), 
schema.hasNullableElements());
+    }
+
+    /**
+     * Creates a builder with direct buffer.
+     *
+     * @param numElements Number of tuple elements.
+     * @param allowNulls True if NULL values are possible, false otherwise.
+     * @return Tuple builder.
+     */
+    public BinaryTupleBuilder createWithDirectBuffer(int numElements, boolean 
allowNulls) {
+        return createWithDirectBuffer(numElements, allowNulls, -1);
+    }
+
+    /**
+     * Creates a builder with direct buffer.
+     *
+     * @param schema Tuple schema.
+     * @param totalValueSize Total estimated length of non-NULL values, -1 if 
not known.
+     * @return Tuple builder.
+     */
+    public BinaryTupleBuilder createWithDirectBuffer(BinaryTupleSchema schema, 
int totalValueSize) {
+        return createWithDirectBuffer(schema.elementCount(), 
schema.hasNullableElements(), totalValueSize);
+    }
+
+    /**
+     * Creates a builder with direct buffer.
+     *
+     * @param numElements Number of tuple elements.
+     * @param allowNulls True if NULL values are possible, false otherwise.
+     * @param totalValueSize Total estimated length of non-NULL values, -1 if 
not known.
+     * @return Tuple builder.
+     */
+    public BinaryTupleBuilder createWithDirectBuffer(int numElements, boolean 
allowNulls, int totalValueSize) {
+        var builder = new BinaryTupleBuilder(numElements, allowNulls, 
totalValueSize);
+        builder.allocateDirect(totalValueSize);

Review Comment:
   A tuple might be created for passing over JNI or sending over network. But 
probably this interface is not sufficient as in these cases it is usually 
required to put several things in the same buffer. Perhaps you are right and it 
makes sense to remove this now and return with a better design later.



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