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

io.lettuce.core.sentinel.SentinelCommandBuilder Maven / Gradle / Ivy

Go to download

Advanced and thread-safe Java Redis client for synchronous, asynchronous, and reactive usage. Supports Cluster, Sentinel, Pipelining, Auto-Reconnect, Codecs and much more.

The newest version!
/*
 * Copyright 2011-Present, Redis Ltd. and Contributors
 * All rights reserved.
 *
 * Licensed under the MIT License.
 *
 * This file contains contributions from third-party contributors
 * 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
 *
 *      https://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 io.lettuce.core.sentinel;

import static io.lettuce.core.protocol.CommandKeyword.*;
import static io.lettuce.core.protocol.CommandType.*;

import java.net.SocketAddress;
import java.util.List;
import java.util.Map;

import io.lettuce.core.ClientListArgs;
import io.lettuce.core.KillArgs;
import io.lettuce.core.codec.RedisCodec;
import io.lettuce.core.internal.LettuceAssert;
import io.lettuce.core.output.IntegerOutput;
import io.lettuce.core.output.KeyOutput;
import io.lettuce.core.output.ListOfMapsOutput;
import io.lettuce.core.output.MapOutput;
import io.lettuce.core.output.SocketAddressOutput;
import io.lettuce.core.output.StatusOutput;
import io.lettuce.core.protocol.BaseRedisCommandBuilder;
import io.lettuce.core.protocol.Command;
import io.lettuce.core.protocol.CommandArgs;
import io.lettuce.core.protocol.CommandKeyword;
import io.lettuce.core.protocol.CommandType;

/**
 * @author Mark Paluch
 * @since 3.0
 */
class SentinelCommandBuilder extends BaseRedisCommandBuilder {

    public SentinelCommandBuilder(RedisCodec codec) {
        super(codec);
    }

    public Command getMasterAddrByKey(K key) {
        CommandArgs args = new CommandArgs<>(codec).add("get-master-addr-by-name").addKey(key);
        return createCommand(SENTINEL, new SocketAddressOutput<>(codec), args);
    }

    public Command>> masters() {
        CommandArgs args = new CommandArgs<>(codec).add("masters");
        return createCommand(SENTINEL, new ListOfMapsOutput<>(codec), args);
    }

    public Command> master(K key) {
        CommandArgs args = new CommandArgs<>(codec).add("master").addKey(key);
        return createCommand(SENTINEL, new MapOutput<>(codec), args);
    }

    public Command>> slaves(K key) {
        CommandArgs args = new CommandArgs<>(codec).add(SLAVES).addKey(key);
        return createCommand(SENTINEL, new ListOfMapsOutput<>(codec), args);
    }

    public Command>> replicas(K key) {
        CommandArgs args = new CommandArgs<>(codec).add(REPLICAS).addKey(key);
        return createCommand(SENTINEL, new ListOfMapsOutput<>(codec), args);
    }

    public Command reset(K key) {
        CommandArgs args = new CommandArgs<>(codec).add(RESET).addKey(key);
        return createCommand(SENTINEL, new IntegerOutput<>(codec), args);
    }

    public Command failover(K key) {
        CommandArgs args = new CommandArgs<>(codec).add(FAILOVER).addKey(key);
        return createCommand(SENTINEL, new StatusOutput<>(codec), args);
    }

    public Command monitor(K key, String ip, int port, int quorum) {
        CommandArgs args = new CommandArgs<>(codec).add(MONITOR).addKey(key).add(ip).add(port).add(quorum);
        return createCommand(SENTINEL, new StatusOutput<>(codec), args);
    }

    public Command set(K key, String option, V value) {
        CommandArgs args = new CommandArgs<>(codec).add(SET).addKey(key).add(option).addValue(value);
        return createCommand(SENTINEL, new StatusOutput<>(codec), args);
    }

    public Command clientGetname() {
        CommandArgs args = new CommandArgs<>(codec).add(GETNAME);
        return createCommand(CLIENT, new KeyOutput<>(codec), args);
    }

    public Command clientSetname(K name) {
        LettuceAssert.notNull(name, "Name must not be null");

        CommandArgs args = new CommandArgs<>(codec).add(SETNAME).addKey(name);
        return createCommand(CLIENT, new StatusOutput<>(codec), args);
    }

    public Command clientSetinfo(String key, String value) {
        CommandArgs args = new CommandArgs<>(codec).add(SETINFO).add(key).add(value);
        return createCommand(CLIENT, new StatusOutput<>(codec), args);
    }

    public Command clientKill(String addr) {
        LettuceAssert.notNull(addr, "Addr must not be null");
        LettuceAssert.notEmpty(addr, "Addr must not be empty");

        CommandArgs args = new CommandArgs<>(codec).add(KILL).add(addr);
        return createCommand(CLIENT, new StatusOutput<>(codec), args);
    }

    public Command clientKill(KillArgs killArgs) {
        LettuceAssert.notNull(killArgs, "KillArgs must not be null");

        CommandArgs args = new CommandArgs<>(codec).add(KILL);
        killArgs.build(args);
        return createCommand(CLIENT, new IntegerOutput<>(codec), args);
    }

    public Command clientPause(long timeout) {
        CommandArgs args = new CommandArgs<>(codec).add(PAUSE).add(timeout);
        return createCommand(CLIENT, new StatusOutput<>(codec), args);
    }

    public Command clientList() {
        CommandArgs args = new CommandArgs<>(codec).add(LIST);
        return createCommand(CLIENT, new StatusOutput<>(codec), args);
    }

    public Command clientList(ClientListArgs clientListArgs) {
        LettuceAssert.notNull(clientListArgs, "ClientListArgs must not be null");

        CommandArgs args = new CommandArgs<>(codec).add(LIST);
        clientListArgs.build(args);
        return createCommand(CLIENT, new StatusOutput<>(codec), args);
    }

    public Command clientInfo() {
        CommandArgs args = new CommandArgs<>(codec).add(CommandKeyword.INFO);
        return createCommand(CLIENT, new StatusOutput<>(codec), args);
    }

    public Command info() {
        return createCommand(CommandType.INFO, new StatusOutput<>(codec));
    }

    public Command info(String section) {
        LettuceAssert.notNull(section, "Section must not be null");

        CommandArgs args = new CommandArgs<>(codec).add(section);
        return createCommand(CommandType.INFO, new StatusOutput<>(codec), args);
    }

    public Command ping() {
        return createCommand(PING, new StatusOutput<>(codec));
    }

    public Command remove(K key) {
        CommandArgs args = new CommandArgs<>(codec).add(CommandKeyword.REMOVE).addKey(key);
        return createCommand(SENTINEL, new StatusOutput<>(codec), args);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy