Search in sources :

Example 1 with MemcacheIncrementResponse

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

the class MemcacheServiceImplTest method incrementTest.

private void incrementTest(MemcacheService memcache, String namespace, Long initialValue) {
    for (Integer key : new Integer[] { INT_123, null }) {
        byte[] sha1bytes = makePbKey(key);
        MemcacheIncrementRequest.Builder requestBuilder = MemcacheIncrementRequest.newBuilder().setNameSpace(namespace).setKey(ByteString.copyFrom(sha1bytes)).setDelta(17).setDirection(MemcacheIncrementRequest.Direction.DECREMENT);
        if (initialValue != null) {
            requestBuilder.setInitialValue(initialValue);
            requestBuilder.setInitialFlags(MemcacheSerialization.Flag.LONG.ordinal());
        }
        MemcacheIncrementRequest request = requestBuilder.build();
        MemcacheIncrementResponse response = MemcacheIncrementResponse.newBuilder().setNewValue(25).build();
        expectAsyncCall("Increment", request, response);
        if (initialValue == null) {
            assertThat(memcache.increment(key, -17)).isEqualTo(Long.valueOf(25));
        } else {
            assertThat(memcache.increment(key, -17, initialValue)).isEqualTo(Long.valueOf(25));
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MemcacheIncrementRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheIncrementRequest) MemcacheIncrementResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheIncrementResponse)

Example 2 with MemcacheIncrementResponse

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

the class LocalMemcacheService method increment.

public MemcacheIncrementResponse increment(Status status, MemcacheIncrementRequest req) {
    MemcacheIncrementResponse.Builder result = MemcacheIncrementResponse.newBuilder();
    final String namespace = req.getNameSpace();
    final Key key = new Key(req.getKey().toByteArray());
    final long delta = req.getDirection() == Direction.DECREMENT ? -req.getDelta() : req.getDelta();
    synchronized (mockCache) {
        // only increment offers atomicity
        CacheEntry ce = getWithExpiration(namespace, key);
        if (ce == null) {
            if (req.hasInitialValue()) {
                // initial value is considered as uint64 and therefore can never be negative
                BigInteger value = BigInteger.valueOf(req.getInitialValue()).and(UINT64_MAX_VALUE);
                int flags = req.hasInitialFlags() ? req.getInitialFlags() : MemcacheSerialization.Flag.LONG.ordinal();
                ce = new CacheEntry(namespace, key, value.toString().getBytes(), flags, 0);
                internalSet(namespace, key, ce);
            } else {
                stats.recordMiss();
                // with hasNewValue() == false
                return result.build();
            }
        }
        stats.recordHit(ce);
        BigInteger value;
        try {
            value = new BigInteger(new String(ce.value, UTF8));
        } catch (NumberFormatException e) {
            status.setSuccessful(false);
            throw new ApiProxy.ApplicationException(MemcacheServiceError.ErrorCode.INVALID_VALUE_VALUE, "Format error");
        } catch (UnsupportedEncodingException e) {
            throw new ApiProxy.UnknownException(UTF8 + " encoding was not found.");
        }
        if (value.compareTo(UINT64_MAX_VALUE) > 0 || value.signum() < 0) {
            status.setSuccessful(false);
            throw new ApiProxy.ApplicationException(MemcacheServiceError.ErrorCode.INVALID_VALUE_VALUE, "Value to be incremented must be in the range of an unsigned 64-bit number");
        }
        value = value.add(BigInteger.valueOf(delta));
        if (value.signum() < 0) {
            value = UINT64_MIN_VALUE;
        } else if (value.compareTo(UINT64_MAX_VALUE) > 0) {
            value = value.and(UINT64_MAX_VALUE);
        }
        stats.recordDelete(ce);
        try {
            ce.value = value.toString().getBytes(UTF8);
        } catch (UnsupportedEncodingException e) {
            throw new ApiProxy.UnknownException(UTF8 + " encoding was not found.");
        }
        // don't change the flags; it keeps its original size/type
        ce.bytes = key.getBytes().length + ce.value.length;
        Map<Key, CacheEntry> namespaceMap = getOrMakeSubMap(mockCache, namespace);
        namespaceMap.remove(key);
        namespaceMap.put(key, ce);
        stats.recordAdd(ce);
        result.setNewValue(value.longValue());
    }
    status.setSuccessful(true);
    return result.build();
}
Also used : ApiProxy(com.google.apphosting.api.ApiProxy) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteString(com.google.protobuf.ByteString) BigInteger(java.math.BigInteger) MemcacheIncrementResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheIncrementResponse)

Example 3 with MemcacheIncrementResponse

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

the class MemcacheServiceImplTest method testIncrementMissing.

@Test
public void testIncrementMissing() {
    MemcacheIncrementRequest request = MemcacheIncrementRequest.newBuilder().setNameSpace("").setKey(ByteString.copyFrom(makePbKey(INT_123))).setDelta(17).setDirection(MemcacheIncrementRequest.Direction.DECREMENT).build();
    MemcacheIncrementResponse response = MemcacheIncrementResponse.getDefaultInstance();
    expectAsyncCall("Increment", request, response);
    Long result = new MemcacheServiceImpl(null).increment(INT_123, -17);
    assertThat(result).isEqualTo(null);
    verifyAsyncCall("Increment", request);
}
Also used : MemcacheIncrementRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheIncrementRequest) MemcacheIncrementResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheIncrementResponse) Test(org.junit.Test)

Aggregations

MemcacheIncrementResponse (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheIncrementResponse)3 MemcacheIncrementRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheIncrementRequest)2 ApiProxy (com.google.apphosting.api.ApiProxy)1 ByteString (com.google.protobuf.ByteString)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 BigInteger (java.math.BigInteger)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1