Skip to content

Commit

Permalink
INTERNAL: Integration get and mget api
Browse files Browse the repository at this point in the history
  • Loading branch information
cheesecrust committed Dec 30, 2024
1 parent c9473d9 commit d2b7b3d
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 77 deletions.
10 changes: 4 additions & 6 deletions src/main/java/net/spy/memcached/MemcachedClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
29 changes: 24 additions & 5 deletions src/main/java/net/spy/memcached/OperationFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,41 @@ public interface OperationFactory {
GetsOperation gets(Collection<String> keys, GetsOperation.Callback cb);

/**
* Create a mget operation.
* Create a get operation.
*
* @param key the key to get
* @param callback the callback that will contain the results
* @return a new GetOperation
*/
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
* @return a new GetsOperation
*/
GetsOperation gets(String key, GetsOperation.Callback callback, boolean isMget);


/**
* Create a get 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<String> keys, GetOperation.Callback cb);
GetOperation get(Collection<String> keys, GetOperation.Callback cb, boolean isMget);

/**
* Create a mgets operation.
* Create a gets operation.
*
* @param keys the collection of keys to get
* @param cb the callback that will contain the results
* @return a new GetOperation
* @return a new GetsOperation
*/
GetsOperation mgets(Collection<String> keys, GetsOperation.Callback cb);
GetsOperation gets(Collection<String> keys, GetsOperation.Callback cb, boolean isMget);

/**
* Create a mutator operation.
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/spy/memcached/ops/BaseOperationFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ 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);
// If MemcachedNode supports this clone feature, it should support get keys operation too.
return get(redirectKeys, (GetOperation.Callback) mcb);
} else if (op instanceof GetsOperation) {
// If MemcachedNode supports this clone feature, it should support mgets operation too.
return mgets(redirectKeys, (GetsOperation.Callback) mcb);
// If MemcachedNode supports this clone feature, it should support gets keys operation too.
return gets(redirectKeys, (GetsOperation.Callback) mcb);
} else if (op instanceof CollectionBulkInsertOperation) {
final CollectionBulkInsert<?> insert = ((CollectionBulkInsertOperation) op).getInsert();
return collectionBulkInsert(insert.clone(node, redirectKeys), mcb);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -95,27 +98,35 @@ public FlushOperation flush(int delay, OperationCallback cb) {
}

public GetOperation get(String key, GetOperation.Callback cb) {
return new GetOperationImpl(key, cb);
return generateGetOp(key, cb, false);
}

public GetOperation get(Collection<String> keys, GetOperation.Callback cb) {
return new GetOperationImpl(keys, cb);
return generateGetOp(keys, cb, false);
}

public GetsOperation gets(String key, GetsOperation.Callback cb) {
return new GetsOperationImpl(key, cb);
return generateGetsOp(key, cb, false);
}

public GetsOperation gets(Collection<String> keys, GetsOperation.Callback cb) {
return new GetsOperationImpl(keys, cb);
return generateGetsOp(keys, cb, false);
}

public GetOperation get(String key, GetOperation.Callback cb, boolean isMget) {
return generateGetOp(key, cb, isMget);
}

public GetOperation get(Collection<String> keys, GetOperation.Callback cb, boolean isMget) {
return generateGetOp(keys, cb, isMget);
}

public GetOperation mget(Collection<String> 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<String> keys, GetsOperation.Callback cb) {
return new MGetsOperationImpl(keys, cb);
public GetsOperation gets(Collection<String> keys, GetsOperation.Callback cb, boolean isMget) {
return generateGetsOp(keys, cb, isMget);
}

public MutatorOperation mutate(Mutator m, String key, int by,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> k, GetOperation.Callback c) {
super(CMD, c, new HashSet<>(k));
public GetOperationImpl(Collection<String> 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<String> keys, GetOperation.Callback c,
boolean isMget) {
if (isMget) {
return new GetOperationImpl(keys, c, CMD_MGET);
}
return new GetOperationImpl(keys, c, CMD);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> keys, GetsOperation.Callback cb) {
super(CMD, cb, new HashSet<>(keys));
public GetsOperationImpl(Collection<String> 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<String> keys, GetsOperation.Callback c,
boolean isMget) {
if (isMget) {
return new GetsOperationImpl(keys, c, CMD_MGETS);
}
return new GetsOperationImpl(keys, c, CMD);
}

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,23 @@ public GetsOperation gets(Collection<String> keys, GetsOperation.Callback callba
"gets is not supported in binary protocol yet.");
}

public GetOperation mget(Collection<String> keys, GetOperation.Callback cb) {
throw new RuntimeException(
"mget is not supported in binary protocol yet.");
@Override
public GetOperation get(String key, Callback callback, boolean isMget) {
return new GetOperationImpl(key, callback);
}

public GetsOperation mgets(Collection<String> keys, GetsOperation.Callback cb) {
@Override
public GetsOperation gets(String key, GetsOperation.Callback callback, boolean isMget) {
return new GetOperationImpl(key, callback);
}

@Override
public GetOperation get(Collection<String> keys, Callback cb, boolean isMget) {
return new MultiGetOperationImpl(keys, cb);
}

@Override
public GetsOperation gets(Collection<String> keys, GetsOperation.Callback cb, boolean isMget) {
throw new RuntimeException(
"mgets is not supported in binary protocol yet.");
}
Expand Down

0 comments on commit d2b7b3d

Please sign in to comment.