Search in sources :

Example 76 with ByteArrayWrapper

use of org.apache.geode.redis.internal.ByteArrayWrapper 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)

Example 77 with ByteArrayWrapper

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

the class ZScoreExecutor 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.ZSCORE));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    ByteArrayWrapper member = new ByteArrayWrapper(commandElems.get(2));
    checkDataType(key, RedisDataType.REDIS_SORTEDSET, context);
    Region<ByteArrayWrapper, DoubleWrapper> keyRegion = getRegion(context, key);
    if (keyRegion == null) {
        command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
        return;
    }
    DoubleWrapper score = keyRegion.get(member);
    if (score == null) {
        command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
        return;
    }
    command.setResponse(Coder.getBulkStringResponse(context.getByteBufAllocator(), score.toString()));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) DoubleWrapper(org.apache.geode.redis.internal.DoubleWrapper)

Example 78 with ByteArrayWrapper

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

the class ZLexCountExecutor 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.ZLEXCOUNT));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    Region<ByteArrayWrapper, DoubleWrapper> keyRegion = getRegion(context, key);
    checkDataType(key, RedisDataType.REDIS_SORTEDSET, context);
    if (keyRegion == null) {
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), 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;
    }
    int count;
    try {
        count = getCount(key, keyRegion, context, Coder.stringToByteArrayWrapper(startString), Coder.stringToByteArrayWrapper(stopString), minInclusive, maxInclusive);
    } catch (Exception e) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), e.toString()));
        return;
    }
    command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), count));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) DoubleWrapper(org.apache.geode.redis.internal.DoubleWrapper)

Example 79 with ByteArrayWrapper

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

the class ZRangeByLexExecutor method getRange.

private List<ByteArrayWrapper> getRange(ByteArrayWrapper key, Region<ByteArrayWrapper, DoubleWrapper> keyRegion, ExecutionHandlerContext context, ByteArrayWrapper start, ByteArrayWrapper stop, boolean startInclusive, boolean stopInclusive, int offset, int limit) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    if (start.equals("-") && stop.equals("+")) {
        List<ByteArrayWrapper> l = new ArrayList<ByteArrayWrapper>(keyRegion.keySet());
        int size = l.size();
        Collections.sort(l);
        if (limit == 0)
            limit += size;
        l = l.subList(Math.min(size, offset), Math.min(offset + limit, size));
        return l;
    } else if (start.equals("+") || stop.equals("-"))
        return null;
    Query query;
    Object[] params;
    if (start.equals("-")) {
        if (stopInclusive) {
            query = getQuery(key, SortedSetQuery.ZRANGEBYLEXNINFI, context);
        } else {
            query = getQuery(key, SortedSetQuery.ZRANGEBYLEXNINF, context);
        }
        params = new Object[] { stop, INFINITY_LIMIT };
    } else if (stop.equals("+")) {
        if (startInclusive) {
            query = getQuery(key, SortedSetQuery.ZRANGEBYLEXPINFI, context);
        } else {
            query = getQuery(key, SortedSetQuery.ZRANGEBYLEXPINF, context);
        }
        params = new Object[] { start, INFINITY_LIMIT };
    } else {
        if (startInclusive) {
            if (stopInclusive) {
                query = getQuery(key, SortedSetQuery.ZRANGEBYLEXSTISI, context);
            } else {
                query = getQuery(key, SortedSetQuery.ZRANGEBYLEXSTI, context);
            }
        } else {
            if (stopInclusive) {
                query = getQuery(key, SortedSetQuery.ZRANGEBYLEXSI, context);
            } else {
                query = getQuery(key, SortedSetQuery.ZRANGEBYLEX, context);
            }
        }
        params = new Object[] { start, stop, INFINITY_LIMIT };
    }
    if (limit > 0)
        params[params.length - 1] = (limit + offset);
    SelectResults<ByteArrayWrapper> results = (SelectResults<ByteArrayWrapper>) query.execute(params);
    List<ByteArrayWrapper> list = results.asList();
    int size = list.size();
    return list.subList(Math.min(size, offset), size);
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) Query(org.apache.geode.cache.query.Query) SortedSetQuery(org.apache.geode.redis.internal.executor.SortedSetQuery) ArrayList(java.util.ArrayList)

Example 80 with ByteArrayWrapper

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

the class ZRangeByScoreExecutor 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;
    }
    boolean withScores = false;
    byte[] elem4Array = null;
    int offset = 0;
    int limit = -1;
    if (commandElems.size() >= 5) {
        elem4Array = commandElems.get(4);
        String elem4 = Coder.bytesToString(elem4Array);
        int limitIndex = 4;
        if (elem4.equalsIgnoreCase("WITHSCORES")) {
            withScores = true;
            limitIndex++;
        }
        if (commandElems.size() >= limitIndex + 2) {
            String limitString = Coder.bytesToString(commandElems.get(limitIndex));
            if (limitString.equalsIgnoreCase("LIMIT")) {
                try {
                    byte[] offsetArray = commandElems.get(limitIndex + 1);
                    byte[] limitArray = commandElems.get(limitIndex + 2);
                    offset = Coder.bytesToInt(offsetArray);
                    limit = Coder.bytesToInt(limitArray);
                } catch (NumberFormatException e) {
                    command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_NUMERIC));
                    return;
                }
            }
            if (offset < 0 || limit < 0) {
                command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_LIMIT));
                return;
            }
            if (limitIndex == 4 && commandElems.size() >= 8) {
                byte[] lastElemArray = commandElems.get(7);
                String lastString = Coder.bytesToString(lastElemArray);
                if (lastString.equalsIgnoreCase("WITHSCORES")) {
                    withScores = true;
                }
            }
        }
    }
    ByteArrayWrapper key = command.getKey();
    checkDataType(key, RedisDataType.REDIS_SORTEDSET, context);
    Region<ByteArrayWrapper, DoubleWrapper> keyRegion = getRegion(context, key);
    if (keyRegion == null) {
        command.setResponse(Coder.getEmptyArrayResponse(context.getByteBufAllocator()));
        return;
    }
    int startIndex = isReverse() ? 3 : 2;
    int stopIndex = isReverse() ? 2 : 3;
    boolean startInclusive = true;
    boolean stopInclusive = true;
    double start;
    double stop;
    byte[] startArray = commandElems.get(startIndex);
    byte[] stopArray = commandElems.get(stopIndex);
    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;
    }
    Collection<?> list;
    try {
        list = getKeys(key, keyRegion, context, start, stop, startInclusive, stopInclusive, offset, limit);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    if (list == null)
        command.setResponse(Coder.getEmptyArrayResponse(context.getByteBufAllocator()));
    else
        command.setResponse(Coder.zRangeResponse(context.getByteBufAllocator(), list, withScores));
}
Also used : FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) DoubleWrapper(org.apache.geode.redis.internal.DoubleWrapper)

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