Search in sources :

Example 6 with ValueWrapper

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

the class DecrementCommand 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 "decr".equals(firstLineElements[0]);
    String key = firstLineElements[1];
    String decrByStr = stripNewline(firstLineElements[2]);
    Long decrBy = Long.parseLong(decrByStr);
    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 - decrBy;
        newVal.putLong(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 7 with ValueWrapper

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

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

the class PrependCommand method processStorageCommand.

@Override
public ByteBuffer processStorageCommand(String key, byte[] value, int flags, Cache cache) {
    Region<Object, ValueWrapper> r = getMemcachedRegion(cache);
    ValueWrapper oldValWrapper = r.get(key);
    String retVal = Reply.NOT_FOUND.toString();
    if (oldValWrapper != null) {
        byte[] oldVal = oldValWrapper.getValue();
        byte[] prependVal = (byte[]) value;
        byte[] newVal = new byte[oldVal.length + prependVal.length];
        System.arraycopy(prependVal, 0, newVal, 0, prependVal.length);
        System.arraycopy(oldVal, 0, newVal, prependVal.length, oldVal.length);
        r.put(key, ValueWrapper.getWrappedValue(newVal, flags));
        retVal = Reply.STORED.toString();
    }
    return asciiCharset.encode(retVal);
}
Also used : ValueWrapper(org.apache.geode.internal.memcached.ValueWrapper)

Example 9 with ValueWrapper

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

the class ReplaceCommand method processBinaryStorageCommand.

@Override
public ByteBuffer processBinaryStorageCommand(Object key, byte[] value, long cas, int flags, Cache cache, RequestReader request) {
    ByteBuffer response = request.getResponse();
    Region<Object, ValueWrapper> r = getMemcachedRegion(cache);
    ValueWrapper val = ValueWrapper.getWrappedValue(value, flags);
    boolean success = false;
    try {
        if (cas != 0L) {
            ValueWrapper expected = ValueWrapper.getDummyValue(cas);
            success = r.replace(key, expected, val);
        } else {
            success = r.replace(key, val) != null;
        }
    } catch (Exception e) {
        return handleBinaryException(key, request, response, "replace", e);
    }
    if (getLogger().fineEnabled()) {
        getLogger().fine("replace:key:" + key + " cas:" + cas + " success:" + success);
    }
    // set status
    if (success) {
        if (isQuiet()) {
            return null;
        }
        response.putShort(POSITION_RESPONSE_STATUS, ResponseStatus.NO_ERROR.asShort());
        response.putLong(POSITION_CAS, val.getVersion());
    } else {
        if (cas != 0L) {
            response.putShort(POSITION_RESPONSE_STATUS, ResponseStatus.KEY_EXISTS.asShort());
        } else {
            response.putShort(POSITION_RESPONSE_STATUS, ResponseStatus.KEY_NOT_FOUND.asShort());
        }
    // set CAS
    // response.putLong(POSITION_CAS, val.getVersion());
    }
    return response;
}
Also used : ValueWrapper(org.apache.geode.internal.memcached.ValueWrapper) ByteBuffer(java.nio.ByteBuffer)

Example 10 with ValueWrapper

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

the class SetCommand method processBinaryStorageCommand.

@Override
public ByteBuffer processBinaryStorageCommand(Object key, byte[] value, long cas, int flags, Cache cache, RequestReader request) {
    ByteBuffer response = request.getResponse();
    Region<Object, ValueWrapper> r = getMemcachedRegion(cache);
    ValueWrapper val = ValueWrapper.getWrappedValue(value, flags);
    boolean success = true;
    try {
        if (cas != 0L) {
            ValueWrapper expected = ValueWrapper.getDummyValue(cas);
            success = r.replace(key, expected, val);
        } else {
            r.put(key, val);
        }
        if (getLogger().fineEnabled()) {
            getLogger().fine("set key:" + key + " succedded:" + success);
        }
        if (success) {
            if (isQuiet()) {
                return null;
            }
            response.putShort(POSITION_RESPONSE_STATUS, ResponseStatus.NO_ERROR.asShort());
            response.putLong(POSITION_CAS, val.getVersion());
        } else {
            response.putShort(POSITION_RESPONSE_STATUS, ResponseStatus.KEY_EXISTS.asShort());
        }
    } catch (Exception e) {
        response = handleBinaryException(key, request, response, "set", e);
    }
    return response;
}
Also used : 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