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