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