Search in sources :

Example 11 with CompletableFutureWrapper

use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.

the class RedissonLocalCachedMap method fastRemoveOperationBatchAsync.

@Override
protected RFuture<List<Long>> fastRemoveOperationBatchAsync(@SuppressWarnings("unchecked") K... keys) {
    if (storeMode == LocalCachedMapOptions.StoreMode.LOCALCACHE) {
        return new CompletableFutureWrapper<>(Collections.emptyList());
    }
    if (invalidateEntryOnChange == 1) {
        List<Object> params = new ArrayList<Object>(keys.length * 2);
        for (K k : keys) {
            ByteBuf keyEncoded = encodeMapKey(k);
            params.add(keyEncoded);
            CacheKey cacheKey = localCacheView.toCacheKey(keyEncoded);
            cache.remove(cacheKey);
            ByteBuf msgEncoded = encode(new LocalCachedMapInvalidate(instanceId, cacheKey.getKeyHash()));
            params.add(msgEncoded);
        }
        return commandExecutor.evalWriteAsync(getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_LIST, "local result = {}; " + "for j = 1, #ARGV, 2 do " + "local val = redis.call('hdel', KEYS[1], ARGV[j]);" + "if val == 1 then " + "redis.call('publish', KEYS[2], ARGV[j+1]); " + "end;" + "table.insert(result, val);" + "end;" + "return result;", Arrays.<Object>asList(getRawName(), listener.getInvalidationTopicName()), params.toArray());
    }
    if (invalidateEntryOnChange == 2) {
        List<Object> params = new ArrayList<Object>(keys.length * 3);
        params.add(System.currentTimeMillis());
        for (K k : keys) {
            ByteBuf keyEncoded = encodeMapKey(k);
            params.add(keyEncoded);
            CacheKey cacheKey = localCacheView.toCacheKey(keyEncoded);
            cache.remove(cacheKey);
            ByteBuf msgEncoded = encode(new LocalCachedMapInvalidate(instanceId, cacheKey.getKeyHash()));
            params.add(msgEncoded);
            byte[] entryId = generateLogEntryId(cacheKey.getKeyHash());
            params.add(entryId);
        }
        return commandExecutor.evalWriteAsync(getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_LIST, "local result = {}; " + "for j = 2, #ARGV, 3 do " + "local val = redis.call('hdel', KEYS[1], ARGV[j]);" + "if val == 1 then " + "redis.call('zadd', KEYS[3], ARGV[1], ARGV[j+2]);" + "redis.call('publish', KEYS[2], ARGV[j+1]); " + "end;" + "table.insert(result, val);" + "end;" + "return result;", Arrays.<Object>asList(getRawName(), listener.getInvalidationTopicName(), listener.getUpdatesLogName()), params.toArray());
    }
    List<Object> params = new ArrayList<Object>(keys.length);
    for (K k : keys) {
        ByteBuf keyEncoded = encodeMapKey(k);
        params.add(keyEncoded);
        CacheKey cacheKey = localCacheView.toCacheKey(keyEncoded);
        cache.remove(cacheKey);
    }
    RFuture<List<Long>> future = commandExecutor.evalWriteAsync(getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_LIST, "local result = {}; " + "for i = 1, #ARGV, 1 do " + "local val = redis.call('hdel', KEYS[1], ARGV[i]); " + "table.insert(result, val); " + "end;" + "return result;", Arrays.<Object>asList(getRawName()), params.toArray());
    return future;
}
Also used : CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) ByteBuf(io.netty.buffer.ByteBuf)

Example 12 with CompletableFutureWrapper

use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.

the class RedissonLocalCachedMap method removeAsync.

@Override
public RFuture<Boolean> removeAsync(Object key, Object value) {
    if (storeMode == LocalCachedMapOptions.StoreMode.LOCALCACHE) {
        ByteBuf mapKey = encodeMapKey(key);
        CacheKey cacheKey = localCacheView.toCacheKey(mapKey);
        if (cacheRemove(cacheKey, key, value)) {
            broadcastLocalCacheStore((V) value, mapKey, cacheKey);
            return new CompletableFutureWrapper<>(true);
        } else {
            mapKey.release();
            return new CompletableFutureWrapper<>(false);
        }
    }
    RFuture<Boolean> future = super.removeAsync(key, value);
    CompletionStage<Boolean> f = future.thenApply(res -> {
        if (res) {
            CacheKey cacheKey = localCacheView.toCacheKey(key);
            cache.remove(cacheKey);
        }
        return res;
    });
    return new CompletableFutureWrapper<>(f);
}
Also used : CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) ByteBuf(io.netty.buffer.ByteBuf)

Example 13 with CompletableFutureWrapper

use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.

the class RedissonLocalCachedMap method removeOperationAsync.

