use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class SPopExecutor 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.SPOP));
return;
}
ByteArrayWrapper key = command.getKey();
@SuppressWarnings("unchecked") Region<ByteArrayWrapper, Boolean> keyRegion = (Region<ByteArrayWrapper, Boolean>) context.getRegionProvider().getRegion(key);
if (keyRegion == null || keyRegion.isEmpty()) {
command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
return;
}
Random rand = new Random();
ByteArrayWrapper[] entries = keyRegion.keySet().toArray(new ByteArrayWrapper[keyRegion.size()]);
ByteArrayWrapper pop = entries[rand.nextInt(entries.length)];
keyRegion.remove(pop);
if (keyRegion.isEmpty()) {
context.getRegionProvider().removeKey(key);
}
command.setResponse(Coder.getBulkStringResponse(context.getByteBufAllocator(), pop.toBytes()));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class SRandMemberExecutor 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.SRANDMEMBER));
return;
}
ByteArrayWrapper key = command.getKey();
@SuppressWarnings("unchecked") Region<ByteArrayWrapper, Boolean> keyRegion = (Region<ByteArrayWrapper, Boolean>) context.getRegionProvider().getRegion(key);
int count = 1;
if (commandElems.size() > 2) {
try {
count = Coder.bytesToInt(commandElems.get(2));
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_NUMERIC));
return;
}
}
if (keyRegion == null || count == 0) {
command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
return;
}
int members = keyRegion.size();
if (members <= count && count != 1) {
command.setResponse(Coder.getBulkStringArrayResponse(context.getByteBufAllocator(), new HashSet<ByteArrayWrapper>(keyRegion.keySet())));
return;
}
Random rand = new Random();
ByteArrayWrapper[] entries = keyRegion.keySet().toArray(new ByteArrayWrapper[members]);
if (count == 1) {
ByteArrayWrapper randEntry = entries[rand.nextInt(entries.length)];
command.setResponse(Coder.getBulkStringResponse(context.getByteBufAllocator(), randEntry.toBytes()));
} else if (count > 0) {
Set<ByteArrayWrapper> randEntries = new HashSet<ByteArrayWrapper>();
do {
ByteArrayWrapper s = entries[rand.nextInt(entries.length)];
randEntries.add(s);
} while (randEntries.size() < count);
command.setResponse(Coder.getBulkStringArrayResponse(context.getByteBufAllocator(), randEntries));
} else {
count = -count;
List<ByteArrayWrapper> randEntries = new ArrayList<ByteArrayWrapper>();
for (int i = 0; i < count; i++) {
ByteArrayWrapper s = entries[rand.nextInt(entries.length)];
randEntries.add(s);
}
command.setResponse(Coder.getBulkStringArrayResponse(context.getByteBufAllocator(), randEntries));
}
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class SRemExecutor 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.SREM));
return;
}
ByteArrayWrapper key = command.getKey();
checkDataType(key, RedisDataType.REDIS_SET, context);
@SuppressWarnings("unchecked") Region<ByteArrayWrapper, Boolean> keyRegion = (Region<ByteArrayWrapper, Boolean>) context.getRegionProvider().getRegion(key);
if (keyRegion == null) {
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), NONE_REMOVED));
return;
}
int numRemoved = 0;
for (int i = 2; i < commandElems.size(); i++) {
Object oldVal;
oldVal = keyRegion.remove(new ByteArrayWrapper(commandElems.get(i)));
if (oldVal != null)
numRemoved++;
}
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), numRemoved));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class LTrimExecutor 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.LTRIM));
return;
}
ByteArrayWrapper key = command.getKey();
byte[] startArray = commandElems.get(2);
byte[] stopArray = commandElems.get(3);
int redisStart;
int redisStop;
checkDataType(key, RedisDataType.REDIS_LIST, context);
Region keyRegion = getRegion(context, key);
if (keyRegion == null) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_KEY_NOT_EXISTS));
return;
}
int listSize = keyRegion.size() - LIST_EMPTY_SIZE;
if (listSize == 0) {
command.setResponse(Coder.getSimpleStringResponse(context.getByteBufAllocator(), SUCCESS));
return;
}
try {
redisStart = Coder.bytesToInt(startArray);
redisStop = Coder.bytesToInt(stopArray);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_NUMERIC));
return;
}
redisStart = getBoundedStartIndex(redisStart, listSize);
redisStop = getBoundedEndIndex(redisStop, listSize);
redisStart = Math.min(redisStart, listSize - 1);
redisStop = Math.min(redisStop, listSize - 1);
if (redisStart == 0 && redisStop == listSize - 1) {
command.setResponse(Coder.getSimpleStringResponse(context.getByteBufAllocator(), SUCCESS));
return;
} else if (redisStart == 0 && redisStop < redisStart) {
context.getRegionProvider().removeKey(key, RedisDataType.REDIS_LIST);
command.setResponse(Coder.getSimpleStringResponse(context.getByteBufAllocator(), SUCCESS));
return;
}
List<Integer> keepList;
try {
keepList = getRange(context, key, redisStart, redisStop, keyRegion);
} catch (Exception e) {
throw new RuntimeException(e);
}
for (Object keyElement : keyRegion.keySet()) {
if (!keepList.contains(keyElement) && keyElement instanceof Integer)
keyRegion.remove(keyElement);
}
// Reset indexes in meta data region
keyRegion.put("head", keepList.get(0));
keyRegion.put("tail", keepList.get(keepList.size() - 1));
command.setResponse(Coder.getSimpleStringResponse(context.getByteBufAllocator(), SUCCESS));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class IncrByExecutor 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.INCRBY));
return;
}
ByteArrayWrapper key = command.getKey();
checkAndSetDataType(key, context);
ByteArrayWrapper valueWrapper = r.get(key);
/*
* Try increment
*/
byte[] incrArray = commandElems.get(INCREMENT_INDEX);
Long increment;
try {
increment = Coder.bytesToLong(incrArray);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_INCREMENT_NOT_USABLE));
return;
}
if (valueWrapper == null) {
r.put(key, new ByteArrayWrapper(incrArray));
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), increment));
return;
}
/*
* Value exists
*/
String stringValue = Coder.bytesToString(valueWrapper.toBytes());
Long value;
try {
value = Long.parseLong(stringValue);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_VALUE_NOT_USABLE));
return;
}
/*
* Check for overflow
*/
if (value >= 0 && increment > (Long.MAX_VALUE - value)) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_OVERFLOW));
return;
}
value += increment;
stringValue = "" + value;
r.put(key, new ByteArrayWrapper(Coder.stringToBytes(stringValue)));
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), value));
}
Aggregations