我正在开发一个类似 Redis 的开源 Memcache API:http ://bit.ly/XVfpRX
谁能帮我分析一下为什么这段代码的String field
andString value
变量的值是错误的:
public Long hset(String key, String field, String value) {
System.out.println("HSET key="+key+" field=" + field + " value="+value);
try {
boolean exist = exists(key);
if(exist){
Set<Tuple<String,String>> hash = (Set<Tuple<String,String>>) memget(key);
Iterator<Tuple<String, String>> it = hash.iterator();
while (it.hasNext()){
Tuple<String, String> t = it.next();
if (t.getFirst().equals(field)){
System.out.println("HSET Removing field=" + t.getFirst() + " value="+t.getSecond());
hash.remove(t);
hash.add(new Tuple<String, String>(field, value));
}
}
memset(key, hash);
return 0L;
} else {
Set<Tuple<String,String>> hash = new HashSet<Tuple<String,String>>();
System.out.println("Adding new tuple key="+key+" field=" + field + " value="+value);
hash.add(new Tuple<String, String>(field, value));
memset(key, hash);
return 1L;
}
} catch(Exception e) { // ClassCastException and NPE
// What to do when CCE is encountered?
Set<Tuple<String,String>> hash = new HashSet<Tuple<String,String>>();
hash.add(new Tuple<String, String>(field, value));
memset(key, hash);
}
return 1L;
}
以下是测试日志:
Start hset tests
HSET key=null field=null value=null
Adding new tuple key=null field=null value=null
MEMSET key=null value=[Tuple [first=null, second=null]]
HSET key=foo field=foo value=bar
Adding new tuple key=foo field=foo value=bar
MEMSET key=foo value=[Tuple [first=foo, second=bar]]
HSET key=foo field=bar value=foobar
MEMSET key=foo value=[Tuple [first=foo, second=bar]]
HSET key=user:1 field=uname value=foo
Adding new tuple key=user:1 field=uname value=foo
MEMSET key=user:1 value=[Tuple [first=uname, second=foo]]
HSET key=user:1 field=fname value=Bar
MEMSET key=user:1 value=[Tuple [first=uname, second=foo]]
HSET key=user:1 field=uname value=bar
HSET Removing field=uname value=foo
MEMSET key=user:1 value=[Tuple [first=uname, second=bar]]
还有实际的 JUnit 测试:
@Test
public void testHset() {
System.out.println("Start hset tests");
Long ret = lingo.hset(key, null, value);
assertEquals(1L, ret.longValue());
ret = lingo.hset("foo", "foo", "bar");
assertEquals(1L, ret.longValue());
ret = lingo.hset("foo", "bar", "foobar");
assertEquals(0L, ret.longValue());
ret = lingo.hset("user:1", "uname", "foo");
assertEquals(1L, ret.longValue());
ret = lingo.hset("user:1", "fname", "Bar");
assertEquals(0L, ret.longValue());
ret = lingo.hset("user:1", "uname", "bar");
assertEquals(0L, ret.longValue());
String username = lingo.hget("user:1", "uname");
String firstname = lingo.hget("user:1", "fname");
assertEquals("bar", username);
assertEquals("Bar", firstname);
System.out.println("End hset tests");
}
问题区域是这样的:
HSET key=foo field=bar value=foobar
MEMSET key=foo value=[Tuple [first=foo, second=bar]]
还有这个:
HSET key=user:1 field=fname value=Bar
MEMSET key=user:1 value=[Tuple [first=uname, second=foo]]
传播到 memset(这是 GAE memcache put 的键值对包装方法)的字段和值是错误的。
更新(添加 hget 方法):
public String hget(String key, String field) {
try {
boolean exist = exists(key);
if(exist){
Set<Tuple<String,String>> hash = (Set<Tuple<String,String>>) memget(key);
Iterator<Tuple<String, String>> it = hash.iterator();
while (it.hasNext()){
Tuple<String, String> t = it.next();
if (t.getFirst().equals(field)){
return t.getSecond();
}
}
}
} catch(Exception e) { // ClassCastException and NPE
// What to do when CCE is encountered?
}
return "nil";
}