Search in sources :

Example 6 with DoubleWrapper

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

the class ZScanExecutor method getIteration.

@SuppressWarnings("unchecked")
@Override
protected List<?> getIteration(Collection<?> list, Pattern matchPattern, int count, int cursor) {
    List<Object> returnList = new ArrayList<Object>();
    int size = list.size();
    int beforeCursor = 0;
    int numElements = 0;
    int i = -1;
    for (Entry<ByteArrayWrapper, DoubleWrapper> entry : (Collection<Entry<ByteArrayWrapper, DoubleWrapper>>) list) {
        ByteArrayWrapper keyWrapper = entry.getKey();
        String key = keyWrapper.toString();
        DoubleWrapper value = entry.getValue();
        i++;
        if (beforeCursor < cursor) {
            beforeCursor++;
            continue;
        } else if (numElements < count) {
            if (matchPattern != null) {
                if (matchPattern.matcher(key).matches()) {
                    returnList.add(keyWrapper);
                    returnList.add(value.toString());
                    numElements++;
                }
            } else {
                returnList.add(keyWrapper);
                returnList.add(value.toString());
                numElements++;
            }
        } else
            break;
    }
    if (i == size - 1)
        returnList.add(0, String.valueOf(0));
    else
        returnList.add(0, String.valueOf(i));
    return returnList;
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) ArrayList(java.util.ArrayList) Collection(java.util.Collection) DoubleWrapper(org.apache.geode.redis.internal.DoubleWrapper)

Example 7 with DoubleWrapper

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

the class ZRemExecutor 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.ZREM));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    checkDataType(key, RedisDataType.REDIS_SORTEDSET, context);
    Region<ByteArrayWrapper, DoubleWrapper> keyRegion = getRegion(context, key);
    if (keyRegion == null) {
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), 0));
        return;
    }
    int numDeletedMembers = 0;
    for (int i = 2; i < commandElems.size(); i++) {
        byte[] memberArray = commandElems.get(i);
        ByteArrayWrapper member = new ByteArrayWrapper(memberArray);
        Object oldVal = keyRegion.remove(member);
        if (oldVal != null)
            numDeletedMembers++;
    }
    if (keyRegion.isEmpty())
        context.getRegionProvider().removeKey(key);
    command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), numDeletedMembers));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) DoubleWrapper(org.apache.geode.redis.internal.DoubleWrapper)

Example 8 with DoubleWrapper

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

the class ZRemRangeByLexExecutor 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.ZREMRANGEBYLEX));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    checkDataType(key, RedisDataType.REDIS_SORTEDSET, context);
    Region<ByteArrayWrapper, DoubleWrapper> keyRegion = getRegion(context, key);
    if (keyRegion == null) {
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), ERROR_NOT_EXISTS));
        return;
    }
    boolean minInclusive = false;
    boolean maxInclusive = false;
    byte[] minArray = commandElems.get(2);
    byte[] maxArray = commandElems.get(3);
    String startString = Coder.bytesToString(minArray);
    String stopString = Coder.bytesToString(maxArray);
    if (minArray[0] == Coder.OPEN_BRACE_ID) {
        startString = startString.substring(1);
        minInclusive = false;
    } else if (minArray[0] == Coder.OPEN_BRACKET_ID) {
        startString = startString.substring(1);
        minInclusive = true;
    } else if (minArray[0] != Coder.HYPHEN_ID) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_ILLEGAL_SYNTAX));
        return;
    }
    if (maxArray[0] == Coder.OPEN_BRACE_ID) {
        stopString = stopString.substring(1);
        maxInclusive = false;
    } else if (maxArray[0] == Coder.OPEN_BRACKET_ID) {
        stopString = stopString.substring(1);
        maxInclusive = true;
    } else if (maxArray[0] != Coder.PLUS_ID) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_ILLEGAL_SYNTAX));
        return;
    }
    Collection<ByteArrayWrapper> removeList;
    try {
        removeList = getRange(key, keyRegion, context, Coder.stringToByteArrayWrapper(startString), Coder.stringToByteArrayWrapper(stopString), minInclusive, maxInclusive);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    int numRemoved = 0;
    for (ByteArrayWrapper entry : removeList) {
        Object oldVal = keyRegion.remove(entry);
        if (oldVal != null)
            numRemoved++;
    }
    command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), numRemoved));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) DoubleWrapper(org.apache.geode.redis.internal.DoubleWrapper)

Example 9 with DoubleWrapper

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

