Search in sources :

Example 31 with ByteArrayWrapper

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

the class IncrByFloatExecutor 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.INCRBYFLOAT));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    checkAndSetDataType(key, context);
    ByteArrayWrapper valueWrapper = r.get(key);
    /*
     * Try increment
     */
    byte[] incrArray = commandElems.get(INCREMENT_INDEX);
    String doub = Coder.bytesToString(incrArray).toLowerCase();
    if (doub.contains("inf") || doub.contains("nan")) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), "Increment would produce NaN or infinity"));
        return;
    } else if (valueWrapper != null && valueWrapper.toString().contains(" ")) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_VALUE_NOT_USABLE));
        return;
    }
    Double increment;
    try {
        increment = Coder.stringToDouble(doub);
    } catch (NumberFormatException e) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_INCREMENT_NOT_USABLE));
        return;
    }
    if (valueWrapper == null) {
        r.put(key, new ByteArrayWrapper(incrArray));
        command.setResponse(Coder.getBulkStringResponse(context.getByteBufAllocator(), increment));
        return;
    }
    /*
     * Value exists
     */
    String stringValue = Coder.bytesToString(valueWrapper.toBytes());
    Double value;
    try {
        value = Coder.stringToDouble(stringValue);
    } catch (NumberFormatException e) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_VALUE_NOT_USABLE));
        return;
    }
    /*
     * Check for overflow
     */
    if (value >= 0 && increment > (Double.MAX_VALUE - value)) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_OVERFLOW));
        return;
    }
    double result = value + increment;
    if (Double.isNaN(result) || Double.isInfinite(result)) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), RedisConstants.ERROR_NAN_INF_INCR));
        return;
    }
    value += increment;
    stringValue = "" + value;
    r.put(key, new ByteArrayWrapper(Coder.stringToBytes(stringValue)));
    command.setResponse(Coder.getBulkStringResponse(context.getByteBufAllocator(), value));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper)

Example 32 with ByteArrayWrapper

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

the class IncrExecutor 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.INCR));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    checkAndSetDataType(key, context);
    ByteArrayWrapper valueWrapper = r.get(key);
    if (valueWrapper == null) {
        byte[] newValue = { Coder.NUMBER_1_BYTE };
        r.put(key, new ByteArrayWrapper(newValue));
        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.MAX_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 : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper)

Example 33 with ByteArrayWrapper

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

the class MSetExecutor 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 || commandElems.size() % 2 == 0) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.MSET));
        return;
    }
    Map<ByteArrayWrapper, ByteArrayWrapper> map = new HashMap<ByteArrayWrapper, ByteArrayWrapper>();
    for (int i = 1; i < commandElems.size(); i += 2) {
        byte[] keyArray = commandElems.get(i);
        ByteArrayWrapper key = new ByteArrayWrapper(keyArray);
        try {
            checkAndSetDataType(key, context);
        } catch (RedisDataTypeMismatchException e) {
            continue;
        }
        byte[] value = commandElems.get(i + 1);
        map.put(key, new ByteArrayWrapper(value));
    }
    r.putAll(map);
    command.setResponse(Coder.getSimpleStringResponse(context.getByteBufAllocator(), SUCCESS));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) HashMap(java.util.HashMap) RedisDataTypeMismatchException(org.apache.geode.redis.internal.RedisDataTypeMismatchException)

Example 34 with ByteArrayWrapper

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

the class MSetNXExecutor 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 || commandElems.size() % 2 == 0) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.MSETNX));
        return;
    }
    boolean hasEntry = false;
    Map<ByteArrayWrapper, ByteArrayWrapper> map = new HashMap<ByteArrayWrapper, ByteArrayWrapper>();
    for (int i = 1; i < commandElems.size(); i += 2) {
        byte[] keyArray = commandElems.get(i);
        ByteArrayWrapper key = new ByteArrayWrapper(keyArray);
        try {
            checkDataType(key, context);
        } catch (RedisDataTypeMismatchException e) {
            hasEntry = true;
            break;
        }
        byte[] value = commandElems.get(i + 1);
        map.put(key, new ByteArrayWrapper(value));
        if (r.containsKey(key)) {
            hasEntry = true;
            break;
        }
    }
    boolean successful = false;
    if (!hasEntry) {
        successful = true;
        for (ByteArrayWrapper k : map.keySet()) {
            try {
                checkAndSetDataType(k, context);
            } catch (RedisDataTypeMismatchException e) {
                successful = false;
                break;
            }
        }
        r.putAll(map);
    }
    if (successful) {
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), SET));
    } else {
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), NOT_SET));
    }
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) HashMap(java.util.HashMap) RedisDataTypeMismatchException(org.apache.geode.redis.internal.RedisDataTypeMismatchException)

Example 35 with ByteArrayWrapper

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

the class SetEXExecutor 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(), getArgsError()));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    byte[] value = commandElems.get(VALUE_INDEX);
    byte[] expirationArray = commandElems.get(2);
    long expiration;
    try {
        expiration = Coder.bytesToLong(expirationArray);
    } catch (NumberFormatException e) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_SECONDS_NOT_A_NUMBER));
        return;
    }
    if (expiration <= 0) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_SECONDS_NOT_LEGAL));
        return;
    }
    if (!timeUnitMillis())
        expiration *= AbstractExecutor.millisInSecond;
    checkAndSetDataType(key, context);
    r.put(key, new ByteArrayWrapper(value));
    context.getRegionProvider().setExpiration(key, expiration);
    command.setResponse(Coder.getSimpleStringResponse(context.getByteBufAllocator(), SUCCESS));
}
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