| 1 | // Copyright (c) 2006 Dustin Sallings <dustin@spy.net> |
| 2 | |
| 3 | package net.spy.memcached.protocol.ascii; |
| 4 | |
| 5 | import java.nio.ByteBuffer; |
| 6 | |
| 7 | import net.spy.memcached.ops.FlushOperation; |
| 8 | import net.spy.memcached.ops.OperationCallback; |
| 9 | import net.spy.memcached.ops.OperationState; |
| 10 | import net.spy.memcached.ops.OperationStatus; |
| 11 | |
| 12 | /** |
| 13 | * Memcached flush_all operation. |
| 14 | */ |
| 15 | final class FlushOperationImpl extends OperationImpl |
| 16 | implements FlushOperation { |
| 17 | |
| 18 | private static final byte[] FLUSH="flush_all\r\n".getBytes(); |
| 19 | |
| 20 | private static final OperationStatus OK= |
| 21 | new OperationStatus(true, "OK"); |
| 22 | |
| 23 | private final int delay; |
| 24 | |
| 25 | public FlushOperationImpl(int d, OperationCallback cb) { |
| 26 | super(cb); |
| 27 | delay=d; |
| 28 | } |
| 29 | |
| 30 | @Override |
| 31 | public void handleLine(String line) { |
| 32 | getLogger().debug("Flush completed successfully"); |
| 33 | getCallback().receivedStatus(matchStatus(line, OK)); |
| 34 | transitionState(OperationState.COMPLETE); |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public void initialize() { |
| 39 | ByteBuffer b=null; |
| 40 | if(delay == -1) { |
| 41 | b=ByteBuffer.wrap(FLUSH); |
| 42 | } else { |
| 43 | b=ByteBuffer.allocate(32); |
| 44 | b.put( ("flush_all " + delay + "\r\n").getBytes()); |
| 45 | b.flip(); |
| 46 | } |
| 47 | setBuffer(b); |
| 48 | } |
| 49 | } |