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

redis.embedded.RedisCluster Maven / Gradle / Ivy

There is a newer version: 0.11.0
Show newest version
package redis.embedded;

import com.google.common.collect.Lists;
import redis.embedded.exceptions.EmbeddedRedisException;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by piotrturek on 22/01/15.
 */
public class RedisCluster implements Redis {
    private final List sentinels = new LinkedList<>();
    private final List servers = new LinkedList<>();

    RedisCluster(List sentinels, List servers) {
        this.servers.addAll(servers);
        this.sentinels.addAll(sentinels);
    }

    @Override
    public boolean isActive() {
        return sentinels.stream().allMatch(Redis::isActive) && servers.stream().allMatch(Redis::isActive);
    }

    @Override
    public void start() throws EmbeddedRedisException {
        sentinels.parallelStream().forEach(Redis::start);
        servers.parallelStream().forEach(Redis::start);
    }

    @Override
    public void stop() throws EmbeddedRedisException {
        servers.parallelStream().forEach(Redis::stop);
        sentinels.parallelStream().forEach(Redis::stop);
    }

    @Override
    public List ports() {
        return Stream.concat(
                sentinels.stream().flatMap(s -> s.ports().stream()),
                servers.stream().flatMap(s -> s.ports().stream())
        ).collect(Collectors.toList());
    }

    public List sentinels() {
        return Lists.newLinkedList(sentinels);
    }

    public List sentinelPorts() {
        return sentinels.stream().flatMap(s -> s.ports().stream()).collect(Collectors.toList());
    }

    public List servers() {
        return Lists.newLinkedList(servers);
    }

    public List serverPorts() {
        return servers.stream().flatMap(s -> s.ports().stream()).collect(Collectors.toList());
    }

    public static RedisClusterBuilder builder() {
        return new RedisClusterBuilder();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy