Search in sources :

Example 46 with ByteArrayWrapper

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

the class HScanExecutor method executeCommand.

@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
    List<byte[]> commandElems = command.getProcessedCommand();
    if (commandElems.size() < 3) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.HSCAN));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    @SuppressWarnings("unchecked") Region<ByteArrayWrapper, ByteArrayWrapper> keyRegion = (Region<ByteArrayWrapper, ByteArrayWrapper>) context.getRegionProvider().getRegion(key);
    checkDataType(key, RedisDataType.REDIS_HASH, context);
    if (keyRegion == null) {
        command.setResponse(Coder.getScanResponse(context.getByteBufAllocator(), new ArrayList<String>()));
        return;
    }
    byte[] cAr = commandElems.get(2);
    String cursorString = Coder.bytesToString(cAr);
    int cursor = 0;
    Pattern matchPattern = null;
    String globMatchPattern = null;
    int count = DEFUALT_COUNT;
    try {
        cursor = Integer.parseInt(cursorString);
    } catch (NumberFormatException e) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_CURSOR));
        return;
    }
    if (cursor < 0) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_CURSOR));
        return;
    }
    if (commandElems.size() > 4) {
        try {
            byte[] bytes = commandElems.get(3);
            String tmp = Coder.bytesToString(bytes);
            if (tmp.equalsIgnoreCase("MATCH")) {
                bytes = commandElems.get(4);
                globMatchPattern = Coder.bytesToString(bytes);
            } else if (tmp.equalsIgnoreCase("COUNT")) {
                bytes = commandElems.get(4);
                count = Coder.bytesToInt(bytes);
            }
        } catch (NumberFormatException e) {
            command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_COUNT));
            return;
        }
    }
    if (commandElems.size() > 6) {
        try {
            byte[] bytes = commandElems.get(5);
            String tmp = Coder.bytesToString(bytes);
            if (tmp.equalsIgnoreCase("MATCH")) {
                bytes = commandElems.get(6);
                globMatchPattern = Coder.bytesToString(bytes);
            } else if (tmp.equalsIgnoreCase("COUNT")) {
                bytes = commandElems.get(6);
                count = Coder.bytesToInt(bytes);
            }
        } catch (NumberFormatException e) {
            command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_COUNT));
            return;
        }
    }
    if (count < 0) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_COUNT));
        return;
    }
    try {
        matchPattern = convertGlobToRegex(globMatchPattern);
    } catch (PatternSyntaxException e) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), RedisConstants.ERROR_ILLEGAL_GLOB));
        return;
    }
    List<Object> returnList = getIteration(new HashSet(keyRegion.entrySet()), matchPattern, count, cursor);
    command.setResponse(Coder.getScanResponse(context.getByteBufAllocator(), returnList));
}
Also used : Pattern(java.util.regex.Pattern) ArrayList(java.util.ArrayList) ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) Region(org.apache.geode.cache.Region) PatternSyntaxException(java.util.regex.PatternSyntaxException) HashSet(java.util.HashSet)

Example 47 with ByteArrayWrapper

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

the class HSetExecutor method executeCommand.

@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
    List<byte[]> commandElems = command.getProcessedCommand();
    if (commandElems.size() < 4) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), getArgsError()));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    Region<ByteArrayWrapper, ByteArrayWrapper> keyRegion = getOrCreateRegion(context, key, RedisDataType.REDIS_HASH);
    byte[] byteField = commandElems.get(FIELD_INDEX);
    ByteArrayWrapper field = new ByteArrayWrapper(byteField);
    byte[] value = commandElems.get(VALUE_INDEX);
    Object oldValue;
    if (onlySetOnAbsent())
        oldValue = keyRegion.putIfAbsent(field, new ByteArrayWrapper(value));
    else
        oldValue = keyRegion.put(field, new ByteArrayWrapper(value));
    if (oldValue == null)
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), NEW_FIELD));
    else
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), EXISTING_FIELD));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper)

Example 48 with ByteArrayWrapper

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

the class HDelExecutor method executeCommand.

@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
    List<byte[]> commandElems = command.getProcessedCommand();
    if (commandElems.size() < 3) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.HDEL));
        return;
    }
    int numDeleted = 0;
    ByteArrayWrapper key = command.getKey();
    checkDataType(key, RedisDataType.REDIS_HASH, context);
    Region<ByteArrayWrapper, ByteArrayWrapper> keyRegion = getRegion(context, key);
    if (keyRegion == null) {
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), numDeleted));
        return;
    }
    for (int i = START_FIELDS_INDEX; i < commandElems.size(); i++) {
        ByteArrayWrapper field = new ByteArrayWrapper(commandElems.get(i));
        Object oldValue = keyRegion.remove(field);
        if (oldValue != null)
            numDeleted++;
    }
    if (keyRegion.isEmpty()) {
        context.getRegionProvider().removeKey(key, RedisDataType.REDIS_HASH);
    }
    command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), numDeleted));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper)

Example 49 with ByteArrayWrapper

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

the class HGetExecutor method executeCommand.

@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
    List<byte[]> commandElems = command.getProcessedCommand();
    if (commandElems.size() < 3) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.HGET));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    checkDataType(key, RedisDataType.REDIS_HASH, context);
    Region<ByteArrayWrapper, ByteArrayWrapper> keyRegion = getRegion(context, key);
    if (keyRegion == null) {
        command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
        return;
    }
    byte[] byteField = commandElems.get(FIELD_INDEX);
    ByteArrayWrapper field = new ByteArrayWrapper(byteField);
    ByteArrayWrapper valueWrapper = keyRegion.get(field);
    if (valueWrapper != null) {
        command.setResponse(Coder.getBulkStringResponse(context.getByteBufAllocator(), valueWrapper.toBytes()));
    } else
        command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper)

Example 50 with ByteArrayWrapper

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

the class HIncrByExecutor method executeCommand.

@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
    List<byte[]> commandElems = command.getProcessedCommand();
    if (commandElems.size() < 4) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.HINCRBY));
        return;
    }
    byte[] incrArray = commandElems.get(INCREMENT_INDEX);
    long increment;
    try {
        increment = Coder.bytesToLong(incrArray);
    } catch (NumberFormatException e) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_INCREMENT_NOT_USABLE));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    Region<ByteArrayWrapper, ByteArrayWrapper> keyRegion = getOrCreateRegion(context, key, RedisDataType.REDIS_HASH);
    byte[] byteField = commandElems.get(FIELD_INDEX);
    ByteArrayWrapper field = new ByteArrayWrapper(byteField);
    /*
     * Put incrememnt as value if field doesn't exist
     */
    ByteArrayWrapper oldValue = keyRegion.get(field);
    if (oldValue == null) {
        keyRegion.put(field, new ByteArrayWrapper(incrArray));
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), increment));
        return;
    }
    /*
     * If the field did exist then increment the field
     */
    long value;
    try {
        value = Long.parseLong(oldValue.toString());
    } catch (NumberFormatException e) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_FIELD_NOT_USABLE));
        return;
    }
    /*
     * Check for overflow
     */
    if ((value >= 0 && increment > (Long.MAX_VALUE - value)) || (value <= 0 && increment < (Long.MIN_VALUE - value))) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_OVERFLOW));
        return;
    }
    value += increment;
    // String newValue = String.valueOf(value);
    keyRegion.put(field, new ByteArrayWrapper(Coder.longToBytes(value)));
    command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), value));
}
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