use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class BitOpExecutor method and.
private void and(ExecutionHandlerContext context, Region<ByteArrayWrapper, ByteArrayWrapper> r, ByteArrayWrapper destKey, byte[][] values, int max) {
byte[] dest = new byte[max];
outer: for (int i = 0; i < max; i++) {
byte b = values[0][i];
for (int j = 1; j < values.length; j++) {
if (values[j] == null) {
break outer;
} else if (i < values[j].length)
b &= values[j][i];
else
b &= 0;
}
dest[i] = b;
}
checkAndSetDataType(destKey, context);
r.put(destKey, new ByteArrayWrapper(dest));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class BitOpExecutor method xor.
private void xor(ExecutionHandlerContext context, Region<ByteArrayWrapper, ByteArrayWrapper> r, ByteArrayWrapper destKey, byte[][] values, int max) {
byte[] dest = new byte[max];
for (int i = 0; i < max; i++) {
byte b = values[0][i];
for (int j = 1; j < values.length; j++) {
byte[] cA = values[j];
if (cA != null && i < cA.length)
b ^= cA[i];
else
b ^= 0;
}
dest[i] = b;
}
checkAndSetDataType(destKey, context);
r.put(destKey, new ByteArrayWrapper(dest));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class BitPosExecutor method executeCommand.
@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
List<byte[]> commandElems = command.getProcessedCommand();
Region<ByteArrayWrapper, ByteArrayWrapper> r = context.getRegionProvider().getStringsRegion();
if (commandElems.size() < 3) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.BITPOS));
return;
}
ByteArrayWrapper key = command.getKey();
checkAndSetDataType(key, context);
ByteArrayWrapper string = r.get(key);
int bit;
int bitPosition = -1;
boolean endSet = false;
try {
byte[] bitAr = commandElems.get(2);
bit = Coder.bytesToInt(bitAr);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_INT));
return;
}
if (bit != 0 && bit != 1) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_BIT));
return;
}
if (string == null || string.length() == 0) {
// Redis
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), -bit));
// command
return;
}
byte[] bytes = string.toBytes();
int start = 0;
int end = bytes.length - 1;
if (commandElems.size() > 3) {
try {
byte[] startAr = commandElems.get(3);
start = Coder.bytesToInt(startAr);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_INT));
return;
}
}
if (commandElems.size() > 4) {
try {
byte[] endAr = commandElems.get(4);
end = Coder.bytesToInt(endAr);
endSet = true;
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_INT));
return;
}
}
if (start < 0)
start += bytes.length;
if (end < 0)
end += bytes.length;
if (start < 0)
start = 0;
if (end < 0)
end = 0;
if (start > bytes.length)
start = bytes.length - 1;
if (end > bytes.length)
end = bytes.length - 1;
if (end < start) {
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), -1));
return;
}
outerLoop: for (int i = start; i <= end; i++) {
int cBit;
byte cByte = bytes[i];
for (int j = 0; j < 8; j++) {
cBit = (cByte & (0x80 >> j)) >> (7 - j);
if (cBit == bit) {
bitPosition = 8 * i + j;
break outerLoop;
}
}
}
if (bit == 0 && bitPosition == -1 && !endSet)
bitPosition = bytes.length * 8;
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), bitPosition));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class DecrByExecutor method executeCommand.
@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
List<byte[]> commandElems = command.getProcessedCommand();
Region<ByteArrayWrapper, ByteArrayWrapper> r = context.getRegionProvider().getStringsRegion();
if (commandElems.size() < 3) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.DECRBY));
return;
}
ByteArrayWrapper key = command.getKey();
checkAndSetDataType(key, context);
ByteArrayWrapper valueWrapper = r.get(key);
/*
* Try increment
*/
byte[] decrArray = commandElems.get(DECREMENT_INDEX);
String decrString = Coder.bytesToString(decrArray);
Long decrement;
try {
decrement = Long.parseLong(decrString);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_DECREMENT_NOT_USABLE));
return;
}
if (valueWrapper == null) {
String negativeDecrString = decrString.charAt(0) == Coder.HYPHEN_ID ? decrString.substring(1) : "-" + decrString;
r.put(key, new ByteArrayWrapper(Coder.stringToBytes(negativeDecrString)));
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), -decrement));
return;
}
/*
* Value exists
*/
String stringValue = Coder.bytesToString(valueWrapper.toBytes());
Long value;
try {
value = Long.parseLong(stringValue);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_VALUE_NOT_USABLE));
return;
}
/*
* Check for overflow Negative decrement is used because the decrement is stored as a positive
* long
*/
if (value <= 0 && -decrement < (Long.MIN_VALUE - value)) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_OVERFLOW));
return;
}
value -= decrement;
stringValue = "" + value;
r.put(key, new ByteArrayWrapper(Coder.stringToBytes(stringValue)));
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), value));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class LRangeExecutor method executeCommand.
@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
List<byte[]> commandElems = command.getProcessedCommand();
if (commandElems.size() < 4) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.LRANGE));
return;
}
ByteArrayWrapper key = command.getKey();
byte[] startArray = commandElems.get(2);
byte[] stopArray = commandElems.get(3);
int redisStart;
int redisStop;
checkDataType(key, RedisDataType.REDIS_LIST, context);
Region<Integer, ByteArrayWrapper> keyRegion = getRegion(context, key);
if (keyRegion == null) {
command.setResponse(Coder.getEmptyArrayResponse(context.getByteBufAllocator()));
return;
}
int listSize = keyRegion.size() - LIST_EMPTY_SIZE;
if (listSize == 0) {
command.setResponse(Coder.getEmptyArrayResponse(context.getByteBufAllocator()));
return;
}
try {
redisStart = Coder.bytesToInt(startArray);
redisStop = Coder.bytesToInt(stopArray);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_NUMERIC));
return;
}
redisStart = getBoundedStartIndex(redisStart, listSize);
redisStop = getBoundedEndIndex(redisStop, listSize);
if (redisStart > redisStop) {
command.setResponse(Coder.getEmptyArrayResponse(context.getByteBufAllocator()));
return;
}
redisStart = Math.min(redisStart, listSize - 1);
redisStop = Math.min(redisStop, listSize - 1);
List<Struct> range;
try {
range = getRange(context, key, redisStart, redisStop, keyRegion);
} catch (Exception e) {
throw new RuntimeException(e);
}
if (range == null)
command.setResponse(Coder.getEmptyArrayResponse(context.getByteBufAllocator()));
else
command.setResponse(Coder.getBulkStringArrayResponseOfValues(context.getByteBufAllocator(), range));
}
Aggregations