Search in sources :

Example 16 with ValueWrapper

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

the class DeleteCommand 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 "delete".equals(firstLineElements[0]);
    String key = stripNewline(firstLineElements[1]);
    boolean noReply = firstLineElements.length > 2;
    Region<Object, ValueWrapper> r = getMemcachedRegion(cache);
    String reply = null;
    try {
        r.destroy(key);
        reply = Reply.DELETED.toString();
    } catch (EntryNotFoundException e) {
        reply = Reply.NOT_FOUND.toString();
    }
    return noReply ? null : asciiCharset.encode(reply);
}
Also used : ValueWrapper(org.apache.geode.internal.memcached.ValueWrapper) CharBuffer(java.nio.CharBuffer) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException)

Example 17 with ValueWrapper

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

the class IncrementCommand 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 incrBy = 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 + incrBy;
            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(), "increment", e);
    }
    if (expiration > 0) {
        StorageCommand.getExpiryExecutor().schedule(new Runnable() {

            @Override
            public void run() {
                r.destroy(key);
            }
        }, expiration, TimeUnit.SECONDS);
    }
    if (getLogger().fineEnabled()) {
        getLogger().fine("incr:key:" + key + " incrBy:" + incrBy + " 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)

Example 18 with ValueWrapper

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

the class PrependCommand 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);
    try {
        ValueWrapper oldValWrapper = r.get(key);
        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);
            ValueWrapper val = ValueWrapper.getWrappedValue(newVal, flags);
            r.put(key, val);
            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_NOT_FOUND.asShort());
        }
    } catch (Exception e) {
        response = handleBinaryException(key, request, response, "prepend", e);
    }
    return response;
}
Also used : ValueWrapper(org.apache.geode.internal.memcached.ValueWrapper) ByteBuffer(java.nio.ByteBuffer)

Example 19 with ValueWrapper

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

the class AddCommand 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);
    try {
        Object oldVal = r.putIfAbsent(key, val);
        // set status
        if (oldVal == null) {
            if (getLogger().fineEnabled()) {
                getLogger().fine("added key: " + key);
            }
            if (isQuiet()) {
                return null;
            }
            response.putShort(POSITION_RESPONSE_STATUS, ResponseStatus.NO_ERROR.asShort());
            // set cas
            response.putLong(POSITION_CAS, val.getVersion());
        } else {
            if (getLogger().fineEnabled()) {
                getLogger().fine("key: " + key + " not added as is already exists");
            }
            response.putShort(POSITION_RESPONSE_STATUS, ResponseStatus.KEY_EXISTS.asShort());
        }
    } catch (Exception e) {
        response = handleBinaryException(key, request, response, "add", e);
    }
    return response;
}
Also used : ValueWrapper(org.apache.geode.internal.memcached.ValueWrapper) ByteBuffer(java.nio.ByteBuffer)

Example 20 with ValueWrapper

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

the class AppendCommand 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[] appendVal = (byte[]) value;
        byte[] oldVal = oldValWrapper.getValue();
        byte[] newVal = new byte[oldVal.length + appendVal.length];
        System.arraycopy(oldVal, 0, newVal, 0, oldVal.length);
        System.arraycopy(appendVal, 0, newVal, oldVal.length, appendVal.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)

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