Search in sources :

Example 1 with MemcacheGetResponse

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

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

the class MemcacheServiceImplTest method expectDeserializationExceptionOnGet.

private void expectDeserializationExceptionOnGet() {
    String key = "deserialize_error_key";
    byte[] pbKey = makePbKey(key);
    Object value = new DeserializationExceptionGenerator();
    MemcacheGetRequest request = MemcacheGetRequest.newBuilder().setNameSpace("").addKey(ByteString.copyFrom(pbKey)).build();
    MemcacheGetResponse response = MemcacheGetResponse.newBuilder().addItem(MemcacheGetResponse.Item.newBuilder().setKey(ByteString.copyFrom(pbKey)).setFlags(Flag.OBJECT.ordinal()).setValue(ByteString.copyFrom(serialize(value).value))).build();
    expectAsyncCall("Get", request, response);
}
Also used : MemcacheGetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetResponse) MemcacheGetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest) ByteString(com.google.protobuf.ByteString)

Example 3 with MemcacheGetResponse

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

the class MemcacheServiceImplTest method oneGetTest.

private void oneGetTest(MemcacheService memcache, String namespace) {
    String[] keys = { ONE, TWO, null, null };
    String[] values = { ONE, null, TWO, null };
    for (int i = 0; i < keys.length; i++) {
        String key = keys[i];
        String value = values[i];
        byte[] pbKey = makePbKey(key);
        MemcacheGetRequest request = MemcacheGetRequest.newBuilder().setNameSpace(namespace).addKey(ByteString.copyFrom(pbKey)).build();
        MemcacheGetResponse response = MemcacheGetResponse.newBuilder().addItem(MemcacheGetResponse.Item.newBuilder().setKey(ByteString.copyFrom(pbKey)).setFlags(value == null ? Flag.OBJECT.ordinal() : Flag.UTF8.ordinal()).setValue(ByteString.copyFrom(serialize(value).value))).build();
        expectAsyncCall("Get", request, response);
        assertThat(memcache.get(key)).isEqualTo(value);
    }
}
Also used : MemcacheGetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetResponse) MemcacheGetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest) ByteString(com.google.protobuf.ByteString)

Example 4 with MemcacheGetResponse

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

the class MemcacheServiceImplTest method containsTest.

private void containsTest(MemcacheService memcache, String namespace) {
    String[] keys = { ONE, null };
    for (String key : keys) {
        byte[] oneKey = makePbKey(key);
        MemcacheGetRequest request = MemcacheGetRequest.newBuilder().setNameSpace(namespace).addKey(ByteString.copyFrom(oneKey)).build();
        MemcacheGetResponse response = MemcacheGetResponse.newBuilder().addItem(MemcacheGetResponse.Item.newBuilder().setFlags(Flag.UTF8.ordinal()).setKey(ByteString.copyFrom(oneKey)).setValue(ByteString.copyFrom(serialize(key).value))).build();
        expectAsyncCall("Get", request, response);
        assertThat(memcache.contains(key)).isTrue();
        verifyAsyncCall("Get", request);
    }
}
Also used : MemcacheGetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetResponse) MemcacheGetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest) ByteString(com.google.protobuf.ByteString)

Example 5 with MemcacheGetResponse

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

the class AsyncMemcacheServiceImpl method doGetAll.

private <K, V> Future<Map<K, V>> doGetAll(Collection<K> keys, boolean forCas, String errorText, Transformer<KeyValuePair<K, MemcacheGetResponse.Item>, V> responseTransformer, Provider<Map<K, V>> defaultValue) {
    MemcacheGetRequest.Builder requestBuilder = MemcacheGetRequest.newBuilder();
    requestBuilder.setNameSpace(getEffectiveNamespace());
    final Map<ByteString, K> byteStringToKey = new HashMap<ByteString, K>(keys.size(), 1);
    for (K key : keys) {
        ByteString pbKey = makePbKey(key);
        byteStringToKey.put(pbKey, key);
        requestBuilder.addKey(pbKey);
    }
    if (forCas) {
        requestBuilder.setForCas(forCas);
    }
    Transformer<MemcacheGetResponse, Map<K, V>> rpcResponseTransformer = new GetAllRpcResponseTransformer<>(byteStringToKey, responseTransformer);
    return makeAsyncCall("Get", requestBuilder.build(), createRpcResponseHandler(MemcacheGetResponse.getDefaultInstance(), errorText, rpcResponseTransformer), defaultValue);
}
Also used : MemcacheGetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetResponse) MemcacheGetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ByteString(com.google.protobuf.ByteString) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

MemcacheGetRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest)9 MemcacheGetResponse (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetResponse)9 ByteString (com.google.protobuf.ByteString)8 IdentifiableValueImpl (com.google.appengine.api.memcache.AsyncMemcacheServiceImpl.IdentifiableValueImpl)3 MemcacheSetRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest)2 MemcacheSetResponse (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 GetAccessTokenRequest (com.google.appengine.api.appidentity.AppIdentityServicePb.GetAccessTokenRequest)1 GetAccessTokenResponse (com.google.appengine.api.appidentity.AppIdentityServicePb.GetAccessTokenResponse)1 ValueAndFlags (com.google.appengine.api.memcache.MemcacheSerialization.ValueAndFlags)1 IdentifiableValue (com.google.appengine.api.memcache.MemcacheService.IdentifiableValue)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1