You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by am...@apache.org on 2021/05/13 07:24:07 UTC

[ignite-3] branch ignite-14388 created (now 7413dd6)

This is an automated email from the ASF dual-hosted git repository.

amashenkov pushed a change to branch ignite-14388
in repository https://gitbox.apache.org/repos/asf/ignite-3.git.


      at 7413dd6  Add affinity columns support.

This branch includes the following new commits:

     new 47b07f1  Calc hashcode for row.
     new 7413dd6  Add affinity columns support.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[ignite-3] 01/02: Calc hashcode for row.

Posted by am...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

amashenkov pushed a commit to branch ignite-14388
in repository https://gitbox.apache.org/repos/asf/ignite-3.git

commit 47b07f16e4458253737c029096e48d05287009da
Author: Andrew Mashenkov <an...@gmail.com>
AuthorDate: Wed May 12 18:20:08 2021 +0300

    Calc hashcode for row.
---
 .../ignite/internal/schema/RowAssembler.java       | 46 +++++++++++++
 .../ignite/internal/schema/SchemaDescriptor.java   | 12 +++-
 .../ignite/internal/schema/RowAssemblerTest.java   | 80 +++++++++++-----------
 3 files changed, 96 insertions(+), 42 deletions(-)

diff --git a/modules/schema/src/main/java/org/apache/ignite/internal/schema/RowAssembler.java b/modules/schema/src/main/java/org/apache/ignite/internal/schema/RowAssembler.java
index 9325566..fb7ab43 100644
--- a/modules/schema/src/main/java/org/apache/ignite/internal/schema/RowAssembler.java
+++ b/modules/schema/src/main/java/org/apache/ignite/internal/schema/RowAssembler.java
@@ -20,6 +20,7 @@ package org.apache.ignite.internal.schema;
 import java.nio.charset.CharacterCodingException;
 import java.nio.charset.CharsetEncoder;
 import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
 import java.util.BitSet;
 import java.util.UUID;
 import org.apache.ignite.internal.schema.BinaryRow.RowFlags;
