use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class SetNXExecutor 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.SETNX));
return;
}
ByteArrayWrapper key = command.getKey();
checkAndSetDataType(key, context);
byte[] value = commandElems.get(VALUE_INDEX);
Object oldValue = r.putIfAbsent(key, new ByteArrayWrapper(value));
if (oldValue != null)
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), NOT_SET));
else
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), SET));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class SetRangeExecutor 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(), ArityDef.SETRANGE));
return;
}
ByteArrayWrapper key = command.getKey();
checkAndSetDataType(key, context);
ByteArrayWrapper wrapper = r.get(key);
int offset;
byte[] value = commandElems.get(3);
try {
byte[] offAr = commandElems.get(2);
offset = Coder.bytesToInt(offAr);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_INT));
return;
}
int totalLength = offset + value.length;
if (offset < 0 || totalLength > 536870911) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_ILLEGAL_OFFSET));
return;
} else if (value.length == 0) {
int length = wrapper == null ? 0 : wrapper.toBytes().length;
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), length));
if (wrapper == null)
context.getRegionProvider().removeKey(key);
return;
}
if (wrapper == null) {
byte[] bytes = new byte[totalLength];
System.arraycopy(value, 0, bytes, offset, value.length);
r.put(key, new ByteArrayWrapper(bytes));
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), bytes.length));
} else {
byte[] bytes = wrapper.toBytes();
int returnLength;
if (totalLength < bytes.length) {
System.arraycopy(value, 0, bytes, offset, value.length);
r.put(key, new ByteArrayWrapper(bytes));
returnLength = bytes.length;
} else {
byte[] newBytes = new byte[totalLength];
System.arraycopy(bytes, 0, newBytes, 0, bytes.length);
System.arraycopy(value, 0, newBytes, offset, value.length);
returnLength = newBytes.length;
r.put(key, new ByteArrayWrapper(newBytes));
}
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), returnLength));
}
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class StrlenExecutor 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.STRLEN));
return;
}
ByteArrayWrapper key = command.getKey();
checkDataType(key, RedisDataType.REDIS_STRING, context);
ByteArrayWrapper valueWrapper = r.get(key);
if (valueWrapper == null)
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), KEY_DOES_NOT_EXIST));
else
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), valueWrapper.toBytes().length));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class DecrExecutor method executeCommand.
@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
List<byte[]> commandElems = command.getProcessedCommand();
RegionProvider rC = context.getRegionProvider();
Region<ByteArrayWrapper, ByteArrayWrapper> r = rC.getStringsRegion();
;
if (commandElems.size() < 2) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.DECR));
return;
}
ByteArrayWrapper key = command.getKey();
checkAndSetDataType(key, context);
ByteArrayWrapper valueWrapper = r.get(key);
if (valueWrapper == null) {
byte[] newValue = INIT_VALUE_BYTES;
r.put(key, new ByteArrayWrapper(newValue));
rC.metaPut(key, RedisDataType.REDIS_STRING);
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.MIN_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 GetBitExecutor 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.GETBIT));
return;
}
ByteArrayWrapper key = command.getKey();
checkAndSetDataType(key, context);
ByteArrayWrapper wrapper = r.get(key);
if (wrapper == null) {
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), 0));
return;
}
int bit = 0;
byte[] bytes = wrapper.toBytes();
int offset;
try {
byte[] offAr = commandElems.get(2);
offset = Coder.bytesToInt(offAr);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_INT));
return;
}
if (offset < 0)
offset += bytes.length * 8;
if (offset < 0 || offset > bytes.length * 8) {
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), 0));
return;
}
int byteIndex = offset / 8;
offset %= 8;
if (byteIndex >= bytes.length) {
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), 0));
return;
}
bit = (bytes[byteIndex] & (0x80 >> offset)) >> (7 - offset);
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), bit));
}
Aggregations