Search in sources :

Example 1 with MemcacheSetResponse

use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse in project appengine-java-standard by GoogleCloudPlatform.

the class LocalMemcacheService method set.

public MemcacheSetResponse set(Status status, MemcacheSetRequest req) {
    MemcacheSetResponse.Builder result = MemcacheSetResponse.newBuilder();
    final String namespace = req.getNameSpace();
    for (int i = 0; i < req.getItemCount(); i++) {
        MemcacheSetRequest.Item item = req.getItem(i);
        Key key = new Key(item.getKey().toByteArray());
        SetPolicy policy = item.getSetPolicy();
        Map<Key, Long> timeoutMap = getOrMakeSubMap(deleteHold, namespace);
        Long timeout = timeoutMap.get(key);
        if (timeout != null && policy == SetPolicy.SET) {
            // A SET operation overrides and clears any timeout that may exist
            timeout = null;
            timeoutMap.remove(key);
        }
        if ((timeout != null && clock.getCurrentTime() < timeout) || (policy == SetPolicy.CAS && !item.hasCasId())) {
            result.addSetStatus(SetStatusCode.NOT_STORED);
            continue;
        }
        synchronized (mockCache) {
            CacheEntry existingEntry = getWithExpiration(namespace, key);
            if ((policy == SetPolicy.REPLACE && existingEntry == null) || (policy == SetPolicy.ADD && existingEntry != null) || (policy == SetPolicy.CAS && existingEntry == null)) {
                result.addSetStatus(SetStatusCode.NOT_STORED);
            } else if (policy == SetPolicy.CAS && (!existingEntry.hasCasId() || existingEntry.getCasId() != item.getCasId())) {
                result.addSetStatus(SetStatusCode.EXISTS);
            } else {
                long expiry = item.hasExpirationTime() ? (long) item.getExpirationTime() : 0L;
                byte[] value = item.getValue().toByteArray();
                int flags = item.getFlags();
                // We create a new cacheEntry every time (rather than updating existing ones) to
                // avoid having to synchronize on reads. (Otherwise a reader on another thread
                // could be exposed to a partially modified entry).
                CacheEntry newEntry = new CacheEntry(namespace, key, value, flags, expiry * 1000);
                internalSet(namespace, key, newEntry);
                result.addSetStatus(SetStatusCode.STORED);
            }
        }
    }
    status.setSuccessful(true);
    return result.build();
}
Also used : MemcacheSetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse) SetPolicy(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest.SetPolicy) AtomicLong(java.util.concurrent.atomic.AtomicLong) ByteString(com.google.protobuf.ByteString) MemcacheSetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest)

Example 2 with MemcacheSetResponse

use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse in project appengine-java-standard by GoogleCloudPlatform.

the class MemcacheServiceImplTest method testPutIfUntouched.

