diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2024-01-21 20:38:08 +0100 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2024-01-25 12:41:32 +0100 |
commit | ea8ff077472aa54a40ebb84b2daf6ceaf4ff3c55 (patch) | |
tree | 7ea467f65509d577196c4faa35d1c9983d472777 /vespajlib | |
parent | 9fd9b63dbae71e52838fa208629eba222d38e828 (diff) |
Support longhasher
Diffstat (limited to 'vespajlib')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/compress/Hasher.java | 17 | ||||
-rw-r--r-- | vespajlib/src/test/java/com/yahoo/tensor/TensorAddressTestCase.java | 44 |
2 files changed, 61 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/compress/Hasher.java b/vespajlib/src/main/java/com/yahoo/compress/Hasher.java index 92a9ed26085..7a3d34eca7b 100644 --- a/vespajlib/src/main/java/com/yahoo/compress/Hasher.java +++ b/vespajlib/src/main/java/com/yahoo/compress/Hasher.java @@ -8,8 +8,25 @@ import net.openhft.hashing.LongHashFunction; * @author baldersheim */ public class Hasher { + private final LongHashFunction hasher; /** Uses net.openhft.hashing.LongHashFunction.xx3() */ public static long xxh3(byte [] data) { return LongHashFunction.xx3().hashBytes(data); } + public static long xxh3(byte [] data, long seed) { + return LongHashFunction.xx3(seed).hashBytes(data); + } + + private Hasher(LongHashFunction hasher) { + this.hasher = hasher; + } + public static Hasher withSeed(long seed) { + return new Hasher(LongHashFunction.xx3(seed)); + } + public long hash(long v) { + return hasher.hashLong(v); + } + public long hash(String s) { + return hasher.hashChars(s); + } } diff --git a/vespajlib/src/test/java/com/yahoo/tensor/TensorAddressTestCase.java b/vespajlib/src/test/java/com/yahoo/tensor/TensorAddressTestCase.java new file mode 100644 index 00000000000..79202e3f07e --- /dev/null +++ b/vespajlib/src/test/java/com/yahoo/tensor/TensorAddressTestCase.java @@ -0,0 +1,44 @@ +// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.tensor; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +/** + * Test for tensor address. + * + * @author baldersheim + */ +public class TensorAddressTestCase { + private void equal(Object a, Object b) { + assertEquals(a.hashCode(), b.hashCode()); + assertEquals(a, b); + } + private void notEqual(Object a, Object b) { + assertNotEquals(a.hashCode(), b.hashCode()); // This might not hold, but is bad if not very rare + assertNotEquals(a, b); + } + @Test + void testStringVersusNumericAddressEquality() { + equal(TensorAddress.ofLabels("1"), TensorAddress.of(1)); + } + @Test + void testInEquality() { + notEqual(TensorAddress.ofLabels("1"), TensorAddress.ofLabels("2")); + notEqual(TensorAddress.of(1), TensorAddress.of(2)); + } + @Test + void testDimensionsEffectsEqualityAndHash() { + notEqual(TensorAddress.ofLabels("1"), TensorAddress.ofLabels("1", "1")); + notEqual(TensorAddress.of(1), TensorAddress.of(1, 1)); + } + @Test + void testAllowNullDimension() { + TensorAddress s1 = TensorAddress.ofLabels("1", null, "2"); + TensorAddress s2 = TensorAddress.ofLabels("1", "2"); + assertNotEquals(s1, s2); + assertEquals(s1.hashCode(), s2.hashCode()); + } +} |