Search in sources :

Example 1 with NumberConvertor

use of org.redisson.client.protocol.convertor.NumberConvertor in project redisson by redisson.

the class RedissonMapCache method addAndGetOperationAsync.

@Override
protected RFuture<V> addAndGetOperationAsync(K key, Number value) {
    ByteBuf keyState = encodeMapKey(key);
    String name = getRawName(key);
    return commandExecutor.evalWriteAsync(name, StringCodec.INSTANCE, new RedisCommand<Object>("EVAL", new NumberConvertor(value.getClass())), "local value = redis.call('hget', KEYS[1], ARGV[2]); " + "local expireDate = 92233720368547758; " + "local t = 0; " + "local val = 0; " + "if value ~= false then " + "t, val = struct.unpack('dLc0', value); " + "local expireDateScore = redis.call('zscore', KEYS[2], ARGV[2]); " + "if expireDateScore ~= false then " + "expireDate = tonumber(expireDateScore) " + "end; " + "if t ~= 0 then " + "local expireIdle = redis.call('zscore', KEYS[3], ARGV[2]); " + "if expireIdle ~= false then " + "if tonumber(expireIdle) > tonumber(ARGV[1]) then " + "redis.call('zadd', KEYS[3], t + tonumber(ARGV[1]), ARGV[2]); " + "end; " + "expireDate = math.min(expireDate, tonumber(expireIdle)) " + "end; " + "end; " + "end; " + "local newValue = tonumber(ARGV[3]); " + "if value ~= false and expireDate > tonumber(ARGV[1]) then " + "newValue = tonumber(val) + newValue; " + "local msg = struct.pack('Lc0Lc0Lc0', string.len(ARGV[2]), ARGV[2], string.len(newValue), newValue, string.len(val), val); " + "redis.call('publish', KEYS[5], msg); " + "else " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[2]), ARGV[2], string.len(ARGV[3]), ARGV[3]); " + "redis.call('publish', KEYS[4], msg); " + "end; " + "local newValuePack = struct.pack('dLc0', t, string.len(newValue), newValue); " + "redis.call('hset', KEYS[1], ARGV[2], newValuePack); " + // last access time
    "local maxSize = tonumber(redis.call('hget', KEYS[8], 'max-size')); " + "if maxSize ~= nil and maxSize ~= 0 then " + "    local currentTime = tonumber(ARGV[1]); " + "    local lastAccessTimeSetName = KEYS[6]; " + "local mode = redis.call('hget', KEYS[8], 'mode'); " + "if mode == false or mode == 'LRU' then " + "redis.call('zadd', lastAccessTimeSetName, currentTime, ARGV[2]); " + "end; " + "    local cacheSize = tonumber(redis.call('hlen', KEYS[1])); " + "    if cacheSize > maxSize then " + "        local lruItems = redis.call('zrange', lastAccessTimeSetName, 0, cacheSize - maxSize - 1); " + "        for index, lruItem in ipairs(lruItems) do " + "            if lruItem and lruItem ~= ARGV[2] then " + "                local lruItemValue = redis.call('hget', KEYS[1], lruItem); " + "                redis.call('hdel', KEYS[1], lruItem); " + "                redis.call('zrem', KEYS[2], lruItem); " + "                redis.call('zrem', KEYS[3], lruItem); " + "                redis.call('zrem', lastAccessTimeSetName, lruItem); " + "                if lruItemValue ~= false then " + "                local removedChannelName = KEYS[7]; " + "local ttl, obj = struct.unpack('dLc0', lruItemValue);" + "                local msg = struct.pack('Lc0Lc0', string.len(lruItem), lruItem, string.len(obj), obj);" + "                redis.call('publish', removedChannelName, msg); " + "end; " + "            end; " + "        end; " + "    end; " + "if mode == 'LFU' then " + "redis.call('zincrby', lastAccessTimeSetName, 1, ARGV[2]); " + "end; " + "end; " + "return tostring(newValue); ", Arrays.<Object>asList(name, getTimeoutSetName(name), getIdleSetName(name), getCreatedChannelName(name), getUpdatedChannelName(name), getLastAccessTimeSetName(name), getRemovedChannelName(name), getOptionsName(name)), System.currentTimeMillis(), keyState, new BigDecimal(value.toString()).toPlainString());
}
Also used : NumberConvertor(org.redisson.client.protocol.convertor.NumberConvertor) ByteBuf(io.netty.buffer.ByteBuf) BigDecimal(java.math.BigDecimal)