@Test
public void testPutIfUntouched() {
    MemcacheServiceImpl memcache = new MemcacheServiceImpl(null);
    String namespace = "";
    long casId = 5;
    for (String key : new String[] { ONE, null }) {
        byte[] pbKey = makePbKey(key);
        MemcacheSerialization.ValueAndFlags oneVaf = serialize(ONE);
        MemcacheSerialization.ValueAndFlags twoVaf = serialize(TWO);
        MemcacheGetRequest getRequest = MemcacheGetRequest.newBuilder().setNameSpace(namespace).setForCas(true).addKey(ByteString.copyFrom(pbKey)).build();
        MemcacheGetResponse getResponse = MemcacheGetResponse.newBuilder().addItem(MemcacheGetResponse.Item.newBuilder().setFlags(oneVaf.flags.ordinal()).setKey(ByteString.copyFrom(pbKey)).setValue(ByteString.copyFrom(oneVaf.value)).setCasId(casId)).build();
        expectAsyncCall("Get", getRequest, getResponse);
        MemcacheSetRequest setRequest = MemcacheSetRequest.newBuilder().setNameSpace(namespace).addItem(MemcacheSetRequest.Item.newBuilder().setValue(ByteString.copyFrom(twoVaf.value)).setFlags(twoVaf.flags.ordinal()).setKey(ByteString.copyFrom(pbKey)).setExpirationTime(0).setSetPolicy(MemcacheSetRequest.SetPolicy.CAS).setCasId(casId)).build();
        MemcacheSetResponse setResponse = MemcacheSetResponse.newBuilder().addSetStatus(MemcacheSetResponse.SetStatusCode.STORED).build();
        expectAsyncCallWithoutReset("Set", setRequest, setResponse);
        IdentifiableValueImpl v = (IdentifiableValueImpl) memcache.getIdentifiable(key);
        assertThat(v.getCasId()).isEqualTo(casId);
        assertThat(v.getValue()).isEqualTo(ONE);
        assertThat(memcache.putIfUntouched(key, v, TWO)).isTrue();
        verifyAsyncCall("Set", setRequest);
    }
}
Also used : MemcacheSetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse) ValueAndFlags(com.google.appengine.api.memcache.MemcacheSerialization.ValueAndFlags) MemcacheGetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetResponse) MemcacheGetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest) IdentifiableValueImpl(com.google.appengine.api.memcache.AsyncMemcacheServiceImpl.IdentifiableValueImpl) ByteString(com.google.protobuf.ByteString) MemcacheSetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest) Test(org.junit.Test)

Example 3 with MemcacheSetResponse

use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse in project appengine-java-standard by GoogleCloudPlatform.

the class MemcacheServiceImplTest method testSetTooLargeSingleItemError.

@Test
public void testSetTooLargeSingleItemError() {
    MemcacheService memcache = new MemcacheServiceImpl(null);
    String namespace = "";
    NamespaceManager.set(namespace);
    int expirySecs = (int) (MILLISECONDS.toSeconds(System.currentTimeMillis()) + 600);
    SetPolicy setPolicy = SetPolicy.SET_ALWAYS;
    MemcacheSetRequest.SetPolicy realPolicy = MemcacheSetRequest.SetPolicy.SET;
    Expiration expiration = Expiration.onDate(new Date(SECONDS.toMillis(expirySecs)));
    String testkey = "testkey";
    byte[] key = makePbKey(testkey);
    String value = Strings.repeat("v", (AsyncMemcacheServiceImpl.MAX_ITEM_SIZE - key.length + 1));
    int itemSize = value.length() + key.length;
    String expectedMessage = String.format("Memcache put: Item may not be more than %d bytes in " + "length; received %d bytes.", AsyncMemcacheServiceImpl.MAX_ITEM_SIZE, itemSize);
    MemcacheSetRequest request = MemcacheSetRequest.newBuilder().setNameSpace(namespace).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(key)).setFlags(Flag.UTF8.ordinal()).setValue(ByteString.copyFrom(serialize(value).value)).setExpirationTime(expirySecs).setSetPolicy(realPolicy)).build();
    MemcacheSetResponse response = MemcacheSetResponse.newBuilder().addSetStatus(MemcacheSetResponse.SetStatusCode.ERROR).build();
    expectAsyncCall("Set", request, response);
    MemcacheServiceException ex = assertThrows(MemcacheServiceException.class, () -> memcache.put(key, value, expiration, setPolicy));
    assertThat(ex).hasMessageThat().isEqualTo(expectedMessage);
}
Also used : MemcacheSetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse) SetPolicy(com.google.appengine.api.memcache.MemcacheService.SetPolicy) ByteString(com.google.protobuf.ByteString) Date(java.util.Date) MemcacheSetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest) Test(org.junit.Test)

Example 4 with MemcacheSetResponse

use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse in project appengine-java-standard by GoogleCloudPlatform.

the class MemcacheServiceImplTest method testSetTooLargeMultiError.

