Search in sources :

Example 1 with ValueWrapper

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

the class FlushAllCommand 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 "flush_all".equals(stripNewline(firstLineElements[0]));
    boolean noReply = false;
    int delay = 0;
    if (firstLineElements.length == 2) {
        if ("noreply".equals(stripNewline(firstLineElements[1]))) {
            noReply = true;
        } else {
            delay = Integer.parseInt(stripNewline(firstLineElements[1]));
        }
    } else if (firstLineElements.length == 3) {
        delay = Integer.parseInt(stripNewline(firstLineElements[1]));
        noReply = true;
    }
    final Region<Object, ValueWrapper> r = getMemcachedRegion(cache);
    if (delay == 0) {
        r.destroyRegion();
    } else {
        StorageCommand.getExpiryExecutor().schedule(new Runnable() {

            public void run() {
                r.destroyRegion();
            }
        }, delay, TimeUnit.SECONDS);
    }
    CharBuffer retVal = CharBuffer.wrap(Reply.OK.toString());
    return noReply ? null : asciiCharset.encode(retVal);
}
Also used : ValueWrapper(org.apache.geode.internal.memcached.ValueWrapper) CharBuffer(java.nio.CharBuffer)

Example 2 with ValueWrapper

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

the class FlushAllCommand method processBinaryCommand.

private ByteBuffer processBinaryCommand(RequestReader request, Cache cache) {
    ByteBuffer buffer = request.getRequest();
    final Region<Object, ValueWrapper> r = getMemcachedRegion(cache);
    int delay = 0;
    int extraLength = buffer.get(EXTRAS_LENGTH_INDEX);
    buffer.position(HEADER_LENGTH);
    if (extraLength != 0) {
        delay = buffer.getInt();
    }
    if (getLogger().fineEnabled()) {
        cache.getLogger().fine("flush:delay:" + delay);
    }
    if (delay == 0) {
        try {
            r.destroyRegion();
        } catch (Exception e) {
            return handleBinaryException("", request, request.getResponse(), "flushall", e);
        }
    } else {
        StorageCommand.getExpiryExecutor().schedule(new Runnable() {

            @Override
            public void run() {
                r.destroyRegion();
            }
        }, delay, TimeUnit.SECONDS);
    }
    ByteBuffer response = request.getResponse();
    response.putShort(POSITION_RESPONSE_STATUS, ResponseStatus.NO_ERROR.asShort());
    return isQuiet() ? null : response;
}
Also used : ValueWrapper(org.apache.geode.internal.memcached.ValueWrapper) ByteBuffer(java.nio.ByteBuffer)

Example 3 with ValueWrapper

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

the class GetCommand method composeReply.

@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "NP_NULL_PARAM_DEREF", justification = "findbugs complains that v is null while putting into buffer, but it is not")
private ByteBuffer composeReply(Map<Object, ValueWrapper> results, boolean isGets) {
    Iterator<Entry<Object, ValueWrapper>> it = results.entrySet().iterator();
    ByteBuffer buffer = getReplyBuffer();
    while (it.hasNext()) {
        Entry<Object, ValueWrapper> e = it.next();
        if (getLogger().fineEnabled()) {
            getLogger().fine("get compose reply:" + e);
        }
        ValueWrapper valWrapper = e.getValue();
        if (valWrapper != null) {
            byte[] v = valWrapper.getValue();
            CharBuffer reply = getLineBuffer();
            reply.put(VALUE).put(W_SPACE);
            reply.put(e.getKey().toString()).put(W_SPACE);
            // flags
            reply.put(Integer.toString(valWrapper.getFlags())).put(W_SPACE);
            String valBytes = v == null ? Integer.toString(0) : Integer.toString(v.length);
            reply.put(valBytes);
            if (isGets) {
                // send the version for gets command
                reply.put(W_SPACE);
                reply.put(Long.toString(valWrapper.getVersion()));
            }
            reply.put(RN);
            reply.flip();
            getAsciiEncoder().encode(reply, buffer, false);
            // put the actual value
            buffer.put(v);
            RN_BUF.rewind();
            buffer.put(RN_BUF);
        }
    }
    END_BUF.rewind();
    buffer.put(END_BUF);
    buffer.flip();
    return buffer;
}
Also used : Entry(java.util.Map.Entry) ValueWrapper(org.apache.geode.internal.memcached.ValueWrapper) CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 4 with ValueWrapper

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

the class GetCommand method processAsciiCommand.

private ByteBuffer processAsciiCommand(RequestReader request, Cache cache) {
    ByteBuffer buffer = request.getRequest();
    CharBuffer flb = getFirstLineBuffer();
    getAsciiDecoder().reset();
    getAsciiDecoder().decode(buffer, flb, false);
    flb.flip();
    String firstLine = getFirstLine();
    String[] firstLineElements = firstLine.split(" ");
    boolean isGets = firstLineElements[0].equals("gets");
    Set<String> keys = new HashSet<String>();
    for (int i = 1; i < firstLineElements.length; i++) {
        keys.add(stripNewline(firstLineElements[i]));
    }
    Region<Object, ValueWrapper> r = getMemcachedRegion(cache);
    Map<Object, ValueWrapper> results = r.getAll(keys);
    return composeReply(results, isGets);
}
Also used : ValueWrapper(org.apache.geode.internal.memcached.ValueWrapper) CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer) HashSet(java.util.HashSet)

Example 5 with ValueWrapper

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

the class AppendCommand 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[] 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);
            ValueWrapper val = ValueWrapper.getWrappedValue(newVal, flags);
            try {
                r.put(key, val);
                if (isQuiet()) {
                    return null;
                }
                response.putShort(POSITION_RESPONSE_STATUS, ResponseStatus.NO_ERROR.asShort());
                response.putLong(POSITION_CAS, val.getVersion());
            } catch (Exception e) {
                response = handleBinaryException(key, request, response, "append", e);
            }
        } else {
            response.putShort(POSITION_RESPONSE_STATUS, ResponseStatus.KEY_NOT_FOUND.asShort());
        }
    } catch (Exception e) {
        response = handleBinaryException(key, request, response, "append", 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