use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class HScanExecutor 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.HSCAN));
return;
}
ByteArrayWrapper key = command.getKey();
@SuppressWarnings("unchecked") Region<ByteArrayWrapper, ByteArrayWrapper> keyRegion = (Region<ByteArrayWrapper, ByteArrayWrapper>) context.getRegionProvider().getRegion(key);
checkDataType(key, RedisDataType.REDIS_HASH, context);
if (keyRegion == null) {
command.setResponse(Coder.getScanResponse(context.getByteBufAllocator(), new ArrayList<String>()));
return;
}
byte[] cAr = commandElems.get(2);
String cursorString = Coder.bytesToString(cAr);
int cursor = 0;
Pattern matchPattern = null;
String globMatchPattern = null;
int count = DEFUALT_COUNT;
try {
cursor = Integer.parseInt(cursorString);
} 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);
String tmp = Coder.bytesToString(bytes);
if (tmp.equalsIgnoreCase("MATCH")) {
bytes = commandElems.get(4);
globMatchPattern = Coder.bytesToString(bytes);
} else if (tmp.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);
String tmp = Coder.bytesToString(bytes);
if (tmp.equalsIgnoreCase("MATCH")) {
bytes = commandElems.get(6);
globMatchPattern = Coder.bytesToString(bytes);
} else if (tmp.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<Object> returnList = getIteration(new HashSet(keyRegion.entrySet()), matchPattern, count, cursor);
command.setResponse(Coder.getScanResponse(context.getByteBufAllocator(), returnList));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class HSetExecutor 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;
}
ByteArrayWrapper key = command.getKey();
Region<ByteArrayWrapper, ByteArrayWrapper> keyRegion = getOrCreateRegion(context, key, RedisDataType.REDIS_HASH);
byte[] byteField = commandElems.get(FIELD_INDEX);
ByteArrayWrapper field = new ByteArrayWrapper(byteField);
byte[] value = commandElems.get(VALUE_INDEX);
Object oldValue;
if (onlySetOnAbsent())
oldValue = keyRegion.putIfAbsent(field, new ByteArrayWrapper(value));
else
oldValue = keyRegion.put(field, new ByteArrayWrapper(value));
if (oldValue == null)
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), NEW_FIELD));
else
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), EXISTING_FIELD));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class HDelExecutor 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.HDEL));
return;
}
int numDeleted = 0;
ByteArrayWrapper key = command.getKey();
checkDataType(key, RedisDataType.REDIS_HASH, context);
Region<ByteArrayWrapper, ByteArrayWrapper> keyRegion = getRegion(context, key);
if (keyRegion == null) {
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), numDeleted));
return;
}
for (int i = START_FIELDS_INDEX; i < commandElems.size(); i++) {
ByteArrayWrapper field = new ByteArrayWrapper(commandElems.get(i));
Object oldValue = keyRegion.remove(field);
if (oldValue != null)
numDeleted++;
}
if (keyRegion.isEmpty()) {
context.getRegionProvider().removeKey(key, RedisDataType.REDIS_HASH);
}
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), numDeleted));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class HGetExecutor 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.HGET));
return;
}
ByteArrayWrapper key = command.getKey();
checkDataType(key, RedisDataType.REDIS_HASH, context);
Region<ByteArrayWrapper, ByteArrayWrapper> keyRegion = getRegion(context, key);
if (keyRegion == null) {
command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
return;
}
byte[] byteField = commandElems.get(FIELD_INDEX);
ByteArrayWrapper field = new ByteArrayWrapper(byteField);
ByteArrayWrapper valueWrapper = keyRegion.get(field);
if (valueWrapper != null) {
command.setResponse(Coder.getBulkStringResponse(context.getByteBufAllocator(), valueWrapper.toBytes()));
} else
command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class HIncrByExecutor 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.HINCRBY));
return;
}
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;
}
ByteArrayWrapper key = command.getKey();
Region<ByteArrayWrapper, ByteArrayWrapper> keyRegion = getOrCreateRegion(context, key, RedisDataType.REDIS_HASH);
byte[] byteField = commandElems.get(FIELD_INDEX);
ByteArrayWrapper field = new ByteArrayWrapper(byteField);
/*
* Put incrememnt as value if field doesn't exist
*/
ByteArrayWrapper oldValue = keyRegion.get(field);
if (oldValue == null) {
keyRegion.put(field, new ByteArrayWrapper(incrArray));
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), increment));
return;
}
/*
* If the field did exist then increment the field
*/
long value;
try {
value = Long.parseLong(oldValue.toString());
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_FIELD_NOT_USABLE));
return;
}
/*
* Check for overflow
*/
if ((value >= 0 && increment > (Long.MAX_VALUE - value)) || (value <= 0 && increment < (Long.MIN_VALUE - value))) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_OVERFLOW));
return;
}
value += increment;
// String newValue = String.valueOf(value);
keyRegion.put(field, new ByteArrayWrapper(Coder.longToBytes(value)));
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), value));
}
Aggregations