Search in sources :

Example 41 with ByteArrayWrapper

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));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper)

Example 42 with ByteArrayWrapper

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);
            }
        }
    }
}
Also used : RedisDataType(org.apache.geode.redis.internal.RedisDataType) ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper)

Example 43 with ByteArrayWrapper

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();
}
Also used : RedisDataType(org.apache.geode.redis.internal.RedisDataType) RegionProvider(org.apache.geode.redis.internal.RegionProvider) InternalCache(org.apache.geode.internal.cache.InternalCache) InternalRegionArguments(org.apache.geode.internal.cache.InternalRegionArguments) IOException(java.io.IOException) AttributesFactory(org.apache.geode.cache.AttributesFactory) ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) HyperLogLogPlus(org.apache.geode.internal.hll.HyperLogLogPlus) InternalGemFireError(org.apache.geode.InternalGemFireError)

Example 44 with ByteArrayWrapper

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));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper)

Example 45 with ByteArrayWrapper

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));
}
Also used : ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) HashMap(java.util.HashMap)

Aggregations

ByteArrayWrapper (org.apache.geode.redis.internal.ByteArrayWrapper)92 DoubleWrapper (org.apache.geode.redis.internal.DoubleWrapper)16 ArrayList (java.util.ArrayList)15 Region (org.apache.geode.cache.Region)14 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 RegionProvider (org.apache.geode.redis.internal.RegionProvider)6 Struct (org.apache.geode.cache.query.Struct)5 RedisDataType (org.apache.geode.redis.internal.RedisDataType)5 HyperLogLogPlus (org.apache.geode.internal.hll.HyperLogLogPlus)4 Collection (java.util.Collection)3 List (java.util.List)3 Pattern (java.util.regex.Pattern)3 PatternSyntaxException (java.util.regex.PatternSyntaxException)3 FunctionDomainException (org.apache.geode.cache.query.FunctionDomainException)3 NameResolutionException (org.apache.geode.cache.query.NameResolutionException)3 QueryInvocationTargetException (org.apache.geode.cache.query.QueryInvocationTargetException)3 TypeMismatchException (org.apache.geode.cache.query.TypeMismatchException)3 Random (java.util.Random)2 Set (java.util.Set)2