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

com.github.lontime.shaded.org.redisson.RedissonSetMultimap Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2013-2021 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 com.github.lontime.shaded.org.redisson;

import io.netty.buffer.ByteBuf;
import com.github.lontime.shaded.org.redisson.api.RFuture;
import com.github.lontime.shaded.org.redisson.api.RSet;
import com.github.lontime.shaded.org.redisson.api.RSetMultimap;
import com.github.lontime.shaded.org.redisson.client.codec.Codec;
import com.github.lontime.shaded.org.redisson.client.protocol.RedisCommand;
import com.github.lontime.shaded.org.redisson.client.protocol.RedisCommands;
import com.github.lontime.shaded.org.redisson.client.protocol.RedisStrictCommand;
import com.github.lontime.shaded.org.redisson.client.protocol.convertor.BooleanAmountReplayConvertor;
import com.github.lontime.shaded.org.redisson.client.protocol.convertor.BooleanReplayConvertor;
import com.github.lontime.shaded.org.redisson.command.CommandAsyncExecutor;
import com.github.lontime.shaded.org.redisson.misc.CompletableFutureWrapper;

import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;

/**
 * @author Nikita Koksharov
 *
 * @param  key
 * @param  value
 */
public class RedissonSetMultimap extends RedissonMultimap implements RSetMultimap {

    private static final RedisStrictCommand SCARD_VALUE = new RedisStrictCommand("SCARD", new BooleanAmountReplayConvertor());
    private static final RedisCommand SISMEMBER_VALUE = new RedisCommand("SISMEMBER", new BooleanReplayConvertor());

    public RedissonSetMultimap(CommandAsyncExecutor connectionManager, String name) {
        super(connectionManager, name);
    }

    public RedissonSetMultimap(Codec codec, CommandAsyncExecutor connectionManager, String name) {
        super(codec, connectionManager, name);
    }

    @Override
    public RFuture sizeAsync() {
        return commandExecutor.evalReadAsync(getRawName(), codec, RedisCommands.EVAL_INTEGER,
                "local keys = redis.call('hgetall', KEYS[1]); " +
                "local size = 0; " +
                "for i, v in ipairs(keys) do " +
                    "if i % 2 == 0 then " +
                        "local name = ARGV[1] .. v; " +
                        "size = size + redis.call('scard', name); " +
                    "end;" +
                "end; " +
                "return size; ",
                Arrays.asList(getRawName()),
                prefix);
    }

    @Override
    public RFuture containsKeyAsync(Object key) {
        String keyHash = keyHash(key);
        String setName = getValuesName(keyHash);
        return commandExecutor.readAsync(getRawName(), codec, SCARD_VALUE, setName);
    }

    @Override
    public RFuture containsValueAsync(Object value) {
        ByteBuf valueState = encodeMapValue(value);

        return commandExecutor.evalReadAsync(getRawName(), codec, RedisCommands.EVAL_BOOLEAN,
                "local keys = redis.call('hgetall', KEYS[1]); " +
                "for i, v in ipairs(keys) do " +
                    "if i % 2 == 0 then " +
                        "local name = ARGV[2] .. v; " +
                        "if redis.call('sismember', name, ARGV[1]) == 1 then "
                            + "return 1; " +
                        "end;" +
                    "end;" +
                "end; " +
                "return 0; ",
                Arrays.asList(getRawName()),
                valueState, prefix);
    }

    @Override
    public RFuture containsEntryAsync(Object key, Object value) {
        String keyHash = keyHash(key);
        ByteBuf valueState = encodeMapValue(value);

        String setName = getValuesName(keyHash);
        return commandExecutor.readAsync(getRawName(), codec, SISMEMBER_VALUE, setName, valueState);
    }

