use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class GetRangeExecutor 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.GETRANGE));
return;
}
ByteArrayWrapper key = command.getKey();
checkDataType(key, RedisDataType.REDIS_STRING, context);
ByteArrayWrapper valueWrapper = r.get(key);
if (valueWrapper == null) {
command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
return;
}
byte[] value = valueWrapper.toBytes();
int length = value.length;
long start;
long end;
try {
byte[] startI = commandElems.get(startIndex);
byte[] stopI = commandElems.get(stopIndex);
start = Coder.bytesToLong(startI);
end = Coder.bytesToLong(stopI);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_INT));
return;
}
start = getBoundedStartIndex(start, length);
end = getBoundedEndIndex(end, length);
/*
* If the properly formatted indexes are illegal, send nil
*/
if (start > end || start == length) {
command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
return;
}
/*
* 1 is added to end because the end in copyOfRange is exclusive but in Redis it is inclusive
*/
if (end != length)
end++;
byte[] returnRange = Arrays.copyOfRange(value, (int) start, (int) end);
if (returnRange == null || returnRange.length == 0) {
command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
return;
}
command.setResponse(Coder.getBulkStringResponse(context.getByteBufAllocator(), returnRange));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class GeodeRedisServer method afterKeyDestroy.
/**
* When a key is removed then this function will make sure the associated queries with the key are
* also removed from each vm to avoid unnecessary data retention
*/
private void afterKeyDestroy(EntryEvent<String, RedisDataType> event) {
if (event.isOriginRemote()) {
final String key = (String) event.getKey();
final RedisDataType value = event.getOldValue();
if (value != null && value != RedisDataType.REDIS_STRING && value != RedisDataType.REDIS_HLL && value != RedisDataType.REDIS_PROTECTED) {
ByteArrayWrapper kW = Coder.stringToByteArrayWrapper(key);
Region<?, ?> r = this.regionCache.getRegion(kW);
if (r != null) {
this.regionCache.removeRegionReferenceLocally(kW, value);
}
}
}
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class GeodeRedisServer method initializeRedis.
private void initializeRedis() {
synchronized (this.cache) {
Region<ByteArrayWrapper, ByteArrayWrapper> stringsRegion;
Region<ByteArrayWrapper, HyperLogLogPlus> hLLRegion;
Region<String, RedisDataType> redisMetaData;
InternalCache gemFireCache = (InternalCache) cache;
try {
if ((stringsRegion = cache.getRegion(STRING_REGION)) == null) {
RegionFactory<ByteArrayWrapper, ByteArrayWrapper> regionFactory = gemFireCache.createRegionFactory(this.DEFAULT_REGION_TYPE);
stringsRegion = regionFactory.create(STRING_REGION);
}
if ((hLLRegion = cache.getRegion(HLL_REGION)) == null) {
RegionFactory<ByteArrayWrapper, HyperLogLogPlus> regionFactory = gemFireCache.createRegionFactory(this.DEFAULT_REGION_TYPE);
hLLRegion = regionFactory.create(HLL_REGION);
}
if ((redisMetaData = cache.getRegion(REDIS_META_DATA_REGION)) == null) {
AttributesFactory af = new AttributesFactory();
af.addCacheListener(metaListener);
af.setDataPolicy(DataPolicy.REPLICATE);
InternalRegionArguments ira = new InternalRegionArguments().setInternalRegion(true).setIsUsedForMetaRegion(true);
redisMetaData = gemFireCache.createVMRegion(REDIS_META_DATA_REGION, af.create(), ira);
}
} catch (IOException | ClassNotFoundException e) {
// only if loading snapshot, not here
InternalGemFireError assErr = new InternalGemFireError(LocalizedStrings.GemFireCache_UNEXPECTED_EXCEPTION.toLocalizedString());
assErr.initCause(e);
throw assErr;
}
this.regionCache = new RegionProvider(stringsRegion, hLLRegion, redisMetaData, expirationFutures, expirationExecutor, this.DEFAULT_REGION_TYPE);
redisMetaData.put(REDIS_META_DATA_REGION, RedisDataType.REDIS_PROTECTED);
redisMetaData.put(HLL_REGION, RedisDataType.REDIS_PROTECTED);
redisMetaData.put(STRING_REGION, RedisDataType.REDIS_PROTECTED);
}
checkForRegions();
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class HLenExecutor 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.HLEN));
return;
}
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(), NOT_EXISTS));
return;
}
final int regionSize = keyRegion.size();
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), regionSize));
}
use of org.apache.geode.redis.internal.ByteArrayWrapper in project geode by apache.
the class HMSetExecutor method executeCommand.
@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
List<byte[]> commandElems = command.getProcessedCommand();
if (commandElems.size() < 3 || commandElems.size() % 2 == 1) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.HMSET));
return;
}
ByteArrayWrapper key = command.getKey();
Region<ByteArrayWrapper, ByteArrayWrapper> keyRegion = getOrCreateRegion(context, key, RedisDataType.REDIS_HASH);
Map<ByteArrayWrapper, ByteArrayWrapper> map = new HashMap<ByteArrayWrapper, ByteArrayWrapper>();
for (int i = 2; i < commandElems.size(); i += 2) {
byte[] fieldArray = commandElems.get(i);
ByteArrayWrapper field = new ByteArrayWrapper(fieldArray);
byte[] value = commandElems.get(i + 1);
map.put(field, new ByteArrayWrapper(value));
}
keyRegion.putAll(map);
command.setResponse(Coder.getSimpleStringResponse(context.getByteBufAllocator(), SUCCESS));
}
Aggregations