我正在开发类似Redis的开源Memcache API:http://bit.ly/XVfpRX
任何人都可以帮我分析为什么此代码的String field
和String 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的键值包装方法)是错误的。
The Tuple code can be found here
更新(添加了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";
}