From faba8ad3ca4d7559b59512cf6c1e27e9511d2fcb Mon Sep 17 00:00:00 2001 From: cheesecrust Date: Thu, 26 Dec 2024 16:59:47 +0900 Subject: [PATCH] INTERNAL: Integration get and mget api --- .../net/spy/memcached/MemcachedClient.java | 10 +++----- .../net/spy/memcached/OperationFactory.java | 25 +++---------------- .../java/net/spy/memcached/ops/APIType.java | 1 - .../memcached/ops/BaseOperationFactory.java | 6 ++--- .../ascii/AsciiMemcachedNodeImpl.java | 7 +++--- .../protocol/ascii/AsciiOperationFactory.java | 16 +++--------- .../protocol/ascii/GetOperationImpl.java | 6 +++++ .../protocol/ascii/GetsOperationImpl.java | 5 ++-- .../protocol/ascii/MGetOperationImpl.java | 20 --------------- .../protocol/ascii/MGetsOperationImpl.java | 21 ---------------- .../binary/BinaryOperationFactory.java | 18 +++---------- .../memcached/OperationFactoryTestBase.java | 8 +++--- 12 files changed, 34 insertions(+), 109 deletions(-) delete mode 100644 src/main/java/net/spy/memcached/protocol/ascii/MGetOperationImpl.java delete mode 100644 src/main/java/net/spy/memcached/protocol/ascii/MGetsOperationImpl.java diff --git a/src/main/java/net/spy/memcached/MemcachedClient.java b/src/main/java/net/spy/memcached/MemcachedClient.java index e2c09c010..415a378e4 100644 --- a/src/main/java/net/spy/memcached/MemcachedClient.java +++ b/src/main/java/net/spy/memcached/MemcachedClient.java @@ -1103,10 +1103,9 @@ public void complete() { Operation op; if (node == null) { - op = opFact.mget(keyList, cb); + op = opFact.get(keyList, cb, false); } 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, false); } 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..7572f9bcc 100644 --- a/src/main/java/net/spy/memcached/OperationFactory.java +++ b/src/main/java/net/spy/memcached/OperationFactory.java @@ -133,42 +133,25 @@ public interface OperationFactory { */ GetsOperation gets(String key, GetsOperation.Callback callback); - /** * Create a get operation. * * @param keys the collection of keys to get * @param cb the callback that will contain the results + * @param isMGet true if the handling node provides mget command * @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 the handling node provides mgets command * @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/APIType.java b/src/main/java/net/spy/memcached/ops/APIType.java index b3de75fc9..f5069cc56 100644 --- a/src/main/java/net/spy/memcached/ops/APIType.java +++ b/src/main/java/net/spy/memcached/ops/APIType.java @@ -25,7 +25,6 @@ public enum APIType { INCR(OperationType.WRITE), DECR(OperationType.WRITE), DELETE(OperationType.WRITE), GET(OperationType.READ), GETS(OperationType.READ), - MGET(OperationType.READ), MGETS(OperationType.READ), // List API Type LOP_CREATE(OperationType.WRITE), diff --git a/src/main/java/net/spy/memcached/ops/BaseOperationFactory.java b/src/main/java/net/spy/memcached/ops/BaseOperationFactory.java index d0a50640f..28f24621e 100644 --- a/src/main/java/net/spy/memcached/ops/BaseOperationFactory.java +++ b/src/main/java/net/spy/memcached/ops/BaseOperationFactory.java @@ -136,11 +136,9 @@ public Operation cloneMultiOperation(KeyedOperation op, MemcachedNode node, assert !op.hasErrored() : "Attempted to clone an errored op"; 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, node.enabledMGetOp()); } 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, node.enabledMGetsOp()); } 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/AsciiMemcachedNodeImpl.java b/src/main/java/net/spy/memcached/protocol/ascii/AsciiMemcachedNodeImpl.java index c85e240c4..cf0be2953 100644 --- a/src/main/java/net/spy/memcached/protocol/ascii/AsciiMemcachedNodeImpl.java +++ b/src/main/java/net/spy/memcached/protocol/ascii/AsciiMemcachedNodeImpl.java @@ -45,10 +45,10 @@ protected void optimize() { // make sure there are at least two get operations in a row before // attempting to optimize them. Operation nxtOp = writeQ.peek(); - if (nxtOp instanceof GetOperation && nxtOp.getAPIType() != APIType.MGET) { + if (nxtOp instanceof GetOperation) { optimizedOp = writeQ.remove(); nxtOp = writeQ.peek(); - if (nxtOp instanceof GetOperation && nxtOp.getAPIType() != APIType.MGET) { + if (nxtOp instanceof GetOperation) { OptimizedGetImpl og = new OptimizedGetImpl( (GetOperation) optimizedOp); optimizedOp = og; @@ -59,8 +59,7 @@ protected void optimize() { og.addOperation(o); } nxtOp = writeQ.peek(); - } while (nxtOp instanceof GetOperation && - nxtOp.getAPIType() != APIType.MGET); + } while (nxtOp instanceof GetOperation); // Initialize the new mega get optimizedOp.initialize(); 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..ba4c0a368 100644 --- a/src/main/java/net/spy/memcached/protocol/ascii/AsciiOperationFactory.java +++ b/src/main/java/net/spy/memcached/protocol/ascii/AsciiOperationFactory.java @@ -98,24 +98,16 @@ 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 GetOperation get(Collection keys, GetOperation.Callback cb, boolean isMGet) { + return new GetOperationImpl(keys, cb, isMGet); } public GetsOperation gets(String key, GetsOperation.Callback cb) { return new GetsOperationImpl(key, cb); } - public GetsOperation gets(Collection keys, GetsOperation.Callback cb) { - return new GetsOperationImpl(keys, cb); - } - - public GetOperation mget(Collection keys, GetOperation.Callback cb) { - return new MGetOperationImpl(keys, cb); - } - - public GetsOperation mgets(Collection keys, GetsOperation.Callback cb) { - return new MGetsOperationImpl(keys, cb); + public GetsOperation gets(Collection keys, GetsOperation.Callback cb, boolean isMGet) { + return new GetsOperationImpl(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..931d60d3b 100644 --- a/src/main/java/net/spy/memcached/protocol/ascii/GetOperationImpl.java +++ b/src/main/java/net/spy/memcached/protocol/ascii/GetOperationImpl.java @@ -15,6 +15,7 @@ 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)); @@ -26,4 +27,9 @@ public GetOperationImpl(Collection k, GetOperation.Callback c) { setAPIType(APIType.GET); } + public GetOperationImpl(Collection keys, GetOperation.Callback cb, boolean isMGet) { + super(isMGet ? CMD_MGET : CMD, cb, new HashSet<>(keys)); + setAPIType(APIType.GET); + } + } 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..d0461995f 100644 --- a/src/main/java/net/spy/memcached/protocol/ascii/GetsOperationImpl.java +++ b/src/main/java/net/spy/memcached/protocol/ascii/GetsOperationImpl.java @@ -13,14 +13,15 @@ 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)); setAPIType(APIType.GETS); } - public GetsOperationImpl(Collection keys, GetsOperation.Callback cb) { - super(CMD, cb, new HashSet<>(keys)); + public GetsOperationImpl(Collection keys, GetsOperation.Callback cb, boolean isMGet) { + super(isMGet ? CMD_MGETS : CMD, cb, new HashSet<>(keys)); setAPIType(APIType.GETS); } 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/binary/BinaryOperationFactory.java b/src/main/java/net/spy/memcached/protocol/binary/BinaryOperationFactory.java index 6d261269e..98a557e90 100644 --- a/src/main/java/net/spy/memcached/protocol/binary/BinaryOperationFactory.java +++ b/src/main/java/net/spy/memcached/protocol/binary/BinaryOperationFactory.java @@ -100,27 +100,17 @@ public GetOperation get(String key, Callback callback) { 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) { + return new MultiGetOperationImpl(keys, cb); } public GetsOperation gets(String key, GetsOperation.Callback cb) { return new GetOperationImpl(key, cb); } - public GetsOperation gets(Collection keys, GetsOperation.Callback callback) { + public GetsOperation gets(Collection keys, GetsOperation.Callback cb, boolean isMGet) { 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."); + "multiple key gets is not supported in binary protocol yet."); } public MutatorOperation mutate(Mutator m, String key, int by, diff --git a/src/test/java/net/spy/memcached/OperationFactoryTestBase.java b/src/test/java/net/spy/memcached/OperationFactoryTestBase.java index 95247a623..a8344aff9 100644 --- a/src/test/java/net/spy/memcached/OperationFactoryTestBase.java +++ b/src/test/java/net/spy/memcached/OperationFactoryTestBase.java @@ -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();