Search in sources :

Example 1 with RegionProvider

use of org.apache.geode.redis.internal.RegionProvider 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 2 with RegionProvider

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

Example 3 with RegionProvider

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

Example 4 with RegionProvider

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

Example 5 with RegionProvider

use of org.apache.geode.redis.internal.RegionProvider in project geode by apache.

the class SetOpExecutor method executeCommand.

@SuppressWarnings("unchecked")
@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
    List<byte[]> commandElems = command.getProcessedCommand();
    int setsStartIndex = isStorage() ? 2 : 1;
    if (commandElems.size() < setsStartIndex + 1) {
        command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), getArgsError()));
        return;
    }
    RegionProvider rC = context.getRegionProvider();
    ByteArrayWrapper destination = null;
    if (isStorage())
        destination = command.getKey();
    ByteArrayWrapper firstSetKey = new ByteArrayWrapper(commandElems.get(setsStartIndex++));
    if (!isStorage())
        checkDataType(firstSetKey, RedisDataType.REDIS_SET, context);
    Region<ByteArrayWrapper, Boolean> region = (Region<ByteArrayWrapper, Boolean>) rC.getRegion(firstSetKey);
    Set<ByteArrayWrapper> firstSet = null;
    if (region != null) {
        firstSet = new HashSet<ByteArrayWrapper>(region.keySet());
    }
    ArrayList<Set<ByteArrayWrapper>> setList = new ArrayList<Set<ByteArrayWrapper>>();
    for (int i = setsStartIndex; i < commandElems.size(); i++) {
        ByteArrayWrapper key = new ByteArrayWrapper(commandElems.get(i));
        checkDataType(key, RedisDataType.REDIS_SET, context);
        region = (Region<ByteArrayWrapper, Boolean>) rC.getRegion(key);
        if (region != null)
            setList.add(region.keySet());
        else if (this instanceof SInterExecutor)
            setList.add(null);
    }
    if (setList.isEmpty()) {
        if (isStorage()) {
            command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), 0));
            context.getRegionProvider().removeKey(destination);
        } else {
            if (firstSet == null)
                command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
            else
                command.setResponse(Coder.getBulkStringArrayResponse(context.getByteBufAllocator(), firstSet));
        }
        return;
    }
    Set<ByteArrayWrapper> resultSet = setOp(firstSet, setList);
    if (isStorage()) {
        // (Region<ByteArrayWrapper, Boolean>)
        Region<ByteArrayWrapper, Boolean> newRegion = null;
        // rC.getRegion(destination);
        rC.removeKey(destination);
        if (resultSet != null) {
            Map<ByteArrayWrapper, Boolean> map = new HashMap<ByteArrayWrapper, Boolean>();
            for (ByteArrayWrapper entry : resultSet) map.put(entry, Boolean.TRUE);
            if (!map.isEmpty()) {
                newRegion = (Region<ByteArrayWrapper, Boolean>) rC.getOrCreateRegion(destination, RedisDataType.REDIS_SET, context);
                newRegion.putAll(map);
            }
            command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), resultSet.size()));
        } else {
            command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), 0));
        }
    } else {
        if (resultSet == null || resultSet.isEmpty())
            command.setResponse(Coder.getEmptyArrayResponse(context.getByteBufAllocator()));
        else
            command.setResponse(Coder.getBulkStringArrayResponse(context.getByteBufAllocator(), resultSet));
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) RegionProvider(org.apache.geode.redis.internal.RegionProvider) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ByteArrayWrapper(org.apache.geode.redis.internal.ByteArrayWrapper) Region(org.apache.geode.cache.Region)

Aggregations

ByteArrayWrapper (org.apache.geode.redis.internal.ByteArrayWrapper)6 RegionProvider (org.apache.geode.redis.internal.RegionProvider)6 RedisDataType (org.apache.geode.redis.internal.RedisDataType)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 InternalGemFireError (org.apache.geode.InternalGemFireError)1 AttributesFactory (org.apache.geode.cache.AttributesFactory)1 Region (org.apache.geode.cache.Region)1 InternalCache (org.apache.geode.internal.cache.InternalCache)1 InternalRegionArguments (org.apache.geode.internal.cache.InternalRegionArguments)1 HyperLogLogPlus (org.apache.geode.internal.hll.HyperLogLogPlus)1