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.CASOperation; |
7 | import net.spy.memcached.ops.OperationCallback; |
8 | import net.spy.memcached.ops.OperationStatus; |
9 | import net.spy.memcached.ops.StoreOperation; |
10 | import net.spy.memcached.ops.StoreType; |
11 | |
12 | class StoreOperationImpl extends OperationImpl |
13 | implements StoreOperation, CASOperation { |
14 | |
15 | private static final int SET=1; |
16 | private static final int ADD=2; |
17 | private static final int REPLACE=3; |
18 | |
19 | static final int SETQ=0x11; |
20 | static final int ADDQ=0x12; |
21 | static final int REPLACEQ=0x13; |
22 | |
23 | // 4-byte flags, 4-byte expiration |
24 | static final int EXTRA_LEN = 8; |
25 | |
26 | private final String key; |
27 | private final StoreType storeType; |
28 | private final int flags; |
29 | private final int exp; |
30 | private final long cas; |
31 | private final byte[] data; |
32 | |
33 | private static int cmdMap(StoreType t) { |
34 | int rv=-1; |
35 | switch(t) { |
36 | case set: rv=SET; break; |
37 | case add: rv=ADD; break; |
38 | case replace: rv=REPLACE; break; |
39 | } |
40 | // Check fall-through. |
41 | assert rv != -1 : "Unhandled store type: " + t; |
42 | return rv; |
43 | } |
44 | |
45 | public StoreOperationImpl(StoreType t, String k, int f, int e, |
46 | byte[] d, long c, OperationCallback cb) { |
47 | super(cmdMap(t), generateOpaque(), cb); |
48 | key=k; |
49 | flags=f; |
50 | exp=e; |
51 | data=d; |
52 | cas=c; |
53 | storeType=t; |
54 | } |
55 | |
56 | @Override |
57 | public void initialize() { |
58 | prepareBuffer(key, cas, data, flags, exp); |
59 | } |
60 | |
61 | @Override |
62 | protected OperationStatus getStatusForErrorCode(int errCode, byte[] errPl) { |
63 | OperationStatus rv=null; |
64 | switch(errCode) { |
65 | case ERR_EXISTS: |
66 | rv=EXISTS_STATUS; |
67 | break; |
68 | case ERR_NOT_FOUND: |
69 | rv=NOT_FOUND_STATUS; |
70 | break; |
71 | } |
72 | return rv; |
73 | } |
74 | |
75 | public Collection<String> getKeys() { |
76 | return Collections.singleton(key); |
77 | } |
78 | |
79 | public byte[] getBytes() { |
80 | return data; |
81 | } |
82 | |
83 | public long getCasValue() { |
84 | return cas; |
85 | } |
86 | |
87 | public int getExpiration() { |
88 | return exp; |
89 | } |
90 | |
91 | public int getFlags() { |
92 | return flags; |
93 | } |
94 | |
95 | public byte[] getData() { |
96 | return data; |
97 | } |
98 | |
99 | public StoreType getStoreType() { |
100 | return storeType; |
101 | } |
102 | |
103 | } |