1 | package net.spy.memcached; |
2 | |
3 | import java.security.MessageDigest; |
4 | import java.security.NoSuchAlgorithmException; |
5 | import java.util.zip.CRC32; |
6 | |
7 | /** |
8 | * Known hashing algorithms for locating a server for a key. |
9 | * Note that all hash algorithms return 64-bits of hash, but only the lower |
10 | * 32-bits are significant. This allows a positive 32-bit number to be |
11 | * returned for all cases. |
12 | */ |
13 | public enum HashAlgorithm { |
14 | |
15 | /** |
16 | * Native hash (String.hashCode()). |
17 | */ |
18 | NATIVE_HASH, |
19 | /** |
20 | * CRC32_HASH as used by the perl API. This will be more consistent both |
21 | * across multiple API users as well as java versions, but is mostly likely |
22 | * significantly slower. |
23 | */ |
24 | CRC32_HASH, |
25 | /** |
26 | * FNV hashes are designed to be fast while maintaining a low collision |
27 | * rate. The FNV speed allows one to quickly hash lots of data while |
28 | * maintaining a reasonable collision rate. |
29 | * |
30 | * @see <a href="http://www.isthe.com/chongo/tech/comp/fnv/">fnv comparisons</a> |
31 | * @see <a href="http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash">fnv at wikipedia</a> |
32 | */ |
33 | FNV1_64_HASH, |
34 | /** |
35 | * Variation of FNV. |
36 | */ |
37 | FNV1A_64_HASH, |
38 | /** |
39 | * 32-bit FNV1. |
40 | */ |
41 | FNV1_32_HASH, |
42 | /** |
43 | * 32-bit FNV1a. |
44 | */ |
45 | FNV1A_32_HASH, |
46 | /** |
47 | * MD5-based hash algorithm used by ketama. |
48 | */ |
49 | KETAMA_HASH; |
50 | |
51 | private static final long FNV_64_INIT = 0xcbf29ce484222325L; |
52 | private static final long FNV_64_PRIME = 0x100000001b3L; |
53 | |
54 | private static final long FNV_32_INIT = 2166136261L; |
55 | private static final long FNV_32_PRIME = 16777619; |
56 | |
57 | /** |
58 | * Compute the hash for the given key. |
59 | * |
60 | * @return a positive integer hash |
61 | */ |
62 | public long hash(final String k) { |
63 | long rv = 0; |
64 | switch (this) { |
65 | case NATIVE_HASH: |
66 | rv = k.hashCode(); |
67 | break; |
68 | case CRC32_HASH: |
69 | // return (crc32(shift) >> 16) & 0x7fff; |
70 | CRC32 crc32 = new CRC32(); |
71 | crc32.update(KeyUtil.getKeyBytes(k)); |
72 | rv = (crc32.getValue() >> 16) & 0x7fff; |
73 | break; |
74 | case FNV1_64_HASH: { |
75 | // Thanks to pierre@demartines.com for the pointer |
76 | rv = FNV_64_INIT; |
77 | int len = k.length(); |
78 | for (int i = 0; i < len; i++) { |
79 | rv *= FNV_64_PRIME; |
80 | rv ^= k.charAt(i); |
81 | } |
82 | } |
83 | break; |
84 | case FNV1A_64_HASH: { |
85 | rv = FNV_64_INIT; |
86 | int len = k.length(); |
87 | for (int i = 0; i < len; i++) { |
88 | rv ^= k.charAt(i); |
89 | rv *= FNV_64_PRIME; |
90 | } |
91 | } |
92 | break; |
93 | case FNV1_32_HASH: { |
94 | rv = FNV_32_INIT; |
95 | int len = k.length(); |
96 | for (int i = 0; i < len; i++) { |
97 | rv *= FNV_32_PRIME; |
98 | rv ^= k.charAt(i); |
99 | } |
100 | } |
101 | break; |
102 | case FNV1A_32_HASH: { |
103 | rv = FNV_32_INIT; |
104 | int len = k.length(); |
105 | for (int i = 0; i < len; i++) { |
106 | rv ^= k.charAt(i); |
107 | rv *= FNV_32_PRIME; |
108 | } |
109 | } |
110 | break; |
111 | case KETAMA_HASH: |
112 | byte[] bKey=computeMd5(k); |
113 | rv = ((long) (bKey[3] & 0xFF) << 24) |
114 | | ((long) (bKey[2] & 0xFF) << 16) |
115 | | ((long) (bKey[1] & 0xFF) << 8) |
116 | | (bKey[0] & 0xFF); |
117 | break; |
118 | default: |
119 | assert false; |
120 | } |
121 | return rv & 0xffffffffL; /* Truncate to 32-bits */ |
122 | } |
123 | |
124 | /** |
125 | * Get the md5 of the given key. |
126 | */ |
127 | public static byte[] computeMd5(String k) { |
128 | MessageDigest md5; |
129 | try { |
130 | md5 = MessageDigest.getInstance("MD5"); |
131 | } catch (NoSuchAlgorithmException e) { |
132 | throw new RuntimeException("MD5 not supported", e); |
133 | } |
134 | md5.reset(); |
135 | md5.update(KeyUtil.getKeyBytes(k)); |
136 | return md5.digest(); |
137 | } |
138 | } |