use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class ZRangeByLexExecutor method getCustomBulkStringArrayResponse.
private ByteBuf getCustomBulkStringArrayResponse(Collection<ByteArrayWrapper> items, ExecutionHandlerContext context) {
Iterator<ByteArrayWrapper> it = items.iterator();
ByteBuf response = context.getByteBufAllocator().buffer();
response.writeByte(Coder.ARRAY_ID);
response.writeBytes(Coder.intToBytes(items.size()));
response.writeBytes(Coder.CRLFar);
while (it.hasNext()) {
ByteArrayWrapper next = it.next();
byte[] byteAr = next.toBytes();
response.writeByte(Coder.BULK_STRING_ID);
response.writeBytes(Coder.intToBytes(byteAr.length));
response.writeBytes(Coder.CRLFar);
response.writeBytes(byteAr);
response.writeBytes(Coder.CRLFar);
}
return response;
}
use of org.apache.geode.redis.internal.ByteArrayWrapper 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.ByteArrayWrapper 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));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper 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.ByteArrayWrapper in project geode by apache.
the class AppendExecutor 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() < 3) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.APPEND));
return;
}
ByteArrayWrapper key = command.getKey();
checkAndSetDataType(key, context);
ByteArrayWrapper string = r.get(key);
byte[] stringByteArray = commandElems.get(VALUE_INDEX);
if (string == null) {
r.put(key, new ByteArrayWrapper(stringByteArray));
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), stringByteArray.length));
} else {
byte[] newValue = concatArrays(string.toBytes(), stringByteArray);
string.setBytes(newValue);
r.put(key, string);
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), newValue.length));
}
}
Aggregations