001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020package org.apache.hadoop.hbase.util; 021 022import org.apache.hadoop.conf.Configuration; 023import org.apache.yetus.audience.InterfaceAudience; 024import org.apache.yetus.audience.InterfaceStability; 025 026/** 027 * This class represents a common API for hashing functions. 028 */ 029@InterfaceAudience.Private 030@InterfaceStability.Stable 031public abstract class Hash { 032 /** Constant to denote invalid hash type. */ 033 public static final int INVALID_HASH = -1; 034 /** Constant to denote {@link JenkinsHash}. */ 035 public static final int JENKINS_HASH = 0; 036 /** Constant to denote {@link MurmurHash}. */ 037 public static final int MURMUR_HASH = 1; 038 /** Constant to denote {@link MurmurHash3}. */ 039 public static final int MURMUR_HASH3 = 2; 040 041 /** 042 * This utility method converts String representation of hash function name 043 * to a symbolic constant. Currently three function types are supported, 044 * "jenkins", "murmur" and "murmur3". 045 * @param name hash function name 046 * @return one of the predefined constants 047 */ 048 public static int parseHashType(String name) { 049 if ("jenkins".equalsIgnoreCase(name)) { 050 return JENKINS_HASH; 051 } else if ("murmur".equalsIgnoreCase(name)) { 052 return MURMUR_HASH; 053 } else if ("murmur3".equalsIgnoreCase(name)) { 054 return MURMUR_HASH3; 055 } else { 056 return INVALID_HASH; 057 } 058 } 059 060 /** 061 * This utility method converts the name of the configured 062 * hash type to a symbolic constant. 063 * @param conf configuration 064 * @return one of the predefined constants 065 */ 066 public static int getHashType(Configuration conf) { 067 String name = conf.get("hbase.hash.type", "murmur"); 068 return parseHashType(name); 069 } 070 071 /** 072 * Get a singleton instance of hash function of a given type. 073 * @param type predefined hash type 074 * @return hash function instance, or null if type is invalid 075 */ 076 public static Hash getInstance(int type) { 077 switch (type) { 078 case JENKINS_HASH: 079 return JenkinsHash.getInstance(); 080 case MURMUR_HASH: 081 return MurmurHash.getInstance(); 082 case MURMUR_HASH3: 083 return MurmurHash3.getInstance(); 084 default: 085 return null; 086 } 087 } 088 089 /** 090 * Get a singleton instance of hash function of a type 091 * defined in the configuration. 092 * @param conf current configuration 093 * @return defined hash type, or null if type is invalid 094 */ 095 public static Hash getInstance(Configuration conf) { 096 int type = getHashType(conf); 097 return getInstance(type); 098 } 099 100 /** 101 * Calculate a hash using bytes from HashKey and the provided seed value. 102 * @param <T> 103 * @param hashKey key to extract the hash 104 * @param initval the seed value 105 * @return hash value 106 */ 107 public abstract <T> int hash(HashKey<T> hashKey, int initval); 108}