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

org.redisson.reactive.RedissonScriptReactive 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.ArrayList;
import java.util.Collections;
import java.util.List;

import org.reactivestreams.Publisher;
import org.redisson.SlotCallback;
import org.redisson.api.RScript;
import org.redisson.api.RScriptReactive;
import org.redisson.client.codec.Codec;
import org.redisson.client.protocol.RedisCommand;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.command.CommandReactiveExecutor;

/**
 * 
 * @author Nikita Koksharov
 *
 */
public class RedissonScriptReactive implements RScriptReactive {

    private final CommandReactiveExecutor commandExecutor;

    public RedissonScriptReactive(CommandReactiveExecutor commandExecutor) {
        this.commandExecutor = commandExecutor;
    }

    @Override
    public Publisher scriptLoad(String luaScript) {
        return commandExecutor.writeAllReactive(RedisCommands.SCRIPT_LOAD, new SlotCallback() {
            volatile String result;
            @Override
            public void onSlotResult(String result) {
                this.result = result;
            }

            @Override
            public String onFinish() {
                return result;
            }
        }, luaScript);
    }

    public Publisher scriptLoad(String key, String luaScript) {
        return commandExecutor.writeReactive(key, RedisCommands.SCRIPT_LOAD, luaScript);
    }

    @Override
    public  Publisher eval(RScript.Mode mode, String luaScript, RScript.ReturnType returnType, List keys, Object... values) {
        return eval(null, mode, commandExecutor.getConnectionManager().getCodec(), luaScript, returnType, keys, values);
    }

    @Override
    public  Publisher eval(RScript.Mode mode, Codec codec, String luaScript, RScript.ReturnType returnType, List keys, Object... values) {
        return eval(null, mode, codec, luaScript, returnType, keys, values);
    }

    public  Publisher eval(String key, RScript.Mode mode, Codec codec, String luaScript, RScript.ReturnType returnType, List keys, Object... values) {
        if (mode == RScript.Mode.READ_ONLY) {
            return commandExecutor.evalReadReactive(key, codec, returnType.getCommand(), luaScript, keys, values);
        }
        return commandExecutor.evalWriteReactive(key, codec, returnType.getCommand(), luaScript, keys, values);
    }

    @Override
    public  Publisher evalSha(RScript.Mode mode, String shaDigest, RScript.ReturnType returnType, List keys, Object... values) {
        return evalSha(null, mode, commandExecutor.getConnectionManager().getCodec(), shaDigest, returnType, keys, values);
    }

    @Override
    public  Publisher evalSha(RScript.Mode mode, Codec codec, String shaDigest, RScript.ReturnType returnType, List keys, Object... values) {
        return evalSha(null, mode, codec, shaDigest, returnType, keys, values);
    }

    public  Publisher evalSha(String key, RScript.Mode mode, Codec codec, String shaDigest, RScript.ReturnType returnType, List keys, Object... values) {
        RedisCommand command = new RedisCommand(returnType.getCommand(), "EVALSHA");
        if (mode == RScript.Mode.READ_ONLY) {
            return commandExecutor.evalReadReactive(key, codec, command, shaDigest, keys, values);
        }
        return commandExecutor.evalWriteReactive(key, codec, command, shaDigest, keys, values);
    }

    @Override
    public Publisher scriptKill() {
        return commandExecutor.writeAllReactive(RedisCommands.SCRIPT_KILL);
    }

    @Override
    public Publisher> scriptExists(final String ... shaDigests) {
         return commandExecutor.writeAllReactive(RedisCommands.SCRIPT_EXISTS, new SlotCallback, List>() {
            volatile List result = new ArrayList(shaDigests.length);
            @Override
            public synchronized void onSlotResult(List result) {
                for (int i = 0; i < result.size(); i++) {
                    if (this.result.size() == i) {
                        this.result.add(false);
                    }
                    this.result.set(i, this.result.get(i) | result.get(i));
                }
            }

            @Override
            public List onFinish() {
                return new ArrayList(result);
            }
        }, (Object[])shaDigests);
    }

    public Publisher> scriptExists(String key, String ... shaDigests) {
        return commandExecutor.writeReactive(key, RedisCommands.SCRIPT_EXISTS, (Object[])shaDigests);
    }

    @Override
    public Publisher scriptFlush() {
        return commandExecutor.writeAllReactive(RedisCommands.SCRIPT_FLUSH);
    }

    @Override
    public  Publisher evalSha(RScript.Mode mode, String shaDigest, RScript.ReturnType returnType) {
        return evalSha(null, mode, commandExecutor.getConnectionManager().getCodec(), shaDigest, returnType, Collections.emptyList());
    }

    @Override
    public  Publisher evalSha(RScript.Mode mode, Codec codec, String shaDigest, RScript.ReturnType returnType) {
        return evalSha(null, mode, codec, shaDigest, returnType, Collections.emptyList());
    }

    @Override
    public  Publisher eval(RScript.Mode mode, String luaScript, RScript.ReturnType returnType) {
        return eval(null, mode, commandExecutor.getConnectionManager().getCodec(), luaScript, returnType, Collections.emptyList());
    }

    @Override
    public  Publisher eval(RScript.Mode mode, Codec codec, String luaScript, RScript.ReturnType returnType) {
        return eval(null, mode, codec, luaScript, returnType, Collections.emptyList());
    }

}