Example 2 with NumberConvertor

use of org.redisson.client.protocol.convertor.NumberConvertor in project redisson by redisson.

the class RedissonReactiveNumberCommands method decrBy.

@Override
public <T extends Number> Flux<NumericResponse<DecrByCommand<T>, T>> decrBy(Publisher<DecrByCommand<T>> commands) {
    return execute(commands, key -> {
        Assert.notNull(key.getKey(), "Key must not be null!");
        Assert.notNull(key.getValue(), "Value must not be null!");
        byte[] keyBuf = toByteArray(key.getKey());
        Mono<T> m = write(keyBuf, StringCodec.INSTANCE, new RedisCommand<Object>("INCRBYFLOAT", new NumberConvertor(key.getValue().getClass())), keyBuf, "-" + new BigDecimal(key.getValue().toString()).toPlainString());
        return m.map(v -> new NumericResponse<>(key, v));
    });
}
Also used : NumberConvertor(org.redisson.client.protocol.convertor.NumberConvertor) BigDecimal(java.math.BigDecimal)

Example 3 with NumberConvertor

use of org.redisson.client.protocol.convertor.NumberConvertor in project redisson by redisson.

the class RedissonMap method addAndGetOperationAsync.

protected RFuture<V> addAndGetOperationAsync(K key, Number value) {
    ByteBuf keyState = encodeMapKey(key);
    String name = getRawName(key);
    RFuture<V> future = commandExecutor.writeAsync(name, StringCodec.INSTANCE, new RedisCommand<>("HINCRBYFLOAT", new NumberConvertor(value.getClass())), name, keyState, new BigDecimal(value.toString()).toPlainString());
    return future;
}
Also used : NumberConvertor(org.redisson.client.protocol.convertor.NumberConvertor) ByteBuf(io.netty.buffer.ByteBuf) BigDecimal(java.math.BigDecimal)

Example 4 with NumberConvertor

use of org.redisson.client.protocol.convertor.NumberConvertor in project redisson by redisson.

the class RedissonLocalCachedMap method addAndGetOperationAsync.

@Override
protected RFuture<V> addAndGetOperationAsync(K key, Number value) {
    ByteBuf keyState = encodeMapKey(key);
    CacheKey cacheKey = localCacheView.toCacheKey(keyState);
    ByteBuf msg = encode(new LocalCachedMapInvalidate(instanceId, cacheKey.getKeyHash()));
    byte[] entryId = generateLogEntryId(cacheKey.getKeyHash());
    RFuture<V> future = commandExecutor.evalWriteAsync(getRawName(), StringCodec.INSTANCE, new RedisCommand<Object>("EVAL", new NumberConvertor(value.getClass())), "local result = redis.call('HINCRBYFLOAT', KEYS[1], ARGV[1], ARGV[2]); " + "if ARGV[3] == '1' then " + "redis.call('publish', KEYS[2], ARGV[4]); " + "end;" + "if ARGV[3] == '2' then " + "redis.call('zadd', KEYS[3], ARGV[5], ARGV[6]);" + "redis.call('publish', KEYS[2], ARGV[4]); " + "end;" + "return result; ", Arrays.<Object>asList(getRawName(), listener.getInvalidationTopicName(), listener.getUpdatesLogName()), keyState, new BigDecimal(value.toString()).toPlainString(), invalidateEntryOnChange, msg, System.currentTimeMillis(), entryId);
    CompletionStage<V> f = future.thenApply(res -> {
        if (res != null) {
            CacheKey cKey = localCacheView.toCacheKey(key);
            cachePut(cKey, key, res);
        }
        return res;
    });
    return new CompletableFutureWrapper<>(f);
}
Also used : NumberConvertor(org.redisson.client.protocol.convertor.NumberConvertor) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) ByteBuf(io.netty.buffer.ByteBuf) BigDecimal(java.math.BigDecimal)

