3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 package org
.apache
.hadoop
.hbase
.util
;
22 import org
.apache
.hadoop
.conf
.Configuration
;
23 import org
.apache
.yetus
.audience
.InterfaceAudience
;
24 import org
.apache
.yetus
.audience
.InterfaceStability
;
27 * This class represents a common API for hashing functions.
29 @InterfaceAudience.Private
30 @InterfaceStability.Stable
31 public abstract class Hash
{
32 /** Constant to denote invalid hash type. */
33 public static final int INVALID_HASH
= -1;
34 /** Constant to denote {@link JenkinsHash}. */
35 public static final int JENKINS_HASH
= 0;
36 /** Constant to denote {@link MurmurHash}. */
37 public static final int MURMUR_HASH
= 1;
38 /** Constant to denote {@link MurmurHash3}. */
39 public static final int MURMUR_HASH3
= 2;
42 * This utility method converts String representation of hash function name
43 * to a symbolic constant. Currently three function types are supported,
44 * "jenkins", "murmur" and "murmur3".
45 * @param name hash function name
46 * @return one of the predefined constants
48 public static int parseHashType(String name
) {
49 if ("jenkins".equalsIgnoreCase(name
)) {
51 } else if ("murmur".equalsIgnoreCase(name
)) {
53 } else if ("murmur3".equalsIgnoreCase(name
)) {
61 * This utility method converts the name of the configured
62 * hash type to a symbolic constant.
63 * @param conf configuration
64 * @return one of the predefined constants
66 public static int getHashType(Configuration conf
) {
67 String name
= conf
.get("hbase.hash.type", "murmur");
68 return parseHashType(name
);
72 * Get a singleton instance of hash function of a given type.
73 * @param type predefined hash type
74 * @return hash function instance, or null if type is invalid
76 public static Hash
getInstance(int type
) {
79 return JenkinsHash
.getInstance();
81 return MurmurHash
.getInstance();
83 return MurmurHash3
.getInstance();
90 * Get a singleton instance of hash function of a type
91 * defined in the configuration.
92 * @param conf current configuration
93 * @return defined hash type, or null if type is invalid
95 public static Hash
getInstance(Configuration conf
) {
96 int type
= getHashType(conf
);
97 return getInstance(type
);
101 * Calculate a hash using bytes from HashKey and the provided seed value.
103 * @param hashKey key to extract the hash
104 * @param initval the seed value
107 public abstract <T
> int hash(HashKey
<T
> hashKey
, int initval
);