the class ZRemRangeByScoreExecutor 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.ZREMRANGEBYSCORE));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    checkDataType(key, RedisDataType.REDIS_SORTEDSET, context);
    Region<ByteArrayWrapper, DoubleWrapper> keyRegion = getRegion(context, key);
    if (keyRegion == null) {
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), NOT_EXISTS));
        return;
    }
    boolean startInclusive = true;
    boolean stopInclusive = true;
    double start;
    double stop;
    byte[] startArray = commandElems.get(2);
    byte[] stopArray = commandElems.get(3);
    String startString = Coder.bytesToString(startArray);
    String stopString = Coder.bytesToString(stopArray);
    if (startArray[0] == Coder.OPEN_BRACE_ID) {
        startString = startString.substring(1);
        startInclusive = false;
    }
    if (stopArray[0] == Coder.OPEN_BRACE_ID) {
        stopString = stopString.substring(1);
        stopInclusive = false;
    }
    try {
        start = Coder.stringToDouble(startString);
        stop = Coder.stringToDouble(stopString);
    } catch (NumberFormatException e) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_NUMERIC));
        return;
    }
    int numRemoved = 0;
    Collection<?> removeList = null;
    try {
        if (start == Double.NEGATIVE_INFINITY && stop == Double.POSITIVE_INFINITY && startInclusive && stopInclusive) {
            numRemoved = keyRegion.size();
            context.getRegionProvider().removeKey(key);
        } else {
            removeList = getKeys(context, key, keyRegion, start, stop, startInclusive, stopInclusive);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    if (removeList != null) {
        for (Object entry : removeList) {
            ByteArrayWrapper remove = null;
            if (entry instanceof Entry)
                remove = (ByteArrayWrapper) ((Entry<?, ?>) entry).getKey();
            else if (entry instanceof Struct)
                remove = (ByteArrayWrapper) ((Struct) entry).getFieldValues()[0];
            Object oldVal = keyRegion.remove(remove);
            if (oldVal != null)
                numRemoved++;
            if (keyRegion.isEmpty())
                context.getRegionProvider().removeKey(key);
        }
    }
    command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), numRemoved));
}
Also used : Struct(org.apache.geode.cache.query.Struct) Entry(java.util.Map.Entry) ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) DoubleWrapper(org.apache.geode.redis.internal.DoubleWrapper)

Example 10 with DoubleWrapper

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

the class ZScanExecutor method executeCommand.

@SuppressWarnings("unchecked")
@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
    List<byte[]> commandElems = command.getProcessedCommand();
    if (commandElems.size() < 3) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.ZSCAN));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    Region<ByteArrayWrapper, DoubleWrapper> keyRegion = (Region<ByteArrayWrapper, DoubleWrapper>) context.getRegionProvider().getRegion(key);
    checkDataType(key, RedisDataType.REDIS_SORTEDSET, context);
    if (keyRegion == null) {
        command.setResponse(Coder.getScanResponse(context.getByteBufAllocator(), new ArrayList<String>()));
        return;
    }
    byte[] cAr = commandElems.get(2);
    // String cursorString = ResponseToByteEncoder.bytesToString(cAr);
    int cursor = 0;
    Pattern matchPattern = null;
    String globMatchPattern = null;
    int count = DEFUALT_COUNT;
    try {
        cursor = Coder.bytesToInt(cAr);
    } 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);
            if (Coder.bytesToString(bytes).equalsIgnoreCase("MATCH")) {
                bytes = commandElems.get(4);
                globMatchPattern = Coder.bytesToString(bytes);
            } else if (Coder.bytesToString(bytes).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);
            if (Coder.bytesToString(bytes).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<ByteArrayWrapper> returnList = (List<ByteArrayWrapper>) 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) ArrayList(java.util.ArrayList) List(java.util.List) DoubleWrapper(org.apache.geode.redis.internal.DoubleWrapper) PatternSyntaxException(java.util.regex.PatternSyntaxException) HashSet(java.util.HashSet)

Aggregations

ByteArrayWrapper (org.apache.geode.redis.internal.ByteArrayWrapper)16 DoubleWrapper (org.apache.geode.redis.internal.DoubleWrapper)16 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 ArrayList (java.util.ArrayList)2 Struct (org.apache.geode.cache.query.Struct)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 Pattern (java.util.regex.Pattern)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 Region (org.apache.geode.cache.Region)1 Entry (org.apache.geode.cache.Region.Entry)1