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

org.redisson.reactive.RedissonBaseMultimapReactive Maven / Gradle / Ivy

/**
 * Copyright 2016 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.reactive;

import java.util.List;
import java.util.Set;
import java.util.function.Supplier;

import org.reactivestreams.Publisher;
import org.redisson.api.RFuture;
import org.redisson.api.RMultimap;
import org.redisson.api.RMultimapReactive;
import org.redisson.client.codec.Codec;
import org.redisson.client.protocol.RedisCommand;
import org.redisson.command.CommandReactiveExecutor;
import org.redisson.misc.Hash;

import io.netty.buffer.ByteBuf;

/**
 * 
 * @author Nikita Koksharov
 *
 * @param  key type
 * @param  value type
 */
abstract class RedissonBaseMultimapReactive extends RedissonExpirableReactive implements RMultimapReactive {

    private final RMultimap instance;
    
    public RedissonBaseMultimapReactive(RMultimap instance, CommandReactiveExecutor commandExecutor, String name) {
        super(commandExecutor, name);
        this.instance = instance;
    }

    public RedissonBaseMultimapReactive(RMultimap instance, Codec codec, CommandReactiveExecutor commandExecutor, String name) {
        super(codec, commandExecutor, name);
        this.instance = instance;
    }

    @Override
    public Publisher size() {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.sizeAsync();
            }
        });
    }

    @Override
    public Publisher containsKey(final Object key) {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.containsKeyAsync(key);
            }
        });
    }

    @Override
    public Publisher containsValue(final Object value) {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.containsValueAsync(value);
            }
        });
    }

    @Override
    public Publisher containsEntry(final Object key, final Object value) {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.containsEntryAsync(key, value);
            }
        });
    }

    @Override
    public Publisher put(final K key, final V value) {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.putAsync(key, value);
            }
        });
    }

    @Override
    public Publisher remove(final Object key, final Object value) {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.removeAsync(key, value);
            }
        });
    }

    @Override
    public Publisher putAll(final K key, final Iterable values) {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.putAllAsync(key, values);
            }
        });
    }

    @Override
    public Publisher keySize() {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.keySizeAsync();
            }
        });
    }

    @Override
    public Publisher fastRemove(final K... keys) {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.fastRemoveAsync(keys);
            }
        });
    }

    @Override
    public Publisher> readAllKeySet() {
        return reactive(new Supplier>>() {
            @Override
            public RFuture> get() {
                return instance.readAllKeySetAsync();
            }
        });
    }

    protected String hash(ByteBuf objectState) {
        return Hash.hashToBase64(objectState);
    }
    
    protected String hashAndRelease(ByteBuf objectState) {
        try {
            return Hash.hashToBase64(objectState);
        } finally {
            objectState.release();
        }
    }

    String getValuesName(String hash) {
        return "{" + getName() + "}:" + hash;
    }
    
    protected  Publisher fastRemove(List mapKeys, List listKeys, RedisCommand evalCommandType) {
        return commandExecutor.evalWriteReactive(getName(), codec, evalCommandType,
                    "local res = redis.call('hdel', KEYS[1], unpack(ARGV)); " +
                    "if res > 0 then " +
                        "redis.call('del', unpack(KEYS, 2, #KEYS)); " +
                    "end; " +
                    "return res; ",
                    listKeys, mapKeys.toArray());
    }
    
}