Skip to content

Commit bd17fe6

Browse files
nbrachetdustin
authored andcommitted
Asynchronous incr/decr with no default.
Theoretically, this can work with default values as well, but only in the binary protocol. Getting this to work with the text protocol would likely be quite painful. http://code.google.com/p/spymemcached/issues/detail?id=12
1 parent 2347126 commit bd17fe6

3 files changed

Lines changed: 68 additions & 0 deletions

File tree

‎src/main/java/net/spy/memcached/MemcachedClient.java‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,44 @@ private long mutateWithDefault(Mutator t, String key,
11001100
return rv;
11011101
}
11021102

1103+
private Future<Long> asyncMutate(Mutator m, String key, int by, long def,
1104+
int exp) {
1105+
final CountDownLatch latch = new CountDownLatch(1);
1106+
final OperationFuture<Long> rv = new OperationFuture<Long>(
1107+
latch, operationTimeout);
1108+
Operation op = addOp(key, opFact.mutate(m, key, by, def, exp,
1109+
new OperationCallback() {
1110+
public void receivedStatus(OperationStatus s) {
1111+
rv.set(new Long(s.isSuccess() ? s.getMessage() : "-1"));
1112+
}
1113+
public void complete() {
1114+
latch.countDown();
1115+
}
1116+
}));
1117+
rv.setOperation(op);
1118+
return rv;
1119+
}
1120+
1121+
/**
1122+
* Asychronous increment.
1123+
*
1124+
* @return a future with the incremented value, or -1 if the
1125+
* increment failed.
1126+
*/
1127+
public Future<Long> asyncIncr(String key, int by) {
1128+
return asyncMutate(Mutator.incr, key, by, 0, -1);
1129+
}
1130+
1131+
/**
1132+
* Asynchronous decrement.
1133+
*
1134+
* @return a future with the decremented value, or -1 if the
1135+
* increment failed.
1136+
*/
1137+
public Future<Long> asyncDecr(String key, int by) {
1138+
return asyncMutate(Mutator.decr, key, by, 0, -1);
1139+
}
1140+
11031141
/**
11041142
* Increment the given counter, returning the new value.
11051143
*

‎src/main/java/net/spy/memcached/MemcachedClientIF.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ Map<String, Object> getBulk(String... keys)
108108

109109
long decr(String key, int by) throws OperationTimeoutException;
110110

111+
Future<Long> asyncIncr(String key, int by);
112+
113+
Future<Long> asyncDecr(String key, int by);
114+
111115
long incr(String key, int by, long def)
112116
throws OperationTimeoutException;
113117

‎src/test/java/net/spy/memcached/ProtocolBaseCase.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,32 @@ public void testMutateWithDefault() throws Exception {
399399
assertEquals(9, client.decr("mtest2", 1, 9));
400400
}
401401

402+
public void testAsyncIncrement() throws Exception {
403+
String k="async-incr";
404+
client.set(k, 0, "5");
405+
Future<Long> f = client.asyncIncr(k, 1);
406+
assertEquals(6, (long)f.get());
407+
}
408+
409+
public void testAsyncIncrementNonExistent() throws Exception {
410+
String k="async-incr-non-existent";
411+
Future<Long> f = client.asyncIncr(k, 1);
412+
assertEquals(-1, (long)f.get());
413+
}
414+
415+
public void testAsyncDecrement() throws Exception {
416+
String k="async-decr";
417+
client.set(k, 0, "5");
418+
Future<Long> f = client.asyncDecr(k, 1);
419+
assertEquals(4, (long)f.get());
420+
}
421+
422+
public void testAsyncDecrementNonExistent() throws Exception {
423+
String k="async-decr-non-existent";
424+
Future<Long> f = client.asyncDecr(k, 1);
425+
assertEquals(-1, (long)f.get());
426+
}
427+
402428
public void testConcurrentMutation() throws Throwable {
403429
int num=SyncThread.getDistinctResultCount(10, new Callable<Long>(){
404430
public Long call() throws Exception {

0 commit comments

Comments
 (0)