Search in sources :

Example 11 with ValueWrapper

use of org.apache.geode.internal.memcached.ValueWrapper in project geode by apache.

the class TouchCommand method processCommand.

@Override
public ByteBuffer processCommand(RequestReader request, Protocol protocol, Cache cache) {
    assert protocol == Protocol.BINARY;
    int newExpTime = 0;
    ByteBuffer buffer = request.getRequest();
    ByteBuffer response = null;
    int extrasLength = buffer.get(EXTRAS_LENGTH_INDEX);
    buffer.position(HEADER_LENGTH);
    if (extrasLength > 0) {
        assert extrasLength == 4;
        newExpTime = buffer.getInt();
    }
    KeyWrapper key = getKey(buffer, HEADER_LENGTH + extrasLength);
    if (newExpTime > 0) {
        StorageCommand.rescheduleExpiration(cache, key, newExpTime);
    }
    if (sendValue()) {
        Region<Object, ValueWrapper> r = getMemcachedRegion(cache);
        ValueWrapper val = null;
        try {
            val = r.get(key);
        } catch (Exception e) {
            return handleBinaryException(key, request, response, "touch", e);
        }
        if (val == null) {
            response = request.getResponse();
            response.putShort(POSITION_RESPONSE_STATUS, ResponseStatus.KEY_NOT_FOUND.asShort());
        } else {
            if (isQuiet()) {
                return null;
            }
            byte[] realValue = val.getValue();
            int responseLength = HEADER_LENGTH + realValue.length;
            response = request.getResponse(responseLength);
            response.limit(responseLength);
            response.putShort(POSITION_RESPONSE_STATUS, ResponseStatus.NO_ERROR.asShort());
            response.put(EXTRAS_LENGTH_INDEX, (byte) EXTRAS_LENGTH);
            response.putInt(TOTAL_BODY_LENGTH_INDEX, EXTRAS_LENGTH + realValue.length);
            response.putLong(POSITION_CAS, val.getVersion());
            response.position(HEADER_LENGTH);
            response.putInt(val.getFlags());
            response.put(realValue);
            response.flip();
        }
    }
    return response;
}
Also used : KeyWrapper(org.apache.geode.internal.memcached.KeyWrapper) ValueWrapper(org.apache.geode.internal.memcached.ValueWrapper) ByteBuffer(java.nio.ByteBuffer)

Example 12 with ValueWrapper

use of org.apache.geode.internal.memcached.ValueWrapper in project geode by apache.

the class GetCommand method processBinaryCommand.

protected ByteBuffer processBinaryCommand(ByteBuffer buffer, RequestReader request, Cache cache, ByteBuffer response) {
    Region<Object, ValueWrapper> r = getMemcachedRegion(cache);
    KeyWrapper key = getKey(buffer, HEADER_LENGTH);
    ValueWrapper val = null;
    try {
        val = r.get(key);
    } catch (Exception e) {
        return handleBinaryException(key, request, response, "get", e);
    }
    if (getLogger().fineEnabled()) {
        getLogger().fine("get:key:" + key + " val:" + val);
    }
    if (val == null) {
        if (isQuiet()) {
            return null;
        }
        response.putShort(POSITION_RESPONSE_STATUS, ResponseStatus.KEY_NOT_FOUND.asShort());
    } else {
        byte[] realValue = val.getValue();
        int responseLength = HEADER_LENGTH + realValue.length + EXTRAS_LENGTH + (sendKeysInResponse() ? key.getKey().length : 0);
        if (response.capacity() < responseLength) {
            response = request.getResponse(responseLength);
        }
        response.limit(responseLength);
        response.putShort(POSITION_RESPONSE_STATUS, ResponseStatus.NO_ERROR.asShort());
        if (sendKeysInResponse()) {
            response.putShort(KEY_LENGTH_INDEX, (short) key.getKey().length);
        }
        response.put(EXTRAS_LENGTH_INDEX, (byte) EXTRAS_LENGTH);
        response.putInt(TOTAL_BODY_LENGTH_INDEX, EXTRAS_LENGTH + realValue.length + (sendKeysInResponse() ? key.getKey().length : 0));
        response.putLong(POSITION_CAS, val.getVersion());
        response.position(HEADER_LENGTH);
        response.putInt(val.getFlags());
        if (sendKeysInResponse()) {
            response.put(key.getKey());
        }
        response.put(realValue);
        response.flip();
    }
    return response;
}
Also used : KeyWrapper(org.apache.geode.internal.memcached.KeyWrapper) ValueWrapper(org.apache.geode.internal.memcached.ValueWrapper)

Example 13 with ValueWrapper

use of org.apache.geode.internal.memcached.ValueWrapper in project geode by apache.

the class IncrementCommand method processAsciiCommand.

private ByteBuffer processAsciiCommand(ByteBuffer buffer, Cache cache) {
    CharBuffer flb = getFirstLineBuffer();
    getAsciiDecoder().reset();
    getAsciiDecoder().decode(buffer, flb, false);
    flb.flip();
    String firstLine = getFirstLine();
    String[] firstLineElements = firstLine.split(" ");
    assert "incr".equals(firstLineElements[0]);
    String key = firstLineElements[1];
    String incrByStr = stripNewline(firstLineElements[2]);
    Long incrBy = Long.parseLong(incrByStr);
    boolean noReply = firstLineElements.length > 3;
    Region<Object, ValueWrapper> r = getMemcachedRegion(cache);
    String reply = Reply.NOT_FOUND.toString();
    ByteBuffer newVal = ByteBuffer.allocate(8);
    while (true) {
        ValueWrapper oldValWrapper = r.get(key);
        if (oldValWrapper == null) {
            break;
        }
        newVal.clear();
        byte[] oldVal = oldValWrapper.getValue();
        long oldLong = getLongFromByteArray(oldVal);
        long newLong = oldLong + incrBy;
        newVal.putLong(0, newLong);
        ValueWrapper newValWrapper = ValueWrapper.getWrappedValue(newVal.array(), 0);
        if (r.replace(key, oldValWrapper, newValWrapper)) {
            reply = newLong + "\r\n";
            break;
        }
    }
    return noReply ? null : asciiCharset.encode(reply);
}
Also used : ValueWrapper(org.apache.geode.internal.memcached.ValueWrapper) CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 14 with ValueWrapper

