1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.util;
21
22 import org.apache.hadoop.hbase.classification.InterfaceAudience;
23 import org.apache.hadoop.hbase.classification.InterfaceStability;
24
25
26
27
28
29
30
31
32 @InterfaceAudience.Private
33 @InterfaceStability.Stable
34 public class MurmurHash extends Hash {
35 private static MurmurHash _instance = new MurmurHash();
36
37 public static Hash getInstance() {
38 return _instance;
39 }
40
41 @Override
42 public int hash(byte[] data, int offset, int length, int seed) {
43 int m = 0x5bd1e995;
44 int r = 24;
45
46 int h = seed ^ length;
47
48 int len_4 = length >> 2;
49
50 for (int i = 0; i < len_4; i++) {
51 int i_4 = (i << 2) + offset;
52 int k = data[i_4 + 3];
53 k = k << 8;
54 k = k | (data[i_4 + 2] & 0xff);
55 k = k << 8;
56 k = k | (data[i_4 + 1] & 0xff);
57 k = k << 8;
58
59 k = k | (data[i_4 + 0] & 0xff);
60 k *= m;
61 k ^= k >>> r;
62 k *= m;
63 h *= m;
64 h ^= k;
65 }
66
67
68 int len_m = len_4 << 2;
69 int left = length - len_m;
70 int i_m = len_m + offset;
71
72 if (left != 0) {
73 if (left >= 3) {
74 h ^= data[i_m + 2] << 16;
75 }
76 if (left >= 2) {
77 h ^= data[i_m + 1] << 8;
78 }
79 if (left >= 1) {
80 h ^= data[i_m];
81 }
82
83 h *= m;
84 }
85
86 h ^= h >>> 13;
87 h *= m;
88 h ^= h >>> 15;
89
90 return h;
91 }
92 }