Search in sources :

Example 86 with ByteArrayWrapper

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

the class SMoveExecutor 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.SMOVE));
        return;
    }
    ByteArrayWrapper source = command.getKey();
    ByteArrayWrapper destination = new ByteArrayWrapper(commandElems.get(2));
    ByteArrayWrapper mem = new ByteArrayWrapper(commandElems.get(3));
    checkDataType(source, RedisDataType.REDIS_SET, context);
    checkDataType(destination, RedisDataType.REDIS_SET, context);
    @SuppressWarnings("unchecked") Region<ByteArrayWrapper, Boolean> sourceRegion = (Region<ByteArrayWrapper, Boolean>) context.getRegionProvider().getRegion(source);
    if (sourceRegion == null) {
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), NOT_MOVED));
        return;
    }
    Object oldVal = sourceRegion.get(mem);
    sourceRegion.remove(mem);
    if (oldVal == null) {
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), NOT_MOVED));
        return;
    }
    @SuppressWarnings("unchecked") Region<ByteArrayWrapper, Boolean> destinationRegion = (Region<ByteArrayWrapper, Boolean>) getOrCreateRegion(context, destination, RedisDataType.REDIS_SET);
    destinationRegion.put(mem, true);
    command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), MOVED));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) Region(org.apache.geode.cache.Region)

Example 87 with ByteArrayWrapper

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

the class SScanExecutor 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.SSCAN));
        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.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("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;
    }
    @SuppressWarnings("unchecked") List<ByteArrayWrapper> returnList = (List<ByteArrayWrapper>) getIteration(new ArrayList(keyRegion.keySet()), 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) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 88 with ByteArrayWrapper

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

the class SAddExecutor 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.SADD));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    @SuppressWarnings("unchecked") Region<ByteArrayWrapper, Boolean> keyRegion = (Region<ByteArrayWrapper, Boolean>) context.getRegionProvider().getOrCreateRegion(key, RedisDataType.REDIS_SET, context);
    if (commandElems.size() >= 4) {
        Map<ByteArrayWrapper, Boolean> entries = new HashMap<ByteArrayWrapper, Boolean>();
        for (int i = 2; i < commandElems.size(); i++) entries.put(new ByteArrayWrapper(commandElems.get(i)), true);
        keyRegion.putAll(entries);
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), entries.size()));
    } else {
        Object v = keyRegion.put(new ByteArrayWrapper(commandElems.get(2)), true);
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), v == null ? 1 : 0));
    }
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) HashMap(java.util.HashMap) Region(org.apache.geode.cache.Region)

Example 89 with ByteArrayWrapper

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

the class BitOpExecutor 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(), ArityDef.BITOP));
        return;
    }
    String operation = command.getStringKey().toUpperCase();
    ByteArrayWrapper destKey = new ByteArrayWrapper(commandElems.get(2));
    checkDataType(destKey, context);
    byte[][] values = new byte[commandElems.size() - 3][];
    int maxLength = 0;
    for (int i = 3; i < commandElems.size(); i++) {
        ByteArrayWrapper key = new ByteArrayWrapper(commandElems.get(i));
        checkDataType(key, context);
        ByteArrayWrapper value = r.get(key);
        if (value == null) {
            values[i - 3] = null;
            continue;
        }
        byte[] val = value.toBytes();
        values[i - 3] = val;
        if (val.length > maxLength) {
            maxLength = val.length;
            byte[] tmp = values[0];
            values[0] = val;
            values[i - 3] = tmp;
        }
        if (i == 3 && operation.equalsIgnoreCase("NOT"))
            break;
    }
    if (operation.equals("AND"))
        and(context, r, destKey, values, maxLength);
    else if (operation.equals("OR"))
        or(context, r, destKey, values, maxLength);
    else if (operation.equals("XOR"))
        xor(context, r, destKey, values, maxLength);
    else if (operation.equals("NOT"))
        not(context, r, destKey, values, maxLength);
    else {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NO_SUCH_OP));
        return;
    }
    command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), maxLength));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper)

Example 90 with ByteArrayWrapper

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

the class BitOpExecutor method not.

private void not(ExecutionHandlerContext context, Region<ByteArrayWrapper, ByteArrayWrapper> r, ByteArrayWrapper destKey, byte[][] values, int max) {
    byte[] dest = new byte[max];
    byte[] cA = values[0];
    for (int i = 0; i < max; i++) {
        if (cA == null)
            dest[i] = ~0;
        else
            dest[i] = (byte) (~cA[i] & 0xFF);
    }
    checkAndSetDataType(destKey, context);
    r.put(destKey, new ByteArrayWrapper(dest));
}
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