Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INTERNA: Change callback method for mutate operation. #786

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/main/java/net/spy/memcached/MemcachedClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import net.spy.memcached.ops.GetOperation;
import net.spy.memcached.ops.GetsOperation;
import net.spy.memcached.ops.Mutator;
import net.spy.memcached.ops.MutatorOperation;
import net.spy.memcached.ops.Operation;
import net.spy.memcached.ops.OperationCallback;
import net.spy.memcached.ops.OperationStatus;
Expand Down Expand Up @@ -1789,15 +1790,24 @@ private OperationFuture<Long> asyncMutate(Mutator m, String key, int by, long de
final OperationFuture<Long> rv = new OperationFuture<>(
latch, operationTimeout);
Operation op = addOp(key, opFact.mutate(m, key, by, def, exp,
new OperationCallback() {
public void receivedStatus(OperationStatus s) {
rv.set(Long.parseLong(s.isSuccess() ? s.getMessage() : "-1"), s);
new MutatorOperation.Callback() {
private Long value;

@Override
public void receivedStatus(OperationStatus status) {
rv.set(value, status);
}

@Override
public void complete() {
latch.countDown();
}
}));

@Override
public void gotMutatedValue(Long result) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotDelta로 이름을 바꿉시다.

value = result;
}
}));
rv.setOperation(op);
return rv;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/spy/memcached/OperationFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public interface OperationFactory {
* @return the new mutator operation
*/
MutatorOperation mutate(Mutator m, String key, int by,
long def, int exp, OperationCallback cb);
long def, int exp, MutatorOperation.Callback cb);

/**
* Get a new StatsOperation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Collection<Operation> clone(KeyedOperation op) {
MutatorOperation mo = (MutatorOperation) op;
rv.add(mutate(mo.getType(), first(op.getKeys()),
mo.getBy(), mo.getDefault(), mo.getExpiration(),
op.getCallback()));
(MutatorOperation.Callback) op.getCallback()));
} else if (op instanceof StoreOperation) {
StoreOperation so = (StoreOperation) op;
rv.add(store(so.getStoreType(), first(op.getKeys()), so.getFlags(),
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/net/spy/memcached/ops/MutatorOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,13 @@ public interface MutatorOperation extends KeyedOperation {
* Get the expiration to set in case of a new entry.
*/
int getExpiration();