@Test
public void testSetTooLargeMultiError() {
    MemcacheService memcache = new MemcacheServiceImpl(null);
    String namespace = "";
    NamespaceManager.set(namespace);
    int expirySecs = (int) (MILLISECONDS.toSeconds(System.currentTimeMillis()) + 600);
    SetPolicy setPolicy = SetPolicy.SET_ALWAYS;
    MemcacheSetRequest.SetPolicy realPolicy = MemcacheSetRequest.SetPolicy.SET;
    Expiration expiration = Expiration.onDate(new Date(SECONDS.toMillis(expirySecs)));
    String expectedMessage = "Memcache put: 1 items failed for exceeding " + AsyncMemcacheServiceImpl.MAX_ITEM_SIZE + " bytes; keys: two. ";
    byte[] key1 = makePbKey(ONE);
    byte[] key2 = makePbKey(TWO);
    byte[] key3 = makePbKey(THREE);
    // Only the second item will fail.
    String value1 = Strings.repeat("v", 1000);
    String value2 = Strings.repeat("v", AsyncMemcacheServiceImpl.MAX_ITEM_SIZE - key2.length + 1);
    String value3 = Strings.repeat("v", AsyncMemcacheServiceImpl.MAX_ITEM_SIZE - key3.length);
    MemcacheSetRequest request = MemcacheSetRequest.newBuilder().setNameSpace(namespace).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(key1)).setFlags(Flag.UTF8.ordinal()).setValue(ByteString.copyFrom(serialize(value1).value)).setExpirationTime(expirySecs).setSetPolicy(realPolicy)).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(key2)).setFlags(Flag.UTF8.ordinal()).setValue(ByteString.copyFrom(serialize(value2).value)).setExpirationTime(expirySecs).setSetPolicy(realPolicy)).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(key3)).setFlags(Flag.UTF8.ordinal()).setValue(ByteString.copyFrom(serialize(value3).value)).setExpirationTime(expirySecs).setSetPolicy(realPolicy)).build();
    MemcacheSetResponse.SetStatusCode[] responses = new MemcacheSetResponse.SetStatusCode[] { MemcacheSetResponse.SetStatusCode.STORED, MemcacheSetResponse.SetStatusCode.ERROR, MemcacheSetResponse.SetStatusCode.STORED };
    MemcacheSetResponse response = MemcacheSetResponse.newBuilder().addSetStatus(responses[0]).addSetStatus(responses[1]).addSetStatus(responses[2]).build();
    Map<Serializable, String> collection = ImmutableMap.of(ONE, value1, TWO, value2, THREE, value3);
    expectAsyncCall("Set", request, response);
    MemcacheServiceException ex = assertThrows(MemcacheServiceException.class, () -> memcache.putAll(collection, expiration, setPolicy));
    assertThat(ex).hasMessageThat().isEqualTo(expectedMessage);
}
Also used : MemcacheSetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse) Serializable(java.io.Serializable) SetStatusCode(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse.SetStatusCode) ByteString(com.google.protobuf.ByteString) Date(java.util.Date) MemcacheSetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest) SetPolicy(com.google.appengine.api.memcache.MemcacheService.SetPolicy) Test(org.junit.Test)

Example 5 with MemcacheSetResponse

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

MemcacheSetRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest)8 MemcacheSetResponse (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse)8 ByteString (com.google.protobuf.ByteString)5 Date (java.util.Date)4 Test (org.junit.Test)3 IdentifiableValueImpl (com.google.appengine.api.memcache.AsyncMemcacheServiceImpl.IdentifiableValueImpl)2 ValueAndFlags (com.google.appengine.api.memcache.MemcacheSerialization.ValueAndFlags)2 SetPolicy (com.google.appengine.api.memcache.MemcacheService.SetPolicy)2 MemcacheGetRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest)2 MemcacheGetResponse (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetResponse)2 SetStatusCode (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse.SetStatusCode)2 LinkedHashMap (java.util.LinkedHashMap)2 GetAccessTokenRequest (com.google.appengine.api.appidentity.AppIdentityServicePb.GetAccessTokenRequest)1 GetAccessTokenResponse (com.google.appengine.api.appidentity.AppIdentityServicePb.GetAccessTokenResponse)1 CasValues (com.google.appengine.api.memcache.MemcacheService.CasValues)1 SetPolicy (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest.SetPolicy)1 Serializable (java.io.Serializable)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1