Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support more than one hashing algorithm.
Tested the crc32 hash algorithm against values returned from the perl client's implementation.
- Loading branch information
Showing
3 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package net.spy.memcached; | ||
|
||
import java.util.zip.CRC32; | ||
|
||
/** | ||
* Known hashing algorithms for locating a server for a key. | ||
*/ | ||
public enum HashAlgorithm { | ||
/** | ||
* Native hash (String.hashCode()). | ||
*/ | ||
NATIVE_HASH, | ||
/** | ||
* CRC32_HASH as used by the perl API. This will be more consistent | ||
* both across multiple API users as well as java versions. | ||
*/ | ||
CRC32_HASH; | ||
|
||
/** | ||
* Compute the hash for the given key. | ||
* @return a positive integer hash | ||
*/ | ||
public int hash(String k) { | ||
int rv=0; | ||
switch(this) { | ||
case NATIVE_HASH: | ||
rv=k.hashCode(); | ||
break; | ||
case CRC32_HASH: | ||
// return (crc32(shift) >> 16) & 0x7fff; | ||
CRC32 crc32=new CRC32(); | ||
crc32.update(k.getBytes()); | ||
rv=(int)(crc32.getValue() & 0xffffffff); | ||
rv=(rv >> 16) & 0x7fff; | ||
break; | ||
default: assert false; | ||
} | ||
return Math.abs(rv); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package net.spy.memcached; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import junit.framework.TestCase; | ||
|
||
/** | ||
* Test the hash algorithms. | ||
*/ | ||
public class HashAlgorithmTest extends TestCase { | ||
|
||
private void assertHash(HashAlgorithm ha, String key, int exp) { | ||
assertTrue(exp >= 0); | ||
assertEquals("Invalid " + ha + " for key " + key, exp, ha.hash(key)); | ||
// System.out.println(ha + "(" + key + ") = " + exp); | ||
} | ||
|
||
// I don't hardcode any values here because they're subject to change | ||
private void assertNativeHash(String key) { | ||
assertHash(HashAlgorithm.NATIVE_HASH, key, Math.abs(key.hashCode())); | ||
} | ||
|
||
public void testNativeHash() { | ||
for(String k : new String[]{"Test1", "Test2", "Test3", "Test4"}) { | ||
assertNativeHash(k); | ||
} | ||
} | ||
|
||
public void testCrc32Hash() { | ||
Map<String, Integer> exp=new HashMap<String, Integer>(); | ||
exp.put("Test1", 19315); | ||
exp.put("Test2", 21114); | ||
exp.put("Test3", 9597); | ||
exp.put("Test4", 15129); | ||
|
||
for(Map.Entry<String, Integer> me : exp.entrySet()) { | ||
assertHash(HashAlgorithm.CRC32_HASH, me.getKey(), me.getValue()); | ||
} | ||
} | ||
} |