use of org.apache.geode.internal.memcached.ValueWrapper in project geode by apache.

the class CASCommand method processAsciiCommand.

private ByteBuffer processAsciiCommand(ByteBuffer buffer, Cache cache) {
    CharBuffer flb = getFirstLineBuffer();
    getAsciiDecoder().reset();
    getAsciiDecoder().decode(buffer, flb, false);
    flb.flip();
    String firstLine = getFirstLine();
    String[] firstLineElements = firstLine.split(" ");
    String key = firstLineElements[1];
    int flags = Integer.parseInt(firstLineElements[2]);
    long expTime = Long.parseLong(firstLineElements[3]);
    int numBytes = Integer.parseInt(firstLineElements[4]);
    long casVersion = Long.parseLong(stripNewline(firstLineElements[5]));
    byte[] value = new byte[numBytes];
    buffer.position(firstLine.length());
    for (int i = 0; i < numBytes; i++) {
        value[i] = buffer.get();
    }
    String reply = Reply.EXISTS.toString();
    Region<Object, ValueWrapper> r = getMemcachedRegion(cache);
    ValueWrapper expected = ValueWrapper.getDummyValue(casVersion);
    if (r.replace(key, expected, ValueWrapper.getWrappedValue(value, flags))) {
        reply = Reply.STORED.toString();
    }
    return asciiCharset.encode(reply);
}
Also used : ValueWrapper(org.apache.geode.internal.memcached.ValueWrapper) CharBuffer(java.nio.CharBuffer)

Example 15 with ValueWrapper

use of org.apache.geode.internal.memcached.ValueWrapper in project geode by apache.

the class DecrementCommand method processBinaryProtocol.

private ByteBuffer processBinaryProtocol(RequestReader request, Cache cache) {
    ByteBuffer buffer = request.getRequest();
    int extrasLength = buffer.get(EXTRAS_LENGTH_INDEX);
    final KeyWrapper key = getKey(buffer, HEADER_LENGTH + extrasLength);
    long decrBy = buffer.getLong(HEADER_LENGTH);
    long initialVal = buffer.getLong(HEADER_LENGTH + LONG_LENGTH);
    int expiration = buffer.getInt(HEADER_LENGTH + LONG_LENGTH + LONG_LENGTH);
    final Region<Object, ValueWrapper> r = getMemcachedRegion(cache);
    ByteBuffer newVal = ByteBuffer.allocate(8);
    boolean notFound = false;
    ValueWrapper newValWrapper = null;
    try {
        while (true) {
            ValueWrapper oldValWrapper = r.get(key);
            if (oldValWrapper == null) {
                if (expiration == -1) {
                    notFound = true;
                } else {
                    newVal.putLong(0, initialVal);
                    newValWrapper = ValueWrapper.getWrappedValue(newVal.array(), 0);
                    r.put(key, newValWrapper);
                }
                break;
            }
            byte[] oldVal = oldValWrapper.getValue();
            long oldLong = getLongFromByteArray(oldVal);
            long newLong = oldLong - decrBy;
            if (newLong < 0) {
                newLong = 0;
            }
            newVal.putLong(0, newLong);
            newValWrapper = ValueWrapper.getWrappedValue(newVal.array(), 0);
            if (r.replace(key, oldValWrapper, newValWrapper)) {
                break;
            }
        }
    } catch (Exception e) {
        return handleBinaryException(key, request, request.getResponse(), "decrement", e);
    }
    if (expiration > 0) {
        StorageCommand.getExpiryExecutor().schedule(new Runnable() {

            @Override
            public void run() {
                r.destroy(key);
            }
        }, expiration, TimeUnit.SECONDS);
    }
    if (getLogger().fineEnabled()) {
        getLogger().fine("decr:key:" + key + " decrBy:" + decrBy + " initVal:" + initialVal + " exp:" + expiration + " notFound:" + notFound);
    }
    ByteBuffer response = null;
    if (notFound) {
        response = request.getResponse();
        response.putShort(POSITION_RESPONSE_STATUS, ResponseStatus.KEY_NOT_FOUND.asShort());
    } else {
        if (isQuiet()) {
            return null;
        }
        response = request.getResponse(HEADER_LENGTH + LONG_LENGTH);
        response.putInt(TOTAL_BODY_LENGTH_INDEX, LONG_LENGTH);
        response.putLong(HEADER_LENGTH, newVal.getLong(0));
        response.putLong(POSITION_CAS, newValWrapper.getVersion());
    }
    return response;
}
Also used : KeyWrapper(org.apache.geode.internal.memcached.KeyWrapper) ValueWrapper(org.apache.geode.internal.memcached.ValueWrapper) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ValueWrapper (org.apache.geode.internal.memcached.ValueWrapper)20 ByteBuffer (java.nio.ByteBuffer)14 CharBuffer (java.nio.CharBuffer)7 KeyWrapper (org.apache.geode.internal.memcached.KeyWrapper)5 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)2 HashSet (java.util.HashSet)1 Entry (java.util.Map.Entry)1