use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class ZScanExecutor method executeCommand.
@SuppressWarnings("unchecked")
@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
List<byte[]> commandElems = command.getProcessedCommand();
if (commandElems.size() < 3) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.ZSCAN));
return;
}
ByteArrayWrapper key = command.getKey();
Region<ByteArrayWrapper, DoubleWrapper> keyRegion = (Region<ByteArrayWrapper, DoubleWrapper>) context.getRegionProvider().getRegion(key);
checkDataType(key, RedisDataType.REDIS_SORTEDSET, context);
if (keyRegion == null) {
command.setResponse(Coder.getScanResponse(context.getByteBufAllocator(), new ArrayList<String>()));
return;
}
byte[] cAr = commandElems.get(2);
// String cursorString = ResponseToByteEncoder.bytesToString(cAr);
int cursor = 0;
Pattern matchPattern = null;
String globMatchPattern = null;
int count = DEFUALT_COUNT;
try {
cursor = Coder.bytesToInt(cAr);
} 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);
if (Coder.bytesToString(bytes).equalsIgnoreCase("MATCH")) {
bytes = commandElems.get(4);
globMatchPattern = Coder.bytesToString(bytes);
} else if (Coder.bytesToString(bytes).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);
if (Coder.bytesToString(bytes).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;
}
List<ByteArrayWrapper> returnList = (List<ByteArrayWrapper>) getIteration(new HashSet(keyRegion.entrySet()), 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 ZScoreExecutor 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.ZSCORE));
return;
}
ByteArrayWrapper key = command.getKey();
ByteArrayWrapper member = new ByteArrayWrapper(commandElems.get(2));
checkDataType(key, RedisDataType.REDIS_SORTEDSET, context);
Region<ByteArrayWrapper, DoubleWrapper> keyRegion = getRegion(context, key);
if (keyRegion == null) {
command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
return;
}
DoubleWrapper score = keyRegion.get(member);
if (score == null) {
command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
return;
}
command.setResponse(Coder.getBulkStringResponse(context.getByteBufAllocator(), score.toString()));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class ZLexCountExecutor 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.ZLEXCOUNT));
return;
}
ByteArrayWrapper key = command.getKey();
Region<ByteArrayWrapper, DoubleWrapper> keyRegion = getRegion(context, key);
checkDataType(key, RedisDataType.REDIS_SORTEDSET, context);
if (keyRegion == null) {
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), NOT_EXISTS));
return;
}
boolean minInclusive = false;
boolean maxInclusive = false;
byte[] minArray = commandElems.get(2);
byte[] maxArray = commandElems.get(3);
String startString = Coder.bytesToString(minArray);
String stopString = Coder.bytesToString(maxArray);
if (minArray[0] == Coder.OPEN_BRACE_ID) {
startString = startString.substring(1);
minInclusive = false;
} else if (minArray[0] == Coder.OPEN_BRACKET_ID) {
startString = startString.substring(1);
minInclusive = true;
} else if (minArray[0] != Coder.HYPHEN_ID) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_ILLEGAL_SYNTAX));
return;
}
if (maxArray[0] == Coder.OPEN_BRACE_ID) {
stopString = stopString.substring(1);
maxInclusive = false;
} else if (maxArray[0] == Coder.OPEN_BRACKET_ID) {
stopString = stopString.substring(1);
maxInclusive = true;
} else if (maxArray[0] != Coder.PLUS_ID) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_ILLEGAL_SYNTAX));
return;
}
int count;
try {
count = getCount(key, keyRegion, context, Coder.stringToByteArrayWrapper(startString), Coder.stringToByteArrayWrapper(stopString), minInclusive, maxInclusive);
} catch (Exception e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), e.toString()));
return;
}
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), count));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class ZRangeByLexExecutor method getRange.
private List<ByteArrayWrapper> getRange(ByteArrayWrapper key, Region<ByteArrayWrapper, DoubleWrapper> keyRegion, ExecutionHandlerContext context, ByteArrayWrapper start, ByteArrayWrapper stop, boolean startInclusive, boolean stopInclusive, int offset, int limit) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
if (start.equals("-") && stop.equals("+")) {
List<ByteArrayWrapper> l = new ArrayList<ByteArrayWrapper>(keyRegion.keySet());
int size = l.size();
Collections.sort(l);
if (limit == 0)
limit += size;
l = l.subList(Math.min(size, offset), Math.min(offset + limit, size));
return l;
} else if (start.equals("+") || stop.equals("-"))
return null;
Query query;
Object[] params;
if (start.equals("-")) {
if (stopInclusive) {
query = getQuery(key, SortedSetQuery.ZRANGEBYLEXNINFI, context);
} else {
query = getQuery(key, SortedSetQuery.ZRANGEBYLEXNINF, context);
}
params = new Object[] { stop, INFINITY_LIMIT };
} else if (stop.equals("+")) {
if (startInclusive) {
query = getQuery(key, SortedSetQuery.ZRANGEBYLEXPINFI, context);
} else {
query = getQuery(key, SortedSetQuery.ZRANGEBYLEXPINF, context);
}
params = new Object[] { start, INFINITY_LIMIT };
} else {
if (startInclusive) {
if (stopInclusive) {
query = getQuery(key, SortedSetQuery.ZRANGEBYLEXSTISI, context);
} else {
query = getQuery(key, SortedSetQuery.ZRANGEBYLEXSTI, context);
}
} else {
if (stopInclusive) {
query = getQuery(key, SortedSetQuery.ZRANGEBYLEXSI, context);
} else {
query = getQuery(key, SortedSetQuery.ZRANGEBYLEX, context);
}
}
params = new Object[] { start, stop, INFINITY_LIMIT };
}
if (limit > 0)
params[params.length - 1] = (limit + offset);
SelectResults<ByteArrayWrapper> results = (SelectResults<ByteArrayWrapper>) query.execute(params);
List<ByteArrayWrapper> list = results.asList();
int size = list.size();
return list.subList(Math.min(size, offset), size);
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class ZRangeByScoreExecutor 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(), getArgsError()));
return;
}
boolean withScores = false;
byte[] elem4Array = null;
int offset = 0;
int limit = -1;
if (commandElems.size() >= 5) {
elem4Array = commandElems.get(4);
String elem4 = Coder.bytesToString(elem4Array);
int limitIndex = 4;
if (elem4.equalsIgnoreCase("WITHSCORES")) {
withScores = true;
limitIndex++;
}
if (commandElems.size() >= limitIndex + 2) {
String limitString = Coder.bytesToString(commandElems.get(limitIndex));
if (limitString.equalsIgnoreCase("LIMIT")) {
try {
byte[] offsetArray = commandElems.get(limitIndex + 1);
byte[] limitArray = commandElems.get(limitIndex + 2);
offset = Coder.bytesToInt(offsetArray);
limit = Coder.bytesToInt(limitArray);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_NUMERIC));
return;
}
}
if (offset < 0 || limit < 0) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_LIMIT));
return;
}
if (limitIndex == 4 && commandElems.size() >= 8) {
byte[] lastElemArray = commandElems.get(7);
String lastString = Coder.bytesToString(lastElemArray);
if (lastString.equalsIgnoreCase("WITHSCORES")) {
withScores = true;
}
}
}
}
ByteArrayWrapper key = command.getKey();
checkDataType(key, RedisDataType.REDIS_SORTEDSET, context);
Region<ByteArrayWrapper, DoubleWrapper> keyRegion = getRegion(context, key);
if (keyRegion == null) {
command.setResponse(Coder.getEmptyArrayResponse(context.getByteBufAllocator()));
return;
}
int startIndex = isReverse() ? 3 : 2;
int stopIndex = isReverse() ? 2 : 3;
boolean startInclusive = true;
boolean stopInclusive = true;
double start;
double stop;
byte[] startArray = commandElems.get(startIndex);
byte[] stopArray = commandElems.get(stopIndex);
String startString = Coder.bytesToString(startArray);
String stopString = Coder.bytesToString(stopArray);
if (startArray[0] == Coder.OPEN_BRACE_ID) {
startString = startString.substring(1);
startInclusive = false;
}
if (stopArray[0] == Coder.OPEN_BRACE_ID) {
stopString = stopString.substring(1);
stopInclusive = false;
}
try {
start = Coder.stringToDouble(startString);
stop = Coder.stringToDouble(stopString);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_NUMERIC));
return;
}
Collection<?> list;
try {
list = getKeys(key, keyRegion, context, start, stop, startInclusive, stopInclusive, offset, limit);
} catch (Exception e) {
throw new RuntimeException(e);
}
if (list == null)
command.setResponse(Coder.getEmptyArrayResponse(context.getByteBufAllocator()));
else
command.setResponse(Coder.zRangeResponse(context.getByteBufAllocator(), list, withScores));
}
Aggregations