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