@@ -69,6 +70,9 @@ public class RowAssembler {
     /** Offset of the varlen table for current chunk. */
     private int varlenTblChunkOff;
 
+    /** Row hashcode. */
+    private int keyHash;
+
     /** Flags. */
     private short flags;
 
@@ -200,6 +204,9 @@ public class RowAssembler {
 
         setNull(curCol);
 
+        if (isAffinityCol())
+            keyHash *= 31;
+
         shiftColumn(0, false);
     }
 
@@ -213,6 +220,9 @@ public class RowAssembler {
 
         buf.put(curOff, val);
 
+        if (isAffinityCol())
+            keyHash = 31 * keyHash + Byte.hashCode(val);
+
         shiftColumn(NativeType.BYTE);
     }
 
@@ -226,6 +236,9 @@ public class RowAssembler {
 
         buf.putShort(curOff, val);
 
+        if (isAffinityCol())
+            keyHash = 31 * keyHash + Short.hashCode(val);
+
         shiftColumn(NativeType.SHORT);
     }
 
@@ -239,6 +252,9 @@ public class RowAssembler {
 
         buf.putInt(curOff, val);
 
+        if (isAffinityCol())
+            keyHash = 31 * keyHash + Integer.hashCode(val);
+
         shiftColumn(NativeType.INTEGER);
     }
 
@@ -252,6 +268,9 @@ public class RowAssembler {
 
         buf.putLong(curOff, val);
 
+        if (isAffinityCol())
+            keyHash += 31 * keyHash + Long.hashCode(val);
+
         shiftColumn(NativeType.LONG);
     }
 
@@ -265,6 +284,9 @@ public class RowAssembler {
 
         buf.putFloat(curOff, val);
 
+        if (isAffinityCol())
+            keyHash += 31 * keyHash + Float.hashCode(val);
+
         shiftColumn(NativeType.FLOAT);
     }
 
@@ -278,6 +300,9 @@ public class RowAssembler {
 
         buf.putDouble(curOff, val);
 
+        if (isAffinityCol())
+            keyHash += 31 * keyHash + Double.hashCode(val);
+
         shiftColumn(NativeType.DOUBLE);
     }
 
@@ -292,6 +317,9 @@ public class RowAssembler {
         buf.putLong(curOff, uuid.getLeastSignificantBits());
         buf.putLong(curOff + 8, uuid.getMostSignificantBits());
 
+        if (isAffinityCol())
+            keyHash += 31 * keyHash + uuid.hashCode();
+
         shiftColumn(NativeType.UUID);
     }
 
@@ -308,6 +336,9 @@ public class RowAssembler {
 
             writeOffset(curVarlenTblEntry, curOff - baseOff);
 
+            if (isAffinityCol())
+                keyHash += 31 * keyHash + val.hashCode();
+
             shiftColumn(written, true);
         }
         catch (CharacterCodingException e) {
@@ -325,6 +356,9 @@ public class RowAssembler {
 
         buf.putBytes(curOff, val);
 
+        if (isAffinityCol())
+            keyHash += 31 * keyHash + Arrays.hashCode(val);
+
         writeOffset(curVarlenTblEntry, curOff - baseOff);
 
         shiftColumn(val.length, true);
@@ -353,6 +387,9 @@ public class RowAssembler {
         for (int i = 0; i < maskType.length() - arr.length; i++)
             buf.put(curOff + arr.length + i, (byte)0);
 
+        if (isAffinityCol())
+            keyHash += 31 * keyHash + Arrays.hashCode(arr);
+
         shiftColumn(maskType);
     }
 
@@ -370,6 +407,7 @@ public class RowAssembler {
         }
 
         buf.putShort(BinaryRow.FLAGS_FIELD_OFFSET, flags);
+        buf.putInt(BinaryRow.KEY_HASH_FIELD_OFFSET, keyHash);
 
         return buf.toArray();
     }
@@ -493,4 +531,12 @@ public class RowAssembler {
 
         curOff = varlenTblChunkOff + varlenTableChunkSize(nonNullVarlenCols);
     }
+
+    /**
+     * @return {@code true} if current column is affinity columns, {@code false} otherwise.
+     */
+    private boolean isAffinityCol() {
+        return (schema.keyColumns() == curCols) && /* Is key column */
+            schema.isAffinityColumn(curCol); /* currCol equals to absolute column schema index */
+    }
 }
diff --git a/modules/schema/src/main/java/org/apache/ignite/internal/schema/SchemaDescriptor.java b/modules/schema/src/main/java/org/apache/ignite/internal/schema/SchemaDescriptor.java
index e0e0dfb..2064068 100644
--- a/modules/schema/src/main/java/org/apache/ignite/internal/schema/SchemaDescriptor.java
+++ b/modules/schema/src/main/java/org/apache/ignite/internal/schema/SchemaDescriptor.java
@@ -64,14 +64,22 @@ public class SchemaDescriptor {
     }
 
     /**
-     * @param idx Index to check.
-     * @return {@code true} if the column belongs to the key chunk.
+     * @param idx Column index to check.
+     * @return {@code true} if the column belongs to the key chunk, {@code false} otherwise.
      */
     public boolean isKeyColumn(int idx) {
         return idx < keyCols.length();
     }
 
     /**
+     * @param idx Column index to check.
+     * @return {@code true} if the columns if key affinity column, {@code false} otherwise.
+     */
+    public boolean isAffinityColumn(int idx) {
+        return isKeyColumn(idx);
+    }
+
+    /**
      * @param colIdx Column index.
      * @return Column instance.
      */
diff --git a/modules/schema/src/test/java/org/apache/ignite/internal/schema/RowAssemblerTest.java b/modules/schema/src/test/java/org/apache/ignite/internal/schema/RowAssemblerTest.java
index b2c56d5..cdf7df4 100644
--- a/modules/schema/src/test/java/org/apache/ignite/internal/schema/RowAssemblerTest.java
+++ b/modules/schema/src/test/java/org/apache/ignite/internal/schema/RowAssemblerTest.java
@@ -49,7 +49,7 @@ public class RowAssemblerTest {
             asm.appendInt(33);
             asm.appendInt(-71);
 
-            assertRowBytesEquals(new byte[] {42, 0, 26, 0, 0, 0, 0, 0, 8, 0, 0, 0, 33, 0, 0, 0, 9, 0, 0, 0, 0, -71, -1, -1, -1}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 26, 0, 33, 0, 0, 0, 8, 0, 0, 0, 33, 0, 0, 0, 9, 0, 0, 0, 0, -71, -1, -1, -1}, asm.build());
         }
 
         { // Null value.
@@ -58,7 +58,7 @@ public class RowAssemblerTest {
             asm.appendInt(-33);
             asm.appendNull();
 
-            assertRowBytesEquals(new byte[] {42, 0, 26, 0, 0, 0, 0, 0, 8, 0, 0, 0, -33, -1, -1, -1, 5, 0, 0, 0, 1}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 26, 0, -33, -1, -1, -1, 8, 0, 0, 0, -33, -1, -1, -1, 5, 0, 0, 0, 1}, asm.build());
         }
 
         { // No value.
@@ -66,7 +66,7 @@ public class RowAssemblerTest {
 
             asm.appendInt(-33);
 
-            assertRowBytesEquals(new byte[] {42, 0, 27, 0, 0, 0, 0, 0, 8, 0, 0, 0, -33, -1, -1, -1}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 27, 0, -33, -1, -1, -1, 8, 0, 0, 0, -33, -1, -1, -1}, asm.build());
         }
     }
 
@@ -83,7 +83,7 @@ public class RowAssemblerTest {
             asm.appendShort((short)33);
             asm.appendShort((short)71L);
 
-            assertRowBytesEquals(new byte[] {42, 0, 30, 0, 0, 0, 0, 0, 6, 0, 0, 0, 33, 0, 6, 0, 0, 0, 71, 0}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 30, 0, 33, 0, 0, 0, 6, 0, 0, 0, 33, 0, 6, 0, 0, 0, 71, 0}, asm.build());
         }
 
         { // No value.
@@ -91,7 +91,7 @@ public class RowAssemblerTest {
 
             asm.appendShort((short)-33);
 
-            assertRowBytesEquals(new byte[] {42, 0, 31, 0, 0, 0, 0, 0, 6, 0, 0, 0, -33, -1}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 31, 0, -33, -1, -1, -1, 6, 0, 0, 0, -33, -1}, asm.build());
         }
     }
 
@@ -108,7 +108,7 @@ public class RowAssemblerTest {
             asm.appendShort((short)-33);
             asm.appendString("val");
 
-            assertRowBytesEquals(new byte[] {42, 0, 10, 0, 0, 0, 0, 0, 6, 0, 0, 0, -33, -1, 12, 0, 0, 0, 0, 1, 0, 9, 0, 118, 97, 108}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 10, 0, -33, -1, -1, -1, 6, 0, 0, 0, -33, -1, 12, 0, 0, 0, 0, 1, 0, 9, 0, 118, 97, 108}, asm.build());
         }
 
         { // Null value.
@@ -117,7 +117,7 @@ public class RowAssemblerTest {
             asm.appendShort((short)33);
             asm.appendNull();
 
-            assertRowBytesEquals(new byte[] {42, 0, 26, 0, 0, 0, 0, 0, 6, 0, 0, 0, 33, 0, 5, 0, 0, 0, 1}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 26, 0, 33, 0, 0, 0, 6, 0, 0, 0, 33, 0, 5, 0, 0, 0, 1}, asm.build());
         }
 
         { // No value.
@@ -125,7 +125,7 @@ public class RowAssemblerTest {
 
             asm.appendShort((short)33);
 
-            assertRowBytesEquals(new byte[] {42, 0, 27, 0, 0, 0, 0, 0, 6, 0, 0, 0, 33, 0}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 27, 0, 33, 0, 0, 0, 6, 0, 0, 0, 33, 0}, asm.build());
         }
     }
 
