ptupitsyn commented on code in PR #4142:
URL: https://github.com/apache/ignite-3/pull/4142#discussion_r1713820821


##########
modules/binary-tuple/src/main/java/org/apache/ignite/internal/binarytuple/inlineschema/TupleWithSchemaMarshalling.java:
##########
@@ -0,0 +1,333 @@
+/*
+ * 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.binarytuple.inlineschema;
+
+import java.math.BigDecimal;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.util.UUID;
+import java.util.function.BiConsumer;
+import org.apache.ignite.internal.binarytuple.BinaryTupleBuilder;
+import org.apache.ignite.internal.binarytuple.BinaryTupleReader;
+import org.apache.ignite.sql.ColumnType;
+import org.apache.ignite.table.Tuple;
+import org.jetbrains.annotations.Nullable;
+
+/** Tuple with schema marshalling. */
+public final class TupleWithSchemaMarshalling {
+    private static final ByteOrder BYTE_ORDER = ByteOrder.LITTLE_ENDIAN;
+
+    /**
+     * Marshal tuple is the following format (LITTLE_ENDIAN):
+     * marshalledTuple       := | size | valuePosition | binaryTupleWithSchema 
|
+     * size                  := int32
+     * valuePosition := int32
+     * binaryTupleWithSchema := | schemaBinaryTuple | valueBinaryTuple |
+     * schemaBinaryTuple := | column1 | ... | columnN |
+     * column                := | columnName | columnType |
+     * columnName            := string
+     * columnType            := int32
+     * valueBinaryTuple := | value1 | ... | valueN |.
+     */
+    public static byte @Nullable [] marshal(Tuple tup) {
+        // Allocate all the memory we need upfront.
+        int size = tup.columnCount();
+        Object[] values = new Object[size];
+        String[] columns = new String[size];
+        ColumnType[] types = new ColumnType[size];
+
+        // Fill in the values, column names, and types.
+        for (int i = 0; i < size; i++) {
+            var value = tup.value(i);
+            values[i] = value;
+            columns[i] = tup.columnName(i);
+            types[i] = inferType(value);
+        }
+
+        BinaryTupleBuilder schemaBuilder = schemaBuilder(columns, types);
+        BinaryTupleBuilder valueBuilder = valueBuilder(values, types);
+
+        byte[] schemaBt = schemaBuilder.build().array();
+        byte[] valueBt = valueBuilder.build().array();

Review Comment:
   There is no allocation - you are right. I mixed this up with Netty `ByteBuf`.
   
   However, **there is a bug**. The resulting array is almost always bigger 
than needed, we must use `ByteBuffer.arrayOffset()` and `limit()` to get the 
data.



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