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();
}
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));
}
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));
}
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"));
}
Aggregations