Search in sources :

Example 36 with ByteArrayWrapper

use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.

the class SetNXExecutor method executeCommand.

@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
    List<byte[]> commandElems = command.getProcessedCommand();
    Region<ByteArrayWrapper, ByteArrayWrapper> r = context.getRegionProvider().getStringsRegion();
    if (commandElems.size() < 3) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.SETNX));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    checkAndSetDataType(key, context);
    byte[] value = commandElems.get(VALUE_INDEX);
    Object oldValue = r.putIfAbsent(key, new ByteArrayWrapper(value));
    if (oldValue != null)
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), NOT_SET));
    else
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), SET));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper)

Example 37 with ByteArrayWrapper

use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.

the class SetRangeExecutor method executeCommand.

@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
    List<byte[]> commandElems = command.getProcessedCommand();
    Region<ByteArrayWrapper, ByteArrayWrapper> r = context.getRegionProvider().getStringsRegion();
    if (commandElems.size() < 4) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.SETRANGE));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    checkAndSetDataType(key, context);
    ByteArrayWrapper wrapper = r.get(key);
    int offset;
    byte[] value = commandElems.get(3);
    try {
        byte[] offAr = commandElems.get(2);
        offset = Coder.bytesToInt(offAr);
    } catch (NumberFormatException e) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_INT));
        return;
    }
    int totalLength = offset + value.length;
    if (offset < 0 || totalLength > 536870911) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_ILLEGAL_OFFSET));
        return;
    } else if (value.length == 0) {
        int length = wrapper == null ? 0 : wrapper.toBytes().length;
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), length));
        if (wrapper == null)
            context.getRegionProvider().removeKey(key);
        return;
    }
    if (wrapper == null) {
        byte[] bytes = new byte[totalLength];
        System.arraycopy(value, 0, bytes, offset, value.length);
        r.put(key, new ByteArrayWrapper(bytes));
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), bytes.length));
    } else {
        byte[] bytes = wrapper.toBytes();
        int returnLength;
        if (totalLength < bytes.length) {
            System.arraycopy(value, 0, bytes, offset, value.length);
            r.put(key, new ByteArrayWrapper(bytes));
            returnLength = bytes.length;
        } else {
            byte[] newBytes = new byte[totalLength];
            System.arraycopy(bytes, 0, newBytes, 0, bytes.length);
            System.arraycopy(value, 0, newBytes, offset, value.length);
            returnLength = newBytes.length;
            r.put(key, new ByteArrayWrapper(newBytes));
        }
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), returnLength));
    }
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper)

Example 38 with ByteArrayWrapper

use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.

the class StrlenExecutor method executeCommand.

@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
    List<byte[]> commandElems = command.getProcessedCommand();
    Region<ByteArrayWrapper, ByteArrayWrapper> r = context.getRegionProvider().getStringsRegion();
    if (commandElems.size() < 2) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.STRLEN));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    checkDataType(key, RedisDataType.REDIS_STRING, context);
    ByteArrayWrapper valueWrapper = r.get(key);
    if (valueWrapper == null)
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), KEY_DOES_NOT_EXIST));
    else
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), valueWrapper.toBytes().length));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper)

Example 39 with ByteArrayWrapper

use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.

the class DecrExecutor method executeCommand.

@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
    List<byte[]> commandElems = command.getProcessedCommand();
    RegionProvider rC = context.getRegionProvider();
    Region<ByteArrayWrapper, ByteArrayWrapper> r = rC.getStringsRegion();
    ;
    if (commandElems.size() < 2) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.DECR));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    checkAndSetDataType(key, context);
    ByteArrayWrapper valueWrapper = r.get(key);
    if (valueWrapper == null) {
        byte[] newValue = INIT_VALUE_BYTES;
        r.put(key, new ByteArrayWrapper(newValue));
        rC.metaPut(key, RedisDataType.REDIS_STRING);
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), INIT_VALUE_INT));
        return;
    }
    /*
     * Value exists
     */
    String stringValue = valueWrapper.toString();
    Long value;
    try {
        value = Long.parseLong(stringValue);
    } catch (NumberFormatException e) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_VALUE_NOT_USABLE));
        return;
    }
    if (value == Long.MIN_VALUE) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_OVERFLOW));
        return;
    }
    value--;
    stringValue = "" + value;
    r.put(key, new ByteArrayWrapper(Coder.stringToBytes(stringValue)));
    command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), value));
}
Also used : RegionProvider(org.apache.geode.redis.internal.RegionProvider) ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper)

Example 40 with ByteArrayWrapper

use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.

the class GetBitExecutor method executeCommand.

@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
    List<byte[]> commandElems = command.getProcessedCommand();
    Region<ByteArrayWrapper, ByteArrayWrapper> r = context.getRegionProvider().getStringsRegion();
    if (commandElems.size() < 3) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.GETBIT));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    checkAndSetDataType(key, context);
    ByteArrayWrapper wrapper = r.get(key);
    if (wrapper == null) {
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), 0));
        return;
    }
    int bit = 0;
    byte[] bytes = wrapper.toBytes();
    int offset;
    try {
        byte[] offAr = commandElems.get(2);
        offset = Coder.bytesToInt(offAr);
    } catch (NumberFormatException e) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_INT));
        return;
    }
    if (offset < 0)
        offset += bytes.length * 8;
    if (offset < 0 || offset > bytes.length * 8) {
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), 0));
        return;
    }
    int byteIndex = offset / 8;
    offset %= 8;
    if (byteIndex >= bytes.length) {
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), 0));
        return;
    }
    bit = (bytes[byteIndex] & (0x80 >> offset)) >> (7 - offset);
    command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), bit));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper)

Aggregations

ByteArrayWrapper (org.apache.geode.redis.internal.ByteArrayWrapper)92 DoubleWrapper (org.apache.geode.redis.internal.DoubleWrapper)16 ArrayList (java.util.ArrayList)15 Region (org.apache.geode.cache.Region)14 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 RegionProvider (org.apache.geode.redis.internal.RegionProvider)6 Struct (org.apache.geode.cache.query.Struct)5 RedisDataType (org.apache.geode.redis.internal.RedisDataType)5 HyperLogLogPlus (org.apache.geode.internal.hll.HyperLogLogPlus)4 Collection (java.util.Collection)3 List (java.util.List)3 Pattern (java.util.regex.Pattern)3 PatternSyntaxException (java.util.regex.PatternSyntaxException)3 FunctionDomainException (org.apache.geode.cache.query.FunctionDomainException)3 NameResolutionException (org.apache.geode.cache.query.NameResolutionException)3 QueryInvocationTargetException (org.apache.geode.cache.query.QueryInvocationTargetException)3 TypeMismatchException (org.apache.geode.cache.query.TypeMismatchException)3 Random (java.util.Random)2 Set (java.util.Set)2