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.GetOperation; |
7 | import net.spy.memcached.ops.GetsOperation; |
8 | import net.spy.memcached.ops.OperationStatus; |
9 | |
10 | class GetOperationImpl extends OperationImpl |
11 | implements GetOperation, GetsOperation { |
12 | |
13 | static final int CMD=0; |
14 | |
15 | /** |
16 | * Length of the extra header stuff for a GET response. |
17 | */ |
18 | static final int EXTRA_HDR_LEN=4; |
19 | |
20 | private final String key; |
21 | |
22 | public GetOperationImpl(String k, GetOperation.Callback cb) { |
23 | super(CMD, generateOpaque(), cb); |
24 | key=k; |
25 | } |
26 | |
27 | public GetOperationImpl(String k, GetsOperation.Callback cb) { |
28 | super(CMD, generateOpaque(), cb); |
29 | key=k; |
30 | } |
31 | |
32 | @Override |
33 | public void initialize() { |
34 | prepareBuffer(key, 0, EMPTY_BYTES); |
35 | } |
36 | |
37 | @Override |
38 | protected void decodePayload(byte[] pl) { |
39 | final int flags=decodeInt(pl, 0); |
40 | final byte[] data=new byte[pl.length - EXTRA_HDR_LEN]; |
41 | System.arraycopy(pl, EXTRA_HDR_LEN, data, 0, pl.length-EXTRA_HDR_LEN); |
42 | // Assume we're processing a get unless the cast fails. |
43 | try { |
44 | GetOperation.Callback cb=(GetOperation.Callback)getCallback(); |
45 | cb.gotData(key, flags, data); |
46 | } catch(ClassCastException e) { |
47 | GetsOperation.Callback cb=(GetsOperation.Callback)getCallback(); |
48 | cb.gotData(key, flags, responseCas, data); |
49 | } |
50 | getCallback().receivedStatus(STATUS_OK); |
51 | } |
52 | |
53 | @Override |
54 | protected OperationStatus getStatusForErrorCode(int errCode, byte[] errPl) { |
55 | return errCode == ERR_NOT_FOUND ? NOT_FOUND_STATUS : null; |
56 | } |
57 | |
58 | public Collection<String> getKeys() { |
59 | return Collections.singleton(key); |
60 | } |
61 | |
62 | } |