All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.redisson.RedissonTimeSeries Maven / Gradle / Ivy

Go to download

Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC

There is a newer version: 3.43.0
Show newest version
/**
 * Copyright (c) 2013-2020 Nikita Koksharov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.redisson;

import org.redisson.api.RFuture;
import org.redisson.api.RTimeSeries;
import org.redisson.api.TimeSeriesEntry;
import org.redisson.client.RedisClient;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.LongCodec;
import org.redisson.client.protocol.RedisCommand;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.client.protocol.decoder.ListScanResult;
import org.redisson.client.protocol.decoder.TimeSeriesEntryReplayDecoder;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.eviction.EvictionScheduler;
import org.redisson.iterator.RedissonBaseIterator;
import org.redisson.misc.RedissonPromise;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

/**
 *
 * @author Nikita Koksharov
 *
 */
public class RedissonTimeSeries extends RedissonExpirable implements RTimeSeries {

    private final EvictionScheduler evictionScheduler;

    public RedissonTimeSeries(EvictionScheduler evictionScheduler, CommandAsyncExecutor connectionManager, String name) {
        super(connectionManager, name);

        this.evictionScheduler = evictionScheduler;
        if (evictionScheduler != null) {
            evictionScheduler.scheduleTimeSeries(getName(), getTimeoutSetName());
        }
    }

    public RedissonTimeSeries(Codec codec, EvictionScheduler evictionScheduler, CommandAsyncExecutor connectionManager, String name) {
        super(codec, connectionManager, name);

        this.evictionScheduler = evictionScheduler;
        if (evictionScheduler != null) {
            evictionScheduler.scheduleTimeSeries(getName(), getTimeoutSetName());
        }
    }

    String getTimeoutSetName() {
        return prefixName("redisson__ts_ttl", getName());
    }

    @Override
    public void add(long timestamp, V value) {
        addAll(Collections.singletonMap(timestamp, value));
    }

    @Override
    public RFuture addAsync(long timestamp, V object) {
        return addAllAsync(Collections.singletonMap(timestamp, object));
    }

    @Override
    public void addAll(Map objects) {
        addAll(objects, 0, null);
    }

    @Override
    public void add(long timestamp, V value, long timeToLive, TimeUnit timeUnit) {
        addAll(Collections.singletonMap(timestamp, value), timeToLive, timeUnit);
    }

    @Override
    public RFuture addAsync(long timestamp, V object, long timeToLive, TimeUnit timeUnit) {
        return addAllAsync(Collections.singletonMap(timestamp, object), timeToLive, timeUnit);
    }

    @Override
    public void addAll(Map objects, long timeToLive, TimeUnit timeUnit) {
        get(addAllAsync(objects, timeToLive, timeUnit));
    }

    @Override
    public RFuture addAllAsync(Map objects) {
        return addAllAsync(objects, 0, null);
    }

    @Override
    public RFuture addAllAsync(Map objects, long timeToLive, TimeUnit timeUnit) {
        long expirationTime = 92233720368547758L;
        if (timeToLive > 0) {
            expirationTime = System.currentTimeMillis() + timeUnit.toMillis(timeToLive);
        }

        List params = new ArrayList<>();
        for (Map.Entry entry : objects.entrySet()) {
            params.add(expirationTime);
            params.add(entry.getKey());
            params.add(encode(entry.getValue()));
        }

        return commandExecutor.evalWriteAsync(getName(), codec, RedisCommands.EVAL_VOID,
       "for i = 1, #ARGV, 3 do " +
                "local val = struct.pack('LLc0', tonumber(ARGV[i+1]), string.len(ARGV[i+2]), ARGV[i+2]); " +
                "redis.call('zadd', KEYS[1], ARGV[i+1], val); " +
                "redis.call('zadd', KEYS[2], ARGV[i], val); " +
             "end; ",
            Arrays.asList(getName(), getTimeoutSetName()),
            params.toArray());
    }

    @Override
    public int size() {
        return get(sizeAsync());
    }

