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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 package org
.apache
.hadoop
.hbase
.util
;
22 import org
.apache
.yetus
.audience
.InterfaceAudience
;
23 import org
.apache
.yetus
.audience
.InterfaceStability
;
26 * This is a very fast, non-cryptographic hash suitable for general hash-based
27 * lookup. See http://murmurhash.googlepages.com/ for more details.
29 * <p>The C version of MurmurHash 2.0 found at that site was ported
30 * to Java by Andrzej Bialecki (ab at getopt org).</p>
32 @InterfaceAudience.Private
33 @InterfaceStability.Stable
34 public class MurmurHash
extends Hash
{
35 private static MurmurHash _instance
= new MurmurHash();
37 public static Hash
getInstance() {
42 public <T
> int hash(HashKey
<T
> hashKey
, int seed
) {
45 int length
= hashKey
.length();
46 int h
= seed ^ length
;
48 int len_4
= length
>> 2;
50 for (int i
= 0; i
< len_4
; i
++) {
52 int k
= hashKey
.get(i_4
+ 3);
54 k
= k
| (hashKey
.get(i_4
+ 2) & 0xff);
56 k
= k
| (hashKey
.get(i_4
+ 1) & 0xff);
58 // noinspection PointlessArithmeticExpression
59 k
= k
| (hashKey
.get(i_4
+ 0) & 0xff);
67 // avoid calculating modulo
68 int len_m
= len_4
<< 2;
69 int left
= length
- len_m
;
74 h ^
= hashKey
.get(i_m
+ 2) << 16;
77 h ^
= hashKey
.get(i_m
+ 1) << 8;
80 h ^
= hashKey
.get(i_m
);