use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheBatchIncrementResponse in project appengine-java-standard by GoogleCloudPlatform.
the class MemcacheServiceImplTest method testIncrementAllDifferentTypes.
@Test
public void testIncrementAllDifferentTypes() {
MemcacheBatchIncrementRequest.Builder batchRequestBuilder = MemcacheBatchIncrementRequest.newBuilder().setNameSpace("hi");
batchRequestBuilder.addItem(MemcacheIncrementRequest.newBuilder().setKey(ByteString.copyFrom(makePbKey("my key 1"))).setDirection(MemcacheIncrementRequest.Direction.INCREMENT).setDelta(22));
batchRequestBuilder.addItem(MemcacheIncrementRequest.newBuilder().setKey(ByteString.copyFrom(makePbKey(null))).setDirection(MemcacheIncrementRequest.Direction.INCREMENT).setDelta(22));
batchRequestBuilder.addItem(MemcacheIncrementRequest.newBuilder().setKey(ByteString.copyFrom(makePbKey(3L))).setDirection(MemcacheIncrementRequest.Direction.INCREMENT).setDelta(22));
MemcacheBatchIncrementResponse.Builder responseBuilder = MemcacheBatchIncrementResponse.newBuilder();
responseBuilder.addItem(MemcacheIncrementResponse.newBuilder().setNewValue(123).setIncrementStatus(IncrementStatusCode.OK));
responseBuilder.addItem(MemcacheIncrementResponse.newBuilder().setIncrementStatus(IncrementStatusCode.NOT_CHANGED));
responseBuilder.addItem(MemcacheIncrementResponse.newBuilder().setIncrementStatus(IncrementStatusCode.ERROR));
MemcacheBatchIncrementRequest request = batchRequestBuilder.build();
MemcacheBatchIncrementResponse response = responseBuilder.build();
expectAsyncCall("BatchIncrement", request, response);
ArrayList<Object> keys = new ArrayList<>();
keys.add("my key 1");
keys.add(null);
keys.add(3L);
Map<Object, Long> expected = new LinkedHashMap<>();
expected.put("my key 1", 123L);
expected.put(null, null);
expected.put(3L, null);
MemcacheService memcache = new MemcacheServiceImpl("hi");
assertThat(memcache.incrementAll(keys, 22L)).isEqualTo(expected);
verifyAsyncCall("BatchIncrement", request);
}
use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheBatchIncrementResponse in project appengine-java-standard by GoogleCloudPlatform.
the class MemcacheServiceImplTest method testIncrementAllOffsets.
@Test
public void testIncrementAllOffsets() {
MemcacheBatchIncrementRequest.Builder batchRequestBuilder = MemcacheBatchIncrementRequest.newBuilder().setNameSpace("hi");
batchRequestBuilder.addItem(MemcacheIncrementRequest.newBuilder().setKey(ByteString.copyFrom(makePbKey("my key 1"))).setDirection(MemcacheIncrementRequest.Direction.DECREMENT).setDelta(33));
batchRequestBuilder.addItem(MemcacheIncrementRequest.newBuilder().setKey(ByteString.copyFrom(makePbKey("my key 2"))).setDirection(MemcacheIncrementRequest.Direction.INCREMENT).setDelta(22));
batchRequestBuilder.addItem(MemcacheIncrementRequest.newBuilder().setKey(ByteString.copyFrom(makePbKey("my key 3"))).setDirection(MemcacheIncrementRequest.Direction.DECREMENT).setDelta(11));
MemcacheBatchIncrementResponse.Builder responseBuilder = MemcacheBatchIncrementResponse.newBuilder();
responseBuilder.addItem(MemcacheIncrementResponse.newBuilder().setNewValue(123).setIncrementStatus(IncrementStatusCode.OK));
responseBuilder.addItem(MemcacheIncrementResponse.newBuilder().setIncrementStatus(IncrementStatusCode.NOT_CHANGED));
responseBuilder.addItem(MemcacheIncrementResponse.newBuilder().setIncrementStatus(IncrementStatusCode.ERROR));
MemcacheBatchIncrementRequest request = batchRequestBuilder.build();
MemcacheBatchIncrementResponse response = responseBuilder.build();
expectAsyncCall("BatchIncrement", request, response);
Map<String, Long> offsets = new LinkedHashMap<>();
offsets.put("my key 1", -33L);
offsets.put("my key 2", 22L);
offsets.put("my key 3", -11L);
Map<String, Long> expected = new LinkedHashMap<>();
expected.put("my key 1", 123L);
expected.put("my key 2", null);
expected.put("my key 3", null);
MemcacheService memcache = new MemcacheServiceImpl("hi");
assertThat(memcache.incrementAll(offsets)).isEqualTo(expected);
}
use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheBatchIncrementResponse in project appengine-java-standard by GoogleCloudPlatform.
the class LocalMemcacheService method batchIncrement.
public MemcacheBatchIncrementResponse batchIncrement(Status status, MemcacheBatchIncrementRequest batchReq) {
MemcacheBatchIncrementResponse.Builder result = MemcacheBatchIncrementResponse.newBuilder();
String namespace = batchReq.getNameSpace();
synchronized (mockCache) {
// only increment offers atomicity
for (MemcacheIncrementRequest req : batchReq.getItemList()) {
MemcacheIncrementResponse.Builder resp = MemcacheIncrementResponse.newBuilder();
Key key = new Key(req.getKey().toByteArray());
long delta = req.getDelta();
if (req.getDirection() == Direction.DECREMENT) {
delta = -delta;
}
CacheEntry ce = getWithExpiration(namespace, key);
long newvalue;
if (ce == null) {
if (req.hasInitialValue()) {
MemcacheSerialization.ValueAndFlags value;
try {
value = MemcacheSerialization.serialize(req.getInitialValue());
} catch (IOException e) {
throw new ApiProxy.UnknownException("Serialzation error: " + e);
}
ce = new CacheEntry(namespace, key, value.value, value.flags.ordinal(), 0);
} else {
stats.recordMiss();
resp.setIncrementStatus(IncrementStatusCode.NOT_CHANGED);
result.addItem(resp);
continue;
}
}
stats.recordHit(ce);
Long longval;
try {
longval = Long.parseLong(new String(ce.value, UTF8));
} catch (NumberFormatException e) {
resp.setIncrementStatus(IncrementStatusCode.NOT_CHANGED);
result.addItem(resp);
continue;
} catch (UnsupportedEncodingException e) {
resp.setIncrementStatus(IncrementStatusCode.NOT_CHANGED);
result.addItem(resp);
continue;
}
if (longval < 0) {
resp.setIncrementStatus(IncrementStatusCode.NOT_CHANGED);
result.addItem(resp);
continue;
}
newvalue = longval;
newvalue += delta;
if (delta < 0 && newvalue < 0) {
newvalue = 0;
}
stats.recordDelete(ce);
try {
ce.value = Long.toString(newvalue).getBytes(UTF8);
} catch (UnsupportedEncodingException e) {
// Shouldn't happen.
throw new ApiProxy.UnknownException(UTF8 + " encoding was not found.");
}
// don't change the flags; it keeps its original size/type
ce.bytes = key.getBytes().length + ce.value.length;
Map<Key, CacheEntry> namespaceMap = getOrMakeSubMap(mockCache, namespace);
namespaceMap.remove(key);
namespaceMap.put(key, ce);
stats.recordAdd(ce);
resp.setIncrementStatus(IncrementStatusCode.OK);
resp.setNewValue(newvalue);
result.addItem(resp);
}
}
status.setSuccessful(true);
return result.build();
}
use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheBatchIncrementResponse in project appengine-java-standard by GoogleCloudPlatform.
the class MemcacheServiceImplTest method testIncrementAllOffsetsInitialValue.
@Test
public void testIncrementAllOffsetsInitialValue() {
MemcacheBatchIncrementRequest.Builder batchRequestBuilder = MemcacheBatchIncrementRequest.newBuilder().setNameSpace("hi");
batchRequestBuilder.addItem(MemcacheIncrementRequest.newBuilder().setKey(ByteString.copyFrom(makePbKey("my key 1"))).setDirection(MemcacheIncrementRequest.Direction.DECREMENT).setInitialValue(44).setInitialFlags(MemcacheSerialization.Flag.LONG.ordinal()).setDelta(33));
batchRequestBuilder.addItem(MemcacheIncrementRequest.newBuilder().setKey(ByteString.copyFrom(makePbKey("my key 2"))).setDirection(MemcacheIncrementRequest.Direction.INCREMENT).setInitialValue(44).setInitialFlags(MemcacheSerialization.Flag.LONG.ordinal()).setDelta(22));
batchRequestBuilder.addItem(MemcacheIncrementRequest.newBuilder().setKey(ByteString.copyFrom(makePbKey("my key 3"))).setDirection(MemcacheIncrementRequest.Direction.DECREMENT).setInitialValue(44).setInitialFlags(MemcacheSerialization.Flag.LONG.ordinal()).setDelta(11));
MemcacheBatchIncrementResponse.Builder responseBuilder = MemcacheBatchIncrementResponse.newBuilder();
responseBuilder.addItem(MemcacheIncrementResponse.newBuilder().setNewValue(123).setIncrementStatus(IncrementStatusCode.OK));
responseBuilder.addItem(MemcacheIncrementResponse.newBuilder().setIncrementStatus(IncrementStatusCode.NOT_CHANGED));
responseBuilder.addItem(MemcacheIncrementResponse.newBuilder().setIncrementStatus(IncrementStatusCode.ERROR));
MemcacheBatchIncrementRequest request = batchRequestBuilder.build();
MemcacheBatchIncrementResponse response = responseBuilder.build();
expectAsyncCall("BatchIncrement", request, response);
Map<String, Long> offsets = new LinkedHashMap<>();
offsets.put("my key 1", -33L);
offsets.put("my key 2", 22L);
offsets.put("my key 3", -11L);
Map<String, Long> expected = new LinkedHashMap<>();
expected.put("my key 1", 123L);
expected.put("my key 2", null);
expected.put("my key 3", null);
MemcacheService memcache = new MemcacheServiceImpl("hi");
assertThat(memcache.incrementAll(offsets, 44L)).isEqualTo(expected);
verifyAsyncCall("BatchIncrement", request);
}
use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheBatchIncrementResponse in project appengine-java-standard by GoogleCloudPlatform.
the class MemcacheServiceImplTest method testIncrementAll.
@Test
public void testIncrementAll() {
MemcacheBatchIncrementRequest.Builder batchRequestBuilder = MemcacheBatchIncrementRequest.newBuilder().setNameSpace("hi");
batchRequestBuilder.addItem(MemcacheIncrementRequest.newBuilder().setKey(ByteString.copyFrom(makePbKey("my key 1"))).setDirection(MemcacheIncrementRequest.Direction.INCREMENT).setDelta(22));
batchRequestBuilder.addItem(MemcacheIncrementRequest.newBuilder().setKey(ByteString.copyFrom(makePbKey("my key 2"))).setDirection(MemcacheIncrementRequest.Direction.INCREMENT).setDelta(22));
batchRequestBuilder.addItem(MemcacheIncrementRequest.newBuilder().setKey(ByteString.copyFrom(makePbKey("my key 3"))).setDirection(MemcacheIncrementRequest.Direction.INCREMENT).setDelta(22));
MemcacheBatchIncrementResponse.Builder responseBuilder = MemcacheBatchIncrementResponse.newBuilder();
responseBuilder.addItem(MemcacheIncrementResponse.newBuilder().setNewValue(123).setIncrementStatus(IncrementStatusCode.OK));
responseBuilder.addItem(MemcacheIncrementResponse.newBuilder().setIncrementStatus(IncrementStatusCode.NOT_CHANGED));
responseBuilder.addItem(MemcacheIncrementResponse.newBuilder().setIncrementStatus(IncrementStatusCode.ERROR));
MemcacheBatchIncrementRequest request = batchRequestBuilder.build();
MemcacheBatchIncrementResponse response = responseBuilder.build();
expectAsyncCall("BatchIncrement", request, response);
ArrayList<String> keys = new ArrayList<>();
keys.add("my key 1");
keys.add("my key 2");
keys.add("my key 3");
Map<String, Long> expected = new LinkedHashMap<>();
expected.put("my key 1", 123L);
expected.put("my key 2", null);
expected.put("my key 3", null);
MemcacheService memcache = new MemcacheServiceImpl("hi");
assertThat(memcache.incrementAll(keys, 22L)).isEqualTo(expected);
verifyAsyncCall("BatchIncrement", request);
}
Aggregations