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


##########
modules/schema/src/main/java/org/apache/ignite/internal/schema/BinaryTupleReader.java:
##########
@@ -0,0 +1,537 @@
+/*
+ * 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.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;
+
+/**
+ * Utility for access to binary tuple elements as typed values.
+ */
+public class BinaryTupleReader extends BinaryTupleParser {
+    /** A helper to locate and handle tuple elements. */
+    private class ElementSink implements BinaryTupleParser.Sink {
+        int offset;
+        int length;
+
+        /** {@inheritDoc} */
+        @Override
+        public void nextElement(int index, int begin, int end) {
+            offset = begin;
+            length = end - begin;
+        }
+
+        boolean isNull() {
+            return offset == 0;
+        }
+
+        byte asByte() {
+            if (length == 0) {
+                return 0;
+            }
+            assert length == Byte.BYTES;
+            return buffer.get(offset);
+        }
+
+        short asShort() {
+            if (length == 0) {
+                return 0;
+            }
+            if (length == Byte.BYTES) {
+                return buffer.get(offset);
+            }
+            assert length == Short.BYTES;
+            return buffer.getShort(offset);
+        }
+
+        int asInt() {
+            if (length == 0) {
+                return 0;
+            }
+            if (length == Byte.BYTES) {
+                return buffer.get(offset);
+            }
+            if (length == Short.BYTES) {
+                return buffer.getShort(offset);
+            }
+            assert length == Integer.BYTES;
+            return buffer.getInt(offset);
+        }
+
+        long asLong() {
+            if (length == 0) {
+                return 0;
+            }
+            if (length == Byte.BYTES) {
+                return buffer.get(offset);
+            }
+            if (length == Short.BYTES) {
+                return buffer.getShort(offset);
+            }
+            if (length == Integer.BYTES) {
+                return buffer.getInt(offset);
+            }
+            assert length == Long.BYTES;
+            return buffer.getLong(offset);
+        }
+
+        float asFloat() {
+            if (length == 0) {
+                return 0.0F;
+            }
+            assert length == Float.BYTES;
+            return buffer.getFloat(offset);
+        }
+
+        double asDouble() {
+            if (length == 0) {
+                return 0.0;
+            }
+            if (length == Float.BYTES) {
+                return buffer.getFloat(offset);
+            }
+            assert length == Double.BYTES;
+            return buffer.getDouble(offset);
+        }
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param numElements Number of tuple elements.
+     * @param bytes Binary tuple.
+     */
+    public BinaryTupleReader(int numElements, byte[] bytes) {
+        this(numElements, 
ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN));
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param numElements Number of tuple elements.
+     * @param buffer Buffer with a binary tuple.
+     */
+    public BinaryTupleReader(int numElements, ByteBuffer buffer) {
+        super(numElements, buffer);
+    }
+
+    /**
+     * Get underlying buffer.
+     *
+     * @return Buffer.
+     */
+    public ByteBuffer getBuffer() {
+        return buffer;
+    }
+
+    /**
+     * Checks whether the given element contains a null value.
+     *
+     * @param index Element index.
+     * @return {@code true} if this element contains a null value, {@code 
false} otherwise.
+     */
+    public boolean hasNullValue(int index) {
+        return seek(index).isNull();
+    }
+
+    /**
+     * Reads value of specified element.
+     *
+     * @param index Element index.
+     * @return Element value.
+     */
+    public byte byteValue(int index) {
+        return seek(index).asByte();
+    }
+
+    /**
+     * Reads value of specified element.
+     *
+     * @param index Element index.
+     * @return Element value.
+     */
+    public Byte byteValueBoxed(int index) {
+        var element = seek(index);
+        return element.isNull() ? null : element.asByte();
+    }
+
+    /**
+     * Reads value of specified element.
+     *
+     * @param index Element index.
+     * @return Element value.
+     */
+    public short shortValue(int index) {
+        return seek(index).asShort();
+    }
+
+    /**
+     * Reads value of specified element.
+     *
+     * @param index Element index.
+     * @return Element value.
+     */
+    public Short shortValueBoxed(int index) {
+        var element = seek(index);
+        return element.isNull() ? null : element.asShort();
+    }
+
+    /**
+     * Reads value of specified element.
+     *
+     * @param index Element index.
+     * @return Element value.
+     */
+    public int intValue(int index) {
+        return seek(index).asInt();
+    }
+
+    /**
+     * Reads value of specified element.
+     *
+     * @param index Element index.
+     * @return Element value.
+     */
+    public Integer intValueBoxed(int index) {
+        var element = seek(index);
+        return element.isNull() ? null : element.asInt();
+    }
+
+    /**
+     * Reads value of specified element.
+     *
+     * @param index Element index.
+     * @return Element value.
+     */
+    public long longValue(int index) {
+        return seek(index).asLong();
+    }
+
+    /**
+     * Reads value of specified element.
+     *
+     * @param index Element index.
+     * @return Element value.
+     */
+    public Long longValueBoxed(int index) {
+        var element = seek(index);
+        return element.isNull() ? null : element.asLong();
+    }
+
+    /**
+     * Reads value of specified element.
+     *
+     * @param index Element index.
+     * @return Element value.
+     */
+    public float floatValue(int index) {
+        return seek(index).asFloat();
+    }
+
+    /**
+     * Reads value of specified element.
+     *
+     * @param index Element index.
+     * @return Element value.
+     */
+    public Float floatValueBoxed(int index) {
+        var element = seek(index);
+        return element.isNull() ? null : element.asFloat();
+    }
+
+    /**
+     * Reads value of specified element.
+     *
+     * @param index Element index.
+     * @return Element value.
+     */
+    public double doubleValue(int index) {
+        return seek(index).asDouble();
+    }
+
+    /**
+     * Reads value of specified element.
+     *
+     * @param index Element index.
+     * @return Element value.
+     */
+    public Double doubleValueBoxed(int index) {
+        var element = seek(index);
+        return element.isNull() ? null : element.asDouble();
+    }
+
+    /**
+     * Reads value of specified element.
+     *
+     * @param index Element index.
+     * @return Element value.
+     */
+    public BigInteger numberValue(int index) {
+        var element = seek(index);
+        if (element.isNull()) {
+            return null;
+        }
+
+        byte[] bytes = new byte[element.length];

Review Comment:
   We should probably add a "TODO" to optimize this code - arrays allocation is 
often not needed, there's a BigInteger constructor that could be used with an 
array and a range of indexes, without explicit copying. Just like you did with 
strings
   EDIT: or we can fix it instead of postponing, as I proposed



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