@@ -142,7 +142,7 @@ public class RowAssemblerTest {
             asm.appendShort((short)-33);
             asm.appendString("val");
 
-            assertRowBytesEquals(new byte[] {42, 0, 14, 0, 0, 0, 0, 0, 6, 0, 0, 0, -33, -1, 11, 0, 0, 0, 1, 0, 8, 0, 118, 97, 108}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 14, 0, -33, -1, -1, -1, 6, 0, 0, 0, -33, -1, 11, 0, 0, 0, 1, 0, 8, 0, 118, 97, 108}, asm.build());
         }
 
         { // No value.
@@ -150,7 +150,7 @@ public class RowAssemblerTest {
 
             asm.appendShort((short)33);
 
-            assertRowBytesEquals(new byte[] {42, 0, 31, 0, 0, 0, 0, 0, 6, 0, 0, 0, 33, 0}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 31, 0, 33, 0, 0, 0, 6, 0, 0, 0, 33, 0}, asm.build());
         }
     }
 
@@ -167,7 +167,7 @@ public class RowAssemblerTest {
             asm.appendShort((short)-33);
             asm.appendByte((byte)71);
 
-            assertRowBytesEquals(new byte[] {42, 0, 28, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, -33, -1, 5, 0, 0, 0, 71}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 28, 0, -33, -1, -1, -1, 7, 0, 0, 0, 0, -33, -1, 5, 0, 0, 0, 71}, asm.build());
         }
 
         { // Null key.
@@ -184,7 +184,7 @@ public class RowAssemblerTest {
 
             asm.appendShort((short)33);
 
-            assertRowBytesEquals(new byte[] {42, 0, 29, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 33, 0}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 29, 0, 33, 0, 0, 0, 7, 0, 0, 0, 0, 33, 0}, asm.build());
         }
     }
 
@@ -201,7 +201,7 @@ public class RowAssemblerTest {
             asm.appendShort((short)-1133);
             asm.appendShort((short)-1071);
 
-            assertRowBytesEquals(new byte[] {42, 0, 24, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, -109, -5, 7, 0, 0, 0, 0, -47, -5}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 24, 0, -109, -5, -1, -1, 7, 0, 0, 0, 0, -109, -5, 7, 0, 0, 0, 0, -47, -5}, asm.build());
         }
 
         { // Null key.
@@ -219,7 +219,7 @@ public class RowAssemblerTest {
             asm.appendShort((short)1133);
             asm.appendNull();
 
-            assertRowBytesEquals(new byte[] {42, 0, 24, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 109, 4, 5, 0, 0, 0, 1}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 24, 0, 109, 4, 0, 0, 7, 0, 0, 0, 0, 109, 4, 5, 0, 0, 0, 1}, asm.build());
         }
 
         { // Null both.
@@ -236,7 +236,7 @@ public class RowAssemblerTest {
 
             asm.appendShort((short)1133);
 
-            assertRowBytesEquals(new byte[] {42, 0, 25, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 109, 4}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 25, 0, 109, 4, 0, 0, 7, 0, 0, 0, 0, 109, 4}, asm.build());
         }
     }
 
@@ -253,7 +253,7 @@ public class RowAssemblerTest {
             asm.appendInt(-33);
             asm.appendString("val");
 
-            assertRowBytesEquals(new byte[] {42, 0, 8, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, -33, -1, -1, -1, 12, 0, 0, 0, 0, 1, 0, 9, 0, 118, 97, 108}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 8, 0, -33, -1, -1, -1, 9, 0, 0, 0, 0, -33, -1, -1, -1, 12, 0, 0, 0, 0, 1, 0, 9, 0, 118, 97, 108}, asm.build());
         }
 
         { // Null key.
@@ -271,7 +271,7 @@ public class RowAssemblerTest {
             asm.appendInt(33);
             asm.appendNull();
 
-            assertRowBytesEquals(new byte[] {42, 0, 24, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 33, 0, 0, 0, 5, 0, 0, 0, 1}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 24, 0, 33, 0, 0, 0, 9, 0, 0, 0, 0, 33, 0, 0, 0, 5, 0, 0, 0, 1}, asm.build());
         }
 
         { // Null both.
@@ -288,7 +288,7 @@ public class RowAssemblerTest {
 
             asm.appendInt(33);
 
-            assertRowBytesEquals(new byte[] {42, 0, 25, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 33, 0, 0, 0}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 25, 0, 33, 0, 0, 0, 9, 0, 0, 0, 0, 33, 0, 0, 0}, asm.build());
         }
     }
 
@@ -305,7 +305,7 @@ public class RowAssemblerTest {
             asm.appendByte((byte)-33);
             asm.appendString("val");
 
-            assertRowBytesEquals(new byte[] {42, 0, 12, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, -33, 11, 0, 0, 0, 1, 0, 8, 0, 118, 97, 108}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 12, 0, -33, -1, -1, -1, 6, 0, 0, 0, 0, -33, 11, 0, 0, 0, 1, 0, 8, 0, 118, 97, 108}, asm.build());
         }
 
         { // Null key.
@@ -322,7 +322,7 @@ public class RowAssemblerTest {
 
             asm.appendByte((byte)33);
 
-            assertRowBytesEquals(new byte[] {42, 0, 29, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 33}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 29, 0, 33, 0, 0, 0, 6, 0, 0, 0, 0, 33}, asm.build());
         }
     }
 
