use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class SetBitExecutor 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.SETBIT));
return;
}
ByteArrayWrapper key = command.getKey();
checkAndSetDataType(key, context);
ByteArrayWrapper wrapper = r.get(key);
long offset;
int value;
int returnBit = 0;
try {
byte[] offAr = commandElems.get(2);
byte[] valAr = commandElems.get(3);
offset = Coder.bytesToLong(offAr);
value = Coder.bytesToInt(valAr);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_INT));
return;
}
if (value != 0 && value != 1) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_VALUE));
return;
}
if (offset < 0 || offset > 4294967295L) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_ILLEGAL_OFFSET));
return;
}
int byteIndex = (int) (offset / 8);
offset %= 8;
if (wrapper == null) {
byte[] bytes = new byte[byteIndex + 1];
if (value == 1)
bytes[byteIndex] = (byte) (0x80 >> offset);
r.put(key, new ByteArrayWrapper(bytes));
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), 0));
} else {
byte[] bytes = wrapper.toBytes();
if (byteIndex < bytes.length)
returnBit = (bytes[byteIndex] & (0x80 >> offset)) >> (7 - offset);
else
returnBit = 0;
if (byteIndex < bytes.length) {
bytes[byteIndex] = value == 1 ? (byte) (bytes[byteIndex] | (0x80 >> offset)) : (byte) (bytes[byteIndex] & ~(0x80 >> offset));
r.put(key, new ByteArrayWrapper(bytes));
} else {
byte[] newBytes = new byte[byteIndex + 1];
System.arraycopy(bytes, 0, newBytes, 0, bytes.length);
newBytes[byteIndex] = value == 1 ? (byte) (newBytes[byteIndex] | (0x80 >> offset)) : (byte) (newBytes[byteIndex] & ~(0x80 >> offset));
r.put(key, new ByteArrayWrapper(newBytes));
}
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), returnBit));
}
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class SetExecutor 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.SET));
return;
}
ByteArrayWrapper key = command.getKey();
checkDataType(key, context);
byte[] value = commandElems.get(VALUE_INDEX);
ByteArrayWrapper valueWrapper = new ByteArrayWrapper(value);
// Set only if not exists
boolean NX = false;
// Set only if exists
boolean XX = false;
long expiration = 0L;
if (commandElems.size() >= 6) {
String elem4;
String elem5;
String elem6;
elem4 = Coder.bytesToString(commandElems.get(3));
elem5 = Coder.bytesToString(commandElems.get(4));
elem6 = Coder.bytesToString(commandElems.get(5));
if (elem4.equalsIgnoreCase("XX") || elem6.equalsIgnoreCase("XX"))
XX = true;
else if (elem4.equalsIgnoreCase("NX") || elem6.equalsIgnoreCase("NX"))
NX = true;
if (elem4.equalsIgnoreCase("PX"))
expiration = getExpirationMillis(elem4, elem5);
else if (elem5.equalsIgnoreCase("PX"))
expiration = getExpirationMillis(elem5, elem6);
else if (elem4.equalsIgnoreCase("EX"))
expiration = getExpirationMillis(elem4, elem5);
else if (elem5.equalsIgnoreCase("EX"))
expiration = getExpirationMillis(elem5, elem6);
} else if (commandElems.size() >= 5) {
String elem4;
String expiry;
elem4 = Coder.bytesToString(commandElems.get(3));
expiry = Coder.bytesToString(commandElems.get(4));
expiration = getExpirationMillis(elem4, expiry);
} else if (commandElems.size() >= 4) {
byte[] elem4 = commandElems.get(3);
if (elem4.length == 2 && Character.toUpperCase(elem4[1]) == 'X') {
if (Character.toUpperCase(elem4[0]) == 'N')
NX = true;
else if (Character.toUpperCase(elem4[0]) == 'X')
XX = true;
}
}
boolean keyWasSet = false;
if (NX)
keyWasSet = setNX(r, command, key, valueWrapper, context);
else if (XX)
keyWasSet = setXX(r, command, key, valueWrapper, context);
else {
checkAndSetDataType(key, context);
r.put(key, valueWrapper);
command.setResponse(Coder.getSimpleStringResponse(context.getByteBufAllocator(), SUCCESS));
keyWasSet = true;
}
if (keyWasSet && expiration > 0L) {
context.getRegionProvider().setExpiration(key, expiration);
}
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class MGetExecutor 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.MGET));
return;
}
Collection<ByteArrayWrapper> keys = new ArrayList<ByteArrayWrapper>();
for (int i = 1; i < commandElems.size(); i++) {
byte[] keyArray = commandElems.get(i);
ByteArrayWrapper key = new ByteArrayWrapper(keyArray);
/*
* try { checkDataType(key, RedisDataType.REDIS_STRING, context); } catch
* (RedisDataTypeMismatchException e) { keys.ad continue; }
*/
keys.add(key);
}
Map<ByteArrayWrapper, ByteArrayWrapper> results = r.getAll(keys);
Collection<ByteArrayWrapper> values = new ArrayList<ByteArrayWrapper>();
/*
* This is done to preserve order in the output
*/
for (ByteArrayWrapper key : keys) values.add(results.get(key));
command.setResponse(Coder.getBulkStringArrayResponse(context.getByteBufAllocator(), values));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class ZRemRangeByLexExecutor 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.ZREMRANGEBYLEX));
return;
}
ByteArrayWrapper key = command.getKey();
checkDataType(key, RedisDataType.REDIS_SORTEDSET, context);
Region<ByteArrayWrapper, DoubleWrapper> keyRegion = getRegion(context, key);
if (keyRegion == null) {
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), ERROR_NOT_EXISTS));
return;
}
boolean minInclusive = false;
boolean maxInclusive = false;
byte[] minArray = commandElems.get(2);
byte[] maxArray = commandElems.get(3);
String startString = Coder.bytesToString(minArray);
String stopString = Coder.bytesToString(maxArray);
if (minArray[0] == Coder.OPEN_BRACE_ID) {
startString = startString.substring(1);
minInclusive = false;
} else if (minArray[0] == Coder.OPEN_BRACKET_ID) {
startString = startString.substring(1);
minInclusive = true;
} else if (minArray[0] != Coder.HYPHEN_ID) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_ILLEGAL_SYNTAX));
return;
}
if (maxArray[0] == Coder.OPEN_BRACE_ID) {
stopString = stopString.substring(1);
maxInclusive = false;
} else if (maxArray[0] == Coder.OPEN_BRACKET_ID) {
stopString = stopString.substring(1);
maxInclusive = true;
} else if (maxArray[0] != Coder.PLUS_ID) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_ILLEGAL_SYNTAX));
return;
}
Collection<ByteArrayWrapper> removeList;
try {
removeList = getRange(key, keyRegion, context, Coder.stringToByteArrayWrapper(startString), Coder.stringToByteArrayWrapper(stopString), minInclusive, maxInclusive);
} catch (Exception e) {
throw new RuntimeException(e);
}
int numRemoved = 0;
for (ByteArrayWrapper entry : removeList) {
Object oldVal = keyRegion.remove(entry);
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 ZRemRangeByScoreExecutor 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.ZREMRANGEBYSCORE));
return;
}
ByteArrayWrapper key = command.getKey();
checkDataType(key, RedisDataType.REDIS_SORTEDSET, context);
Region<ByteArrayWrapper, DoubleWrapper> keyRegion = getRegion(context, key);
if (keyRegion == null) {
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), NOT_EXISTS));
return;
}
boolean startInclusive = true;
boolean stopInclusive = true;
double start;
double stop;
byte[] startArray = commandElems.get(2);
byte[] stopArray = commandElems.get(3);
String startString = Coder.bytesToString(startArray);
String stopString = Coder.bytesToString(stopArray);
if (startArray[0] == Coder.OPEN_BRACE_ID) {
startString = startString.substring(1);
startInclusive = false;
}
if (stopArray[0] == Coder.OPEN_BRACE_ID) {
stopString = stopString.substring(1);
stopInclusive = false;
}
try {
start = Coder.stringToDouble(startString);
stop = Coder.stringToDouble(stopString);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_NUMERIC));
return;
}
int numRemoved = 0;
Collection<?> removeList = null;
try {
if (start == Double.NEGATIVE_INFINITY && stop == Double.POSITIVE_INFINITY && startInclusive && stopInclusive) {
numRemoved = keyRegion.size();
context.getRegionProvider().removeKey(key);
} else {
removeList = getKeys(context, key, keyRegion, start, stop, startInclusive, stopInclusive);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
if (removeList != null) {
for (Object entry : removeList) {
ByteArrayWrapper remove = null;
if (entry instanceof Entry)
remove = (ByteArrayWrapper) ((Entry<?, ?>) entry).getKey();
else if (entry instanceof Struct)
remove = (ByteArrayWrapper) ((Struct) entry).getFieldValues()[0];
Object oldVal = keyRegion.remove(remove);
if (oldVal != null)
numRemoved++;
if (keyRegion.isEmpty())
context.getRegionProvider().removeKey(key);
}
}
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), numRemoved));
}
Aggregations