Search in sources :

Example 1 with CardinalityMergeException

use of org.apache.geode.internal.hll.CardinalityMergeException 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 2 with CardinalityMergeException

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

Aggregations

ArrayList (java.util.ArrayList)2 CardinalityMergeException (org.apache.geode.internal.hll.CardinalityMergeException)2 HyperLogLogPlus (org.apache.geode.internal.hll.HyperLogLogPlus)2 ByteArrayWrapper (org.apache.geode.redis.internal.ByteArrayWrapper)2