diff --git a/src/main/java/net/spy/memcached/MemcachedClient.java b/src/main/java/net/spy/memcached/MemcachedClient.java index e2c09c010..503241f91 100644 --- a/src/main/java/net/spy/memcached/MemcachedClient.java +++ b/src/main/java/net/spy/memcached/MemcachedClient.java @@ -908,7 +908,7 @@ public void gotData(String k, int flags, byte[] data) { public void complete() { latch.countDown(); } - }); + }, false); future.setOperation(op); addOp(key, op); return future; @@ -960,7 +960,7 @@ public void gotData(String k, int flags, long cas, byte[] data) { public void complete() { latch.countDown(); } - }); + }, false); rv.setOperation(op); addOp(key, op); return rv; @@ -1103,10 +1103,9 @@ public void complete() { Operation op; if (node == null) { - op = opFact.mget(keyList, cb); + op = opFact.get(keyList, cb, true); } else { - op = node.enabledMGetOp() ? opFact.mget(keyList, cb) - : opFact.get(keyList, cb); + op = opFact.get(keyList, cb, node.enabledMGetOp()); } conn.addOperation(node, op); ops.add(op); @@ -1240,10 +1239,9 @@ public void complete() { Operation op; if (node == null) { - op = opFact.mgets(keyList, cb); + op = opFact.gets(keyList, cb, true); } else { - op = node.enabledMGetsOp() ? opFact.mgets(keyList, cb) - : opFact.gets(keyList, cb); + op = opFact.gets(keyList, cb, node.enabledMGetOp()); } conn.addOperation(node, op); ops.add(op); diff --git a/src/main/java/net/spy/memcached/OperationFactory.java b/src/main/java/net/spy/memcached/OperationFactory.java index bd93b47d3..1285b6c93 100644 --- a/src/main/java/net/spy/memcached/OperationFactory.java +++ b/src/main/java/net/spy/memcached/OperationFactory.java @@ -120,18 +120,20 @@ public interface OperationFactory { * * @param key the key to get * @param callback the callback that will contain the results + * @param isMget true if this is a multi-key get * @return a new GetOperation */ - GetOperation get(String key, GetOperation.Callback callback); + GetOperation get(String key, GetOperation.Callback callback, boolean isMget); /** * Create a gets operation. * * @param key the key to get * @param callback the callback that will contain the results + * @param isMget true if this is a multi-key get * @return a new GetsOperation */ - GetsOperation gets(String key, GetsOperation.Callback callback); + GetsOperation gets(String key, GetsOperation.Callback callback, boolean isMget); /** @@ -139,36 +141,20 @@ public interface OperationFactory { * * @param keys the collection of keys to get * @param cb the callback that will contain the results + * @param isMget true if this is a multi-key get * @return a new GetOperation */ - GetOperation get(Collection keys, GetOperation.Callback cb); + GetOperation get(Collection keys, GetOperation.Callback cb, boolean isMget); /** * Create a gets operation. * * @param keys the collection of keys to get * @param cb the callback that will contain the results + * @param isMget true if this is a multi-key get * @return a new GetsOperation */ - GetsOperation gets(Collection keys, GetsOperation.Callback cb); - - /** - * Create a mget operation. - * - * @param keys the collection of keys to get - * @param cb the callback that will contain the results - * @return a new GetOperation - */ - GetOperation mget(Collection keys, GetOperation.Callback cb); - - /** - * Create a mgets operation. - * - * @param keys the collection of keys to get - * @param cb the callback that will contain the results - * @return a new GetOperation - */ - GetsOperation mgets(Collection keys, GetsOperation.Callback cb); + GetsOperation gets(Collection keys, GetsOperation.Callback cb, boolean isMget); /** * Create a mutator operation. diff --git a/src/main/java/net/spy/memcached/ops/BaseOperationFactory.java b/src/main/java/net/spy/memcached/ops/BaseOperationFactory.java index d0a50640f..b8c3d99d7 100644 --- a/src/main/java/net/spy/memcached/ops/BaseOperationFactory.java +++ b/src/main/java/net/spy/memcached/ops/BaseOperationFactory.java @@ -52,13 +52,13 @@ public Collection clone(KeyedOperation op) { GetOperation.Callback getCb = new MultiGetOperationCallback( op.getCallback(), op.getKeys().size()); for (String k : op.getKeys()) { - rv.add(get(k, getCb)); + rv.add(get(k, getCb, false)); } } else if (op instanceof GetsOperation) { GetsOperation.Callback getsCb = new MultiGetsOperationCallback( op.getCallback(), op.getKeys().size()); for (String k : op.getKeys()) { - rv.add(gets(k, getsCb)); + rv.add(gets(k, getsCb, false)); } } else if (op instanceof CASOperation) { CASOperation cop = (CASOperation) op; @@ -137,10 +137,10 @@ public Operation cloneMultiOperation(KeyedOperation op, MemcachedNode node, if (op instanceof GetOperation) { // If MemcachedNode supports this clone feature, it should support mget operation too. - return mget(redirectKeys, (GetOperation.Callback) mcb); + return get(redirectKeys, (GetOperation.Callback) mcb, true); } else if (op instanceof GetsOperation) { // If MemcachedNode supports this clone feature, it should support mgets operation too. - return mgets(redirectKeys, (GetsOperation.Callback) mcb); + return gets(redirectKeys, (GetsOperation.Callback) mcb, true); } else if (op instanceof CollectionBulkInsertOperation) { final CollectionBulkInsert insert = ((CollectionBulkInsertOperation) op).getInsert(); return collectionBulkInsert(insert.clone(node, redirectKeys), mcb); diff --git a/src/main/java/net/spy/memcached/protocol/ascii/AsciiOperationFactory.java b/src/main/java/net/spy/memcached/protocol/ascii/AsciiOperationFactory.java index f755a2fc9..4e04d8a1b 100644 --- a/src/main/java/net/spy/memcached/protocol/ascii/AsciiOperationFactory.java +++ b/src/main/java/net/spy/memcached/protocol/ascii/AsciiOperationFactory.java @@ -81,6 +81,9 @@ import net.spy.memcached.ops.StoreType; import net.spy.memcached.ops.VersionOperation; +import static net.spy.memcached.protocol.ascii.GetOperationImpl.generateGetOp; +import static net.spy.memcached.protocol.ascii.GetsOperationImpl.generateGetsOp; + /** * Operation factory for the ascii protocol. */ @@ -94,28 +97,20 @@ public FlushOperation flush(int delay, OperationCallback cb) { return new FlushOperationImpl(delay, cb); } - public GetOperation get(String key, GetOperation.Callback cb) { - return new GetOperationImpl(key, cb); - } - - public GetOperation get(Collection keys, GetOperation.Callback cb) { - return new GetOperationImpl(keys, cb); - } - - public GetsOperation gets(String key, GetsOperation.Callback cb) { - return new GetsOperationImpl(key, cb); + public GetOperation get(String key, GetOperation.Callback cb, boolean isMget) { + return generateGetOp(key, cb, isMget); } - public GetsOperation gets(Collection keys, GetsOperation.Callback cb) { - return new GetsOperationImpl(keys, cb); + public GetOperation get(Collection keys, GetOperation.Callback cb, boolean isMget) { + return generateGetOp(keys, cb, isMget); } - public GetOperation mget(Collection keys, GetOperation.Callback cb) { - return new MGetOperationImpl(keys, cb); + public GetsOperation gets(String key, GetsOperation.Callback cb, boolean isMget) { + return generateGetsOp(key, cb, isMget); } - public GetsOperation mgets(Collection keys, GetsOperation.Callback cb) { - return new MGetsOperationImpl(keys, cb); + public GetsOperation gets(Collection keys, GetsOperation.Callback cb, boolean isMget) { + return generateGetsOp(keys, cb, isMget); } public MutatorOperation mutate(Mutator m, String key, int by, diff --git a/src/main/java/net/spy/memcached/protocol/ascii/GetOperationImpl.java b/src/main/java/net/spy/memcached/protocol/ascii/GetOperationImpl.java index ffcbfd8a8..3087583a9 100644 --- a/src/main/java/net/spy/memcached/protocol/ascii/GetOperationImpl.java +++ b/src/main/java/net/spy/memcached/protocol/ascii/GetOperationImpl.java @@ -15,15 +15,32 @@ class GetOperationImpl extends BaseGetOpImpl implements GetOperation { private static final String CMD = "get"; + private static final String CMD_MGET = "mget"; - public GetOperationImpl(String key, GetOperation.Callback c) { - super(CMD, c, Collections.singleton(key)); + public GetOperationImpl(String key, GetOperation.Callback c, String command) { + super(command, c, Collections.singleton(key)); setAPIType(APIType.GET); } - public GetOperationImpl(Collection k, GetOperation.Callback c) { - super(CMD, c, new HashSet<>(k)); + public GetOperationImpl(Collection k, GetOperation.Callback c, String command) { + super(command, c, new HashSet<>(k)); setAPIType(APIType.GET); } + public static GetOperationImpl generateGetOp(String key, GetOperation.Callback c, + boolean isMget) { + if (isMget) { + return new GetOperationImpl(key, c, CMD_MGET); + } + return new GetOperationImpl(key, c, CMD); + } + + public static GetOperationImpl generateGetOp(Collection keys, GetOperation.Callback c, + boolean isMget) { + if (isMget) { + return new GetOperationImpl(keys, c, CMD_MGET); + } + return new GetOperationImpl(keys, c, CMD); + } + } diff --git a/src/main/java/net/spy/memcached/protocol/ascii/GetsOperationImpl.java b/src/main/java/net/spy/memcached/protocol/ascii/GetsOperationImpl.java index a0282a0f6..8a90a8804 100644 --- a/src/main/java/net/spy/memcached/protocol/ascii/GetsOperationImpl.java +++ b/src/main/java/net/spy/memcached/protocol/ascii/GetsOperationImpl.java @@ -13,15 +13,32 @@ class GetsOperationImpl extends BaseGetOpImpl implements GetsOperation { private static final String CMD = "gets"; + private static final String CMD_MGETS = "mgets"; - public GetsOperationImpl(String key, GetsOperation.Callback cb) { - super(CMD, cb, Collections.singleton(key)); + public GetsOperationImpl(String key, GetsOperation.Callback cb, String command) { + super(command, cb, Collections.singleton(key)); setAPIType(APIType.GETS); } - public GetsOperationImpl(Collection keys, GetsOperation.Callback cb) { - super(CMD, cb, new HashSet<>(keys)); + public GetsOperationImpl(Collection keys, GetsOperation.Callback cb, String command) { + super(command, cb, new HashSet<>(keys)); setAPIType(APIType.GETS); } + public static GetsOperationImpl generateGetsOp(String key, GetsOperation.Callback c, + boolean isMget) { + if (isMget) { + return new GetsOperationImpl(key, c, CMD_MGETS); + } + return new GetsOperationImpl(key, c, CMD); + } + + public static GetsOperationImpl generateGetsOp(Collection keys, GetsOperation.Callback c, + boolean isMget) { + if (isMget) { + return new GetsOperationImpl(keys, c, CMD_MGETS); + } + return new GetsOperationImpl(keys, c, CMD); + } + } diff --git a/src/main/java/net/spy/memcached/protocol/ascii/MGetOperationImpl.java b/src/main/java/net/spy/memcached/protocol/ascii/MGetOperationImpl.java deleted file mode 100644 index 41ff1f7af..000000000 --- a/src/main/java/net/spy/memcached/protocol/ascii/MGetOperationImpl.java +++ /dev/null @@ -1,20 +0,0 @@ -package net.spy.memcached.protocol.ascii; - -import java.util.Collection; -import java.util.HashSet; - -import net.spy.memcached.ops.APIType; -import net.spy.memcached.ops.GetOperation; - -/** - * Operation for retrieving data. - */ -public final class MGetOperationImpl extends BaseGetOpImpl implements GetOperation { - - private static final String CMD = "mget"; - - public MGetOperationImpl(Collection k, Callback c) { - super(CMD, c, new HashSet<>(k)); - setAPIType(APIType.MGET); - } -} diff --git a/src/main/java/net/spy/memcached/protocol/ascii/MGetsOperationImpl.java b/src/main/java/net/spy/memcached/protocol/ascii/MGetsOperationImpl.java deleted file mode 100644 index c0a92b97b..000000000 --- a/src/main/java/net/spy/memcached/protocol/ascii/MGetsOperationImpl.java +++ /dev/null @@ -1,21 +0,0 @@ -package net.spy.memcached.protocol.ascii; - -import java.util.Collection; -import java.util.HashSet; - -import net.spy.memcached.ops.APIType; -import net.spy.memcached.ops.GetsOperation; - -/** - * Operation for retrieving data. - */ -public final class MGetsOperationImpl extends BaseGetOpImpl implements GetsOperation { - - private static final String CMD = "mgets"; - - public MGetsOperationImpl(Collection k, Callback c) { - super(CMD, c, new HashSet<>(k)); - setAPIType(APIType.MGETS); - } - -} diff --git a/src/main/java/net/spy/memcached/protocol/ascii/OptimizedGetImpl.java b/src/main/java/net/spy/memcached/protocol/ascii/OptimizedGetImpl.java index d37cfbf4b..efb7089b5 100644 --- a/src/main/java/net/spy/memcached/protocol/ascii/OptimizedGetImpl.java +++ b/src/main/java/net/spy/memcached/protocol/ascii/OptimizedGetImpl.java @@ -10,13 +10,14 @@ */ final class OptimizedGetImpl extends GetOperationImpl { + private static final String CMD = "get"; private final ProxyCallback pcb; /** * Construct an optimized get starting with the given get operation. */ public OptimizedGetImpl(GetOperation firstGet) { - super(new HashSet<>(), new ProxyCallback()); + super(new HashSet<>(), new ProxyCallback(), CMD); pcb = (ProxyCallback) getCallback(); addOperation(firstGet); } diff --git a/src/main/java/net/spy/memcached/protocol/binary/BinaryOperationFactory.java b/src/main/java/net/spy/memcached/protocol/binary/BinaryOperationFactory.java index 6d261269e..1c66ab381 100644 --- a/src/main/java/net/spy/memcached/protocol/binary/BinaryOperationFactory.java +++ b/src/main/java/net/spy/memcached/protocol/binary/BinaryOperationFactory.java @@ -96,33 +96,39 @@ public FlushOperation flush(int delay, OperationCallback cb) { return new FlushOperationImpl(cb); } - public GetOperation get(String key, Callback callback) { + public GetOperation get(String key, Callback callback, boolean isMget) { + if (isMget) { + throw new RuntimeException( + "mget is not supported in binary protocol yet."); + } return new GetOperationImpl(key, callback); } - public GetOperation get(Collection value, Callback cb) { - return new MultiGetOperationImpl(value, cb); + public GetOperation get(Collection keys, Callback cb, boolean isMget) { + if (isMget) { + throw new RuntimeException( + "mget is not supported in binary protocol yet."); + } + return new MultiGetOperationImpl(keys, cb); } - public GetsOperation gets(String key, GetsOperation.Callback cb) { - return new GetOperationImpl(key, cb); + public GetsOperation gets(String key, GetsOperation.Callback callback, boolean isMget) { + if (isMget) { + throw new RuntimeException( + "mgets is not supported in binary protocol yet."); + } + return new GetOperationImpl(key, callback); } - public GetsOperation gets(Collection keys, GetsOperation.Callback callback) { + public GetsOperation gets(Collection keys, GetsOperation.Callback cb, boolean isMget) { + if (isMget) { + throw new RuntimeException( + "mgets is not supported in binary protocol yet."); + } throw new RuntimeException( "gets is not supported in binary protocol yet."); } - public GetOperation mget(Collection keys, GetOperation.Callback cb) { - throw new RuntimeException( - "mget is not supported in binary protocol yet."); - } - - public GetsOperation mgets(Collection keys, GetsOperation.Callback cb) { - throw new RuntimeException( - "mgets is not supported in binary protocol yet."); - } - public MutatorOperation mutate(Mutator m, String key, int by, long def, int exp, OperationCallback cb) { return new MutatorOperationImpl(m, key, by, def, exp, cb); diff --git a/src/test/java/net/spy/memcached/OperationFactoryTestBase.java b/src/test/java/net/spy/memcached/OperationFactoryTestBase.java index 95247a623..9ad60e080 100644 --- a/src/test/java/net/spy/memcached/OperationFactoryTestBase.java +++ b/src/test/java/net/spy/memcached/OperationFactoryTestBase.java @@ -192,7 +192,7 @@ void testConcatenationOperationPrependCloning() { @Test void testSingleGetOperationCloning() { GetOperation.Callback callback = context.mock(GetOperation.Callback.class); - GetOperation op = ofact.get(TEST_KEY, callback); + GetOperation op = ofact.get(TEST_KEY, callback, false); GetOperation op2 = cloneOne(GetOperation.class, op); assertKey(op2); @@ -202,7 +202,7 @@ void testSingleGetOperationCloning() { @Test void testSingleGetsOperationCloning() { GetsOperation.Callback callback = context.mock(GetsOperation.Callback.class); - GetsOperation op = ofact.gets(TEST_KEY, callback); + GetsOperation op = ofact.gets(TEST_KEY, callback, false); GetsOperation op2 = cloneOne(GetsOperation.class, op); assertKey(op2); @@ -214,7 +214,7 @@ void testSingleGetsOperationCloning() { void testMultipleGetOperationCloning() { Collection keys = Arrays.asList("k1", "k2", "k3"); GetOperation.Callback callback = context.mock(GetOperation.Callback.class); - GetOperation op = ofact.get(keys, callback); + GetOperation op = ofact.get(keys, callback, false); Collection ops = ofact.clone(op); assertEquals(3, ops.size()); @@ -244,7 +244,7 @@ void testMultipleGetOperationFanout() { e.oneOf(callback).gotData(e.with("k3"), e.with(3), e.with(any(byte[].class))); })); - GetOperation op = ofact.get(keys, callback); + GetOperation op = ofact.get(keys, callback, false); // Transition each operation callback into the complete state. Iterator ki = keys.iterator(); @@ -262,7 +262,7 @@ void testMultipleGetOperationFanout() { public void testMultipleGetsOperationCloning() { Collection keys = Arrays.asList("k1", "k2", "k3"); GetsOperation.Callback callback = context.mock(GetsOperation.Callback.class); - GetsOperation op = ofact.gets(keys, callback); + GetsOperation op = ofact.gets(keys, callback, false); Collection ops = ofact.clone(op); assertEquals(3, ops.size()); @@ -296,7 +296,7 @@ public void testMultipleGetsOperationFanout() { e.with(casId[2]), e.with(any(byte[].class))); })); - GetsOperation op = ofact.gets(keys, callback); + GetsOperation op = ofact.gets(keys, callback, false); // Transition each operation callback into the complete state. Iterator ki = keys.iterator(); diff --git a/src/test/java/net/spy/memcached/protocol/TCPMemcachedNodeImplTest.java b/src/test/java/net/spy/memcached/protocol/TCPMemcachedNodeImplTest.java index 78883c9df..0c458d2a2 100644 --- a/src/test/java/net/spy/memcached/protocol/TCPMemcachedNodeImplTest.java +++ b/src/test/java/net/spy/memcached/protocol/TCPMemcachedNodeImplTest.java @@ -95,7 +95,7 @@ public void gotData(String key, int flags, byte[] data) { @Override public void complete() { } - }); + }, false); op.initialize(); fromOperations.add(op); } diff --git a/src/test/manual/net/spy/memcached/MultibyteKeyTest.java b/src/test/manual/net/spy/memcached/MultibyteKeyTest.java index 53f868a33..4a9b946ed 100644 --- a/src/test/manual/net/spy/memcached/MultibyteKeyTest.java +++ b/src/test/manual/net/spy/memcached/MultibyteKeyTest.java @@ -123,7 +123,7 @@ public void complete() { @Override public void gotData(String key, int flags, long cas, byte[] data) { } - }).initialize(); // BaseGetOpImpl.initialize() + }, false).initialize(); // BaseGetOpImpl.initialize() } catch (java.nio.BufferOverflowException e) { fail(); }