    @Override
    public RFuture putAsync(K key, V value) {
        ByteBuf keyState = encodeMapKey(key);
        String keyHash = hash(keyState);
        ByteBuf valueState = encodeMapValue(value);

        String setName = getValuesName(keyHash);
        return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_BOOLEAN,
                "redis.call('hset', KEYS[1], ARGV[1], ARGV[2]); " +
                "return redis.call('sadd', KEYS[2], ARGV[3]); ",
            Arrays.asList(getRawName(), setName), keyState, keyHash, valueState);
    }

    @Override
    public RFuture removeAsync(Object key, Object value) {
        ByteBuf keyState = encodeMapKey(key);
        String keyHash = hash(keyState);
        ByteBuf valueState = encodeMapValue(value);

        String setName = getValuesName(keyHash);
        return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_BOOLEAN,
                "local res = redis.call('srem', KEYS[2], ARGV[2]); "
              + "if res == 1 and redis.call('scard', KEYS[2]) == 0 then "
                  + "redis.call('hdel', KEYS[1], ARGV[1]); "
              + "end; "
              + "return res; ",
            Arrays.asList(getRawName(), setName), keyState, valueState);
    }

    @Override
    public RFuture putAllAsync(K key, Iterable values) {
        List params = new ArrayList();
        ByteBuf keyState = encodeMapKey(key);
        params.add(keyState);
        String keyHash = hash(keyState);
        params.add(keyHash);
        for (Object value : values) {
            ByteBuf valueState = encodeMapValue(value);
            params.add(valueState);
        }

        String setName = getValuesName(keyHash);
        return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_BOOLEAN_AMOUNT,
                "redis.call('hset', KEYS[1], ARGV[1], ARGV[2]); " +
                "return redis.call('sadd', KEYS[2], unpack(ARGV, 3, #ARGV)); ",
            Arrays.asList(getRawName(), setName), params.toArray());
    }

    @Override
    public RSet get(final K key) {
        String keyHash = keyHash(key);
        final String setName = getValuesName(keyHash);

        return new RedissonSet(codec, commandExecutor, setName, null) {
            
            @Override
            public RFuture addAsync(V value) {
                return RedissonSetMultimap.this.putAsync(key, value);
            }
            
            @Override
            public RFuture addAllAsync(Collection c) {
                return RedissonSetMultimap.this.putAllAsync(key, c);
            }
            
            @Override
            public RFuture removeAsync(Object value) {
                return RedissonSetMultimap.this.removeAsync(key, value);
            }
            
            @Override
            public RFuture removeAllAsync(Collection c) {
                if (c.isEmpty()) {
                    return new CompletableFutureWrapper<>(false);
                }
                
                List args = new ArrayList(c.size() + 1);
                args.add(encodeMapKey(key));
                encode(args, c);
                
                return commandExecutor.evalWriteAsync(RedissonSetMultimap.this.getRawName(), codec, RedisCommands.EVAL_BOOLEAN_AMOUNT,
                        "local count = redis.call('srem', KEYS[2], unpack(ARGV, 2, #ARGV));" +
                        "if count > 0 then " +
                            "if redis.call('scard', KEYS[2]) == 0 then " +
                                "redis.call('hdel', KEYS[1], ARGV[1]); " +
                            "end; " +
                            "return 1;" +
                        "end;" +
                        "return 0; ",
                    Arrays.asList(RedissonSetMultimap.this.getRawName(), setName),
                    args.toArray());
            }
            
            @Override
            public RFuture deleteAsync() {
                ByteBuf keyState = encodeMapKey(key);
                return RedissonSetMultimap.this.fastRemoveAsync(Arrays.asList(keyState),
                        Arrays.asList(RedissonSetMultimap.this.getRawName(), setName), RedisCommands.EVAL_BOOLEAN_AMOUNT);
            }
            
            @Override
            public RFuture clearExpireAsync() {
                throw new UnsupportedOperationException("This operation is not supported for SetMultimap values Set");
            }
            
            @Override
            public RFuture expireAsync(long timeToLive, TimeUnit timeUnit, String param, String... keys) {
                throw new UnsupportedOperationException("This operation is not supported for SetMultimap values Set");
            }

            @Override
            protected RFuture expireAtAsync(long timestamp, String param, String... keys) {
                throw new UnsupportedOperationException("This operation is not supported for SetMultimap values Set");
            }
            
            @Override
            public RFuture remainTimeToLiveAsync() {
                throw new UnsupportedOperationException("This operation is not supported for SetMultimap values Set");
            }
            
            @Override
            public RFuture renameAsync(String newName) {
                throw new UnsupportedOperationException("This operation is not supported for SetMultimap values Set");
            }
            
            @Override
            public RFuture renamenxAsync(String newName) {
                throw new UnsupportedOperationException("This operation is not supported for SetMultimap values Set");
            }
            
        };
    }

    @Override
    public Set getAll(K key) {
        return (Set) super.getAll(key);
    }

    @Override
    public RFuture> getAllAsync(K key) {
        String keyHash = keyHash(key);
        String setName = getValuesName(keyHash);

        return commandExecutor.readAsync(getRawName(), codec, RedisCommands.SMEMBERS, setName);
    }

    @Override
    public Set removeAll(Object key) {
        return (Set) get(removeAllAsync(key));
    }

    @Override
    public RFuture> removeAllAsync(Object key) {
        ByteBuf keyState = encodeMapKey(key);
        String keyHash = hash(keyState);

        String setName = getValuesName(keyHash);
        return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_SET,
                "redis.call('hdel', KEYS[1], ARGV[1]); " +
                "local members = redis.call('smembers', KEYS[2]); " +
                "redis.call('del', KEYS[2]); " +
                "return members; ",
            Arrays.asList(getRawName(), setName), keyState);
    }

    @Override
    public Set> entries() {
        return (Set>) super.entries();
    }

    @Override
    public Set replaceValues(K key, Iterable values) {
        return (Set) get(replaceValuesAsync(key, values));
    }

    @Override
    Iterator valuesIterator() {
        return new RedissonSetMultimapIterator(RedissonSetMultimap.this, commandExecutor, codec) {
            @Override
            V getValue(V entry) {
                return (V) entry;
            }
        };
    }

    @Override
    RedissonSetMultimapIterator> entryIterator() {
        return new RedissonSetMultimapIterator<>(RedissonSetMultimap.this, commandExecutor, codec);
    }

    @Override
    public RFuture> replaceValuesAsync(K key, Iterable values) {
        List params = new ArrayList();
        ByteBuf keyState = encodeMapKey(key);
        params.add(keyState);
        String keyHash = hash(keyState);
        params.add(keyHash);
        for (Object value : values) {
            ByteBuf valueState = encodeMapValue(value);
            params.add(valueState);
        }

        String setName = getValuesName(keyHash);
        return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_SET,
                "redis.call('hset', KEYS[1], ARGV[1], ARGV[2]); " +
                "local members = redis.call('smembers', KEYS[2]); " +
                "redis.call('del', KEYS[2]); " +
                "redis.call('sadd', KEYS[2], unpack(ARGV, 3, #ARGV)); " +
                "return members; ",
            Arrays.asList(getRawName(), setName), params.toArray());
    }

}