1 /** 2 * 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 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 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. 18 */ 19 20 package org.apache.hadoop.hbase.util; 21 22 import org.apache.hadoop.classification.InterfaceAudience; 23 import org.apache.hadoop.classification.InterfaceStability; 24 import org.apache.hadoop.conf.Configuration; 25 26 /** 27 * This class represents a common API for hashing functions. 28 */ 29 @InterfaceAudience.Public 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 39 /** 40 * This utility method converts String representation of hash function name 41 * to a symbolic constant. Currently two function types are supported, 42 * "jenkins" and "murmur". 43 * @param name hash function name 44 * @return one of the predefined constants 45 */ 46 public static int parseHashType(String name) { 47 if ("jenkins".equalsIgnoreCase(name)) { 48 return JENKINS_HASH; 49 } else if ("murmur".equalsIgnoreCase(name)) { 50 return MURMUR_HASH; 51 } else { 52 return INVALID_HASH; 53 } 54 } 55 56 /** 57 * This utility method converts the name of the configured 58 * hash type to a symbolic constant. 59 * @param conf configuration 60 * @return one of the predefined constants 61 */ 62 public static int getHashType(Configuration conf) { 63 String name = conf.get("hbase.hash.type", "murmur"); 64 return parseHashType(name); 65 } 66 67 /** 68 * Get a singleton instance of hash function of a given type. 69 * @param type predefined hash type 70 * @return hash function instance, or null if type is invalid 71 */ 72 public static Hash getInstance(int type) { 73 switch(type) { 74 case JENKINS_HASH: 75 return JenkinsHash.getInstance(); 76 case MURMUR_HASH: 77 return MurmurHash.getInstance(); 78 default: 79 return null; 80 } 81 } 82 83 /** 84 * Get a singleton instance of hash function of a type 85 * defined in the configuration. 86 * @param conf current configuration 87 * @return defined hash type, or null if type is invalid 88 */ 89 public static Hash getInstance(Configuration conf) { 90 int type = getHashType(conf); 91 return getInstance(type); 92 } 93 94 /** 95 * Calculate a hash using all bytes from the input argument, and 96 * a seed of -1. 97 * @param bytes input bytes 98 * @return hash value 99 */ 100 public int hash(byte[] bytes) { 101 return hash(bytes, bytes.length, -1); 102 } 103 104 /** 105 * Calculate a hash using all bytes from the input argument, 106 * and a provided seed value. 107 * @param bytes input bytes 108 * @param initval seed value 109 * @return hash value 110 */ 111 public int hash(byte[] bytes, int initval) { 112 return hash(bytes, 0, bytes.length, initval); 113 } 114 115 /** 116 * Calculate a hash using bytes from 0 to <code>length</code>, and 117 * the provided seed value 118 * @param bytes input bytes 119 * @param length length of the valid bytes after offset to consider 120 * @param initval seed value 121 * @return hash value 122 */ 123 public int hash(byte[] bytes, int length, int initval) { 124 return hash(bytes, 0, length, initval); 125 } 126 127 /** 128 * Calculate a hash using bytes from <code>offset</code> to <code>offset + 129 * length</code>, and the provided seed value. 130 * @param bytes input bytes 131 * @param offset the offset into the array to start consideration 132 * @param length length of the valid bytes after offset to consider 133 * @param initval seed value 134 * @return hash value 135 */ 136 public abstract int hash(byte[] bytes, int offset, int length, int initval); 137 }