1 | package net.spy.memcached.protocol.binary; |
2 | |
3 | import java.util.Collection; |
4 | import java.util.Collections; |
5 | |
6 | import net.spy.memcached.ops.MutatorOperation; |
7 | import net.spy.memcached.ops.Mutator; |
8 | import net.spy.memcached.ops.OperationCallback; |
9 | import net.spy.memcached.ops.OperationStatus; |
10 | |
11 | class MutatorOperationImpl extends OperationImpl implements |
12 | MutatorOperation { |
13 | |
14 | private static final int CMD_INCR=5; |
15 | private static final int CMD_DECR=6; |
16 | |
17 | private final Mutator mutator; |
18 | private final String key; |
19 | private final long by; |
20 | private final int exp; |
21 | private final long def; |
22 | |
23 | public MutatorOperationImpl(Mutator m, String k, long b, |
24 | long d, int e, OperationCallback cb) { |
25 | super(m == Mutator.incr ? CMD_INCR : CMD_DECR, generateOpaque(), cb); |
26 | assert d >= 0 : "Default value is below zero"; |
27 | mutator=m; |
28 | key=k; |
29 | by=b; |
30 | exp=e; |
31 | def=d; |
32 | } |
33 | |
34 | @Override |
35 | public void initialize() { |
36 | // We're passing around a long so we can cover an unsigned integer. |
37 | byte[] defBytes=new byte[8]; |
38 | defBytes[0]=(byte)((def >> 56) & 0xff); |
39 | defBytes[1]=(byte)((def >> 48) & 0xff); |
40 | defBytes[2]=(byte)((def >> 40) & 0xff); |
41 | defBytes[3]=(byte)((def >> 32) & 0xff); |
42 | defBytes[4]=(byte)((def >> 24) & 0xff); |
43 | defBytes[5]=(byte)((def >> 16) & 0xff); |
44 | defBytes[6]=(byte)((def >> 8) & 0xff); |
45 | defBytes[7]=(byte)(def & 0xff); |
46 | prepareBuffer(key, 0, EMPTY_BYTES, by, defBytes, exp); |
47 | } |
48 | |
49 | @Override |
50 | protected OperationStatus getStatusForErrorCode(int errCode, byte[] errPl) { |
51 | return errCode == ERR_NOT_FOUND ? NOT_FOUND_STATUS : null; |
52 | } |
53 | |
54 | @Override |
55 | protected void decodePayload(byte[] pl) { |
56 | getCallback().receivedStatus(new OperationStatus(true, |
57 | String.valueOf(decodeLong(pl, 0)))); |
58 | } |
59 | |
60 | public Collection<String> getKeys() { |
61 | return Collections.singleton(key); |
62 | } |
63 | |
64 | public int getBy() { |
65 | return (int) by; |
66 | } |
67 | |
68 | public long getDefault() { |
69 | return def; |
70 | } |
71 | |
72 | public int getExpiration() { |
73 | return exp; |
74 | } |
75 | |
76 | public Mutator getType() { |
77 | return mutator; |
78 | } |
79 | |
80 | } |