use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class HIncrByFloatExecutor 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.HINCRBYFLOAT));
return;
}
byte[] incrArray = commandElems.get(INCREMENT_INDEX);
Double increment;
try {
increment = Coder.bytesToDouble(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.getBulkStringResponse(context.getByteBufAllocator(), increment));
return;
}
/*
* If the field did exist then increment the field
*/
String valueS = oldValue.toString();
if (valueS.contains(" ")) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_FIELD_NOT_USABLE));
return;
}
Double value;
try {
value = Coder.stringToDouble(valueS);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_FIELD_NOT_USABLE));
return;
}
value += increment;
keyRegion.put(field, new ByteArrayWrapper(Coder.doubleToBytes(value)));
command.setResponse(Coder.getBulkStringResponse(context.getByteBufAllocator(), value));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class PersistExecutor 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.PERSIST));
return;
}
ByteArrayWrapper key = command.getKey();
boolean canceled = context.getRegionProvider().cancelKeyExpiration(key);
if (canceled)
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), TIMEOUT_REMOVED));
else
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), KEY_NOT_EXIST_OR_NO_TIMEOUT));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class TTLExecutor 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(), getArgsError()));
return;
}
ByteArrayWrapper key = command.getKey();
RegionProvider rC = context.getRegionProvider();
boolean exists = false;
RedisDataType val = rC.getRedisDataType(key);
if (val != null)
exists = true;
if (!exists) {
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), NOT_EXISTS));
return;
}
long ttl = rC.getExpirationDelayMillis(key);
if (ttl == 0L) {
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), NO_TIMEOUT));
return;
}
if (!timeUnitMillis())
ttl = ttl / millisInSecond;
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), ttl));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class ExpireAtExecutor 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(), getArgsError()));
return;
}
RegionProvider rC = context.getRegionProvider();
ByteArrayWrapper wKey = command.getKey();
byte[] timestampByteArray = commandElems.get(TIMESTAMP_INDEX);
long timestamp;
try {
timestamp = Coder.bytesToLong(timestampByteArray);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_TIMESTAMP_NOT_USABLE));
return;
}
if (!timeUnitMillis())
timestamp = timestamp * millisInSecond;
long currentTimeMillis = System.currentTimeMillis();
if (timestamp <= currentTimeMillis) {
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), NOT_SET));
return;
}
long delayMillis = timestamp - currentTimeMillis;
boolean expirationSet = false;
if (rC.hasExpiration(wKey))
expirationSet = rC.modifyExpiration(wKey, delayMillis);
else
expirationSet = rC.setExpiration(wKey, delayMillis);
if (expirationSet)
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 HValsExecutor 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.HVALS));
return;
}
ByteArrayWrapper key = command.getKey();
checkDataType(key, RedisDataType.REDIS_HASH, context);
Region<ByteArrayWrapper, ByteArrayWrapper> keyRegion = getRegion(context, key);
if (keyRegion == null) {
command.setResponse(Coder.getEmptyArrayResponse(context.getByteBufAllocator()));
return;
}
Collection<ByteArrayWrapper> vals = new ArrayList(keyRegion.values());
if (vals.isEmpty()) {
command.setResponse(Coder.getEmptyArrayResponse(context.getByteBufAllocator()));
return;
}
command.setResponse(Coder.getBulkStringArrayResponse(context.getByteBufAllocator(), vals));
}
Aggregations