Search in sources :

Example 56 with ByteArrayWrapper

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

the class PFAddExecutor 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.PFADD));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    checkAndSetDataType(key, context);
    Region<ByteArrayWrapper, HyperLogLogPlus> keyRegion = context.getRegionProvider().gethLLRegion();
    HyperLogLogPlus hll = keyRegion.get(key);
    boolean changed = false;
    if (hll == null)
        hll = new HyperLogLogPlus(DEFAULT_HLL_DENSE);
    for (int i = 2; i < commandElems.size(); i++) {
        byte[] bytes = commandElems.get(i);
        boolean offerChange = hll.offer(bytes);
        if (offerChange)
            changed = true;
    }
    keyRegion.put(key, hll);
    if (changed)
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), 1));
    else
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), 0));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) HyperLogLogPlus(org.apache.geode.internal.hll.HyperLogLogPlus)

Example 57 with ByteArrayWrapper

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

the class PFCountExecutor 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.PFCOUNT));
        return;
    }
    Region<ByteArrayWrapper, HyperLogLogPlus> keyRegion = context.getRegionProvider().gethLLRegion();
    List<HyperLogLogPlus> hlls = new ArrayList<HyperLogLogPlus>();
    for (int i = 1; i < commandElems.size(); i++) {
        ByteArrayWrapper k = new ByteArrayWrapper(commandElems.get(i));
        checkDataType(k, RedisDataType.REDIS_HLL, context);
        HyperLogLogPlus h = keyRegion.get(k);
        if (h != null)
            hlls.add(h);
    }
    if (hlls.isEmpty()) {
        command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), 0));
        return;
    }
    HyperLogLogPlus tmp = hlls.remove(0);
    HyperLogLogPlus[] estimators = hlls.toArray(new HyperLogLogPlus[hlls.size()]);
    try {
        tmp = (HyperLogLogPlus) tmp.merge(estimators);
    } catch (CardinalityMergeException e) {
        throw new RuntimeException(e);
    }
    long cardinality = tmp.cardinality();
    command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), cardinality));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) HyperLogLogPlus(org.apache.geode.internal.hll.HyperLogLogPlus) ArrayList(java.util.ArrayList) CardinalityMergeException(org.apache.geode.internal.hll.CardinalityMergeException)

Example 58 with ByteArrayWrapper

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

the class PFMergeExecutor 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.PFMERGE));
        return;
    }
    ByteArrayWrapper destKey = command.getKey();
    checkAndSetDataType(destKey, context);
    Region<ByteArrayWrapper, HyperLogLogPlus> keyRegion = context.getRegionProvider().gethLLRegion();
    HyperLogLogPlus mergedHLL = keyRegion.get(destKey);
    if (mergedHLL == null)
        mergedHLL = new HyperLogLogPlus(DEFAULT_HLL_DENSE);
    List<HyperLogLogPlus> hlls = new ArrayList<HyperLogLogPlus>();
    for (int i = 2; i < commandElems.size(); i++) {
        ByteArrayWrapper k = new ByteArrayWrapper(commandElems.get(i));
        checkDataType(k, RedisDataType.REDIS_HLL, context);
        HyperLogLogPlus h = keyRegion.get(k);
        if (h != null)
            hlls.add(h);
    }
    if (hlls.isEmpty()) {
        context.getRegionProvider().removeKey(destKey);
        command.setResponse(Coder.getSimpleStringResponse(context.getByteBufAllocator(), "OK"));
        return;
    }
    HyperLogLogPlus[] estimators = hlls.toArray(new HyperLogLogPlus[hlls.size()]);
    try {
        mergedHLL = (HyperLogLogPlus) mergedHLL.merge(estimators);
    } catch (CardinalityMergeException e) {
        throw new RuntimeException(e);
    }
    keyRegion.put(destKey, mergedHLL);
    command.setResponse(Coder.getSimpleStringResponse(context.getByteBufAllocator(), "OK"));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) HyperLogLogPlus(org.apache.geode.internal.hll.HyperLogLogPlus) ArrayList(java.util.ArrayList) CardinalityMergeException(org.apache.geode.internal.hll.CardinalityMergeException)

Example 59 with ByteArrayWrapper

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

the class LIndexExecutor 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.LINDEX));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    byte[] indexArray = commandElems.get(2);
    checkDataType(key, RedisDataType.REDIS_LIST, context);
    Region<Integer, ByteArrayWrapper> keyRegion = getRegion(context, key);
    if (keyRegion == null) {
        command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
        return;
    }
    int listSize = keyRegion.size() - LIST_EMPTY_SIZE;
    Integer redisIndex;
    try {
        redisIndex = Coder.bytesToInt(indexArray);
    } catch (NumberFormatException e) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_NUMERIC));
        return;
    }
    if (redisIndex < 0)
        // Since the redisIndex is negative here, this will reset it to be a standard 0 based index
        redisIndex = listSize + redisIndex;
    /*
     * If the index is still less than 0 that means the index has shot off back past the beginning,
     * which means the index isn't real and a nil is returned
     */
    if (redisIndex < 0) {
        command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
        return;
    }
    /*
     * Now we must get that element from the region
     */
    Struct entry;
    try {
        entry = getEntryAtIndex(context, key, redisIndex);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    if (entry == null) {
        command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
        return;
    }
    Object[] entryArray = entry.getFieldValues();
    ByteArrayWrapper valueWrapper = (ByteArrayWrapper) entryArray[1];
    command.setResponse(Coder.getBulkStringResponse(context.getByteBufAllocator(), valueWrapper.toBytes()));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) Struct(org.apache.geode.cache.query.Struct)

Example 60 with ByteArrayWrapper

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

the class HKeysExecutor 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.HKEYS));
        return;
    }
    ByteArrayWrapper key = command.getKey();
    checkDataType(key, RedisDataType.REDIS_HASH, context);
    Region<ByteArrayWrapper, ByteArrayWrapper> keyRegion = getRegion(context, key);
    if (keyRegion == null) {
        command.setResponse(Coder.getEmptyArrayResponse(context.getByteBufAllocator()));
        return;
    }
    Set<ByteArrayWrapper> keys = new HashSet(keyRegion.keySet());
    if (keys.isEmpty()) {
        command.setResponse(Coder.getEmptyArrayResponse(context.getByteBufAllocator()));
        return;
    }
    // String response = getBulkStringArrayResponse(keys);
    command.setResponse(Coder.getBulkStringArrayResponse(context.getByteBufAllocator(), keys));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) HashSet(java.util.HashSet)

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