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

org.redisson.spring.data.connection.RedissonReactiveServerCommands Maven / Gradle / Ivy

There is a newer version: 3.36.0
Show newest version
/**
 * Copyright (c) 2013-2024 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.spring.data.connection;

import java.util.List;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import org.redisson.api.RFuture;
import org.redisson.client.codec.LongCodec;
import org.redisson.client.codec.StringCodec;
import org.redisson.client.handler.State;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.client.protocol.RedisStrictCommand;
import org.redisson.client.protocol.decoder.ObjectDecoder;
import org.redisson.client.protocol.decoder.TimeLongObjectDecoder;
import org.redisson.misc.CompletableFutureWrapper;
import org.redisson.reactive.CommandReactiveExecutor;
import org.springframework.data.redis.connection.ReactiveServerCommands;
import org.springframework.data.redis.core.types.RedisClientInfo;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * 
 * @author Nikita Koksharov
 *
 */
public class RedissonReactiveServerCommands extends RedissonBaseReactive implements ReactiveServerCommands {

    RedissonReactiveServerCommands(CommandReactiveExecutor executorService) {
        super(executorService);
    }

    static final RedisStrictCommand BGREWRITEAOF = new RedisStrictCommand("BGREWRITEAOF");
    
    @Override
    public Mono bgReWriteAof() {
        return write(null, StringCodec.INSTANCE, BGREWRITEAOF);
    }

    static final RedisStrictCommand BGSAVE = new RedisStrictCommand("BGSAVE");
    
    @Override
    public Mono bgSave() {
        return write(null, StringCodec.INSTANCE, BGSAVE);
    }

    @Override
    public Mono lastSave() {
        return write(null, StringCodec.INSTANCE, RedisCommands.LASTSAVE);
    }

    static final RedisStrictCommand SAVE = new RedisStrictCommand("SAVE");

    @Override
    public Mono save() {
        return write(null, StringCodec.INSTANCE, SAVE);
    }

    @Override
    public Mono dbSize() {
        return executorService.reactive(() -> {
            List> futures = executorService.readAllAsync(RedisCommands.DBSIZE);
            CompletableFuture f = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
            CompletableFuture s = f.thenApply(r -> futures.stream().mapToLong(v -> v.getNow(0L)).sum());
            return new CompletableFutureWrapper<>(s);
        });
    }
    
    private static final RedisStrictCommand FLUSHDB = new RedisStrictCommand("FLUSHDB");

    @Override
    public Mono flushDb() {
        return executorService.reactive(() -> {
            RFuture f = executorService.writeAllVoidAsync(FLUSHDB);
            return toStringFuture(f);
        });
    }

    private static final RedisStrictCommand FLUSHALL = new RedisStrictCommand("FLUSHALL");

    @Override
    public Mono flushAll() {
        return executorService.reactive(() -> {
            RFuture f = executorService.writeAllVoidAsync(FLUSHALL);
            return toStringFuture(f);
        });
    }

    static final RedisStrictCommand INFO_DEFAULT = new RedisStrictCommand("INFO", "DEFAULT", new ObjectDecoder(new PropertiesDecoder()));
    static final RedisStrictCommand INFO = new RedisStrictCommand("INFO", new ObjectDecoder(new PropertiesDecoder()));
    
    @Override
    public Mono info() {
        return read(null, StringCodec.INSTANCE, INFO_DEFAULT);
    }

    @Override
    public Mono info(String section) {
        return read(null, StringCodec.INSTANCE, INFO, section);
    }

    static final RedisStrictCommand CONFIG_GET = new RedisStrictCommand("CONFIG", "GET", new PropertiesListDecoder());
    
    @Override
    public Mono getConfig(String pattern) {
        return read(null, StringCodec.INSTANCE, CONFIG_GET, pattern);
    }
    
    static final RedisStrictCommand CONFIG_SET = new RedisStrictCommand("CONFIG", "SET");

    @Override
    public Mono setConfig(String param, String value) {
        return write(null, StringCodec.INSTANCE, CONFIG_SET, param, value);
    }
    
    static final RedisStrictCommand CONFIG_RESETSTAT = new RedisStrictCommand("CONFIG", "RESETSTAT");

    @Override
    public Mono resetConfigStats() {
        return write(null, StringCodec.INSTANCE, CONFIG_RESETSTAT);
    }

    static final RedisStrictCommand TIME = new RedisStrictCommand("TIME", new TimeLongObjectDecoder());
    
    @Override
    public Mono time() {
        return read(null, LongCodec.INSTANCE, TIME);
    }

    @Override
    public Mono time(TimeUnit timeUnit) {
        return read(null, LongCodec.INSTANCE, new RedisStrictCommand<>("TIME", new TimeLongObjectDecoder() {
            @Override
            public Long decode(List parts, State state) {
                Long time = super.decode(parts, state);
                return timeUnit.convert(time, TimeUnit.MILLISECONDS);
            }
        }));
    }

    @Override
    public Mono killClient(String host, int port) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Mono setClientName(String name) {
        throw new UnsupportedOperationException("Should be defined through Redisson Config object");
    }

    @Override
    public Mono getClientName() {
        throw new UnsupportedOperationException();
    }

    @Override
    public Flux getClientList() {
        throw new UnsupportedOperationException();
    }

}