use of org.apache.geode.redis.internal.DoubleWrapper in project geode by apache.
the class ZScanExecutor method getIteration.
@SuppressWarnings("unchecked")
@Override
protected List<?> getIteration(Collection<?> list, Pattern matchPattern, int count, int cursor) {
List<Object> returnList = new ArrayList<Object>();
int size = list.size();
int beforeCursor = 0;
int numElements = 0;
int i = -1;
for (Entry<ByteArrayWrapper, DoubleWrapper> entry : (Collection<Entry<ByteArrayWrapper, DoubleWrapper>>) list) {
ByteArrayWrapper keyWrapper = entry.getKey();
String key = keyWrapper.toString();
DoubleWrapper value = entry.getValue();
i++;
if (beforeCursor < cursor) {
beforeCursor++;
continue;
} else if (numElements < count) {
if (matchPattern != null) {
if (matchPattern.matcher(key).matches()) {
returnList.add(keyWrapper);
returnList.add(value.toString());
numElements++;
}
} else {
returnList.add(keyWrapper);
returnList.add(value.toString());
numElements++;
}
} else
break;
}
if (i == size - 1)
returnList.add(0, String.valueOf(0));
else
returnList.add(0, String.valueOf(i));
return returnList;
}
use of org.apache.geode.redis.internal.DoubleWrapper in project geode by apache.
the class ZRemExecutor 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.ZREM));
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(), 0));
return;
}
int numDeletedMembers = 0;
for (int i = 2; i < commandElems.size(); i++) {
byte[] memberArray = commandElems.get(i);
ByteArrayWrapper member = new ByteArrayWrapper(memberArray);
Object oldVal = keyRegion.remove(member);
if (oldVal != null)
numDeletedMembers++;
}
if (keyRegion.isEmpty())
context.getRegionProvider().removeKey(key);
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), numDeletedMembers));
}
use of org.apache.geode.redis.internal.DoubleWrapper in project geode by apache.
the class ZRemRangeByLexExecutor 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.ZREMRANGEBYLEX));
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(), ERROR_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;
}
Collection<ByteArrayWrapper> removeList;
try {
removeList = getRange(key, keyRegion, context, Coder.stringToByteArrayWrapper(startString), Coder.stringToByteArrayWrapper(stopString), minInclusive, maxInclusive);
} catch (Exception e) {
throw new RuntimeException(e);
}
int numRemoved = 0;
for (ByteArrayWrapper entry : removeList) {
Object oldVal = keyRegion.remove(entry);
if (oldVal != null)
numRemoved++;
}
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), numRemoved));
}
use of org.apache.geode.redis.internal.DoubleWrapper in project geode by apache.
the class ZRemRangeByScoreExecutor 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.ZREMRANGEBYSCORE));
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(), 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 numRemoved = 0;
Collection<?> removeList = null;
try {
if (start == Double.NEGATIVE_INFINITY && stop == Double.POSITIVE_INFINITY && startInclusive && stopInclusive) {
numRemoved = keyRegion.size();
context.getRegionProvider().removeKey(key);
} else {
removeList = getKeys(context, key, keyRegion, start, stop, startInclusive, stopInclusive);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
if (removeList != null) {
for (Object entry : removeList) {
ByteArrayWrapper remove = null;
if (entry instanceof Entry)
remove = (ByteArrayWrapper) ((Entry<?, ?>) entry).getKey();
else if (entry instanceof Struct)
remove = (ByteArrayWrapper) ((Struct) entry).getFieldValues()[0];
Object oldVal = keyRegion.remove(remove);
if (oldVal != null)
numRemoved++;
if (keyRegion.isEmpty())
context.getRegionProvider().removeKey(key);
}
}
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), numRemoved));
}
use of org.apache.geode.redis.internal.DoubleWrapper 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));
}
Aggregations