| 1 | // Copyright (c) 2006 Dustin Sallings <dustin@spy.net> |
| 2 | |
| 3 | package net.spy.memcached.protocol.ascii; |
| 4 | |
| 5 | import java.nio.ByteBuffer; |
| 6 | import java.util.Collection; |
| 7 | import java.util.Collections; |
| 8 | |
| 9 | import net.spy.memcached.KeyUtil; |
| 10 | import net.spy.memcached.ops.MutatorOperation; |
| 11 | import net.spy.memcached.ops.Mutator; |
| 12 | import net.spy.memcached.ops.OperationCallback; |
| 13 | import net.spy.memcached.ops.OperationState; |
| 14 | import net.spy.memcached.ops.OperationStatus; |
| 15 | |
| 16 | /** |
| 17 | * Operation for mutating integers inside of memcached. |
| 18 | */ |
| 19 | final class MutatorOperationImpl extends OperationImpl |
| 20 | implements MutatorOperation { |
| 21 | |
| 22 | public static final int OVERHEAD=32; |
| 23 | |
| 24 | private static final OperationStatus NOT_FOUND= |
| 25 | new OperationStatus(false, "NOT_FOUND"); |
| 26 | |
| 27 | private final Mutator mutator; |
| 28 | private final String key; |
| 29 | private final int amount; |
| 30 | |
| 31 | public MutatorOperationImpl(Mutator m, String k, int amt, |
| 32 | OperationCallback c) { |
| 33 | super(c); |
| 34 | mutator=m; |
| 35 | key=k; |
| 36 | amount=amt; |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public void handleLine(String line) { |
| 41 | getLogger().debug("Result: %s", line); |
| 42 | OperationStatus found=null; |
| 43 | if(line.equals("NOT_FOUND")) { |
| 44 | found=NOT_FOUND; |
| 45 | } else { |
| 46 | found=new OperationStatus(true, line); |
| 47 | } |
| 48 | getCallback().receivedStatus(found); |
| 49 | transitionState(OperationState.COMPLETE); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public void initialize() { |
| 54 | int size=KeyUtil.getKeyBytes(key).length + OVERHEAD; |
| 55 | ByteBuffer b=ByteBuffer.allocate(size); |
| 56 | setArguments(b, mutator.name(), key, amount); |
| 57 | b.flip(); |
| 58 | setBuffer(b); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | protected void wasCancelled() { |
| 63 | // XXX: Replace this comment with why the hell I did this. |
| 64 | getCallback().receivedStatus(CANCELLED); |
| 65 | } |
| 66 | |
| 67 | public Collection<String> getKeys() { |
| 68 | return Collections.singleton(key); |
| 69 | } |
| 70 | |
| 71 | public int getBy() { |
| 72 | return amount; |
| 73 | } |
| 74 | |
| 75 | public long getDefault() { |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | public int getExpiration() { |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | public Mutator getType() { |
| 84 | return mutator; |
| 85 | } |
| 86 | |
| 87 | } |