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

com.github.lontime.shaded.org.redisson.RedissonFuction 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 com.github.lontime.shaded.org.redisson.api.*;
import com.github.lontime.shaded.org.redisson.client.codec.ByteArrayCodec;
import com.github.lontime.shaded.org.redisson.client.codec.Codec;
import com.github.lontime.shaded.org.redisson.client.codec.StringCodec;
import com.github.lontime.shaded.org.redisson.client.protocol.RedisCommands;
import com.github.lontime.shaded.org.redisson.command.CommandAsyncExecutor;
import com.github.lontime.shaded.org.redisson.misc.CompletableFutureWrapper;

import java.util.*;
import java.util.concurrent.CompletableFuture;

/**
 *
 * @author Nikita Koksharov
 *
 */
public class RedissonFuction implements RFunction {

    private final Codec codec;
    private final CommandAsyncExecutor commandExecutor;

    public RedissonFuction(CommandAsyncExecutor commandExecutor) {
        this.commandExecutor = commandExecutor;
        this.codec = commandExecutor.getConnectionManager().getCodec();
    }

    public RedissonFuction(CommandAsyncExecutor commandExecutor, Codec codec) {
        this.commandExecutor = commandExecutor;
        this.codec = codec;
    }

    @Override
    public void delete(String libraryName) {
        commandExecutor.get(deleteAsync(libraryName));
    }

    @Override
    public RFuture deleteAsync(String libraryName) {
        return commandExecutor.writeAllVoidAsync(RedisCommands.FUNCTION_DELETE, libraryName);
    }

    @Override
    public byte[] dump() {
        return commandExecutor.get(dumpAsync());
    }

    @Override
    public RFuture dumpAsync() {
        return commandExecutor.readAsync((String) null, ByteArrayCodec.INSTANCE, RedisCommands.FUNCTION_DUMP);
    }

    @Override
    public void flush() {
        commandExecutor.get(flushAsync());
    }

    @Override
    public RFuture flushAsync() {
        return commandExecutor.writeAllVoidAsync(RedisCommands.FUNCTION_FLUSH);
    }

    @Override
    public void kill() {
        commandExecutor.get(killAsync());
    }

    @Override
    public RFuture killAsync() {
        List> futures = commandExecutor.executeAllAsync(RedisCommands.FUNCTION_KILL);
        CompletableFuture f = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
        return new CompletableFutureWrapper<>(f);
    }

    @Override
    public List list() {
        return commandExecutor.get(listAsync());
    }

    @Override
    public RFuture> listAsync() {
        return commandExecutor.readAsync((String) null, ByteArrayCodec.INSTANCE, RedisCommands.FUNCTION_LIST);
    }

    @Override
    public List list(String namePattern) {
        return commandExecutor.get(listAsync(namePattern));
    }


    @Override
    public RFuture> listAsync(String namePattern) {
        return commandExecutor.readAsync((String) null, ByteArrayCodec.INSTANCE, RedisCommands.FUNCTION_LIST, "LIBRARYNAME", namePattern);
    }

    @Override
    public void load(String libraryName, String code) {
        commandExecutor.get(loadAsync(libraryName, code));
    }

    @Override
    public RFuture loadAsync(String libraryName, String code) {
        return commandExecutor.writeAllVoidAsync(RedisCommands.FUNCTION_LOAD, "Lua", libraryName, code);
    }

    @Override
    public void loadAndReplace(String libraryName, String code) {
        commandExecutor.get(loadAndReplaceAsync(libraryName, code));
    }

    @Override
    public RFuture loadAndReplaceAsync(String libraryName, String code) {
        return commandExecutor.writeAllVoidAsync(RedisCommands.FUNCTION_LOAD,
                                                "Lua", libraryName, "REPLACE", code);
    }

    @Override
    public void restore(byte[] payload) {
        commandExecutor.get(restoreAsync(payload));
    }

    @Override
    public RFuture restoreAsync(byte[] payload) {
        return commandExecutor.writeAllVoidAsync(RedisCommands.FUNCTION_RESTORE, payload);
    }

    @Override
    public void restoreAndReplace(byte[] payload) {
        commandExecutor.get(restoreAndReplaceAsync(payload));
    }

    @Override
    public RFuture restoreAndReplaceAsync(byte[] payload) {
        return commandExecutor.writeAllVoidAsync(RedisCommands.FUNCTION_RESTORE, payload, "REPLACE");
    }

    @Override
    public void restoreAfterFlush(byte[] payload) {
        commandExecutor.get(restoreAfterFlushAsync(payload));
    }

    @Override
    public RFuture restoreAfterFlushAsync(byte[] payload) {
        return commandExecutor.writeAllVoidAsync(RedisCommands.FUNCTION_RESTORE, payload, "FLUSH");
    }

    private List encode(Collection values, Codec codec) {
        List result = new ArrayList<>(values.size());
        for (Object object : values) {
            result.add(commandExecutor.encode(codec, object));
        }
        return result;
    }

    @Override
    public  R call(String key, FunctionMode mode, String name, FunctionResult returnType, List keys, Object... values) {
        return commandExecutor.get(callAsync(key, mode, name, returnType, keys, values));
    }

    @Override
    public  R call(FunctionMode mode, String name, FunctionResult returnType, List keys, Object... values) {
        return commandExecutor.get(callAsync(mode, name, returnType, keys, values));
    }

    @Override
    public  R call(FunctionMode mode, String name, FunctionResult returnType) {
        return commandExecutor.get(callAsync(mode, name, returnType));
    }

    @Override
    public FunctionStats stats() {
        return commandExecutor.get(statsAsync());
    }

    @Override
    public RFuture statsAsync() {
        return commandExecutor.readAsync((String) null, StringCodec.INSTANCE, RedisCommands.FUNCTION_STATS);
    }

    @Override
    public  RFuture callAsync(String key, FunctionMode mode, String name, FunctionResult returnType, List keys, Object... values) {
        List args = new ArrayList<>();
        args.add(name);
        args.add(keys.size());
        if (keys.size() > 0) {
            args.add(keys);
        }
        args.addAll(encode(Arrays.asList(values), codec));
        if (mode == FunctionMode.READ) {
            return commandExecutor.readAsync(key, codec, returnType.getCommand(), args.toArray());
        }
        return commandExecutor.writeAsync(key, codec, returnType.getCommand(), args.toArray());
    }

    @Override
    public  RFuture callAsync(FunctionMode mode, String name, FunctionResult returnType, List keys, Object... values) {
        return callAsync(null, mode, name, returnType, keys, values);
    }

    @Override
    public  RFuture callAsync(FunctionMode mode, String name, FunctionResult returnType) {
        return callAsync(mode, name, returnType, Collections.emptyList());

    }
}