interface Callback extends OperationCallback {
/**
* Callback for mutate operation.
*
* @param result the value that was mutated.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

주석도 변화량을 매개변수로 넣어줘야 한다는 점을 더욱 명확하게 나타내는 문장으로 바꾸고요.

*/
void gotMutatedValue(Long result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public GetsOperation mgets(Collection<String> keys, GetsOperation.Callback cb) {
}

public MutatorOperation mutate(Mutator m, String key, int by,
long def, int exp, OperationCallback cb) {
long def, int exp, MutatorOperation.Callback cb) {
return new MutatorOperationImpl(m, key, by, def, exp, cb);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import net.spy.memcached.ops.APIType;
import net.spy.memcached.ops.Mutator;
import net.spy.memcached.ops.MutatorOperation;
import net.spy.memcached.ops.OperationCallback;
import net.spy.memcached.ops.OperationState;
import net.spy.memcached.ops.OperationStatus;
import net.spy.memcached.ops.OperationType;
Expand All @@ -52,15 +51,17 @@ final class MutatorOperationImpl extends OperationImpl
private final int amount;
private final long def;
private final int exp;
private final MutatorOperation.Callback cb;

public MutatorOperationImpl(Mutator m, String k, int amt, long d, int e,
OperationCallback c) {
super(c);
MutatorOperation.Callback cb) {
super(cb);
mutator = m;
key = k;
amount = amt;
def = d;
exp = e;
this.cb = cb;
if (m == Mutator.incr) {
setAPIType(APIType.INCR);
} else if (m == Mutator.decr) {
Expand Down Expand Up @@ -88,13 +89,15 @@ public void handleLine(String line) {
// <result value>\r\n
boolean allDigit = line.chars().allMatch(Character::isDigit);
OperationStatus status;
long result = -1L;
if (allDigit) {
status = new OperationStatus(true, line, StatusCode.SUCCESS);
result = Long.parseLong(line);
} else {
status = matchStatus(line, NOT_FOUND, TYPE_MISMATCH);
}

getCallback().receivedStatus(status);
cb.gotMutatedValue(result);
cb.receivedStatus(status);
transitionState(OperationState.COMPLETE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public GetsOperation mgets(Collection<String> keys, GetsOperation.Callback cb) {
}

public MutatorOperation mutate(Mutator m, String key, int by,
long def, int exp, OperationCallback cb) {
long def, int exp, MutatorOperation.Callback cb) {
return new MutatorOperationImpl(m, key, by, def, exp, cb);
}

Expand Down
28 changes: 24 additions & 4 deletions src/test/java/net/spy/memcached/OperationFactoryTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public abstract class OperationFactoryTestBase extends MockObjectTestCase {
public final static String TEST_KEY = "someKey";
protected OperationFactory ofact = null;
protected OperationCallback genericCallback;
protected MutatorOperation.Callback genericMutationCallback;
private byte[] testData;

@Override
Expand All @@ -47,6 +48,21 @@ public void receivedStatus(OperationStatus status) {
fail("Unexpected status: " + status);
}
};
genericMutationCallback = new MutatorOperation.Callback() {
@Override
public void gotMutatedValue(Long result) {
}

@Override
public void receivedStatus(OperationStatus status) {
fail("Unexpected status: " + status);
}

@Override
public void complete() {
fail("Unexpected invocation");
}
};

testData = new byte[64];
new Random().nextBytes(testData);
Expand Down Expand Up @@ -83,31 +99,31 @@ public void testMutatorOperationIncrCloning() {
long def = 28775;
int by = 7735;
MutatorOperation op = ofact.mutate(Mutator.incr, TEST_KEY, by, def,
exp, genericCallback);
exp, genericMutationCallback);

MutatorOperation op2 = cloneOne(MutatorOperation.class, op);
assertKey(op2);
assertEquals(exp, op2.getExpiration());
assertEquals(def, op2.getDefault());
assertEquals(by, op2.getBy());
assertSame(Mutator.incr, op2.getType());
assertCallback(op2);
assertMutationCallback(op2);
}

public void testMutatorOperationDecrCloning() {
int exp = 823862;
long def = 28775;
int by = 7735;
MutatorOperation op = ofact.mutate(Mutator.decr, TEST_KEY, by, def,
exp, genericCallback);
exp, genericMutationCallback);

MutatorOperation op2 = cloneOne(MutatorOperation.class, op);
assertKey(op2);
assertEquals(exp, op2.getExpiration());
assertEquals(def, op2.getDefault());
assertEquals(by, op2.getBy());
assertSame(Mutator.decr, op2.getType());
assertCallback(op2);
assertMutationCallback(op2);
}

public void testStoreOperationAddCloning() {
Expand Down Expand Up @@ -287,6 +303,10 @@ protected void assertCallback(Operation op) {
assertSame(genericCallback, op.getCallback());
}

protected void assertMutationCallback(Operation op) {
assertSame(genericMutationCallback, op.getCallback());
}

private void assertBytes(byte[] bytes) {
assertTrue(Arrays.equals(testData, bytes));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public void testMutatorOperationIncrCloning() {
long def = 28775;
int by = 7735;
MutatorOperation op = ofact.mutate(Mutator.incr, TEST_KEY, by, def,
exp, genericCallback);
exp, genericMutationCallback);

MutatorOperation op2 = cloneOne(MutatorOperation.class, op);
assertKey(op2);
assertEquals(-1, op2.getExpiration());
assertEquals(-1, op2.getDefault());
assertEquals(by, op2.getBy());
assertSame(Mutator.incr, op2.getType());
assertCallback(op2);
assertMutationCallback(op2);
}

@Override
Expand All @@ -35,15 +35,15 @@ public void testMutatorOperationDecrCloning() {
long def = 28775;
int by = 7735;
MutatorOperation op = ofact.mutate(Mutator.decr, TEST_KEY, by, def,
exp, genericCallback);
exp, genericMutationCallback);

MutatorOperation op2 = cloneOne(MutatorOperation.class, op);
assertKey(op2);
assertEquals(-1, op2.getExpiration());
assertEquals(-1, op2.getDefault());
assertEquals(by, op2.getBy());
assertSame(Mutator.decr, op2.getType());
assertCallback(op2);
assertMutationCallback(op2);
}

}
18 changes: 17 additions & 1 deletion src/test/manual/net/spy/memcached/MultibyteKeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import net.spy.memcached.ops.GetAttrOperation;
import net.spy.memcached.ops.GetsOperation;
import net.spy.memcached.ops.Mutator;
import net.spy.memcached.ops.MutatorOperation;
import net.spy.memcached.ops.OperationCallback;
import net.spy.memcached.ops.OperationStatus;
import net.spy.memcached.ops.StoreType;
Expand Down Expand Up @@ -273,7 +274,22 @@ public void complete() {
@Test
public void MutatorOperationImplTest() {
try {
opFact.mutate(Mutator.incr, MULTIBYTE_KEY, 1, 1L, 0, genericCallback).initialize();
opFact.mutate(Mutator.incr, MULTIBYTE_KEY, 1, 1L, 0, new MutatorOperation.Callback() {
@Override
public void gotMutatedValue(Long result) {

}

@Override
public void receivedStatus(OperationStatus status) {

}

@Override
public void complete() {

}
}).initialize();
} catch (java.nio.BufferOverflowException e) {
Assert.fail();
}
Expand Down