use of org.apache.geode.redis.internal.DoubleWrapper in project geode by apache.
the class ZAddExecutor method executeCommand.
@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
List<byte[]> commandElems = command.getProcessedCommand();
if (commandElems.size() < 4 || commandElems.size() % 2 == 1) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.ZADD));
return;
}
ByteArrayWrapper key = command.getKey();
int numberOfAdds = 0;
if (commandElems.size() > 4) {
Map<ByteArrayWrapper, DoubleWrapper> map = new HashMap<ByteArrayWrapper, DoubleWrapper>();
for (int i = 2; i < commandElems.size(); i++) {
byte[] scoreArray = commandElems.get(i++);
byte[] memberArray = commandElems.get(i);
Double score;
try {
score = Coder.bytesToDouble(scoreArray);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_NUMERICAL));
return;
}
map.put(new ByteArrayWrapper(memberArray), new DoubleWrapper(score));
numberOfAdds++;
}
Region<ByteArrayWrapper, DoubleWrapper> keyRegion = getOrCreateRegion(context, key, RedisDataType.REDIS_SORTEDSET);
keyRegion.putAll(map);
} else {
byte[] scoreArray = commandElems.get(2);
byte[] memberArray = commandElems.get(3);
Double score;
try {
score = Coder.bytesToDouble(scoreArray);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_NUMERICAL));
return;
}
Region<ByteArrayWrapper, DoubleWrapper> keyRegion = getOrCreateRegion(context, key, RedisDataType.REDIS_SORTEDSET);
Object oldVal = keyRegion.put(new ByteArrayWrapper(memberArray), new DoubleWrapper(score));
if (oldVal == null)
numberOfAdds = 1;
}
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), numberOfAdds));
}
use of org.apache.geode.redis.internal.DoubleWrapper in project geode by apache.
the class ZCountExecutor 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.ZCOUNT));
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 startInclusive = true;
boolean stopInclusive = true;
double start;
double stop;
byte[] startArray = commandElems.get(2);
byte[] stopArray = commandElems.get(3);
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;
}
int count;
try {
count = getCount(key, keyRegion, context, start, stop, startInclusive, stopInclusive);
} catch (Exception e) {
throw new RuntimeException(e);
}
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), count));
}
use of org.apache.geode.redis.internal.DoubleWrapper in project geode by apache.
the class ZIncrByExecutor 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.ZINCRBY));
return;
}
ByteArrayWrapper key = command.getKey();
Region<ByteArrayWrapper, DoubleWrapper> keyRegion = getOrCreateRegion(context, key, RedisDataType.REDIS_SORTEDSET);
ByteArrayWrapper member = new ByteArrayWrapper(commandElems.get(3));
double incr;
try {
byte[] incrArray = commandElems.get(2);
incr = Coder.bytesToDouble(incrArray);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_NUMERIC));
return;
}
DoubleWrapper score = keyRegion.get(member);
if (score == null) {
keyRegion.put(member, new DoubleWrapper(incr));
command.setResponse(Coder.getBulkStringResponse(context.getByteBufAllocator(), incr));
return;
}
double result = score.score + incr;
if (Double.isNaN(result)) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NAN));
return;
}
score.score = result;
keyRegion.put(member, score);
command.setResponse(Coder.getBulkStringResponse(context.getByteBufAllocator(), score.score));
}
use of org.apache.geode.redis.internal.DoubleWrapper in project geode by apache.
the class ZRangeByLexExecutor 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.ZRANGEBYLEX));
return;
}
boolean existsLimit = false;
if (commandElems.size() >= 7) {
byte[] fifthElem = commandElems.get(4);
existsLimit = Coder.bytesToString(fifthElem).equalsIgnoreCase("LIMIT");
}
int offset = 0;
int limit = 0;
if (existsLimit) {
try {
byte[] offsetArray = commandElems.get(5);
byte[] limitArray = commandElems.get(6);
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;
}
ByteArrayWrapper key = command.getKey();
Region<ByteArrayWrapper, DoubleWrapper> keyRegion = getOrCreateRegion(context, key, RedisDataType.REDIS_SORTEDSET);
if (keyRegion == null) {
command.setResponse(Coder.getEmptyArrayResponse(context.getByteBufAllocator()));
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;
}
Collection<ByteArrayWrapper> list = null;
if (!(existsLimit && limit == 0)) {
try {
list = getRange(key, keyRegion, context, Coder.stringToByteArrayWrapper(startString), Coder.stringToByteArrayWrapper(stopString), minInclusive, maxInclusive, offset, limit);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
if (list == null)
command.setResponse(Coder.getEmptyArrayResponse(context.getByteBufAllocator()));
else
command.setResponse(getCustomBulkStringArrayResponse(list, context));
}
use of org.apache.geode.redis.internal.DoubleWrapper in project geode by apache.
the class ZRemRangeByRankExecutor 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.ZREMRANGEBYRANK));
return;
}
ByteArrayWrapper key = command.getKey();
checkDataType(key, RedisDataType.REDIS_SORTEDSET, context);
Region<ByteArrayWrapper, DoubleWrapper> keyRegion = getRegion(context, key);
if (keyRegion == null) {
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), NONE_REMOVED));
return;
}
int startRank;
int stopRank;
try {
startRank = Coder.bytesToInt(commandElems.get(2));
stopRank = Coder.bytesToInt(commandElems.get(3));
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_NUMERIC));
return;
}
int sSetSize = keyRegion.size();
startRank = getBoundedStartIndex(startRank, sSetSize);
stopRank = getBoundedEndIndex(stopRank, sSetSize);
if (stopRank > sSetSize - 1)
stopRank = sSetSize - 1;
if (startRank > stopRank) {
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), 0));
return;
}
int numRemoved = 0;
List<?> removeList = null;
try {
if (startRank == 0 && stopRank == sSetSize - 1) {
numRemoved = keyRegion.size();
context.getRegionProvider().removeKey(key);
} else {
removeList = getRemoveKeys(context, key, startRank, stopRank);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
if (removeList != null) {
for (Object entry : removeList) {
ByteArrayWrapper removeKey;
if (entry instanceof Entry)
removeKey = (ByteArrayWrapper) ((Entry<?, ?>) entry).getKey();
else
removeKey = (ByteArrayWrapper) ((Struct) entry).getFieldValues()[0];
Object oldVal = keyRegion.remove(removeKey);
if (oldVal != null)
numRemoved++;
}
if (keyRegion.isEmpty())
context.getRegionProvider().removeKey(key);
}
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), numRemoved));
}
Aggregations