use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class SMoveExecutor 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.SMOVE));
return;
}
ByteArrayWrapper source = command.getKey();
ByteArrayWrapper destination = new ByteArrayWrapper(commandElems.get(2));
ByteArrayWrapper mem = new ByteArrayWrapper(commandElems.get(3));
checkDataType(source, RedisDataType.REDIS_SET, context);
checkDataType(destination, RedisDataType.REDIS_SET, context);
@SuppressWarnings("unchecked") Region<ByteArrayWrapper, Boolean> sourceRegion = (Region<ByteArrayWrapper, Boolean>) context.getRegionProvider().getRegion(source);
if (sourceRegion == null) {
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), NOT_MOVED));
return;
}
Object oldVal = sourceRegion.get(mem);
sourceRegion.remove(mem);
if (oldVal == null) {
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), NOT_MOVED));
return;
}
@SuppressWarnings("unchecked") Region<ByteArrayWrapper, Boolean> destinationRegion = (Region<ByteArrayWrapper, Boolean>) getOrCreateRegion(context, destination, RedisDataType.REDIS_SET);
destinationRegion.put(mem, true);
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), MOVED));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class SScanExecutor 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.SSCAN));
return;
}
ByteArrayWrapper key = command.getKey();
checkDataType(key, RedisDataType.REDIS_SET, context);
@SuppressWarnings("unchecked") Region<ByteArrayWrapper, Boolean> keyRegion = (Region<ByteArrayWrapper, Boolean>) context.getRegionProvider().getRegion(key);
if (keyRegion == null) {
command.setResponse(Coder.getScanResponse(context.getByteBufAllocator(), new ArrayList<String>()));
return;
}
byte[] cAr = commandElems.get(2);
String cursorString = Coder.bytesToString(cAr);
int cursor = 0;
Pattern matchPattern = null;
String globMatchPattern = null;
int count = DEFUALT_COUNT;
try {
cursor = Integer.parseInt(cursorString);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_CURSOR));
return;
}
if (cursor < 0) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_CURSOR));
return;
}
if (commandElems.size() > 4) {
try {
byte[] bytes = commandElems.get(3);
String tmp = Coder.bytesToString(bytes);
if (tmp.equalsIgnoreCase("MATCH")) {
bytes = commandElems.get(4);
globMatchPattern = Coder.bytesToString(bytes);
} else if (tmp.equalsIgnoreCase("COUNT")) {
bytes = commandElems.get(4);
count = Coder.bytesToInt(bytes);
}
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_COUNT));
return;
}
}
if (commandElems.size() > 6) {
try {
byte[] bytes = commandElems.get(5);
String tmp = Coder.bytesToString(bytes);
if (tmp.equalsIgnoreCase("COUNT")) {
bytes = commandElems.get(6);
count = Coder.bytesToInt(bytes);
}
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_COUNT));
return;
}
}
if (count < 0) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_COUNT));
return;
}
try {
matchPattern = convertGlobToRegex(globMatchPattern);
} catch (PatternSyntaxException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), RedisConstants.ERROR_ILLEGAL_GLOB));
return;
}
@SuppressWarnings("unchecked") List<ByteArrayWrapper> returnList = (List<ByteArrayWrapper>) getIteration(new ArrayList(keyRegion.keySet()), matchPattern, count, cursor);
command.setResponse(Coder.getScanResponse(context.getByteBufAllocator(), returnList));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class SAddExecutor 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.SADD));
return;
}
ByteArrayWrapper key = command.getKey();
@SuppressWarnings("unchecked") Region<ByteArrayWrapper, Boolean> keyRegion = (Region<ByteArrayWrapper, Boolean>) context.getRegionProvider().getOrCreateRegion(key, RedisDataType.REDIS_SET, context);
if (commandElems.size() >= 4) {
Map<ByteArrayWrapper, Boolean> entries = new HashMap<ByteArrayWrapper, Boolean>();
for (int i = 2; i < commandElems.size(); i++) entries.put(new ByteArrayWrapper(commandElems.get(i)), true);
keyRegion.putAll(entries);
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), entries.size()));
} else {
Object v = keyRegion.put(new ByteArrayWrapper(commandElems.get(2)), true);
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), v == null ? 1 : 0));
}
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class BitOpExecutor 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() < 4) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.BITOP));
return;
}
String operation = command.getStringKey().toUpperCase();
ByteArrayWrapper destKey = new ByteArrayWrapper(commandElems.get(2));
checkDataType(destKey, context);
byte[][] values = new byte[commandElems.size() - 3][];
int maxLength = 0;
for (int i = 3; i < commandElems.size(); i++) {
ByteArrayWrapper key = new ByteArrayWrapper(commandElems.get(i));
checkDataType(key, context);
ByteArrayWrapper value = r.get(key);
if (value == null) {
values[i - 3] = null;
continue;
}
byte[] val = value.toBytes();
values[i - 3] = val;
if (val.length > maxLength) {
maxLength = val.length;
byte[] tmp = values[0];
values[0] = val;
values[i - 3] = tmp;
}
if (i == 3 && operation.equalsIgnoreCase("NOT"))
break;
}
if (operation.equals("AND"))
and(context, r, destKey, values, maxLength);
else if (operation.equals("OR"))
or(context, r, destKey, values, maxLength);
else if (operation.equals("XOR"))
xor(context, r, destKey, values, maxLength);
else if (operation.equals("NOT"))
not(context, r, destKey, values, maxLength);
else {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NO_SUCH_OP));
return;
}
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), maxLength));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class BitOpExecutor method not.
private void not(ExecutionHandlerContext context, Region<ByteArrayWrapper, ByteArrayWrapper> r, ByteArrayWrapper destKey, byte[][] values, int max) {
byte[] dest = new byte[max];
byte[] cA = values[0];
for (int i = 0; i < max; i++) {
if (cA == null)
dest[i] = ~0;
else
dest[i] = (byte) (~cA[i] & 0xFF);
}
checkAndSetDataType(destKey, context);
r.put(destKey, new ByteArrayWrapper(dest));
}
Aggregations