@@ -340,7 +340,7 @@ public class RowAssemblerTest {
             asm.appendUuid(uuidVal);
 
             assertRowBytesEquals(new byte[] {
-                42, 0, 18, 0, 0, 0, 0, 0, 11, 0, 0, 0, 1, 0, 8, 0, 107, 101, 121,
+                42, 0, 18, 0, 95, -98, 1, 0, 11, 0, 0, 0, 1, 0, 8, 0, 107, 101, 121,
                 21, 0, 0, 0, 0, -117, -61, -31, 85, 61, -32, 57, 68, 111, 67, 56, -3, -99, -37, -58, -73}, asm.build());
         }
 
@@ -350,7 +350,7 @@ public class RowAssemblerTest {
             asm.appendString("key");
             asm.appendNull();
 
-            assertRowBytesEquals(new byte[] {42, 0, 18, 0, 0, 0, 0, 0, 11, 0, 0, 0, 1, 0, 8, 0, 107, 101, 121, 5, 0, 0, 0, 1}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 18, 0, 95, -98, 1, 0, 11, 0, 0, 0, 1, 0, 8, 0, 107, 101, 121, 5, 0, 0, 0, 1}, asm.build());
         }
 
         { // No value.
@@ -358,7 +358,7 @@ public class RowAssemblerTest {
 
             asm.appendString("key");
 
-            assertRowBytesEquals(new byte[] {42, 0, 19, 0, 0, 0, 0, 0, 11, 0, 0, 0, 1, 0, 8, 0, 107, 101, 121}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 19, 0, 95, -98, 1, 0, 11, 0, 0, 0, 1, 0, 8, 0, 107, 101, 121}, asm.build());
         }
     }
 
@@ -376,7 +376,7 @@ public class RowAssemblerTest {
             asm.appendUuid(uuidVal);
 
             assertRowBytesEquals(new byte[] {
-                42, 0, 22, 0, 0, 0, 0, 0, 11, 0, 0, 0, 1, 0, 8, 0, 107, 101, 121,
+                42, 0, 22, 0, 95, -98, 1, 0, 11, 0, 0, 0, 1, 0, 8, 0, 107, 101, 121,
                 20, 0, 0, 0, -117, -61, -31, 85, 61, -32, 57, 68, 111, 67, 56, -3, -99, -37, -58, -73}, asm.build());
         }
 
@@ -385,7 +385,7 @@ public class RowAssemblerTest {
 
             asm.appendString("key");
 
-            assertRowBytesEquals(new byte[] {42, 0, 23, 0, 0, 0, 0, 0, 11, 0, 0, 0, 1, 0, 8, 0, 107, 101, 121}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 23, 0, 95, -98, 1, 0, 11, 0, 0, 0, 1, 0, 8, 0, 107, 101, 121}, asm.build());
         }
     }
 
@@ -402,7 +402,7 @@ public class RowAssemblerTest {
             asm.appendString("key");
             asm.appendBytes(new byte[] {-1, 1, 0, 120});
 
-            assertRowBytesEquals(new byte[] {42, 0, 2, 0, 0, 0, 0, 0, 11, 0, 0, 0, 1, 0, 8, 0, 107, 101, 121, 13, 0, 0, 0, 0, 1, 0, 9, 0, -1, 1, 0, 120}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 2, 0, 95, -98, 1, 0, 11, 0, 0, 0, 1, 0, 8, 0, 107, 101, 121, 13, 0, 0, 0, 0, 1, 0, 9, 0, -1, 1, 0, 120}, asm.build());
         }
 
         { // Null value.
@@ -411,7 +411,7 @@ public class RowAssemblerTest {
             asm.appendString("key");
             asm.appendNull();
 
-            assertRowBytesEquals(new byte[] {42, 0, 18, 0, 0, 0, 0, 0, 11, 0, 0, 0, 1, 0, 8, 0, 107, 101, 121, 5, 0, 0, 0, 1}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 18, 0, 95, -98, 1, 0, 11, 0, 0, 0, 1, 0, 8, 0, 107, 101, 121, 5, 0, 0, 0, 1}, asm.build());
         }
 
         { // No value.
@@ -419,7 +419,7 @@ public class RowAssemblerTest {
 
             asm.appendString("key");
 
-            assertRowBytesEquals(new byte[] {42, 0, 19, 0, 0, 0, 0, 0, 11, 0, 0, 0, 1, 0, 8, 0, 107, 101, 121}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 19, 0, 95, -98, 1, 0, 11, 0, 0, 0, 1, 0, 8, 0, 107, 101, 121}, asm.build());
         }
     }
 
@@ -436,7 +436,7 @@ public class RowAssemblerTest {
             asm.appendString("key");
             asm.appendBytes(new byte[] {-1, 1, 0, 120});
 
-            assertRowBytesEquals(new byte[] {42, 0, 6, 0, 0, 0, 0, 0, 11, 0, 0, 0, 1, 0, 8, 0, 107, 101, 121, 12, 0, 0, 0, 1, 0, 8, 0, -1, 1, 0, 120}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 6, 0, 95, -98, 1, 0, 11, 0, 0, 0, 1, 0, 8, 0, 107, 101, 121, 12, 0, 0, 0, 1, 0, 8, 0, -1, 1, 0, 120}, asm.build());
         }
 
         { // No value.
@@ -444,7 +444,7 @@ public class RowAssemblerTest {
 
             asm.appendString("key");
 
-            assertRowBytesEquals(new byte[] {42, 0, 23, 0, 0, 0, 0, 0, 11, 0, 0, 0, 1, 0, 8, 0, 107, 101, 121}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 23, 0, 95, -98, 1, 0, 11, 0, 0, 0, 1, 0, 8, 0, 107, 101, 121}, asm.build());
         }
     }
 
