Skip to content
This repository was archived by the owner on Feb 8, 2019. It is now read-only.

Commit 0800ced

Browse files
author
Wouter de Bie
committed
Ported Ruby's memcached client hashing algorithm. WARNING: This might break other stuff\!\!
1 parent 6a7fb63 commit 0800ced

File tree

5 files changed

+439
-264
lines changed

5 files changed

+439
-264
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.meetup.memcached;
2+
3+
import java.io.UnsupportedEncodingException;
4+
import java.security.MessageDigest;
5+
import java.security.NoSuchAlgorithmException;
6+
7+
public class AeSimpleSHA1 {
8+
9+
private static String convertToHex(byte[] data) {
10+
StringBuffer buf = new StringBuffer();
11+
for (int i = 0; i < data.length; i++) {
12+
int halfbyte = (data[i] >>> 4) & 0x0F;
13+
int two_halfs = 0;
14+
do {
15+
if ((0 <= halfbyte) && (halfbyte <= 9))
16+
buf.append((char) ('0' + halfbyte));
17+
else
18+
buf.append((char) ('a' + (halfbyte - 10)));
19+
halfbyte = data[i] & 0x0F;
20+
} while(two_halfs++ < 1);
21+
}
22+
return buf.toString();
23+
}
24+
25+
public static String SHA1(String text)
26+
throws NoSuchAlgorithmException, UnsupportedEncodingException {
27+
MessageDigest md;
28+
md = MessageDigest.getInstance("SHA-1");
29+
byte[] sha1hash = new byte[40];
30+
md.update(text.getBytes("iso-8859-1"), 0, text.length());
31+
sha1hash = md.digest();
32+
return convertToHex(sha1hash);
33+
}
34+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.meetup.memcached;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
* Created by IntelliJ IDEA.
7+
* User: wouter
8+
* Date: Jun 11, 2010
9+
* Time: 1:14:04 PM
10+
* To change this template use File | Settings | File Templates.
11+
*/
12+
public class Continuum {
13+
/*
14+
upper = ary.size - 1
15+
lower = 0
16+
idx = 0
17+
18+
while(lower <= upper) do
19+
idx = (lower + upper) / 2
20+
comp = ary[idx].value <=> value
21+
22+
if comp == 0
23+
return idx
24+
elsif comp > 0
25+
upper = idx - 1
26+
else
27+
lower = idx + 1
28+
end
29+
end
30+
return upper*/
31+
32+
public static int binarySearch(ArrayList<ContinuumEntry> buckets, long value) {
33+
int upper = buckets.size() - 1;
34+
int lower = 0;
35+
int idx = 0;
36+
while(lower <= upper){
37+
idx = (lower + upper) / 2;
38+
39+
long bucket_value = buckets.get(idx).getValue();
40+
41+
if(bucket_value == value){
42+
return idx;
43+
} else if(bucket_value > value){
44+
upper = idx - 1;
45+
} else {
46+
lower = idx + 1;
47+
}
48+
}
49+
if(upper == -1){
50+
return buckets.size() - 1;
51+
}
52+
return upper; //To change body of created methods use File | Settings | File Templates.
53+
}
54+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.meetup.memcached;
2+
3+
/**
4+
* Created by IntelliJ IDEA.
5+
* User: wouter
6+
* Date: Jun 11, 2010
7+
* Time: 12:31:13 PM
8+
* To change this template use File | Settings | File Templates.
9+
* <p/>
10+
* class Entry
11+
* attr_reader :value
12+
* attr_reader :server
13+
* <p/>
14+
* def initialize(val, srv)
15+
*
16+
* @value = val
17+
* @server = srv
18+
* end
19+
* <p/>
20+
* def inspect
21+
* "<#{value}, #{server.host}:#{server.port}>"
22+
* end
23+
* end
24+
*/
25+
public class ContinuumEntry implements Comparable<ContinuumEntry>{
26+
27+
28+
private Long value;
29+
private String server;
30+
31+
public ContinuumEntry(Long value, String server) {
32+
33+
this.value = value;
34+
this.server = server;
35+
}
36+
37+
public Long getValue() {
38+
return value;
39+
}
40+
41+
public void setValue(Long value) {
42+
this.value = value;
43+
}
44+
45+
public String getServer() {
46+
return server;
47+
}
48+
49+
public void setServer(String server) {
50+
this.server = server;
51+
}
52+
53+
public int compareTo(ContinuumEntry o) {
54+
if(this.getValue() > o.getValue()) {
55+
return 1;
56+
} else if(this.getValue() < o.getValue()) {
57+
return -1;
58+
}
59+
return 0;
60+
}
61+
}

src/com/meetup/memcached/MemcachedClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1635,7 +1635,7 @@ public Map<String,Object> getMulti( String[] keys, Integer[] hashCodes, boolean
16351635
* Pass a SockIO object which is ready to receive data and a HashMap<br/>
16361636
* to store the results.
16371637
*
1638-
* @param sock socket waiting to pass back data
1638+
* @param input socket waiting to pass back data
16391639
* @param hm hashmap to store data into
16401640
* @param asString if true, and if we are using NativehHandler, return string val
16411641
* @throws IOException if io exception happens while reading from socket

0 commit comments

Comments
 (0)