@Override
protected RFuture<V> removeOperationAsync(K key) {
    ByteBuf keyEncoded = encodeMapKey(key);
    CacheKey cacheKey = localCacheView.toCacheKey(keyEncoded);
    CacheValue value = cache.remove(cacheKey);
    if (storeMode == LocalCachedMapOptions.StoreMode.LOCALCACHE) {
        keyEncoded.release();
        LocalCachedMapInvalidate msg = new LocalCachedMapInvalidate(instanceId, cacheKey.getKeyHash());
        listener.getInvalidationTopic().publishAsync(msg);
        V val = null;
        if (value != null) {
            val = (V) value.getValue();
        }
        return new CompletableFutureWrapper<>(val);
    }
    byte[] entryId = generateLogEntryId(cacheKey.getKeyHash());
    ByteBuf msgEncoded = encode(new LocalCachedMapInvalidate(instanceId, cacheKey.getKeyHash()));
    return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_MAP_VALUE, "local v = redis.call('hget', KEYS[1], ARGV[1]); " + "if redis.call('hdel', KEYS[1], ARGV[1]) == 1 then " + "if ARGV[3] == '1' then " + "redis.call('publish', KEYS[2], ARGV[2]); " + "end; " + "if ARGV[3] == '2' then " + "redis.call('zadd', KEYS[3], ARGV[4], ARGV[5]);" + "redis.call('publish', KEYS[2], ARGV[2]); " + "end;" + "end; " + "return v", Arrays.<Object>asList(getRawName(), listener.getInvalidationTopicName(), listener.getUpdatesLogName()), keyEncoded, msgEncoded, invalidateEntryOnChange, System.currentTimeMillis(), entryId);
}
Also used : CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) ByteBuf(io.netty.buffer.ByteBuf)

Example 14 with CompletableFutureWrapper

use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.

the class RedissonLocalCachedMap method putIfAbsentAsync.

@Override
public RFuture<V> putIfAbsentAsync(K key, V value) {
    if (storeMode == LocalCachedMapOptions.StoreMode.LOCALCACHE) {
        ByteBuf mapKey = encodeMapKey(key);
        CacheKey cacheKey = localCacheView.toCacheKey(mapKey);
        CacheValue prevValue = cachePutIfAbsent(cacheKey, key, value);
        if (prevValue == null) {
            broadcastLocalCacheStore((V) value, mapKey, cacheKey);
            return new CompletableFutureWrapper((Void) null);
        } else {
            mapKey.release();
            return new CompletableFutureWrapper<>((V) prevValue.getValue());
        }
    }
    RFuture<V> future = super.putIfAbsentAsync(key, value);
    CompletionStage<V> f = future.thenApply(res -> {
        if (res == null) {
            CacheKey cacheKey = localCacheView.toCacheKey(key);
            cachePut(cacheKey, key, value);
        }
        return res;
    });
    return new CompletableFutureWrapper<>(f);
}
Also used : CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) ByteBuf(io.netty.buffer.ByteBuf)

Example 15 with CompletableFutureWrapper

use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.

the class RedissonReliableTopic method addListenerAsync.

@Override
public <M> RFuture<String> addListenerAsync(Class<M> type, MessageListener<M> listener) {
    String id = generateId();
    listeners.put(id, new Entry(type, listener));
    if (subscriberId.get() != null) {
        return RedissonPromise.newSucceededFuture(id);
    }
    if (subscriberId.compareAndSet(null, id)) {
        renewExpiration();
        StreamMessageId startId = StreamMessageId.ALL;
        RFuture<Void> addFuture = commandExecutor.evalWriteNoRetryAsync(getRawName(), StringCodec.INSTANCE, RedisCommands.EVAL_VOID, "local value = redis.call('incr', KEYS[3]); " + "redis.call('zadd', KEYS[4], ARGV[3], ARGV[2]); " + "redis.call('zadd', KEYS[1], value, ARGV[2]); " + "redis.call('hset', KEYS[2], ARGV[2], ARGV[1]); ", Arrays.asList(getSubscribersName(), getMapName(), getCounter(), getTimeout()), startId, id, System.currentTimeMillis() + commandExecutor.getConnectionManager().getCfg().getReliableTopicWatchdogTimeout());
        CompletionStage<String> f = addFuture.thenApply(r -> {
            poll(id, startId);
            return id;
        });
        return new CompletableFutureWrapper<>(f);
    }
    return RedissonPromise.newSucceededFuture(id);
}
Also used : CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) StreamMessageId(org.redisson.api.StreamMessageId)

Aggregations

CompletableFutureWrapper (org.redisson.misc.CompletableFutureWrapper)38 ByteBuf (io.netty.buffer.ByteBuf)16 AtomicLong (java.util.concurrent.atomic.AtomicLong)9 RFuture (org.redisson.api.RFuture)8 CompletableFuture (java.util.concurrent.CompletableFuture)7 StringCodec (org.redisson.client.codec.StringCodec)7 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 ByteBufUtil (io.netty.buffer.ByteBufUtil)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 CommandAsyncExecutor (org.redisson.command.CommandAsyncExecutor)5 Timeout (io.netty.util.Timeout)4 java.util.concurrent (java.util.concurrent)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 RSemaphore (org.redisson.api.RSemaphore)4 RTopic (org.redisson.api.RTopic)4 RedissonClient (org.redisson.api.RedissonClient)4 RedisCommand (org.redisson.client.protocol.RedisCommand)4 TimerTask (io.netty.util.TimerTask)3