Search in sources :

Example 26 with ByteArrayWrapper

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

the class SPopExecutor method executeCommand.

@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
    List<byte[]> commandElems = command.getProcessedCommand();
    if (commandElems.size() < 2) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.SPOP));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    @SuppressWarnings("unchecked") Region<ByteArrayWrapper, Boolean> keyRegion = (Region<ByteArrayWrapper, Boolean>) context.getRegionProvider().getRegion(key);
    if (keyRegion == null || keyRegion.isEmpty()) {
        command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
        return;
    }
    Random rand = new Random();
    ByteArrayWrapper[] entries = keyRegion.keySet().toArray(new ByteArrayWrapper[keyRegion.size()]);
    ByteArrayWrapper pop = entries[rand.nextInt(entries.length)];
    keyRegion.remove(pop);
    if (keyRegion.isEmpty()) {
        context.getRegionProvider().removeKey(key);
    }
    command.setResponse(Coder.getBulkStringResponse(context.getByteBufAllocator(), pop.toBytes()));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) Random(java.util.Random) Region(org.apache.geode.cache.Region)

Example 27 with ByteArrayWrapper

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

the class SRandMemberExecutor method executeCommand.

@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
    List<byte[]> commandElems = command.getProcessedCommand();
    if (commandElems.size() < 2) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.SRANDMEMBER));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    @SuppressWarnings("unchecked") Region<ByteArrayWrapper, Boolean> keyRegion = (Region<ByteArrayWrapper, Boolean>) context.getRegionProvider().getRegion(key);
    int count = 1;
    if (commandElems.size() > 2) {
        try {
            count = Coder.bytesToInt(commandElems.get(2));
        } catch (NumberFormatException e) {
            command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_NUMERIC));
            return;
        }
    }
    if (keyRegion == null || count == 0) {
        command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
        return;
    }
    int members = keyRegion.size();
    if (members <= count && count != 1) {
        command.setResponse(Coder.getBulkStringArrayResponse(context.getByteBufAllocator(), new HashSet<ByteArrayWrapper>(keyRegion.keySet())));
        return;
    }
    Random rand = new Random();
    ByteArrayWrapper[] entries = keyRegion.keySet().toArray(new ByteArrayWrapper[members]);
    if (count == 1) {
        ByteArrayWrapper randEntry = entries[rand.nextInt(entries.length)];
        command.setResponse(Coder.getBulkStringResponse(context.getByteBufAllocator(), randEntry.toBytes()));
    } else if (count > 0) {
        Set<ByteArrayWrapper> randEntries = new HashSet<ByteArrayWrapper>();
        do {
            ByteArrayWrapper s = entries[rand.nextInt(entries.length)];
            randEntries.add(s);
        } while (randEntries.size() < count);
        command.setResponse(Coder.getBulkStringArrayResponse(context.getByteBufAllocator(), randEntries));
    } else {
        count = -count;
        List<ByteArrayWrapper> randEntries = new ArrayList<ByteArrayWrapper>();
        for (int i = 0; i < count; i++) {
            ByteArrayWrapper s = entries[rand.nextInt(entries.length)];
            randEntries.add(s);
        }
        command.setResponse(Coder.getBulkStringArrayResponse(context.getByteBufAllocator(), randEntries));
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) Random(java.util.Random) Region(org.apache.geode.cache.Region) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 28 with ByteArrayWrapper

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

the class SRemExecutor 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.SREM));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    checkDataType(key, RedisDataType.REDIS_SET, context);
    @SuppressWarnings("unchecked") Region<ByteArrayWrapper, Boolean> keyRegion = (Region<ByteArrayWrapper, Boolean>) context.getRegionProvider().getRegion(key);
    if (keyRegion == null) {
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), NONE_REMOVED));
        return;
    }
    int numRemoved = 0;
    for (int i = 2; i < commandElems.size(); i++) {
        Object oldVal;
        oldVal = keyRegion.remove(new ByteArrayWrapper(commandElems.get(i)));
        if (oldVal != null)
            numRemoved++;
    }
    command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), numRemoved));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) Region(org.apache.geode.cache.Region)

Example 29 with ByteArrayWrapper

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

the class LTrimExecutor 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.LTRIM));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    byte[] startArray = commandElems.get(2);
    byte[] stopArray = commandElems.get(3);
    int redisStart;
    int redisStop;
    checkDataType(key, RedisDataType.REDIS_LIST, context);
    Region keyRegion = getRegion(context, key);
    if (keyRegion == null) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_KEY_NOT_EXISTS));
        return;
    }
    int listSize = keyRegion.size() - LIST_EMPTY_SIZE;
    if (listSize == 0) {
        command.setResponse(Coder.getSimpleStringResponse(context.getByteBufAllocator(), SUCCESS));
        return;
    }
    try {
        redisStart = Coder.bytesToInt(startArray);
        redisStop = Coder.bytesToInt(stopArray);
    } catch (NumberFormatException e) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_NUMERIC));
        return;
    }
    redisStart = getBoundedStartIndex(redisStart, listSize);
    redisStop = getBoundedEndIndex(redisStop, listSize);
    redisStart = Math.min(redisStart, listSize - 1);
    redisStop = Math.min(redisStop, listSize - 1);
    if (redisStart == 0 && redisStop == listSize - 1) {
        command.setResponse(Coder.getSimpleStringResponse(context.getByteBufAllocator(), SUCCESS));
        return;
    } else if (redisStart == 0 && redisStop < redisStart) {
        context.getRegionProvider().removeKey(key, RedisDataType.REDIS_LIST);
        command.setResponse(Coder.getSimpleStringResponse(context.getByteBufAllocator(), SUCCESS));
        return;
    }
    List<Integer> keepList;
    try {
        keepList = getRange(context, key, redisStart, redisStop, keyRegion);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    for (Object keyElement : keyRegion.keySet()) {
        if (!keepList.contains(keyElement) && keyElement instanceof Integer)
            keyRegion.remove(keyElement);
    }
    // Reset indexes in meta data region
    keyRegion.put("head", keepList.get(0));
    keyRegion.put("tail", keepList.get(keepList.size() - 1));
    command.setResponse(Coder.getSimpleStringResponse(context.getByteBufAllocator(), SUCCESS));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) Region(org.apache.geode.cache.Region)

Example 30 with ByteArrayWrapper

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

the class IncrByExecutor 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.INCRBY));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    checkAndSetDataType(key, context);
    ByteArrayWrapper valueWrapper = r.get(key);
    /*
     * Try increment
     */
    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;
    }
    if (valueWrapper == null) {
        r.put(key, new ByteArrayWrapper(incrArray));
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), increment));
        return;
    }
    /*
     * Value exists
     */
    String stringValue = Coder.bytesToString(valueWrapper.toBytes());
    Long value;
    try {
        value = Long.parseLong(stringValue);
    } catch (NumberFormatException e) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_VALUE_NOT_USABLE));
        return;
    }
    /*
     * Check for overflow
     */
    if (value >= 0 && increment > (Long.MAX_VALUE - value)) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_OVERFLOW));
        return;
    }
    value += increment;
    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)

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