From fe81635b4f67004ae78e36e6dab1b8dda892f11b Mon Sep 17 00:00:00 2001 From: Dustin Sallings Date: Wed, 26 Jul 2006 16:29:58 -0700 Subject: [PATCH] Consolidate the callback interfaces so there's basically just one. --- .../net/spy/memcached/MemcachedClient.java | 24 ++++++------ .../spy/memcached/ops/DeleteOperation.java | 3 ++ .../net/spy/memcached/ops/GetOperation.java | 15 ++++++-- .../spy/memcached/ops/MutatorOperation.java | 28 +++++++++----- src/java/net/spy/memcached/ops/Operation.java | 32 +++++++++++++++- .../spy/memcached/ops/OperationCallback.java | 17 +++++++++ .../net/spy/memcached/ops/StatsOperation.java | 17 +++++++-- .../net/spy/memcached/ops/StoreOperation.java | 38 +++++++++++++------ .../spy/memcached/ops/VersionOperation.java | 10 ++--- 9 files changed, 134 insertions(+), 50 deletions(-) create mode 100644 src/java/net/spy/memcached/ops/OperationCallback.java diff --git a/src/java/net/spy/memcached/MemcachedClient.java b/src/java/net/spy/memcached/MemcachedClient.java index 1a4fcfca4..086c47f2a 100644 --- a/src/java/net/spy/memcached/MemcachedClient.java +++ b/src/java/net/spy/memcached/MemcachedClient.java @@ -25,6 +25,7 @@ import net.spy.memcached.ops.GetOperation; import net.spy.memcached.ops.MutatorOperation; import net.spy.memcached.ops.Operation; +import net.spy.memcached.ops.OperationCallback; import net.spy.memcached.ops.StatsOperation; import net.spy.memcached.ops.StoreOperation; import net.spy.memcached.ops.VersionOperation; @@ -120,8 +121,8 @@ private Future asyncStore(StoreOperation.StoreType storeType, final SynchronizationObject sync =new SynchronizationObject(null); Operation op=new StoreOperation(storeType, key, co.getFlags(), exp, - co.getData(), new StoreOperation.Callback() { - public void storeResult(String val) { + co.getData(), new OperationCallback() { + public void receivedStatus(String val) { sync.set(val); }}); OperationFuture rv=new OperationFuture(sync, op); @@ -183,7 +184,7 @@ public Future asyncGet(final String key) { Operation op=new GetOperation(key, new GetOperation.Callback() { private Object val=null; - public void getComplete() { + public void receivedStatus(String line) { sync.set(val); } public void gotData(String k, int flags, byte[] data) { @@ -235,7 +236,7 @@ public Future> asyncGetBulk(Collection keys) { final SynchronizationObject sync =new SynchronizationObject(requests); GetOperation.Callback cb=new GetOperation.Callback() { - public void getComplete() { + public void receivedStatus(String line) { requests.decrementAndGet(); sync.set(requests); } @@ -302,8 +303,8 @@ public Map getVersions() { final SocketAddress sa=conn.getAddressOf(i); ai.incrementAndGet(); addOp(i, new VersionOperation( - new VersionOperation.Callback() { - public void versionResult(String s) { + new OperationCallback() { + public void receivedStatus(String s) { rv.put(sa, s); ai.decrementAndGet(); sync.set(ai); @@ -331,7 +332,7 @@ public Map> getStats() { public void gotStat(String name, String val) { rv.get(sa).put(name, val); } - public void statsComplete() { + public void receivedStatus(String line) { todo.decrementAndGet(); sync.set(todo); }})); @@ -344,12 +345,9 @@ private long mutate(MutatorOperation.Mutator m, String key, int by) { final SynchronizationObject sync= new SynchronizationObject(null); addOp(getServerForKey(key), new MutatorOperation(m, key, by, - new MutatorOperation.Callback() { - public void mutatorResult(Long val) { - if(val == null) { - val=new Long(-1); - } - sync.set(val); + new OperationCallback() { + public void receivedStatus(String val) { + sync.set(new Long(val==null?"-1":val)); }})); try { sync.waitUntilNotNull(Long.MAX_VALUE, TimeUnit.MILLISECONDS); diff --git a/src/java/net/spy/memcached/ops/DeleteOperation.java b/src/java/net/spy/memcached/ops/DeleteOperation.java index fd9e0d1fc..65d7072bd 100644 --- a/src/java/net/spy/memcached/ops/DeleteOperation.java +++ b/src/java/net/spy/memcached/ops/DeleteOperation.java @@ -5,6 +5,9 @@ import java.nio.ByteBuffer; +/** + * Operation to delete an item from the cache. + */ public class DeleteOperation extends Operation { private static final int OVERHEAD=32; diff --git a/src/java/net/spy/memcached/ops/GetOperation.java b/src/java/net/spy/memcached/ops/GetOperation.java index e7c44ea4f..9bf8e549d 100644 --- a/src/java/net/spy/memcached/ops/GetOperation.java +++ b/src/java/net/spy/memcached/ops/GetOperation.java @@ -35,7 +35,7 @@ public void handleLine(String line) { if(line.equals("END")) { getLogger().debug("Get complete!"); if(cb != null) { - cb.getComplete(); + cb.receivedStatus(line); } transitionState(State.COMPLETE); data=null; @@ -107,8 +107,17 @@ public void initialize() { keys=null; } - public interface Callback { + /** + * Operation callback for the get request. + */ + public interface Callback extends OperationCallback { + /** + * Callback for each result from a get. + * + * @param key the key that was retrieved + * @param flags the flags for this value + * @param data the data stored under this key + */ void gotData(String key, int flags, byte[] data); - void getComplete(); } } diff --git a/src/java/net/spy/memcached/ops/MutatorOperation.java b/src/java/net/spy/memcached/ops/MutatorOperation.java index 09c56cbe2..021bf03d2 100644 --- a/src/java/net/spy/memcached/ops/MutatorOperation.java +++ b/src/java/net/spy/memcached/ops/MutatorOperation.java @@ -12,14 +12,26 @@ public class MutatorOperation extends Operation { public static final int OVERHEAD=32; - public enum Mutator { incr, decr } + /** + * Type of mutation to perform. + */ + public enum Mutator { + /** + * Increment a value on the memcached server. + */ + incr, + /** + * Decrement a value on the memcached server. + */ + decr + } private Mutator mutator=null; private String key=null; private int amount=0; - private Callback cb=null; + private OperationCallback cb=null; - public MutatorOperation(Mutator m, String k, int amt, Callback c) { + public MutatorOperation(Mutator m, String k, int amt, OperationCallback c) { super(); mutator=m; key=k; @@ -30,12 +42,12 @@ public MutatorOperation(Mutator m, String k, int amt, Callback c) { @Override public void handleLine(String line) { getLogger().debug("Result: %s", line); - Long found=null; + String found=null; if(!line.equals("NOT_FOUND")) { - found=new Long(line); + found=line; } if(cb != null) { - cb.mutatorResult(found); + cb.receivedStatus(found); } transitionState(State.COMPLETE); } @@ -49,8 +61,4 @@ public void initialize() { setBuffer(b); } - public interface Callback { - void mutatorResult(Long val); - } - } diff --git a/src/java/net/spy/memcached/ops/Operation.java b/src/java/net/spy/memcached/ops/Operation.java index bff0bb479..d75013e5d 100644 --- a/src/java/net/spy/memcached/ops/Operation.java +++ b/src/java/net/spy/memcached/ops/Operation.java @@ -11,8 +11,36 @@ * Operations on a memcached connection. */ public abstract class Operation extends SpyObject { - public enum State { WRITING, READING, COMPLETE } - public enum ReadType { LINE, DATA } + /** + * State of this operation. + */ + public enum State { + /** + * State indicating this operation is writing data to the server. + */ + WRITING, + /** + * State indicating this operation is reading data from the server. + */ + READING, + /** + * State indicating this operation is complete. + */ + COMPLETE + } + /** + * Data read types. + */ + public enum ReadType { + /** + * Read type indicating an operation currently wants to read lines. + */ + LINE, + /** + * Read type indicating an operation currently wants to read raw data. + */ + DATA + } private State state=State.WRITING; private ReadType readType=ReadType.LINE; diff --git a/src/java/net/spy/memcached/ops/OperationCallback.java b/src/java/net/spy/memcached/ops/OperationCallback.java new file mode 100644 index 000000000..2c8f456af --- /dev/null +++ b/src/java/net/spy/memcached/ops/OperationCallback.java @@ -0,0 +1,17 @@ +// Copyright (c) 2006 Dustin Sallings +// arch-tag: 47B3D166-3964-49D4-9B4C-A46B749B12A5 + +package net.spy.memcached.ops; + +/** + * Callback that's invoked with the response of an operation. + */ +public interface OperationCallback { + + /** + * Method invoked with the status when the operation is complete. + * + * @param line the line containing the final status of the operation + */ + void receivedStatus(String line); +} diff --git a/src/java/net/spy/memcached/ops/StatsOperation.java b/src/java/net/spy/memcached/ops/StatsOperation.java index e7e6358f1..5b10204d6 100644 --- a/src/java/net/spy/memcached/ops/StatsOperation.java +++ b/src/java/net/spy/memcached/ops/StatsOperation.java @@ -5,6 +5,9 @@ import java.nio.ByteBuffer; +/** + * Operation to retrieve statistics from a memcached server. + */ public class StatsOperation extends Operation { private static final byte[] MSG="stats\r\n".getBytes(); @@ -20,7 +23,7 @@ public StatsOperation(Callback c) { public void handleLine(String line) { if(line.equals("END")) { if(cb != null) { - cb.statsComplete(); + cb.receivedStatus(line); } transitionState(State.COMPLETE); } else { @@ -37,8 +40,16 @@ public void initialize() { setBuffer(ByteBuffer.wrap(MSG)); } - public interface Callback { - void statsComplete(); + /** + * Callback for stats operation. + */ + public interface Callback extends OperationCallback { + /** + * Invoked once for every stat returned from the server. + * + * @param name the name of the stat + * @param val the stat value. + */ void gotStat(String name, String val); } diff --git a/src/java/net/spy/memcached/ops/StoreOperation.java b/src/java/net/spy/memcached/ops/StoreOperation.java index 4fc1a24bd..2dfedf691 100644 --- a/src/java/net/spy/memcached/ops/StoreOperation.java +++ b/src/java/net/spy/memcached/ops/StoreOperation.java @@ -5,9 +5,30 @@ import java.nio.ByteBuffer; +/** + * Operation to store data in a memcached server. + */ public class StoreOperation extends Operation { - public enum StoreType { set, add, replace } + /** + * The type of storage operation to perform. + */ + public enum StoreType { + /** + * Unconditionally store a value in the cache. + */ + set, + /** + * Store a value in the cache iff there is not already something stored + * for the given key. + */ + add, + /** + * Store a value in the cache iff there is already something stored for + * the given key. + */ + replace + } // Overhead storage stuff to make sure the buffer pushes out far enough. private static final int OVERHEAD = 32; @@ -17,10 +38,10 @@ public enum StoreType { set, add, replace } private int flags=0; private int exp=0; private byte[] data=null; - private Callback cb=null; + private OperationCallback cb=null; public StoreOperation(StoreType t, String key, int flags, int exp, - byte[] data, Callback callback) { + byte[] data, OperationCallback callback) { super(); this.type=t; this.key=key; @@ -31,9 +52,9 @@ public StoreOperation(StoreType t, String key, int flags, int exp, } @Override - public void handleLine(String firstLine) { + public void handleLine(String line) { if(cb != null) { - cb.storeResult(firstLine); + cb.receivedStatus(line); } transitionState(State.COMPLETE); } @@ -52,11 +73,4 @@ public void initialize() { setBuffer(bb); } - public interface Callback { - - /** - * Report the result of an asynchronous callback. - */ - public void storeResult(String val); - } } diff --git a/src/java/net/spy/memcached/ops/VersionOperation.java b/src/java/net/spy/memcached/ops/VersionOperation.java index 3b94819e1..33a9f6498 100644 --- a/src/java/net/spy/memcached/ops/VersionOperation.java +++ b/src/java/net/spy/memcached/ops/VersionOperation.java @@ -12,9 +12,9 @@ public class VersionOperation extends Operation { private static final byte[] REQUEST="version\r\n".getBytes(); - private Callback cb=null; + private OperationCallback cb=null; - public VersionOperation(Callback c) { + public VersionOperation(OperationCallback c) { super(); cb=c; } @@ -23,7 +23,7 @@ public VersionOperation(Callback c) { public void handleLine(String line) { if(cb != null) { assert line.startsWith("VERSION "); - cb.versionResult(line.substring("VERSION ".length())); + cb.receivedStatus(line.substring("VERSION ".length())); } transitionState(State.COMPLETE); } @@ -33,8 +33,4 @@ public void initialize() { setBuffer(ByteBuffer.wrap(REQUEST)); } - public interface Callback { - public void versionResult(String s); - } - }