001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020package org.apache.hadoop.hbase.util; 021 022import org.apache.yetus.audience.InterfaceAudience; 023import org.apache.yetus.audience.InterfaceStability; 024 025/** 026 * This is a very fast, non-cryptographic hash suitable for general hash-based 027 * lookup. See http://murmurhash.googlepages.com/ for more details. 028 * 029 * <p>The C version of MurmurHash 2.0 found at that site was ported 030 * to Java by Andrzej Bialecki (ab at getopt org).</p> 031 */ 032@InterfaceAudience.Private 033@InterfaceStability.Stable 034public class MurmurHash extends Hash { 035 private static MurmurHash _instance = new MurmurHash(); 036 037 public static Hash getInstance() { 038 return _instance; 039 } 040 041 @Override 042 public <T> int hash(HashKey<T> hashKey, int seed) { 043 int m = 0x5bd1e995; 044 int r = 24; 045 int length = hashKey.length(); 046 int h = seed ^ length; 047 048 int len_4 = length >> 2; 049 050 for (int i = 0; i < len_4; i++) { 051 int i_4 = (i << 2); 052 int k = hashKey.get(i_4 + 3); 053 k = k << 8; 054 k = k | (hashKey.get(i_4 + 2) & 0xff); 055 k = k << 8; 056 k = k | (hashKey.get(i_4 + 1) & 0xff); 057 k = k << 8; 058 // noinspection PointlessArithmeticExpression 059 k = k | (hashKey.get(i_4 + 0) & 0xff); 060 k *= m; 061 k ^= k >>> r; 062 k *= m; 063 h *= m; 064 h ^= k; 065 } 066 067 // avoid calculating modulo 068 int len_m = len_4 << 2; 069 int left = length - len_m; 070 int i_m = len_m; 071 072 if (left != 0) { 073 if (left >= 3) { 074 h ^= hashKey.get(i_m + 2) << 16; 075 } 076 if (left >= 2) { 077 h ^= hashKey.get(i_m + 1) << 8; 078 } 079 if (left >= 1) { 080 h ^= hashKey.get(i_m); 081 } 082 083 h *= m; 084 } 085 086 h ^= h >>> 13; 087 h *= m; 088 h ^= h >>> 15; 089 090 return h; 091 } 092}