Search in sources :

Example 1 with KeyWrapper

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

the class DeleteCommand method processBinaryCommand.

private ByteBuffer processBinaryCommand(RequestReader request, Cache cache) {
    ByteBuffer buffer = request.getRequest();
    ByteBuffer response = request.getResponse();
    KeyWrapper key = getKey(buffer, HEADER_LENGTH);
    Region<Object, ValueWrapper> r = getMemcachedRegion(cache);
    try {
        r.destroy(key);
        if (isQuiet()) {
            return null;
        }
        response.putShort(POSITION_RESPONSE_STATUS, ResponseStatus.NO_ERROR.asShort());
    } catch (EntryNotFoundException e) {
        response.putShort(POSITION_RESPONSE_STATUS, ResponseStatus.KEY_NOT_FOUND.asShort());
    } catch (Exception e) {
        response = handleBinaryException(key, request, response, "delete", e);
    }
    if (getLogger().fineEnabled()) {
        getLogger().fine("delete:key:" + key);
    }
    return response;
}
Also used : KeyWrapper(org.apache.geode.internal.memcached.KeyWrapper) ValueWrapper(org.apache.geode.internal.memcached.ValueWrapper) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) ByteBuffer(java.nio.ByteBuffer) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException)

Example 2 with KeyWrapper

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

the class StorageCommand method processBinaryComand.

private ByteBuffer processBinaryComand(RequestReader request, Cache cache) {
    ByteBuffer buffer = request.getRequest();
    int extrasLength = buffer.get(EXTRAS_LENGTH_INDEX);
    int flags = 0, expTime = 0;
    KeyWrapper key = getKey(buffer, HEADER_LENGTH + extrasLength);
    if (extrasLength > 0) {
        assert extrasLength == 8;
        buffer.position(HEADER_LENGTH);
        flags = buffer.getInt();
        expTime = buffer.getInt();
    }
    byte[] value = getValue(buffer);
    long cas = buffer.getLong(POSITION_CAS);
    ByteBuffer retVal = processBinaryStorageCommand(key, value, cas, flags, cache, request);
    if (expTime > 0) {
        scheduleExpiration(key, expTime, cache);
    }
    if (getLogger().fineEnabled()) {
        getLogger().fine("key:" + key);
        getLogger().fine("value:" + Arrays.toString(value));
    }
    return retVal;
}
Also used : KeyWrapper(org.apache.geode.internal.memcached.KeyWrapper) ByteBuffer(java.nio.ByteBuffer)

Example 3 with KeyWrapper

use of org.apache.geode.internal.memcached.KeyWrapper 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 4 with KeyWrapper

use of org.apache.geode.internal.memcached.KeyWrapper 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 5 with KeyWrapper

use of org.apache.geode.internal.memcached.KeyWrapper 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

KeyWrapper (org.apache.geode.internal.memcached.KeyWrapper)6 ByteBuffer (java.nio.ByteBuffer)5 ValueWrapper (org.apache.geode.internal.memcached.ValueWrapper)5 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)1