Search in sources :

Example 1 with CasValues

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());
}
Also used : CasValues(com.google.appengine.api.memcache.MemcacheService.CasValues) ArrayList(java.util.ArrayList) ValueAndFlags(com.google.appengine.api.memcache.MemcacheSerialization.ValueAndFlags) MemcacheSetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with CasValues

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());
}
Also used : IdentifiableValue(com.google.appengine.api.memcache.MemcacheService.IdentifiableValue) CasValues(com.google.appengine.api.memcache.MemcacheService.CasValues) EqualsTester(com.google.common.testing.EqualsTester) IdentifiableValueImpl(com.google.appengine.api.memcache.AsyncMemcacheServiceImpl.IdentifiableValueImpl) Test(org.junit.Test)

Example 3 with CasValues

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);
}
Also used : MemcacheSetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse) CasValues(com.google.appengine.api.memcache.MemcacheService.CasValues) IdentifiableValueImpl(com.google.appengine.api.memcache.AsyncMemcacheServiceImpl.IdentifiableValueImpl) ValueAndFlags(com.google.appengine.api.memcache.MemcacheSerialization.ValueAndFlags) ByteString(com.google.protobuf.ByteString) Date(java.util.Date) MemcacheSetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

CasValues (com.google.appengine.api.memcache.MemcacheService.CasValues)3 IdentifiableValueImpl (com.google.appengine.api.memcache.AsyncMemcacheServiceImpl.IdentifiableValueImpl)2 ValueAndFlags (com.google.appengine.api.memcache.MemcacheSerialization.ValueAndFlags)2 MemcacheSetRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest)2 LinkedHashMap (java.util.LinkedHashMap)2 IdentifiableValue (com.google.appengine.api.memcache.MemcacheService.IdentifiableValue)1 MemcacheSetResponse (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse)1 EqualsTester (com.google.common.testing.EqualsTester)1 ByteString (com.google.protobuf.ByteString)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 Test (org.junit.Test)1