    @Override
    public RFuture sizeAsync() {
        return commandExecutor.evalReadAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_INTEGER,
       "local values = redis.call('zrangebyscore', KEYS[2], 0, ARGV[1]);" +
             "return redis.call('zcard', KEYS[1]) - #values;",
            Arrays.asList(getName(), getTimeoutSetName()),
            System.currentTimeMillis());
    }

    @Override
    public V get(long timestamp) {
        return get(getAsync(timestamp));
    }

    @Override
    public RFuture getAsync(long timestamp) {
        return commandExecutor.evalReadAsync(getName(), codec, RedisCommands.EVAL_OBJECT,
       "local values = redis.call('zrangebyscore', KEYS[1], ARGV[2], ARGV[2]);" +
             "if #values == 0 then " +
                 "return nil;" +
             "end;" +

             "local expirationDate = redis.call('zscore', KEYS[2], values[1]); " +
             "if expirationDate ~= false and tonumber(expirationDate) <= tonumber(ARGV[1]) then " +
                 "return nil;" +
             "end;" +
             "local t, val = struct.unpack('LLc0', values[1]); " +
             "return val;",
            Arrays.asList(getName(), getTimeoutSetName()),
            System.currentTimeMillis(), timestamp);
    }

    @Override
    public boolean remove(long timestamp) {
        return get(removeAsync(timestamp));
    }

    @Override
    public RFuture removeAsync(long timestamp) {
        return commandExecutor.evalWriteAsync(getName(), codec, RedisCommands.EVAL_BOOLEAN,
       "local values = redis.call('zrangebyscore', KEYS[1], ARGV[2], ARGV[2]);" +
             "if #values == 0 then " +
                 "return 0;" +
             "end;" +

             "local expirationDate = redis.call('zscore', KEYS[2], values[1]); " +
             "if expirationDate ~= false and tonumber(expirationDate) <= tonumber(ARGV[1]) then " +
                 "return 0;" +
             "end;" +
             "redis.call('zrem', KEYS[2], values[1]); " +
             "redis.call('zrem', KEYS[1], values[1]); " +
             "return 1;",
            Arrays.asList(getName(), getTimeoutSetName()),
            System.currentTimeMillis(), timestamp);
    }

    @Override
    public V last() {
        return get(lastAsync());
    }

    @Override
    public RFuture lastAsync() {
        return listAsync(-1, 1, RedisCommands.EVAL_FIRST_LIST);
    }

    @Override
    public RFuture> lastAsync(int count) {
        return listAsync(-1, count, RedisCommands.EVAL_LIST);
    }

    @Override
    public V first() {
        return get(firstAsync());
    }

    @Override
    public RFuture firstAsync() {
        return listAsync(0, 1, RedisCommands.EVAL_FIRST_LIST);
    }

    @Override
    public RFuture> firstAsync(int count) {
        return listAsync(0, count, RedisCommands.EVAL_LIST);
    }

    @Override
    public Collection first(int count) {
        return get(listAsync(0, count, RedisCommands.EVAL_LIST));
    }

    @Override
    public Collection last(int count) {
        return get(listAsync(-1, count, RedisCommands.EVAL_LIST_REVERSE));
    }

    @Override
    public Long firstTimestamp() {
        return get(firstTimestampAsync());
    }

    @Override
    public RFuture firstTimestampAsync() {
        return listTimestampAsync(0, 1, RedisCommands.EVAL_FIRST_LIST);
    }

    @Override
    public Long lastTimestamp() {
        return get(lastTimestampAsync());
    }

    @Override
    public RFuture lastTimestampAsync() {
        return listTimestampAsync(-1, 1, RedisCommands.EVAL_FIRST_LIST);
    }

    private  RFuture listTimestampAsync(int startScore, int limit, RedisCommand evalCommandType) {
        return commandExecutor.evalReadAsync(getName(), codec, evalCommandType,
               "local values;" +
               "if ARGV[2] == '0' then " +
                    "values = redis.call('zrangebyscore', KEYS[2], ARGV[1], '+inf', 'limit', 0, ARGV[3]);" +
               "else " +
                    "values = redis.call('zrevrangebyscore', KEYS[2], '+inf', ARGV[1], 'limit', 0, ARGV[3]);" +
               "end; " +

             "local result = {}; " +
             "for i, v in ipairs(values) do " +
                 "local t, val = struct.unpack('LLc0', v); " +
                 "table.insert(result, t);" +
             "end;" +
             "return result;",
            Arrays.asList(getName(), getTimeoutSetName()),
            System.currentTimeMillis(), startScore, limit);
    }

    private  RFuture listAsync(int startScore, int limit, RedisCommand evalCommandType) {
        return commandExecutor.evalReadAsync(getName(), codec, evalCommandType,
               "local values;" +
               "if ARGV[2] == '0' then " +
                    "values = redis.call('zrangebyscore', KEYS[2], ARGV[1], '+inf', 'limit', 0, ARGV[3]);" +
               "else " +
                    "values = redis.call('zrevrangebyscore', KEYS[2], '+inf', ARGV[1], 'limit', 0, ARGV[3]);" +
               "end; " +

             "local result = {}; " +
             "for i, v in ipairs(values) do " +
                 "local t, val = struct.unpack('LLc0', v); " +
                 "table.insert(result, val);" +
             "end;" +
             "return result;",
            Arrays.asList(getName(), getTimeoutSetName()),
            System.currentTimeMillis(), startScore, limit);
    }

    @Override
    public int removeRange(long startTimestamp, long endTimestamp) {
        return get(removeRangeAsync(startTimestamp, endTimestamp));
    }

    @Override
    public RFuture removeRangeAsync(long startTimestamp, long endTimestamp) {
        return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_INTEGER,
       "local values = redis.call('zrangebyscore', KEYS[1], ARGV[2], ARGV[3]);" +
             "if #values == 0 then " +
                 "return nil;" +
             "end;" +

             "local counter = 0; " +
             "for i, v in ipairs(values) do " +
                 "local expirationDate = redis.call('zscore', KEYS[2], v); " +
                 "if tonumber(expirationDate) > tonumber(ARGV[1]) then " +
                     "local t, val = struct.unpack('LLc0', v); " +
                     "counter = counter + 1; " +
                     "redis.call('zrem', KEYS[2], v); " +
                     "redis.call('zrem', KEYS[1], v); " +
                 "end;" +
             "end;" +
             "return counter;",
            Arrays.asList(getName(), getTimeoutSetName()),
            System.currentTimeMillis(), startTimestamp, endTimestamp);
    }

    @Override
    public Collection range(long startTimestamp, long endTimestamp) {
        return get(rangeAsync(startTimestamp, endTimestamp));
    }

    @Override
    public Collection> entryRange(long startTimestamp, long endTimestamp) {
        return get(entryRangeAsync(false, startTimestamp, endTimestamp));
    }

    @Override
    public Collection> entryRangeReversed(long startTimestamp, long endTimestamp) {
        return get(entryRangeAsync(true, startTimestamp, endTimestamp));
    }

    @Override
    public RFuture>> entryRangeReversedAsync(long startTimestamp, long endTimestamp) {
        return entryRangeAsync(true, startTimestamp, endTimestamp);
    }

    private static final RedisCommand>> ENTRIES =
                            new RedisCommand<>("EVAL", new TimeSeriesEntryReplayDecoder<>());

    @Override
    public RFuture>> entryRangeAsync(long startTimestamp, long endTimestamp) {
        return entryRangeAsync(false, startTimestamp, endTimestamp);
    }

    public RFuture>> entryRangeAsync(boolean reverse, long startTimestamp, long endTimestamp) {
        return commandExecutor.evalReadAsync(getName(), codec, ENTRIES,
             "local result = {}; " +

             "local values;" +
             "if ARGV[4] == '0' then " +
                 "values = redis.call('zrangebyscore', KEYS[1], ARGV[2], ARGV[3], 'withscores');" +
             "else " +
                 "values = redis.call('zrevrangebyscore', KEYS[1], ARGV[3], ARGV[2], 'withscores');" +
             "end;" +
             "if #values == 0 then " +
                 "return result;" +
             "end;" +

             "for i = 1, #values, 2 do " +
                 "local expirationDate = redis.call('zscore', KEYS[2], values[i]); " +
                 "if tonumber(expirationDate) > tonumber(ARGV[1]) then " +
                     "local t, val = struct.unpack('LLc0', values[i]); " +
                     "table.insert(result, val);" +
                     "table.insert(result, values[i+1]);" +
                 "end;" +
             "end;" +
             "return result;",
            Arrays.asList(getName(), getTimeoutSetName()),
            System.currentTimeMillis(), startTimestamp, endTimestamp, Boolean.compare(reverse, false));
    }

    @Override
    public RFuture> rangeAsync(long startTimestamp, long endTimestamp) {
        return commandExecutor.evalReadAsync(getName(), codec, RedisCommands.EVAL_LIST,
       "local values = redis.call('zrangebyscore', KEYS[1], ARGV[2], ARGV[3]);" +
             "if #values == 0 then " +
                 "return nil;" +
             "end;" +

             "local result = {}; " +
             "for i, v in ipairs(values) do " +
                 "local expirationDate = redis.call('zscore', KEYS[2], v); " +
                 "if tonumber(expirationDate) > tonumber(ARGV[1]) then " +
                     "local t, val = struct.unpack('LLc0', v); " +
                     "table.insert(result, val);" +
                 "end;" +
             "end;" +
             "return result;",
            Arrays.asList(getName(), getTimeoutSetName()),
            System.currentTimeMillis(), startTimestamp, endTimestamp);
    }

    @Override
    public Collection rangeReversed(long startTimestamp, long endTimestamp) {
        return get(rangeReversedAsync(startTimestamp, endTimestamp));
    }

    @Override
    public RFuture> rangeReversedAsync(long startTimestamp, long endTimestamp) {
        return commandExecutor.evalReadAsync(getName(), codec, RedisCommands.EVAL_LIST,
       "local values = redis.call('zrevrangebyscore', KEYS[1], ARGV[3], ARGV[2]);" +
             "if #values == 0 then " +
                 "return nil;" +
             "end;" +

             "local result = {}; " +
             "for i, v in ipairs(values) do " +
                 "local expirationDate = redis.call('zscore', KEYS[2], v); " +
                 "if tonumber(expirationDate) > tonumber(ARGV[1]) then " +
                     "local t, val = struct.unpack('LLc0', v); " +
                     "table.insert(result, val);" +
                 "end;" +
             "end;" +
             "return result;",
            Arrays.asList(getName(), getTimeoutSetName()),
            System.currentTimeMillis(), startTimestamp, endTimestamp);
    }

    @Override
    public Collection pollFirst(int count) {
        return get(pollFirstAsync(count));
    }

    @Override
    public Collection pollLast(int count) {
        return get(pollLastAsync(count));
    }

    @Override
    public RFuture> pollFirstAsync(int count) {
        if (count <= 0) {
            return RedissonPromise.newSucceededFuture(Collections.emptyList());
        }

        return pollAsync(0, count, RedisCommands.EVAL_LIST);
    }

    @Override
    public RFuture> pollLastAsync(int count) {
        if (count <= 0) {
            return RedissonPromise.newSucceededFuture(Collections.emptyList());
        }
        return pollAsync(-1, count, RedisCommands.EVAL_LIST_REVERSE);
    }

    @Override
    public V pollFirst() {
        return get(pollFirstAsync());
    }

    @Override
    public V pollLast() {
        return get(pollLastAsync());
    }

    @Override
    public RFuture pollFirstAsync() {
        return pollAsync(0, 1, RedisCommands.EVAL_FIRST_LIST);
    }

    @Override
    public RFuture pollLastAsync() {
        return pollAsync(-1, 1, RedisCommands.EVAL_FIRST_LIST);
    }

    private  RFuture pollAsync(int startScore, int limit, RedisCommand command) {
        return commandExecutor.evalWriteAsync(getName(), codec, command,
               "local values;" +
               "if ARGV[2] == '0' then " +
                    "values = redis.call('zrangebyscore', KEYS[2], ARGV[1], '+inf', 'limit', 0, ARGV[3]);" +
               "else " +
                    "values = redis.call('zrevrangebyscore', KEYS[2], '+inf', ARGV[1], 'limit', 0, ARGV[3]);" +
               "end; " +

             "local result = {}; " +
             "for i, v in ipairs(values) do " +
                 "redis.call('zrem', KEYS[2], v); " +
                 "redis.call('zrem', KEYS[1], v); " +
                 "local t, val = struct.unpack('LLc0', v); " +
                 "table.insert(result, val);" +
             "end;" +
             "return result;",
            Arrays.asList(getName(), getTimeoutSetName()),
            System.currentTimeMillis(), startScore, limit);
    }

    public ListScanResult scanIterator(String name, RedisClient client, long startPos, int count) {
        RFuture> f = scanIteratorAsync(name, client, startPos, count);
        return get(f);
    }

    public RFuture> scanIteratorAsync(String name, RedisClient client, long startPos, int count) {
        List params = new ArrayList<>();
        params.add(startPos);
        params.add(System.currentTimeMillis());
        params.add(count);

        return commandExecutor.evalReadAsync(client, name, codec, RedisCommands.EVAL_ZSCAN,
                  "local result = {}; "
                + "local res = redis.call('zrange', KEYS[1], ARGV[1], tonumber(ARGV[1]) + tonumber(ARGV[3]) - 1); "
                + "for i, value in ipairs(res) do "
                   + "local expirationDate = redis.call('zscore', KEYS[2], value); " +
                     "if tonumber(expirationDate) > tonumber(ARGV[2]) then " +
                         "local t, val = struct.unpack('LLc0', value); " +
                         "table.insert(result, val);" +
                     "end;"
                + "end;" +

                  "local nextPos = tonumber(ARGV[1]) + tonumber(ARGV[3]); " +
                  "if #res < tonumber(ARGV[3]) then " +
                    "nextPos = 0;" +
                  "end;"

                + "return {nextPos, result};",
                Arrays.asList(name, getTimeoutSetName()),
                params.toArray());
    }

    @Override
    public Iterator iterator(int count) {
        return new RedissonBaseIterator() {

            @Override
            protected ListScanResult iterator(RedisClient client, long nextIterPos) {
                return scanIterator(getName(), client, nextIterPos, count);
            }

            @Override
            protected void remove(Object value) {
                throw new UnsupportedOperationException();
            }

        };
    }

    @Override
    public Iterator iterator() {
        return iterator(10);
    }

    @Override
    public Stream stream() {
        return toStream(iterator());
    }

    @Override
    public Stream stream(int count) {
        return toStream(iterator(count));
    }

    @Override
    public void destroy() {
        if (evictionScheduler != null) {
            evictionScheduler.remove(getName());
        }
    }

    @Override
    public RFuture deleteAsync() {
        return deleteAsync(getName(), getTimeoutSetName());
    }

    @Override
    public RFuture expireAsync(long timeToLive, TimeUnit timeUnit) {
        return expireAsync(timeToLive, timeUnit, getName(), getTimeoutSetName());
    }

    @Override
    public RFuture expireAtAsync(long timestamp) {
        return expireAtAsync(timestamp, getName(), getTimeoutSetName());
    }

    @Override
    public RFuture clearExpireAsync() {
        return clearExpireAsync(getName(), getTimeoutSetName());
    }

    @Override
    public RFuture sizeInMemoryAsync() {
        List keys = Arrays.asList(getName(), getTimeoutSetName());
        return super.sizeInMemoryAsync(keys);
    }

}