| 1 | package net.spy.memcached.protocol.ascii; |
| 2 | |
| 3 | import java.nio.ByteBuffer; |
| 4 | import java.util.Collection; |
| 5 | import java.util.Collections; |
| 6 | |
| 7 | import net.spy.memcached.CASResponse; |
| 8 | import net.spy.memcached.KeyUtil; |
| 9 | import net.spy.memcached.ops.CASOperation; |
| 10 | import net.spy.memcached.ops.CASOperationStatus; |
| 11 | import net.spy.memcached.ops.OperationCallback; |
| 12 | import net.spy.memcached.ops.OperationState; |
| 13 | import net.spy.memcached.ops.OperationStatus; |
| 14 | import net.spy.memcached.ops.StoreType; |
| 15 | |
| 16 | class CASOperationImpl extends OperationImpl implements CASOperation { |
| 17 | |
| 18 | // Overhead storage stuff to make sure the buffer pushes out far enough. |
| 19 | // This is "cas" + length(flags) + length(length(data)) + length(cas id) |
| 20 | // + spaces |
| 21 | private static final int OVERHEAD = 64; |
| 22 | |
| 23 | private static final OperationStatus STORED= |
| 24 | new CASOperationStatus(true, "STORED", CASResponse.OK); |
| 25 | private static final OperationStatus NOT_FOUND= |
| 26 | new CASOperationStatus(false, "NOT_FOUND", CASResponse.NOT_FOUND); |
| 27 | private static final OperationStatus EXISTS= |
| 28 | new CASOperationStatus(false, "EXISTS", CASResponse.EXISTS); |
| 29 | |
| 30 | private final String key; |
| 31 | private final long casValue; |
| 32 | private final int flags; |
| 33 | private final int exp; |
| 34 | private final byte[] data; |
| 35 | |
| 36 | public CASOperationImpl(String k, long c, int f, int e, |
| 37 | byte[] d, OperationCallback cb) { |
| 38 | super(cb); |
| 39 | key=k; |
| 40 | casValue=c; |
| 41 | flags=f; |
| 42 | exp=e; |
| 43 | data=d; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public void handleLine(String line) { |
| 48 | assert getState() == OperationState.READING |
| 49 | : "Read ``" + line + "'' when in " + getState() + " state"; |
| 50 | getCallback().receivedStatus(matchStatus(line, |
| 51 | STORED, NOT_FOUND, EXISTS)); |
| 52 | transitionState(OperationState.COMPLETE); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void initialize() { |
| 57 | ByteBuffer bb=ByteBuffer.allocate(data.length |
| 58 | + KeyUtil.getKeyBytes(key).length + OVERHEAD); |
| 59 | setArguments(bb, "cas", key, flags, exp, data.length, casValue); |
| 60 | assert bb.remaining() >= data.length + 2 |
| 61 | : "Not enough room in buffer, need another " |
| 62 | + (2 + data.length - bb.remaining()); |
| 63 | bb.put(data); |
| 64 | bb.put(CRLF); |
| 65 | bb.flip(); |
| 66 | setBuffer(bb); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | protected void wasCancelled() { |
| 71 | // XXX: Replace this comment with why I did this |
| 72 | getCallback().receivedStatus(CANCELLED); |
| 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 casValue; |
| 85 | } |
| 86 | |
| 87 | public int getExpiration() { |
| 88 | return exp; |
| 89 | } |
| 90 | |
| 91 | public int getFlags() { |
| 92 | return flags; |
| 93 | } |
| 94 | |
| 95 | public StoreType getStoreType() { |
| 96 | return StoreType.set; |
| 97 | } |
| 98 | |
| 99 | } |