@@ -461,7 +461,7 @@ public class RowAssemblerTest {
             asm.appendString("key");
             asm.appendShort((short)-71);
 
-            assertRowBytesEquals(new byte[] {42, 0, 16, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 1, 0, 9, 0, 107, 101, 121, 7, 0, 0, 0, 0, -71, -1}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 16, 0, 95, -98, 1, 0, 12, 0, 0, 0, 0, 1, 0, 9, 0, 107, 101, 121, 7, 0, 0, 0, 0, -71, -1}, asm.build());
         }
 
         { // Null key.
@@ -479,7 +479,7 @@ public class RowAssemblerTest {
             asm.appendString("key");
             asm.appendNull();
 
-            assertRowBytesEquals(new byte[] {42, 0, 16, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 1, 0, 9, 0, 107, 101, 121, 5, 0, 0, 0, 1}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 16, 0, 95, -98, 1, 0, 12, 0, 0, 0, 0, 1, 0, 9, 0, 107, 101, 121, 5, 0, 0, 0, 1}, asm.build());
         }
 
         { // Null both.
@@ -496,7 +496,7 @@ public class RowAssemblerTest {
 
             asm.appendString("key");
 
-            assertRowBytesEquals(new byte[] {42, 0, 17, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 1, 0, 9, 0, 107, 101, 121}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 17, 0, 95, -98, 1, 0, 12, 0, 0, 0, 0, 1, 0, 9, 0, 107, 101, 121}, asm.build());
         }
     }
 
@@ -513,7 +513,7 @@ public class RowAssemblerTest {
             asm.appendString("key");
             asm.appendShort((short)-71L);
 
-            assertRowBytesEquals(new byte[] {42, 0, 20, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 1, 0, 9, 0, 107, 101, 121, 6, 0, 0, 0, -71, -1}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 20, 0, 95, -98, 1, 0, 12, 0, 0, 0, 0, 1, 0, 9, 0, 107, 101, 121, 6, 0, 0, 0, -71, -1}, asm.build());
         }
 
         { // Null key.
@@ -530,7 +530,7 @@ public class RowAssemblerTest {
 
             asm.appendString("key");
 
-            assertRowBytesEquals(new byte[] {42, 0, 21, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 1, 0, 9, 0, 107, 101, 121}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 21, 0, 95, -98, 1, 0, 12, 0, 0, 0, 0, 1, 0, 9, 0, 107, 101, 121}, asm.build());
         }
     }
 
@@ -547,7 +547,7 @@ public class RowAssemblerTest {
             asm.appendString("key");
             asm.appendBytes(new byte[] {-1, 1, 0, 120});
 
-            assertRowBytesEquals(new byte[] {42, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 1, 0, 9, 0, 107, 101, 121, 13, 0, 0, 0, 0, 1, 0, 9, 0, -1, 1, 0, 120}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 0, 0, 95, -98, 1, 0, 12, 0, 0, 0, 0, 1, 0, 9, 0, 107, 101, 121, 13, 0, 0, 0, 0, 1, 0, 9, 0, -1, 1, 0, 120}, asm.build());
         }
 
         { // Null key.
@@ -565,7 +565,7 @@ public class RowAssemblerTest {
             asm.appendString("key");
             asm.appendNull();
 
-            assertRowBytesEquals(new byte[] {42, 0, 16, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 1, 0, 9, 0, 107, 101, 121, 5, 0, 0, 0, 1}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 16, 0, 95, -98, 1, 0, 12, 0, 0, 0, 0, 1, 0, 9, 0, 107, 101, 121, 5, 0, 0, 0, 1}, asm.build());
         }
 
         { // Null both.
@@ -582,7 +582,7 @@ public class RowAssemblerTest {
 
             asm.appendString("key");
 
-            assertRowBytesEquals(new byte[] {42, 0, 17, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 1, 0, 9, 0, 107, 101, 121}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 17, 0, 95, -98, 1, 0, 12, 0, 0, 0, 0, 1, 0, 9, 0, 107, 101, 121}, asm.build());
         }
     }
 
@@ -599,7 +599,7 @@ public class RowAssemblerTest {
             asm.appendString("key");
             asm.appendBytes(new byte[] {-1, 1, 0, 120});
 
-            assertRowBytesEquals(new byte[] {42, 0, 4, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 1, 0, 9, 0, 107, 101, 121, 12, 0, 0, 0, 1, 0, 8, 0, -1, 1, 0, 120}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 4, 0, 95, -98, 1, 0, 12, 0, 0, 0, 0, 1, 0, 9, 0, 107, 101, 121, 12, 0, 0, 0, 1, 0, 8, 0, -1, 1, 0, 120}, asm.build());
         }
 
         { // Null key.
@@ -616,7 +616,7 @@ public class RowAssemblerTest {
 
             asm.appendString("key");
 
-            assertRowBytesEquals(new byte[] {42, 0, 21, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 1, 0, 9, 0, 107, 101, 121}, asm.build());
+            assertRowBytesEquals(new byte[] {42, 0, 21, 0, 95, -98, 1, 0, 12, 0, 0, 0, 0, 1, 0, 9, 0, 107, 101, 121}, asm.build());
         }
     }
 

[ignite-3] 02/02: Add affinity columns support.

Posted by am...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

amashenkov pushed a commit to branch ignite-14388
in repository https://gitbox.apache.org/repos/asf/ignite-3.git

