use of org.apache.geode.redis.internal.DoubleWrapper 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.DoubleWrapper 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.DoubleWrapper 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));
}
use of org.apache.geode.redis.internal.DoubleWrapper in project geode by apache.
the class ZCardExecutor 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.ZCARD));
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));
else
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), keyRegion.size()));
}
use of org.apache.geode.redis.internal.DoubleWrapper in project geode by apache.
the class ZRangeExecutor 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;
if (commandElems.size() >= 5) {
byte[] fifthElem = commandElems.get(4);
withScores = Coder.bytesToString(fifthElem).equalsIgnoreCase("WITHSCORES");
}
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 start;
int stop;
int sSetSize = keyRegion.size();
try {
byte[] startArray = commandElems.get(2);
byte[] stopArray = commandElems.get(3);
start = Coder.bytesToInt(startArray);
stop = Coder.bytesToInt(stopArray);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_NUMERIC));
return;
}
start = getBoundedStartIndex(start, sSetSize);
stop = getBoundedEndIndex(stop, sSetSize);
if (start > stop || start == sSetSize) {
command.setResponse(Coder.getEmptyArrayResponse(context.getByteBufAllocator()));
return;
}
if (stop == sSetSize)
stop--;
List<?> list;
try {
list = getRange(context, key, start, stop);
} catch (Exception e) {
throw new RuntimeException(e);
}
command.setResponse(Coder.zRangeResponse(context.getByteBufAllocator(), list, withScores));
}
Aggregations