Search in sources :

Example 1 with HyperLogLogPlus

use of org.apache.geode.internal.hll.HyperLogLogPlus in project geode by apache.

the class GeodeRedisServer method initializeRedis.

private void initializeRedis() {
    synchronized (this.cache) {
        Region<ByteArrayWrapper, ByteArrayWrapper> stringsRegion;
        Region<ByteArrayWrapper, HyperLogLogPlus> hLLRegion;
        Region<String, RedisDataType> redisMetaData;
        InternalCache gemFireCache = (InternalCache) cache;
        try {
            if ((stringsRegion = cache.getRegion(STRING_REGION)) == null) {
                RegionFactory<ByteArrayWrapper, ByteArrayWrapper> regionFactory = gemFireCache.createRegionFactory(this.DEFAULT_REGION_TYPE);
                stringsRegion = regionFactory.create(STRING_REGION);
            }
            if ((hLLRegion = cache.getRegion(HLL_REGION)) == null) {
                RegionFactory<ByteArrayWrapper, HyperLogLogPlus> regionFactory = gemFireCache.createRegionFactory(this.DEFAULT_REGION_TYPE);
                hLLRegion = regionFactory.create(HLL_REGION);
            }
            if ((redisMetaData = cache.getRegion(REDIS_META_DATA_REGION)) == null) {
                AttributesFactory af = new AttributesFactory();
                af.addCacheListener(metaListener);
                af.setDataPolicy(DataPolicy.REPLICATE);
                InternalRegionArguments ira = new InternalRegionArguments().setInternalRegion(true).setIsUsedForMetaRegion(true);
                redisMetaData = gemFireCache.createVMRegion(REDIS_META_DATA_REGION, af.create(), ira);
            }
        } catch (IOException | ClassNotFoundException e) {
            // only if loading snapshot, not here
            InternalGemFireError assErr = new InternalGemFireError(LocalizedStrings.GemFireCache_UNEXPECTED_EXCEPTION.toLocalizedString());
            assErr.initCause(e);
            throw assErr;
        }
        this.regionCache = new RegionProvider(stringsRegion, hLLRegion, redisMetaData, expirationFutures, expirationExecutor, this.DEFAULT_REGION_TYPE);
        redisMetaData.put(REDIS_META_DATA_REGION, RedisDataType.REDIS_PROTECTED);
        redisMetaData.put(HLL_REGION, RedisDataType.REDIS_PROTECTED);
        redisMetaData.put(STRING_REGION, RedisDataType.REDIS_PROTECTED);
    }
    checkForRegions();
}
Also used : RedisDataType(org.apache.geode.redis.internal.RedisDataType) RegionProvider(org.apache.geode.redis.internal.RegionProvider) InternalCache(org.apache.geode.internal.cache.InternalCache) InternalRegionArguments(org.apache.geode.internal.cache.InternalRegionArguments) IOException(java.io.IOException) AttributesFactory(org.apache.geode.cache.AttributesFactory) ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) HyperLogLogPlus(org.apache.geode.internal.hll.HyperLogLogPlus) InternalGemFireError(org.apache.geode.InternalGemFireError)

Example 2 with HyperLogLogPlus

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

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

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

HyperLogLogPlus (org.apache.geode.internal.hll.HyperLogLogPlus)4 ByteArrayWrapper (org.apache.geode.redis.internal.ByteArrayWrapper)4 ArrayList (java.util.ArrayList)2 CardinalityMergeException (org.apache.geode.internal.hll.CardinalityMergeException)2 IOException (java.io.IOException)1 InternalGemFireError (org.apache.geode.InternalGemFireError)1 AttributesFactory (org.apache.geode.cache.AttributesFactory)1 InternalCache (org.apache.geode.internal.cache.InternalCache)1 InternalRegionArguments (org.apache.geode.internal.cache.InternalRegionArguments)1 RedisDataType (org.apache.geode.redis.internal.RedisDataType)1 RegionProvider (org.apache.geode.redis.internal.RegionProvider)1