View Javadoc

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 static java.lang.Integer.rotateLeft;
23  
24  import java.io.FileInputStream;
25  import java.io.IOException;
26  
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.classification.InterfaceStability;
29  
30  /**
31   * Produces 32-bit hash for hash table lookup.
32   *
33   * <pre>lookup3.c, by Bob Jenkins, May 2006, Public Domain.
34   *
35   * You can use this free for any purpose.  It's in the public domain.
36   * It has no warranty.
37   * </pre>
38   *
39   * @see <a href="http://burtleburtle.net/bob/c/lookup3.c">lookup3.c</a>
40   * @see <a href="http://www.ddj.com/184410284">Hash Functions (and how this
41   * function compares to others such as CRC, MD?, etc</a>
42   * @see <a href="http://burtleburtle.net/bob/hash/doobs.html">Has update on the
43   * Dr. Dobbs Article</a>
44   */
45  @InterfaceAudience.Public
46  @InterfaceStability.Stable
47  public class JenkinsHash extends Hash {
48    private static final int BYTE_MASK = 0xff;
49  
50    private static JenkinsHash _instance = new JenkinsHash();
51  
52    public static Hash getInstance() {
53      return _instance;
54    }
55  
56    /**
57     * taken from  hashlittle() -- hash a variable-length key into a 32-bit value
58     *
59     * @param key the key (the unaligned variable-length array of bytes)
60     * @param nbytes number of bytes to include in hash
61     * @param initval can be any integer value
62     * @return a 32-bit value.  Every bit of the key affects every bit of the
63     * return value.  Two keys differing by one or two bits will have totally
64     * different hash values.
65     *
66     * <p>The best hash table sizes are powers of 2.  There is no need to do mod
67     * a prime (mod is sooo slow!).  If you need less than 32 bits, use a bitmask.
68     * For example, if you need only 10 bits, do
69     * <code>h = (h & hashmask(10));</code>
70     * In which case, the hash table should have hashsize(10) elements.
71     *
72     * <p>If you are hashing n strings byte[][] k, do it like this:
73     * for (int i = 0, h = 0; i < n; ++i) h = hash( k[i], h);
74     *
75     * <p>By Bob Jenkins, 2006.  bob_jenkins@burtleburtle.net.  You may use this
76     * code any way you wish, private, educational, or commercial.  It's free.
77     *
78     * <p>Use for hash table lookup, or anything where one collision in 2^^32 is
79     * acceptable.  Do NOT use for cryptographic purposes.
80    */
81    @Override
82    @SuppressWarnings("fallthrough")
83    public int hash(byte[] key, int off, int nbytes, int initval) {
84      int length = nbytes;
85      int a, b, c;
86      a = b = c = 0xdeadbeef + length + initval;
87      int offset = off;
88      for (; length > 12; offset += 12, length -= 12) {
89        a += (key[offset] & BYTE_MASK);
90        a += ((key[offset + 1] & BYTE_MASK) <<  8);
91        a += ((key[offset + 2] & BYTE_MASK) << 16);
92        a += ((key[offset + 3] & BYTE_MASK) << 24);
93        b += (key[offset + 4] & BYTE_MASK);
94        b += ((key[offset + 5] & BYTE_MASK) <<  8);
95        b += ((key[offset + 6] & BYTE_MASK) << 16);
96        b += ((key[offset + 7] & BYTE_MASK) << 24);
97        c += (key[offset + 8] & BYTE_MASK);
98        c += ((key[offset + 9] & BYTE_MASK) <<  8);
99        c += ((key[offset + 10] & BYTE_MASK) << 16);
100       c += ((key[offset + 11] & BYTE_MASK) << 24);
101 
102       /*
103        * mix -- mix 3 32-bit values reversibly.
104        * This is reversible, so any information in (a,b,c) before mix() is
105        * still in (a,b,c) after mix().
106        *
107        * If four pairs of (a,b,c) inputs are run through mix(), or through
108        * mix() in reverse, there are at least 32 bits of the output that
109        * are sometimes the same for one pair and different for another pair.
110        *
111        * This was tested for:
112        * - pairs that differed by one bit, by two bits, in any combination
113        *   of top bits of (a,b,c), or in any combination of bottom bits of
114        *   (a,b,c).
115        * - "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
116        *   the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
117        *    is commonly produced by subtraction) look like a single 1-bit
118        *    difference.
119        * - the base values were pseudorandom, all zero but one bit set, or
120        *   all zero plus a counter that starts at zero.
121        *
122        * Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
123        * satisfy this are
124        *     4  6  8 16 19  4
125        *     9 15  3 18 27 15
126        *    14  9  3  7 17  3
127        * Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing for
128        * "differ" defined as + with a one-bit base and a two-bit delta.  I
129        * used http://burtleburtle.net/bob/hash/avalanche.html to choose
130        * the operations, constants, and arrangements of the variables.
131        *
132        * This does not achieve avalanche.  There are input bits of (a,b,c)
133        * that fail to affect some output bits of (a,b,c), especially of a.
134        * The most thoroughly mixed value is c, but it doesn't really even
135        * achieve avalanche in c.
136        *
137        * This allows some parallelism.  Read-after-writes are good at doubling
138        * the number of bits affected, so the goal of mixing pulls in the
139        * opposite direction as the goal of parallelism.  I did what I could.
140        * Rotates seem to cost as much as shifts on every machine I could lay
141        * my hands on, and rotates are much kinder to the top and bottom bits,
142        * so I used rotates.
143        *
144        * #define mix(a,b,c) \
145        * { \
146        *   a -= c;  a ^= rot(c, 4);  c += b; \
147        *   b -= a;  b ^= rot(a, 6);  a += c; \
148        *   c -= b;  c ^= rot(b, 8);  b += a; \
149        *   a -= c;  a ^= rot(c,16);  c += b; \
150        *   b -= a;  b ^= rot(a,19);  a += c; \
151        *   c -= b;  c ^= rot(b, 4);  b += a; \
152        * }
153        *
154        * mix(a,b,c);
155        */
156       a -= c; a ^= rotateLeft(c, 4); c += b;
157       b -= a; b ^= rotateLeft(a, 6); a += c;
158       c -= b; c ^= rotateLeft(b, 8); b += a;
159       a -= c; a ^= rotateLeft(c, 16); c += b;
160       b -= a; b ^= rotateLeft(a, 19); a += c;
161       c -= b; c ^= rotateLeft(b, 4); b += a;
162     }
163 
164     //-------------------------------- last block: affect all 32 bits of (c)
165     switch (length) {                   // all the case statements fall through
166     case 12:
167     	c += ((key[offset + 11] & BYTE_MASK) << 24);
168     case 11:
169       c += ((key[offset + 10] & BYTE_MASK) << 16);
170     case 10:
171       c += ((key[offset + 9] & BYTE_MASK) <<  8);
172     case  9:
173       c += (key[offset + 8] & BYTE_MASK);
174     case  8:
175       b += ((key[offset + 7] & BYTE_MASK) << 24);
176     case  7:
177       b += ((key[offset + 6] & BYTE_MASK) << 16);
178     case  6:
179       b += ((key[offset + 5] & BYTE_MASK) <<  8);
180     case  5:
181       b += (key[offset + 4] & BYTE_MASK);
182     case  4:
183       a += ((key[offset + 3] & BYTE_MASK) << 24);
184     case  3:
185       a += ((key[offset + 2] & BYTE_MASK) << 16);
186     case  2:
187       a += ((key[offset + 1] & BYTE_MASK) <<  8);
188     case  1:
189       //noinspection PointlessArithmeticExpression
190       a += (key[offset + 0] & BYTE_MASK);
191       break;
192     case  0:
193       return c;
194     }
195     /*
196      * final -- final mixing of 3 32-bit values (a,b,c) into c
197      *
198      * Pairs of (a,b,c) values differing in only a few bits will usually
199      * produce values of c that look totally different.  This was tested for
200      * - pairs that differed by one bit, by two bits, in any combination
201      *   of top bits of (a,b,c), or in any combination of bottom bits of
202      *   (a,b,c).
203      *
204      * - "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
205      *   the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
206      *   is commonly produced by subtraction) look like a single 1-bit
207      *   difference.
208      *
209      * - the base values were pseudorandom, all zero but one bit set, or
210      *   all zero plus a counter that starts at zero.
211      *
212      * These constants passed:
213      *   14 11 25 16 4 14 24
214      *   12 14 25 16 4 14 24
215      * and these came close:
216      *    4  8 15 26 3 22 24
217      *   10  8 15 26 3 22 24
218      *   11  8 15 26 3 22 24
219      *
220      * #define final(a,b,c) \
221      * {
222      *   c ^= b; c -= rot(b,14); \
223      *   a ^= c; a -= rot(c,11); \
224      *   b ^= a; b -= rot(a,25); \
225      *   c ^= b; c -= rot(b,16); \
226      *   a ^= c; a -= rot(c,4);  \
227      *   b ^= a; b -= rot(a,14); \
228      *   c ^= b; c -= rot(b,24); \
229      * }
230      *
231      */
232     c ^= b; c -= rotateLeft(b, 14);
233     a ^= c; a -= rotateLeft(c, 11);
234     b ^= a; b -= rotateLeft(a, 25);
235     c ^= b; c -= rotateLeft(b, 16);
236     a ^= c; a -= rotateLeft(c, 4);
237     b ^= a; b -= rotateLeft(a, 14);
238     c ^= b; c -= rotateLeft(b, 24);
239     return c;
240   }
241 
242   /**
243    * Compute the hash of the specified file
244    * @param args name of file to compute hash of.
245    * @throws IOException e
246    */
247   public static void main(String[] args) throws IOException {
248     if (args.length != 1) {
249       System.err.println("Usage: JenkinsHash filename");
250       System.exit(-1);
251     }
252     FileInputStream in = new FileInputStream(args[0]);
253     byte[] bytes = new byte[512];
254     int value = 0;
255     JenkinsHash hash = new JenkinsHash();
256     try {
257       for (int length = in.read(bytes); length > 0; length = in.read(bytes)) {
258         value = hash.hash(bytes, length, value);
259       }
260     } finally {
261       in.close();
262     }
263     System.out.println(Math.abs(value));
264   }
265 }