Example 5 with NumberConvertor

use of org.redisson.client.protocol.convertor.NumberConvertor in project redisson by redisson.

the class BaseTransactionalMap method addAndGetOperationAsync.

protected RFuture<V> addAndGetOperationAsync(K key, Number value) {
    RPromise<V> result = new RedissonPromise<V>();
    long threadId = Thread.currentThread().getId();
    executeLocked(result, key, new Runnable() {

        @Override
        public void run() {
            HashValue keyHash = toKeyHash(key);
            MapEntry entry = state.get(keyHash);
            if (entry != null) {
                BigDecimal currentValue = BigDecimal.ZERO;
                if (entry != MapEntry.NULL) {
                    currentValue = (BigDecimal) entry.getValue();
                }
                BigDecimal res = currentValue.add(new BigDecimal(value.toString()));
                operations.add(new MapAddAndGetOperation(map, key, value, transactionId, threadId));
                state.put(keyHash, new MapEntry(key, res));
                if (deleted != null) {
                    deleted = false;
                }
                NumberConvertor convertor = new NumberConvertor(value.getClass());
                result.trySuccess((V) convertor.convert(res.toPlainString()));
                return;
            }
            map.getAsync(key).onComplete((r, e) -> {
                if (e != null) {
                    result.tryFailure(e);
                    return;
                }
                BigDecimal currentValue = new BigDecimal(r.toString());
                BigDecimal res = currentValue.add(new BigDecimal(value.toString()));
                operations.add(new MapAddAndGetOperation(map, key, value, transactionId, threadId));
                state.put(keyHash, new MapEntry(key, res));
                if (deleted != null) {
                    deleted = false;
                }
                NumberConvertor convertor = new NumberConvertor(value.getClass());
                result.trySuccess((V) convertor.convert(res.toPlainString()));
            });
        }
    });
    return result;
}
Also used : HashValue(org.redisson.misc.HashValue) RPromise(org.redisson.misc.RPromise) TransactionalOperation(org.redisson.transaction.operation.TransactionalOperation) Arrays(java.util.Arrays) CommandAsyncExecutor(org.redisson.command.CommandAsyncExecutor) UnlinkOperation(org.redisson.transaction.operation.UnlinkOperation) RedissonMultiLock(org.redisson.RedissonMultiLock) HashMap(java.util.HashMap) Hash(org.redisson.misc.Hash) org.redisson.transaction.operation.map(org.redisson.transaction.operation.map) RedisClient(org.redisson.client.RedisClient) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RFuture(org.redisson.api.RFuture) RMap(org.redisson.api.RMap) BigDecimal(java.math.BigDecimal) RedissonObject(org.redisson.RedissonObject) ByteBuf(io.netty.buffer.ByteBuf) ScanResult(org.redisson.ScanResult) TouchOperation(org.redisson.transaction.operation.TouchOperation) RLock(org.redisson.api.RLock) Map(java.util.Map) RedissonMap(org.redisson.RedissonMap) MapScanResult(org.redisson.client.protocol.decoder.MapScanResult) Iterator(java.util.Iterator) Collection(java.util.Collection) RedissonPromise(org.redisson.misc.RedissonPromise) Set(java.util.Set) DeleteOperation(org.redisson.transaction.operation.DeleteOperation) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) NumberConvertor(org.redisson.client.protocol.convertor.NumberConvertor) Entry(java.util.Map.Entry) RedissonPromise(org.redisson.misc.RedissonPromise) NumberConvertor(org.redisson.client.protocol.convertor.NumberConvertor) HashValue(org.redisson.misc.HashValue) BigDecimal(java.math.BigDecimal)

Aggregations

BigDecimal (java.math.BigDecimal)5 NumberConvertor (org.redisson.client.protocol.convertor.NumberConvertor)5 ByteBuf (io.netty.buffer.ByteBuf)4 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 RedissonMap (org.redisson.RedissonMap)1 RedissonMultiLock (org.redisson.RedissonMultiLock)1 RedissonObject (org.redisson.RedissonObject)1 ScanResult (org.redisson.ScanResult)1 RFuture (org.redisson.api.RFuture)1