Skip to content

Commit 53d14b3

Browse files
daschlMichael Nitschinger
authored andcommitted
SPY-183: Allow touch operations to be cloned.
Motivation ---------- In case a touch operation needs to be rescheduled, it needs to be cloneable (like any other keyed operation). Modifications ------------- Apply the same clone logic as with any other keyed operation. Also added getter methods to the operations so that the expiration time can be extracted on cloning. Result ------ Correct behavior when a touch op needs to be cloned. Change-Id: Iad343b4dbdcd5dfd4d9ec53bf5335dcf4775f9c9 Reviewed-on: http://review.couchbase.org/48076 Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com> Reviewed-by: Simon Baslé <simon@couchbase.com>
1 parent 4bb435b commit 53d14b3

File tree

8 files changed

+59
-27
lines changed

8 files changed

+59
-27
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@
2323

2424
package net.spy.memcached;
2525

26-
import java.util.Collection;
27-
import java.util.Map;
28-
29-
import javax.security.auth.callback.CallbackHandler;
30-
3126
import net.spy.memcached.ops.CASOperation;
3227
import net.spy.memcached.ops.ConcatenationOperation;
3328
import net.spy.memcached.ops.ConcatenationType;
@@ -53,11 +48,16 @@
5348
import net.spy.memcached.ops.StoreOperation;
5449
import net.spy.memcached.ops.StoreType;
5550
import net.spy.memcached.ops.TapOperation;
51+
import net.spy.memcached.ops.TouchOperation;
5652
import net.spy.memcached.ops.UnlockOperation;
5753
import net.spy.memcached.ops.VersionOperation;
5854
import net.spy.memcached.tapmessage.RequestMessage;
5955
import net.spy.memcached.tapmessage.TapOpcode;
6056

