use of com.google.appengine.api.memcache.MemcacheService.CasValues in project appengine-java-standard by GoogleCloudPlatform.
the class AsyncMemcacheServiceImpl method doPutAll.
private <T> Future<Set<T>> doPutAll(Map<T, ?> values, Expiration expires, MemcacheSetRequest.SetPolicy policy, String operation) {
MemcacheSetRequest.Builder requestBuilder = MemcacheSetRequest.newBuilder();
requestBuilder.setNameSpace(getEffectiveNamespace());
List<T> requestedKeys = new ArrayList<T>(values.size());
Set<Integer> oversized = new HashSet<>();
int itemIndex = 0;
for (Map.Entry<T, ?> entry : values.entrySet()) {
MemcacheSetRequest.Item.Builder itemBuilder = MemcacheSetRequest.Item.newBuilder();
requestedKeys.add(entry.getKey());
itemBuilder.setKey(makePbKey(entry.getKey()));
ValueAndFlags vaf;
if (policy == MemcacheSetRequest.SetPolicy.CAS) {
CasValues value = (CasValues) entry.getValue();
if (value == null) {
throw new IllegalArgumentException(entry.getKey() + " has a null for CasValues");
}
vaf = serializeValue(value.getNewValue());
if (!(value.getOldValue() instanceof IdentifiableValueImpl)) {
throw new IllegalArgumentException(entry.getKey() + " CasValues has an oldValue instance of an unapproved " + "IdentifiableValue implementation. Perhaps you implemented your own " + "version of IdentifiableValue? If so, don't do this.");
}
itemBuilder.setCasId(((IdentifiableValueImpl) value.getOldValue()).getCasId());
if (value.getExipration() != null) {
itemBuilder.setExpirationTime(value.getExipration().getSecondsValue());
} else {
itemBuilder.setExpirationTime(expires == null ? 0 : expires.getSecondsValue());
}
} else {
vaf = serializeValue(entry.getValue());
itemBuilder.setExpirationTime(expires == null ? 0 : expires.getSecondsValue());
}
itemBuilder.setValue(ByteString.copyFrom(vaf.value));
itemBuilder.setFlags(vaf.flags.ordinal());
itemBuilder.setSetPolicy(policy);
requestBuilder.addItem(itemBuilder);
int itemSize = itemBuilder.getKey().size() + itemBuilder.getValue().size();
if (itemSize > MAX_ITEM_SIZE) {
oversized.add(itemIndex);
}
itemIndex++;
}
return makeAsyncCall("Set", requestBuilder.build(), createRpcResponseHandlerForPut(requestBuilder.getItemBuilderList(), requestBuilder.getNameSpace(), MemcacheSetResponse.getDefaultInstance(), "Memcache " + operation + ": Unknown exception setting " + values.size() + " keys", new PutAllResponseTransformer<>(requestedKeys, oversized)), DefaultValueProviders.<T>emptySet());
}
use of com.google.appengine.api.memcache.MemcacheService.CasValues in project appengine-java-standard by GoogleCloudPlatform.
the class MemcacheServiceImplTest method testCasValues.
@Test
public void testCasValues() {
IdentifiableValue idVal1 = new IdentifiableValueImpl("oldVal1", 1);
CasValues casVal = new CasValues(idVal1, "newVal1");
assertThat(new CasValues(idVal1, "newVal1")).isEqualTo(casVal);
assertThat(new CasValues(idVal1, "newVal1").hashCode()).isEqualTo(casVal.hashCode());
// Explicitly call equals() to guarantee that CasValues.equals() is being invoked.
new EqualsTester().addEqualityGroup(casVal).addEqualityGroup("str3").testEquals();
assertThat(new CasValues(idVal1, null)).isNotEqualTo(casVal);
assertThat(new CasValues(idVal1, null).hashCode()).isNotEqualTo(casVal.hashCode());
IdentifiableValue idVal2 = new IdentifiableValueImpl("oldVal1", 2);
assertThat(new CasValues(idVal2, "newVal1")).isNotEqualTo(casVal);
// Test expiration
Expiration exp1 = Expiration.byDeltaMillis(10);
casVal = new CasValues(idVal1, "newVal1", exp1);
assertThat(new CasValues(idVal1, "newVal1")).isNotEqualTo(casVal);
assertThat(new CasValues(idVal1, "newVal1").hashCode()).isNotEqualTo(casVal.hashCode());
assertThat(new CasValues(idVal1, "newVal1", exp1)).isEqualTo(casVal);
assertThat(new CasValues(idVal1, "newVal1", exp1).hashCode()).isEqualTo(casVal.hashCode());
}
use of com.google.appengine.api.memcache.MemcacheService.CasValues in project appengine-java-standard by GoogleCloudPlatform.
the class MemcacheServiceImplTest method multiPutIfUntouched.
private void multiPutIfUntouched(String ns, Expiration requestExpiration) {
CasValues one = new CasValues(new IdentifiableValueImpl(ONE, 10), "new." + ONE, Expiration.onDate(new Date(200)));
ValueAndFlags oneValueAndFlag = serialize(one.getNewValue());
CasValues two = new CasValues(new IdentifiableValueImpl(TWO, 20), null);
ValueAndFlags twoValueAndFlag = serialize(two.getNewValue());
MemcacheServiceImpl memcache = new MemcacheServiceImpl(null);
MemcacheSetRequest setRequest = MemcacheSetRequest.newBuilder().setNameSpace(ns).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(makePbKey(null))).setValue(ByteString.copyFrom(oneValueAndFlag.value)).setFlags(oneValueAndFlag.flags.ordinal()).setExpirationTime(one.getExipration().getSecondsValue()).setSetPolicy(MemcacheSetRequest.SetPolicy.CAS).setCasId(10)).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(makePbKey("2"))).setValue(ByteString.copyFrom(twoValueAndFlag.value)).setFlags(twoValueAndFlag.flags.ordinal()).setExpirationTime(requestExpiration == null ? 0 : requestExpiration.getSecondsValue()).setSetPolicy(MemcacheSetRequest.SetPolicy.CAS).setCasId(20)).build();
MemcacheSetResponse setResponse = MemcacheSetResponse.newBuilder().addSetStatus(MemcacheSetResponse.SetStatusCode.STORED).addSetStatus(MemcacheSetResponse.SetStatusCode.EXISTS).build();
expectAsyncCall("Set", setRequest, setResponse);
Map<String, CasValues> values = new LinkedHashMap<>();
values.put(null, one);
values.put("2", two);
Set<String> response = memcache.putIfUntouched(values, requestExpiration);
assertThat(response).hasSize(1);
assertThat(response).contains(null);
verifyAsyncCall("Set", setRequest);
}
Aggregations