View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.util;
22  
23  import org.apache.hadoop.conf.Configuration;
24  
25  /**
26   * This class represents a common API for hashing functions.
27   */
28  public abstract class Hash {
29    /** Constant to denote invalid hash type. */
30    public static final int INVALID_HASH = -1;
31    /** Constant to denote {@link JenkinsHash}. */
32    public static final int JENKINS_HASH = 0;
33    /** Constant to denote {@link MurmurHash}. */
34    public static final int MURMUR_HASH  = 1;
35  
36    /**
37     * This utility method converts String representation of hash function name
38     * to a symbolic constant. Currently two function types are supported,
39     * "jenkins" and "murmur".
40     * @param name hash function name
41     * @return one of the predefined constants
42     */
43    public static int parseHashType(String name) {
44      if ("jenkins".equalsIgnoreCase(name)) {
45        return JENKINS_HASH;
46      } else if ("murmur".equalsIgnoreCase(name)) {
47        return MURMUR_HASH;
48      } else {
49        return INVALID_HASH;
50      }
51    }
52  
53    /**
54     * This utility method converts the name of the configured
55     * hash type to a symbolic constant.
56     * @param conf configuration
57     * @return one of the predefined constants
58     */
59    public static int getHashType(Configuration conf) {
60      String name = conf.get("hbase.hash.type", "murmur");
61      return parseHashType(name);
62    }
63  
64    /**
65     * Get a singleton instance of hash function of a given type.
66     * @param type predefined hash type
67     * @return hash function instance, or null if type is invalid
68     */
69    public static Hash getInstance(int type) {
70      switch(type) {
71      case JENKINS_HASH:
72        return JenkinsHash.getInstance();
73      case MURMUR_HASH:
74        return MurmurHash.getInstance();
75      default:
76        return null;
77      }
78    }
79  
80    /**
81     * Get a singleton instance of hash function of a type
82     * defined in the configuration.
83     * @param conf current configuration
84     * @return defined hash type, or null if type is invalid
85     */
86    public static Hash getInstance(Configuration conf) {
87      int type = getHashType(conf);
88      return getInstance(type);
89    }
90  
91    /**
92     * Calculate a hash using all bytes from the input argument, and
93     * a seed of -1.
94     * @param bytes input bytes
95     * @return hash value
96     */
97    public int hash(byte[] bytes) {
98      return hash(bytes, bytes.length, -1);
99    }
100 
101   /**
102    * Calculate a hash using all bytes from the input argument,
103    * and a provided seed value.
104    * @param bytes input bytes
105    * @param initval seed value
106    * @return hash value
107    */
108   public int hash(byte[] bytes, int initval) {
109     return hash(bytes, 0, bytes.length, initval);
110   }
111 
112   /**
113    * Calculate a hash using bytes from 0 to <code>length</code>, and
114    * the provided seed value
115    * @param bytes input bytes
116    * @param length length of the valid bytes after offset to consider
117    * @param initval seed value
118    * @return hash value
119    */
120   public int hash(byte[] bytes, int length, int initval) {
121     return hash(bytes, 0, length, initval);
122   }
123 
124   /**
125    * Calculate a hash using bytes from <code>offset</code> to <code>offset + 
126    * length</code>, and the provided seed value.
127    * @param bytes input bytes
128    * @param offset the offset into the array to start consideration
129    * @param length length of the valid bytes after offset to consider
130    * @param initval seed value
131    * @return hash value
132    */
133   public abstract int hash(byte[] bytes, int offset, int length, int initval);
134 }