ascherbakoff commented on a change in pull request #168:
URL: https://github.com/apache/ignite-3/pull/168#discussion_r655492113



##########
File path: 
modules/schema/src/main/java/org/apache/ignite/internal/schema/row/ChunkFormat.java
##########
@@ -0,0 +1,299 @@
+/*
+ * 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.row;
+
+import org.apache.ignite.internal.schema.BinaryRow;
+
+/**
+ * Chunk format.
+ */
+abstract class ChunkFormat {
+    /** First two flag bits reserved for format code. */
+    public static final int FORMAT_CODE_MASK = 0x03;
+
+    /** Flag indicates key chunk omits vartable. */
+    public static final int OMIT_NULL_MAP_FLAG = 1 << 2;
+
+    /** Flag indicates value chunk omits null map. */
+    public static final int OMIT_VARTBL_FLAG = 1 << 3;
+
+    /** Writer factory for tiny-sized chunks. */
+    private static final ChunkFormat TINY = new ChunkFormat(Byte.BYTES, 
Byte.BYTES, (byte)1) {
+        /** {@inheritDoc} */
+        @Override void writeVarlenOffset(ExpandableByteBuf buf, int vartblOff, 
int entryIdx, int off) {
+            assert off < (1 << 8) && off >= 0 : "Varlen offset overflow: 
offset=" + off;
+
+            buf.put(vartblOff + vartableEntryOffset(entryIdx), (byte)off);
+        }
+
+        /** {@inheritDoc} */
+        @Override int readVarlenOffset(BinaryRow row, int vartblOff, int 
entryIdx) {
+            return row.readByte(vartblOff + vartableEntryOffset(entryIdx)) & 
0xFF;
+        }
+
+        /** {@inheritDoc} */
+        @Override void writeVartableSize(ExpandableByteBuf buf, int vartblOff, 
int size) {
+            assert size < (1 << 8) && size >= 0 : "Vartable size overflow: 
size=" + size;
+
+            buf.put(vartblOff, (byte)size);
+        }
+
+        /** {@inheritDoc} */
+        @Override int readVartableSize(BinaryRow row, int vartblOff) {
+            return row.readByte(vartblOff) & 0xFF;
+        }
+    };
+
+    /** Writer factory for med-sized chunks. */
+    private static final ChunkFormat MEDIUM = new ChunkFormat(Short.BYTES, 
Short.BYTES, (byte)2) {
+        /** {@inheritDoc} */
+        @Override void writeVarlenOffset(ExpandableByteBuf buf, int vartblOff, 
int entryIdx, int off) {
+            assert off < (1 << 16) && off >= 0 : "Varlen offset overflow: 
offset=" + off;
+
+            buf.putShort(vartblOff + vartableEntryOffset(entryIdx), 
(short)off);
+        }
+
+        /** {@inheritDoc} */
+        @Override int readVarlenOffset(BinaryRow row, int vartblOff, int 
entryIdx) {
+            return row.readShort(vartblOff + vartableEntryOffset(entryIdx)) & 
0xFFFF;
+        }
+
+        /** {@inheritDoc} */
+        @Override void writeVartableSize(ExpandableByteBuf buf, int vartblOff, 
int size) {
+            assert size < (1 << 16) && size >= 0 : "Vartable size overflow: 
size=" + size;
+
+            buf.putShort(vartblOff, (short)size);
+        }
+
+        /** {@inheritDoc} */
+        @Override int readVartableSize(BinaryRow row, int vartblOff) {
+            return row.readShort(vartblOff) & 0xFFFF;
+        }
+    };
+
+    /** Writer factory for large-sized chunks. */
+    private static final ChunkFormat LARGE = new ChunkFormat(Short.BYTES, 
Integer.BYTES, (byte)0) {
+        /** {@inheritDoc} */
+        @Override void writeVarlenOffset(ExpandableByteBuf buf, int vartblOff, 
int entryIdx, int off) {
+            buf.putInt(vartblOff + vartableEntryOffset(entryIdx), off);
+        }
+
+        /** {@inheritDoc} */
+        @Override int readVarlenOffset(BinaryRow row, int vartblOff, int 
entryIdx) {
+            return row.readInteger(vartblOff + vartableEntryOffset(entryIdx));
+        }
+
+        /** {@inheritDoc} */
+        @Override void writeVartableSize(ExpandableByteBuf buf, int vartblOff, 
int size) {
+            assert size < (1 << 16) && size >= 0 : "Vartable size overflow: 
size=" + size;
+
+            buf.putShort(vartblOff, (short)size);
+        }
+
+        /** {@inheritDoc} */
+        @Override int readVartableSize(BinaryRow row, int vartblOff) {
+            return row.readShort(vartblOff) & 0xFFFF;
+        }
+    };
+
+    /**
+     * Return chunk formatter.
+     *
+     * @param payloadLen Payload size in bytes.
+     * @return Chunk formatter.
+     */
+    static ChunkFormat formatter(int payloadLen) {

Review comment:
       This approach can create different binary representations of a key, 
depending on the payloadLen. It should be avoided. For example, we can always 
use LARGE format for a key.




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

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to