commit 7413dd64d7521f46326508d9853e4fa944bdcefe
Author: Andrew Mashenkov <an...@gmail.com>
AuthorDate: Wed May 12 22:51:31 2021 +0300

    Add affinity columns support.
---
 .../ignite/internal/schema/RowAssembler.java       |   9 +-
 .../ignite/internal/schema/SchemaDescriptor.java   |  29 +++-
 .../ignite/internal/schema/RowAssemblerTest.java   | 162 +++++++++++++++++++++
 3 files changed, 196 insertions(+), 4 deletions(-)

diff --git a/modules/schema/src/main/java/org/apache/ignite/internal/schema/RowAssembler.java b/modules/schema/src/main/java/org/apache/ignite/internal/schema/RowAssembler.java
index fb7ab43..2a594bf 100644
--- a/modules/schema/src/main/java/org/apache/ignite/internal/schema/RowAssembler.java
+++ b/modules/schema/src/main/java/org/apache/ignite/internal/schema/RowAssembler.java
@@ -25,6 +25,8 @@ import java.util.BitSet;
 import java.util.UUID;
 import org.apache.ignite.internal.schema.BinaryRow.RowFlags;
 
+import static org.apache.ignite.internal.schema.BinaryRow.RowFlags.OMIT_KEY_VARTBL_FLAG;
+import static org.apache.ignite.internal.schema.BinaryRow.RowFlags.OMIT_VAL_VARTBL_FLAG;
 import static org.apache.ignite.internal.schema.BinaryRow.VARLEN_COLUMN_OFFSET_FIELD_SIZE;
 import static org.apache.ignite.internal.schema.BinaryRow.VARLEN_TABLE_SIZE_FIELD_SIZE;
 
@@ -187,7 +189,7 @@ public class RowAssembler {
         buf.putShort(0, (short)schema.version());
 
         if (nonNullVarlenKeyCols == 0)
-            flags |= RowFlags.OMIT_KEY_VARTBL_FLAG;
+            flags |= OMIT_KEY_VARTBL_FLAG;
         else
             buf.putShort(varlenTblChunkOff, (short)nonNullVarlenKeyCols);
     }
