Search in sources :

Example 1 with ValueAndFlags

use of com.google.appengine.api.memcache.MemcacheSerialization.ValueAndFlags 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 ValueAndFlags

use of com.google.appengine.api.memcache.MemcacheSerialization.ValueAndFlags in project appengine-java-standard by GoogleCloudPlatform.

the class AsyncMemcacheServiceImpl method doPut.

/**
 * Note: non-null oldValue implies Compare-and-Swap operation.
 */
private Future<Boolean> doPut(Object key, IdentifiableValue oldValue, Object value, Expiration expires, MemcacheSetRequest.SetPolicy policy) {
    MemcacheSetRequest.Builder requestBuilder = MemcacheSetRequest.newBuilder();
    requestBuilder.setNameSpace(getEffectiveNamespace());
    MemcacheSetRequest.Item.Builder itemBuilder = MemcacheSetRequest.Item.newBuilder();
    ValueAndFlags vaf = serializeValue(value);
    itemBuilder.setValue(ByteString.copyFrom(vaf.value));
    itemBuilder.setFlags(vaf.flags.ordinal());
    itemBuilder.setKey(makePbKey(key));
    itemBuilder.setExpirationTime(expires == null ? 0 : expires.getSecondsValue());
    itemBuilder.setSetPolicy(policy);
    if (policy == MemcacheSetRequest.SetPolicy.CAS) {
        if (oldValue == null) {
            throw new IllegalArgumentException("oldValue must not be null.");
        }
        if (!(oldValue instanceof IdentifiableValueImpl)) {
            throw new IllegalArgumentException("oldValue is an instance of an unapproved IdentifiableValue implementation.  " + "Perhaps you implemented your own version of IdentifiableValue?  " + "If so, don't do this.");
        }
        itemBuilder.setCasId(((IdentifiableValueImpl) oldValue).getCasId());
    }
    final int itemSize = itemBuilder.getKey().size() + itemBuilder.getValue().size();
    requestBuilder.addItem(itemBuilder);
    // When creating string for logging truncate with ellipsis if necessary.
    String valueAsString = Ascii.truncate(String.valueOf(value), MAX_LOGGED_VALUE_SIZE, "...");
    return makeAsyncCall("Set", requestBuilder.build(), createRpcResponseHandlerForPut(Arrays.asList(itemBuilder), requestBuilder.getNameSpace(), MemcacheSetResponse.getDefaultInstance(), String.format("Memcache put: exception setting 1 key (%s) to '%s'", key, valueAsString), new PutResponseTransformer(key, itemSize)), DefaultValueProviders.falseValue());
}
Also used : ValueAndFlags(com.google.appengine.api.memcache.MemcacheSerialization.ValueAndFlags) ByteString(com.google.protobuf.ByteString) MemcacheSetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest)

Example 3 with ValueAndFlags

use of com.google.appengine.api.memcache.MemcacheSerialization.ValueAndFlags 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

ValueAndFlags (com.google.appengine.api.memcache.MemcacheSerialization.ValueAndFlags)3 MemcacheSetRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest)3 CasValues (com.google.appengine.api.memcache.MemcacheService.CasValues)2 ByteString (com.google.protobuf.ByteString)2 LinkedHashMap (java.util.LinkedHashMap)2 IdentifiableValueImpl (com.google.appengine.api.memcache.AsyncMemcacheServiceImpl.IdentifiableValueImpl)1 MemcacheSetResponse (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse)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