57+
import javax.security.auth.callback.CallbackHandler;
58+
import java.util.Collection;
59+
import java.util.Map;
60+
6161
/**
6262
* Factory that builds operations for protocol handlers.
6363
*/
@@ -247,7 +247,7 @@ StoreOperation store(StoreType storeType, String key, int flags, int exp,
247247
* @param cb The status callback
248248
* @return A touch operation
249249
*/
250-
KeyedOperation touch(String key, int expiration, OperationCallback cb);
250+
TouchOperation touch(String key, int expiration, OperationCallback cb);
251251

252252
/**
253253
* Get a concatenation operation.

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ public Collection<Operation> clone(KeyedOperation op) {
9292
} else if(op instanceof GetAndTouchOperation) {
9393
GetAndTouchOperation gt = (GetAndTouchOperation) op;
9494
rv.add(getAndTouch(first(gt.getKeys()), gt.getExpiration(),
95-
(GetAndTouchOperation.Callback) gt.getCallback()));
95+
(GetAndTouchOperation.Callback) gt.getCallback()));
96+
} else if (op instanceof TouchOperation) {
97+
TouchOperation tt = (TouchOperation) op;
98+
rv.add(touch(first(tt.getKeys()), tt.getExpiration(), tt.getCallback()));
9699
} else if (op instanceof GetlOperation) {
97100
GetlOperation gl = (GetlOperation) op;
98101
rv.add(getl(first(gl.getKeys()), gl.getExpiration(),

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
* Touch operation marker.
2828
**/
2929
public interface TouchOperation extends KeyedOperation {
30-
// TODO: hook this in with binary protocol
30+
31+
/**
32+
* Get the expiration to set in case of a new entry.
33+
*/
34+
int getExpiration();
3135
}
3236

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@
2323

2424
package net.spy.memcached.protocol.ascii;
2525

26-
import java.util.ArrayList;
27-
import java.util.Collection;
28-
import java.util.Map;
29-
30-
import javax.security.auth.callback.CallbackHandler;
31-
3226
import net.spy.memcached.ops.BaseOperationFactory;
3327
import net.spy.memcached.ops.CASOperation;
3428
import net.spy.memcached.ops.ConcatenationOperation;
@@ -57,11 +51,17 @@
5751
import net.spy.memcached.ops.StoreOperation;
5852
import net.spy.memcached.ops.StoreType;
5953
import net.spy.memcached.ops.TapOperation;
54+
import net.spy.memcached.ops.TouchOperation;
6055
import net.spy.memcached.ops.UnlockOperation;
6156
import net.spy.memcached.ops.VersionOperation;
6257
import net.spy.memcached.tapmessage.RequestMessage;
6358
import net.spy.memcached.tapmessage.TapOpcode;
6459

60+
import javax.security.auth.callback.CallbackHandler;
61+
import java.util.ArrayList;
62+
import java.util.Collection;
63+
import java.util.Map;
64+
6565
/**
6666
* Operation factory for the ascii protocol.
6767
*/
@@ -133,7 +133,7 @@ public StoreOperation store(StoreType storeType, String key, int flags,
133133
return new StoreOperationImpl(storeType, key, flags, exp, data, cb);
134134
}
135135

136-
public KeyedOperation touch(String key, int expiration,
136+
public TouchOperation touch(String key, int expiration,
137137
OperationCallback cb) {
138138
return new TouchOperationImpl(key, expiration, cb);
139139
}

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@
2323

2424
package net.spy.memcached.protocol.ascii;
2525

26-
import java.nio.ByteBuffer;
27-
import java.util.Collection;
28-
import java.util.Collections;
29-
3026
import net.spy.memcached.KeyUtil;
31-
import net.spy.memcached.ops.StatusCode;
32-
import net.spy.memcached.ops.TouchOperation;
27+
import net.spy.memcached.ops.OperationCallback;
3328
import net.spy.memcached.ops.OperationState;
3429
import net.spy.memcached.ops.OperationStatus;
35-
import net.spy.memcached.ops.OperationCallback;
30+
import net.spy.memcached.ops.StatusCode;
31+
import net.spy.memcached.ops.TouchOperation;
32+
33+
import java.nio.ByteBuffer;
34+
import java.util.Collection;
35+
import java.util.Collections;
3636

3737

3838
/**
@@ -46,9 +46,9 @@ final class TouchOperationImpl extends OperationImpl implements TouchOperation {
4646
new OperationStatus(true, "TOUCHED", StatusCode.SUCCESS);
4747

4848
private final String key;
49-
private final long exp;
49+
private final int exp;
5050

51-
public TouchOperationImpl(String k, long t, OperationCallback cb) {
51+
public TouchOperationImpl(String k, int t, OperationCallback cb) {
5252
super(cb);
5353
key = k;
5454
exp = t;
@@ -77,6 +77,11 @@ public void initialize() {
7777
setBuffer(b);
7878
}
7979

80+
@Override
81+
public int getExpiration() {
82+
return exp;
83+
}
84+
8085
@Override
8186
public String toString() {
8287
return "Cmd: touch key: " + key + " exp: " + exp;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import net.spy.memcached.ops.StoreOperation;
5454
import net.spy.memcached.ops.StoreType;
5555
import net.spy.memcached.ops.TapOperation;
56+
import net.spy.memcached.ops.TouchOperation;
5657
import net.spy.memcached.ops.UnlockOperation;
5758
import net.spy.memcached.ops.VersionOperation;
5859
import net.spy.memcached.tapmessage.RequestMessage;
@@ -140,7 +141,7 @@ public StoreOperation store(StoreType storeType, String key, int flags,
140141
return new StoreOperationImpl(storeType, key, flags, exp, data, 0, cb);
141142
}
142143

143-
public KeyedOperation touch(String key, int expiration,
144+
public TouchOperation touch(String key, int expiration,
144145
OperationCallback cb) {
145146
return new TouchOperationImpl(key, expiration, cb);
146147
}

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

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

2626
import net.spy.memcached.ops.OperationCallback;
27-
import static net.spy.memcached.protocol.binary.OperationImpl.STATUS_OK;
27+
import net.spy.memcached.ops.TouchOperation;
2828

2929
/**
3030
* Operation to reset a timeout in Membase server.
3131
*/
32-
public class TouchOperationImpl extends SingleKeyOperationImpl {
32+
public class TouchOperationImpl extends SingleKeyOperationImpl
33+
implements TouchOperation {
3334

3435
static final byte CMD = 0x1c;
3536

@@ -50,6 +51,11 @@ protected void decodePayload(byte[] pl) {
5051
getCallback().receivedStatus(STATUS_OK);
5152
}
5253

54+
@Override
55+
public int getExpiration() {
56+
return exp;
57+
}
58+
5359
@Override
5460
public String toString() {
5561
return super.toString() + " Exp: " + exp;

src/test/java/net/spy/memcached/protocol/binary/OperationFactoryTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import net.spy.memcached.OperationFactory;
2626
import net.spy.memcached.OperationFactoryTestBase;
2727
import net.spy.memcached.ops.GetAndTouchOperation;
28+
import net.spy.memcached.ops.OperationCallback;
29+
import net.spy.memcached.ops.TouchOperation;
2830

2931
/**
3032
* An OperationFactoryTest.
@@ -46,4 +48,15 @@ public void testGetAndTouchOperationCloning() {
4648
assertSame(callback, op2.getCallback());
4749
}
4850

51+
public void testTouchOperationCloning() {
52+
OperationCallback callback =
53+
(OperationCallback) mock(OperationCallback.class).proxy();
54+
55+
TouchOperation op = ofact.touch(TEST_KEY, 0, callback);
56+
57+
TouchOperation op2 = cloneOne(TouchOperation.class, op);
58+
assertKey(op2);
59+
assertSame(callback, op2.getCallback());
60+
}
61+
4962
}

0 commit comments

Comments
 (0)