@@ -429,7 +431,8 @@ public class RowAssembler {
      * @param off Offset to write.
      */
     private void writeOffset(int tblEntryIdx, int off) {
-        assert (flags & (baseOff == BinaryRow.KEY_CHUNK_OFFSET ? RowFlags.OMIT_KEY_VARTBL_FLAG : RowFlags.OMIT_VAL_VARTBL_FLAG)) == 0;
+        assert (flags & (baseOff == BinaryRow.KEY_CHUNK_OFFSET ? OMIT_KEY_VARTBL_FLAG : OMIT_VAL_VARTBL_FLAG)) == 0 :
+            "Illegal writing of varlen when 'omit vartable' flag is set for a chunk.";
 
         buf.putShort(varlenTblChunkOff + Row.varlenItemOffset(tblEntryIdx), (short)off);
     }
@@ -510,7 +513,7 @@ public class RowAssembler {
             initOffsets(baseOff + chunkLen, nonNullVarlenValCols);
 
             if (nonNullVarlenValCols == 0)
-                flags |= RowFlags.OMIT_VAL_VARTBL_FLAG;
+                flags |= OMIT_VAL_VARTBL_FLAG;
             else
                 buf.putShort(varlenTblChunkOff, (short)nonNullVarlenValCols);
         }
diff --git a/modules/schema/src/main/java/org/apache/ignite/internal/schema/SchemaDescriptor.java b/modules/schema/src/main/java/org/apache/ignite/internal/schema/SchemaDescriptor.java
index 2064068..d597215 100644
--- a/modules/schema/src/main/java/org/apache/ignite/internal/schema/SchemaDescriptor.java
+++ b/modules/schema/src/main/java/org/apache/ignite/internal/schema/SchemaDescriptor.java
@@ -18,6 +18,7 @@
 package org.apache.ignite.internal.schema;
 
 import java.util.Arrays;
+import java.util.BitSet;
 import java.util.HashMap;
 import java.util.Map;
 import org.apache.ignite.internal.tostring.S;
@@ -40,12 +41,29 @@ public class SchemaDescriptor {
     /** Mapping 'Column name' -> Column. */
     private final Map<String, Column> colMap;
 
+    /** Affinity column bit mask. */
+    private final BitSet affColsMask;
+
     /**
      * @param ver Schema version.
      * @param keyCols Key columns.
      * @param valCols Value columns.
      */
     public SchemaDescriptor(int ver, Column[] keyCols, Column[] valCols) {
+        this(ver, keyCols, null, valCols);
+    }
+
+    /**
+     * @param ver Schema version.
+     * @param keyCols Key columns.
+     * @param affCols Affinity column names.
+     * @param valCols Value columns.
+     */
+    public SchemaDescriptor(int ver, Column[] keyCols, @Nullable String[] affCols, Column[] valCols) {
+        assert affCols == null || affCols.length > 0;
+        assert keyCols.length > 0 : "No key columns are conigured.";
+        assert valCols.length > 0 : "No value columns are conigured.";
+
         this.ver = ver;
         this.keyCols = new Columns(0, keyCols);
         this.valCols = new Columns(keyCols.length, valCols);
@@ -54,6 +72,15 @@ public class SchemaDescriptor {
 
         Arrays.stream(this.keyCols.columns()).forEach(c -> colMap.put(c.name(), c));
         Arrays.stream(this.valCols.columns()).forEach(c -> colMap.put(c.name(), c));
+
+        if (affCols == null)
+            affColsMask = null;
+        else {
+            affColsMask = new BitSet(keyCols.length);
+
+            for (int i = 0; i < affCols.length; i++)
+                affColsMask.set(column(affCols[i]).schemaIndex());
+        }
     }
 
     /**
@@ -76,7 +103,7 @@ public class SchemaDescriptor {
      * @return {@code true} if the columns if key affinity column, {@code false} otherwise.
      */
     public boolean isAffinityColumn(int idx) {
-        return isKeyColumn(idx);
+        return affColsMask == null ? isKeyColumn(idx) : affColsMask.get(idx);
     }
 
     /**
diff --git a/modules/schema/src/test/java/org/apache/ignite/internal/schema/RowAssemblerTest.java b/modules/schema/src/test/java/org/apache/ignite/internal/schema/RowAssemblerTest.java
index cdf7df4..090f43f 100644
--- a/modules/schema/src/test/java/org/apache/ignite/internal/schema/RowAssemblerTest.java
+++ b/modules/schema/src/test/java/org/apache/ignite/internal/schema/RowAssemblerTest.java
@@ -36,6 +36,9 @@ public class RowAssemblerTest {
     /** Uuid test value. */
     public final java.util.UUID uuidVal = new UUID(-5204230847775358097L, 4916207022290092939L);
 
+    /**
+     * Validate row layout for schema of fix-len non-null key and fix-len nullable value.
+     */
     @Test
     public void testFixedKeyFixedNullableValue() {
         Column[] keyCols = new Column[] {new Column("keyIntCol", INTEGER, false)};
@@ -70,6 +73,9 @@ public class RowAssemblerTest {
         }
     }
 
+    /**
+     * Validate row layout for schema of fix-len non-null key and fix-len non-null value.
+     */
     @Test
     public void testFixedKeyFixedValue() {
         Column[] keyCols = new Column[] {new Column("keyShortCol", SHORT, false)};
@@ -95,6 +101,9 @@ public class RowAssemblerTest {
         }
     }
 
+    /**
+     * Validate row layout for schema of fix-len non-null key and var-len nullable value.
+     */
     @Test
     public void testFixedKeyVarlenNullableValue() {
         Column[] keyCols = new Column[] {new Column("keyShortCol", SHORT, false)};
@@ -129,6 +138,9 @@ public class RowAssemblerTest {
         }
     }
 
+    /**
+     * Validate row layout for schema of fix-len non-null key and var-len non-null value.
+     */
     @Test
     public void testFixedKeyVarlenValue() {
         Column[] keyCols = new Column[] {new Column("keyShortCol", SHORT, false)};
@@ -154,6 +166,9 @@ public class RowAssemblerTest {
         }
     }
 
+    /**
+     * Validate row layout for schema of fix-len nullable key and fix-len non-null value.
+     */
     @Test
     public void testFixedNullableKeyFixedValue() {
         Column[] keyCols = new Column[] {new Column("keyShortCol", SHORT, true)};
@@ -188,6 +203,9 @@ public class RowAssemblerTest {
         }
     }
 
+    /**
+     * Validate row layout for schema of fix-len nullable key and fix-len nullable value.
+     */
     @Test
     public void testFixedNullableKeyFixedNullableValue() {
         Column[] keyCols = new Column[] {new Column("keyShortCol", SHORT, true)};
@@ -240,6 +258,9 @@ public class RowAssemblerTest {
         }
     }
 
+    /**
+     * Validate row layout for schema of fix-len nullable key and var-len nullable value.
+     */
     @Test
     public void testFixedNullableKeyVarlenNullableValue() {
         Column[] keyCols = new Column[] {new Column("keyIntCol", INTEGER, true)};
@@ -292,6 +313,9 @@ public class RowAssemblerTest {
         }
     }
 
+    /**
+     * Validate row layout for schema of fix-len nullable key and var-len non-null value.
+     */
     @Test
     public void testFixedNullableKeyVarlenValue() {
         Column[] keyCols = new Column[] {new Column("keyByteCol", BYTE, true)};
@@ -326,6 +350,9 @@ public class RowAssemblerTest {
         }
     }
 
+    /**
+     * Validate row layout for schema of var-len non-null key and fix-len nullable value.
+     */
     @Test
     public void testVarlenKeyFixedNullableValue() {
         Column[] keyCols = new Column[] {new Column("keyStrCol", STRING, false)};
@@ -362,6 +389,9 @@ public class RowAssemblerTest {
         }
     }
 
+    /**
+     * Validate row layout for schema of var-len non-null key and fix-len non-null value.
+     */
     @Test
     public void testVarlenKeyFixedValue() {
         Column[] keyCols = new Column[] {new Column("keyStrCol", STRING, false)};
@@ -389,6 +419,9 @@ public class RowAssemblerTest {
         }
     }
 
+    /**
+     * Validate row layout for schema of var-len non-null key and var-len nullable value.
+     */
     @Test
     public void testVarlenKeyVarlenNullableValue() {
         Column[] keyCols = new Column[] {new Column("keyStrCol", STRING, false)};
@@ -423,6 +456,9 @@ public class RowAssemblerTest {
         }
     }
 
+    /**
+     * Validate row layout for schema of var-len non-null key and var-len non-null value.
+     */
     @Test
     public void testVarlenKeyVarlenValue() {
         Column[] keyCols = new Column[] {new Column("keyStrCol", STRING, false)};
@@ -448,6 +484,9 @@ public class RowAssemblerTest {
         }
     }
 
+    /**
+     * Validate row layout for schema of var-len nullable key and fix-len nullable value.
+     */
     @Test
     public void testVarlenNullableKeyFixedNullableValue() {
         Column[] keyCols = new Column[] {new Column("keyStrCol", STRING, true)};
@@ -500,6 +539,9 @@ public class RowAssemblerTest {
         }
     }
 
+    /**
+     * Validate row layout for schema of var-len nullable key and fix-len non-null value.
+     */
     @Test
     public void testVarlenNullableKeyFixedValue() {
         Column[] keyCols = new Column[] {new Column("keyStrCol", STRING, true)};
@@ -534,6 +576,9 @@ public class RowAssemblerTest {
         }
     }
 
+    /**
+     * Validate row layout for schema of var-len nullable key and var-len nullable value.
+     */
     @Test
     public void testVarlenNullableKeyVarlenNullableValue() {
         Column[] keyCols = new Column[] {new Column("keyStrCol", STRING, true)};
@@ -586,6 +631,9 @@ public class RowAssemblerTest {
         }
     }
 
+    /**
+     * Validate row layout for schema of var-len nullable key and var-len non-null value.
+     */
     @Test
     public void testVarlenNullableKeyVarlenValue() {
         Column[] keyCols = new Column[] {new Column("keyStrCol", STRING, true)};
@@ -621,6 +669,120 @@ public class RowAssemblerTest {
     }
 
     /**
+     * Validate row layout for key\value columns of different types.
+     */
+    @Test
+    public void testhMixedTypes() {
+        Column[] keyCols = new Column[] {
+            new Column("keyShortCol", SHORT, false),
+            new Column("keyStrCol", STRING, false)
+        };
+        Column[] valCols = new Column[] {
+            new Column("valIntCol", INTEGER, true),
+            new Column("valStrCol", STRING, true)
+        };
+
+        SchemaDescriptor schema = new SchemaDescriptor(42, keyCols, valCols);
+
+        {
+            RowAssembler asm = new RowAssembler(schema, 0, 1, 1);
+
+            asm.appendShort((short)33);
+            asm.appendString("keystr");
+            asm.appendInt(73);
+            asm.appendString("valstr");
+
+            assertRowBytesEquals(new byte[] {
+                42, 0, 2, 0, -110, -109, 94, -68,
+                16, 0, 0, 0, 1, 0, 10, 0, 33, 0, 107, 101, 121, 115, 116, 114,
+                19, 0, 0, 0, 0, 1, 0, 13, 0, 73, 0, 0, 0, 118, 97, 108, 115, 116, 114}, asm.build());
+        }
+
+        { // Null value.
+            RowAssembler asm = new RowAssembler(schema, 0, 1, 0);
+
+            asm.appendShort((short)33);
+            asm.appendString("keystr2");
+            asm.appendNull();
+            asm.appendNull();
+
+            assertRowBytesEquals(new byte[] {
+                42, 0, 18, 0, 32, 99, 115, -49,
+                17, 0, 0, 0, 1, 0, 10, 0, 33, 0, 107, 101, 121, 115, 116, 114, 50,
+                5, 0, 0, 0, 3}, asm.build());
+        }
+
+        { // No value.
+            RowAssembler asm = new RowAssembler(schema, 0, 1, 0);
+
+            asm.appendShort((short)33);
+            asm.appendString("keystr");
+
+            assertRowBytesEquals(new byte[] {
+                42, 0, 19, 0, -110, -109, 94, -68,
+                16, 0, 0, 0, 1, 0, 10, 0, 33, 0, 107, 101, 121, 115, 116, 114}, asm.build());
+
+        }
+    }
+
+    /**
+     * Validate row layout for key\value columns of different types.
+     */
+    @Test
+    public void testhMixedTypesWithAffinity() {
+        Column[] keyCols = new Column[] {
+            new Column("keyShortCol", SHORT, false),
+            new Column("keyStrCol", STRING, false)
+        };
+        Column[] valCols = new Column[] {
+            new Column("valIntCol", INTEGER, true),
+            new Column("valStrCol", STRING, true)
+        };
+
+        SchemaDescriptor schema = new SchemaDescriptor(42, keyCols, new String[] {"keyShortCol"}, valCols);
+
+        {
+            RowAssembler asm = new RowAssembler(schema, 0, 1, 1);
+
+            asm.appendShort((short)33);
+            asm.appendString("keystr");
+            asm.appendInt(73);
+            asm.appendString("valstr");
+
+            assertRowBytesEquals(new byte[] {
+                42, 0, 2, 0, 33, 0, 0, 0,
+                16, 0, 0, 0, 1, 0, 10, 0, 33, 0, 107, 101, 121, 115, 116, 114,
+                19, 0, 0, 0, 0, 1, 0, 13, 0, 73, 0, 0, 0, 118, 97, 108, 115, 116, 114}, asm.build());
+        }
+
+        { // Null value.
+            RowAssembler asm = new RowAssembler(schema, 0, 1, 0);
+
+            asm.appendShort((short)33);
+            asm.appendString("keystr2");
+            asm.appendNull();
+            asm.appendNull();
+
+            assertRowBytesEquals(new byte[] {
+                42, 0, 18, 0, 33, 0, 0, 0,
+                17, 0, 0, 0, 1, 0, 10, 0, 33, 0, 107, 101, 121, 115, 116, 114, 50,
+                5, 0, 0, 0, 3}, asm.build());
+        }
+
+        { // No value.
+            RowAssembler asm = new RowAssembler(schema, 0, 1, 0);
+
+            asm.appendShort((short)33);
+            asm.appendString("keystr");
+
+            assertRowBytesEquals(new byte[] {
+                42, 0, 19, 0, 33, 0, 0, 0,
+                16, 0, 0, 0, 1, 0, 10, 0, 33, 0, 107, 101, 121, 115, 116, 114}, asm.build());
+
+        }
+    }
+
+    /**
      * @param expected Expected row bytes.
      * @param actual Actual row bytes.
      */