This is an automated email from the ASF dual-hosted git repository. snlee pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-pinot.git
The following commit(s) were added to refs/heads/master by this push: new 6b5b091 Adding a hash code partition function (#4220) 6b5b091 is described below commit 6b5b0912eb680db526bccd0d0bdf42b73b860063 Author: Seunghyun Lee <sn...@linkedin.com> AuthorDate: Tue May 21 09:23:29 2019 -0700 Adding a hash code partition function (#4220) - Adding a hash code partition function using object.hashCode() - Adding a unit test for hash code partition function --- .../data/partition/HashCodePartitionFunction.java | 52 ++++++++++++++++++++++ .../data/partition/PartitionFunctionFactory.java | 5 ++- .../core/data/partition/PartitionFunctionTest.java | 26 +++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/pinot-core/src/main/java/org/apache/pinot/core/data/partition/HashCodePartitionFunction.java b/pinot-core/src/main/java/org/apache/pinot/core/data/partition/HashCodePartitionFunction.java new file mode 100644 index 0000000..763647e --- /dev/null +++ b/pinot-core/src/main/java/org/apache/pinot/core/data/partition/HashCodePartitionFunction.java @@ -0,0 +1,52 @@ +/** + * 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.pinot.core.data.partition; + +import com.google.common.base.Preconditions; + +/** + * Hash code partition function, where: + * <ul> + * <li> partitionId = value.hashCode() % {@link #_numPartitions}</li> + * </ul> + */ +public class HashCodePartitionFunction implements PartitionFunction { + private static final String NAME = "HashCode"; + private final int _numPartitions; + + public HashCodePartitionFunction(int numPartitions) { + Preconditions.checkArgument(numPartitions > 0, "Number of partitions must be > 0, specified", numPartitions); + _numPartitions = numPartitions; + } + + @Override + public int getPartition(Object value) { + return Math.abs(value.hashCode()) % _numPartitions; + } + + @Override + public int getNumPartitions() { + return _numPartitions; + } + + @Override + public String toString() { + return NAME; + } +} diff --git a/pinot-core/src/main/java/org/apache/pinot/core/data/partition/PartitionFunctionFactory.java b/pinot-core/src/main/java/org/apache/pinot/core/data/partition/PartitionFunctionFactory.java index b204e0e..7cb1b5b 100644 --- a/pinot-core/src/main/java/org/apache/pinot/core/data/partition/PartitionFunctionFactory.java +++ b/pinot-core/src/main/java/org/apache/pinot/core/data/partition/PartitionFunctionFactory.java @@ -29,7 +29,7 @@ import javax.annotation.Nonnull; public class PartitionFunctionFactory { // Enum for various partition functions to be added. public enum PartitionFunctionType { - Modulo, Murmur, ByteArray; + Modulo, Murmur, ByteArray, HashCode; // Add more functions here. private static final Map<String, PartitionFunctionType> VALUE_MAP = new HashMap<>(); @@ -75,6 +75,9 @@ public class PartitionFunctionFactory { case ByteArray: return new ByteArrayPartitionFunction(numPartitions); + case HashCode: + return new HashCodePartitionFunction(numPartitions); + default: throw new IllegalArgumentException("Illegal partition function name: " + functionName); } diff --git a/pinot-core/src/test/java/org/apache/pinot/core/data/partition/PartitionFunctionTest.java b/pinot-core/src/test/java/org/apache/pinot/core/data/partition/PartitionFunctionTest.java index f0ce9b0..974d151 100644 --- a/pinot-core/src/test/java/org/apache/pinot/core/data/partition/PartitionFunctionTest.java +++ b/pinot-core/src/test/java/org/apache/pinot/core/data/partition/PartitionFunctionTest.java @@ -130,4 +130,30 @@ public class PartitionFunctionTest { } } } + + @Test + public void testHashCodePartitioner() { + long seed = System.currentTimeMillis(); + Random random = new Random(seed); + + for (int i = 0; i < 1000; i++) { + int expectedNumPartitions = Math.abs(random.nextInt()); + + // Avoid divide-by-zero. + if (expectedNumPartitions == 0) { + expectedNumPartitions = 1; + } + + String functionName = "HaShCoDe"; + PartitionFunction partitionFunction = + PartitionFunctionFactory.getPartitionFunction(functionName, expectedNumPartitions); + Assert.assertEquals(partitionFunction.toString().toLowerCase(), functionName.toLowerCase()); + Assert.assertEquals(partitionFunction.getNumPartitions(), expectedNumPartitions); + + for (int j = 0; j < 1000; j++) { + Integer value = random.nextInt(); + Assert.assertEquals(partitionFunction.getPartition(value), Math.abs(value.hashCode()) % expectedNumPartitions); + } + } + } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org