Skip to content

Commit 495b4ee

Browse files
Mike Wiederholdingenthr
authored andcommitted
JCBC-110: Delete operation in MemcachedClient now returns it's cas
Change-Id: Id70241db2e4111f335b76ed9e6c6179d266aee94 Reviewed-on: http://review.couchbase.org/20852 Reviewed-by: Matt Ingenthron <[email protected]> Tested-by: Matt Ingenthron <[email protected]>
1 parent 7d35d12 commit 495b4ee

File tree

9 files changed

+50
-11
lines changed

9 files changed

+50
-11
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1861,11 +1861,15 @@ public OperationFuture<Boolean> delete(String key) {
18611861
final CountDownLatch latch = new CountDownLatch(1);
18621862
final OperationFuture<Boolean> rv = new OperationFuture<Boolean>(key,
18631863
latch, operationTimeout);
1864-
DeleteOperation op = opFact.delete(key, new OperationCallback() {
1864+
DeleteOperation op = opFact.delete(key, new DeleteOperation.Callback() {
18651865
public void receivedStatus(OperationStatus s) {
18661866
rv.set(s.isSuccess(), s);
18671867
}
18681868

1869+
public void gotData(long cas) {
1870+
rv.setCas(cas);
1871+
}
1872+
18691873
public void complete() {
18701874
latch.countDown();
18711875
}

src/main/java/net/spy/memcached/OperationFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public interface OperationFactory {
7676
* @param operationCallback the status callback
7777
* @return the new DeleteOperation
7878
*/
79-
DeleteOperation delete(String key, OperationCallback operationCallback);
79+
DeleteOperation delete(String key, DeleteOperation.Callback callback);
8080

8181
/**
8282
* Create a Unlock operation.

src/main/java/net/spy/memcached/ops/BaseOperationFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public Collection<Operation> clone(KeyedOperation op) {
6565
cop.getFlags(), cop.getExpiration(), cop.getData(),
6666
(StoreOperation.Callback) cop.getCallback()));
6767
} else if(op instanceof DeleteOperation) {
68-
rv.add(delete(first(op.getKeys()), op.getCallback()));
68+
rv.add(delete(first(op.getKeys()),
69+
(DeleteOperation.Callback)op.getCallback()));
6970
} else if (op instanceof MutatorOperation) {
7071
MutatorOperation mo = (MutatorOperation) op;
7172
rv.add(mutate(mo.getType(), first(op.getKeys()), mo.getBy(),

src/main/java/net/spy/memcached/ops/DeleteOperation.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,16 @@
2626
* Deletion operation.
2727
*/
2828
public interface DeleteOperation extends KeyedOperation {
29-
// nothing in particular.
29+
/**
30+
* Delete operation callback.
31+
*/
32+
interface Callback extends OperationCallback {
33+
/**
34+
* Callback for each result from a Store.
35+
*
36+
* @param key the key that was retrieved
37+
* @param cas the CAS value for this record
38+
*/
39+
void gotData(long cas);
40+
}
3041
}

src/main/java/net/spy/memcached/ops/ObserveOperation.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
package net.spy.memcached.ops;
2424

25+
import net.spy.memcached.MemcachedNode;
2526
import net.spy.memcached.ObserveResponse;
2627

2728
/**

src/main/java/net/spy/memcached/protocol/ascii/AsciiOperationFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
*/
6666
public class AsciiOperationFactory extends BaseOperationFactory {
6767

68-
public DeleteOperation delete(String key, OperationCallback cb) {
68+
public DeleteOperation delete(String key, DeleteOperation.Callback cb) {
6969
return new DeleteOperationImpl(key, cb);
7070
}
7171

src/main/java/net/spy/memcached/protocol/binary/BinaryOperationFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
public class BinaryOperationFactory extends BaseOperationFactory {
6868

6969
public DeleteOperation
70-
delete(String key, OperationCallback operationCallback) {
70+
delete(String key, DeleteOperation.Callback operationCallback) {
7171
return new DeleteOperationImpl(key, operationCallback);
7272
}
7373

src/main/java/net/spy/memcached/protocol/binary/DeleteOperationImpl.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
package net.spy.memcached.protocol.binary;
2525

2626
import net.spy.memcached.ops.DeleteOperation;
27-
import net.spy.memcached.ops.OperationCallback;
2827

2928
class DeleteOperationImpl extends SingleKeyOperationImpl implements
3029
DeleteOperation {
@@ -33,11 +32,11 @@ class DeleteOperationImpl extends SingleKeyOperationImpl implements
3332

3433
private final long cas;
3534

36-
public DeleteOperationImpl(String k, OperationCallback cb) {
35+
public DeleteOperationImpl(String k, DeleteOperation.Callback cb) {
3736
this(k, 0, cb);
3837
}
3938

40-
public DeleteOperationImpl(String k, long c, OperationCallback cb) {
39+
public DeleteOperationImpl(String k, long c, DeleteOperation.Callback cb) {
4140
super(CMD, generateOpaque(), k, cb);
4241
cas = c;
4342
}
@@ -47,6 +46,12 @@ public void initialize() {
4746
prepareBuffer(key, cas, EMPTY_BYTES);
4847
}
4948

49+
@Override
50+
protected void decodePayload(byte[] pl) {
51+
super.decodePayload(pl);
52+
((DeleteOperation.Callback) getCallback()).gotData(responseCas);
53+
}
54+
5055
@Override
5156
public String toString() {
5257
return super.toString() + " Cas: " + cas;

src/test/java/net/spy/memcached/OperationFactoryTestBase.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public abstract class OperationFactoryTestBase extends MockObjectTestCase {
5555
protected OperationFactory ofact = null;
5656
protected OperationCallback genericCallback;
5757
protected StoreOperation.Callback storeCallback;
58+
protected DeleteOperation.Callback deleteCallback;
5859
private byte[] testData;
5960

6061
@Override
@@ -82,6 +83,18 @@ public void receivedStatus(OperationStatus status) {
8283
fail("Unexpected status: " + status);
8384
}
8485
};
86+
deleteCallback = new DeleteOperation.Callback() {
87+
public void complete() {
88+
fail("Unexpected invocation");
89+
}
90+
91+
public void gotData(long cas) {
92+
}
93+
94+
public void receivedStatus(OperationStatus status) {
95+
fail("Unexpected status: " + status);
96+
}
97+
};
8598
testData = new byte[64];
8699
new Random().nextBytes(testData);
87100
}
@@ -92,11 +105,11 @@ public void receivedStatus(OperationStatus status) {
92105
protected abstract OperationFactory getOperationFactory();
93106

94107
public void testDeleteOperationCloning() {
95-
DeleteOperation op = ofact.delete(TEST_KEY, genericCallback);
108+
DeleteOperation op = ofact.delete(TEST_KEY, deleteCallback);
96109

97110
DeleteOperation op2 = cloneOne(DeleteOperation.class, op);
98111
assertEquals(TEST_KEY, op2.getKeys().iterator().next());
99-
assertCallback(op2);
112+
assertDeleteCallback(op2);
100113
}
101114

102115
public void testCASOperationCloning() {
@@ -274,6 +287,10 @@ protected void assertStoreCallback(Operation op) {
274287
assertSame(storeCallback, op.getCallback());
275288
}
276289

290+
protected void assertDeleteCallback(Operation op) {
291+
assertSame(deleteCallback, op.getCallback());
292+
}
293+
277294
private void assertBytes(byte[] bytes) {
278295
assertTrue(Arrays.equals(testData, bytes));
279296
}

0 commit comments

Comments
 (0)