use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class IncrByFloatExecutor 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.INCRBYFLOAT));
return;
}
ByteArrayWrapper key = command.getKey();
checkAndSetDataType(key, context);
ByteArrayWrapper valueWrapper = r.get(key);
/*
* Try increment
*/
byte[] incrArray = commandElems.get(INCREMENT_INDEX);
String doub = Coder.bytesToString(incrArray).toLowerCase();
if (doub.contains("inf") || doub.contains("nan")) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), "Increment would produce NaN or infinity"));
return;
} else if (valueWrapper != null && valueWrapper.toString().contains(" ")) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_VALUE_NOT_USABLE));
return;
}
Double increment;
try {
increment = Coder.stringToDouble(doub);
} 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.getBulkStringResponse(context.getByteBufAllocator(), increment));
return;
}
/*
* Value exists
*/
String stringValue = Coder.bytesToString(valueWrapper.toBytes());
Double value;
try {
value = Coder.stringToDouble(stringValue);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_VALUE_NOT_USABLE));
return;
}
/*
* Check for overflow
*/
if (value >= 0 && increment > (Double.MAX_VALUE - value)) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_OVERFLOW));
return;
}
double result = value + increment;
if (Double.isNaN(result) || Double.isInfinite(result)) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), RedisConstants.ERROR_NAN_INF_INCR));
return;
}
value += increment;
stringValue = "" + value;
r.put(key, new ByteArrayWrapper(Coder.stringToBytes(stringValue)));
command.setResponse(Coder.getBulkStringResponse(context.getByteBufAllocator(), value));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class IncrExecutor 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() < 2) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.INCR));
return;
}
ByteArrayWrapper key = command.getKey();
checkAndSetDataType(key, context);
ByteArrayWrapper valueWrapper = r.get(key);
if (valueWrapper == null) {
byte[] newValue = { Coder.NUMBER_1_BYTE };
r.put(key, new ByteArrayWrapper(newValue));
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), INIT_VALUE_INT));
return;
}
/*
* Value exists
*/
String stringValue = valueWrapper.toString();
Long value;
try {
value = Long.parseLong(stringValue);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_VALUE_NOT_USABLE));
return;
}
if (value == Long.MAX_VALUE) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_OVERFLOW));
return;
}
value++;
stringValue = "" + value;
r.put(key, new ByteArrayWrapper(Coder.stringToBytes(stringValue)));
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), value));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class MSetExecutor 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 || commandElems.size() % 2 == 0) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.MSET));
return;
}
Map<ByteArrayWrapper, ByteArrayWrapper> map = new HashMap<ByteArrayWrapper, ByteArrayWrapper>();
for (int i = 1; i < commandElems.size(); i += 2) {
byte[] keyArray = commandElems.get(i);
ByteArrayWrapper key = new ByteArrayWrapper(keyArray);
try {
checkAndSetDataType(key, context);
} catch (RedisDataTypeMismatchException e) {
continue;
}
byte[] value = commandElems.get(i + 1);
map.put(key, new ByteArrayWrapper(value));
}
r.putAll(map);
command.setResponse(Coder.getSimpleStringResponse(context.getByteBufAllocator(), SUCCESS));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class MSetNXExecutor 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 || commandElems.size() % 2 == 0) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.MSETNX));
return;
}
boolean hasEntry = false;
Map<ByteArrayWrapper, ByteArrayWrapper> map = new HashMap<ByteArrayWrapper, ByteArrayWrapper>();
for (int i = 1; i < commandElems.size(); i += 2) {
byte[] keyArray = commandElems.get(i);
ByteArrayWrapper key = new ByteArrayWrapper(keyArray);
try {
checkDataType(key, context);
} catch (RedisDataTypeMismatchException e) {
hasEntry = true;
break;
}
byte[] value = commandElems.get(i + 1);
map.put(key, new ByteArrayWrapper(value));
if (r.containsKey(key)) {
hasEntry = true;
break;
}
}
boolean successful = false;
if (!hasEntry) {
successful = true;
for (ByteArrayWrapper k : map.keySet()) {
try {
checkAndSetDataType(k, context);
} catch (RedisDataTypeMismatchException e) {
successful = false;
break;
}
}
r.putAll(map);
}
if (successful) {
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), SET));
} else {
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), NOT_SET));
}
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class SetEXExecutor 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() < 4) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), getArgsError()));
return;
}
ByteArrayWrapper key = command.getKey();
byte[] value = commandElems.get(VALUE_INDEX);
byte[] expirationArray = commandElems.get(2);
long expiration;
try {
expiration = Coder.bytesToLong(expirationArray);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_SECONDS_NOT_A_NUMBER));
return;
}
if (expiration <= 0) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_SECONDS_NOT_LEGAL));
return;
}
if (!timeUnitMillis())
expiration *= AbstractExecutor.millisInSecond;
checkAndSetDataType(key, context);
r.put(key, new ByteArrayWrapper(value));
context.getRegionProvider().setExpiration(key, expiration);
command.setResponse(Coder.getSimpleStringResponse(context.getByteBufAllocator(), SUCCESS));
}
Aggregations