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

com.github.phantomthief.jedis.JedisClusterHelper Maven / Gradle / Ivy

The newest version!
/**
 * 
 */
package com.github.phantomthief.jedis;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

import com.github.phantomthief.concurrent.AdaptiveExecutor;

import redis.clients.jedis.JedisCluster;

/**
 * @author w.vela
 */
public class JedisClusterHelper {

    private final Supplier clusterFactory;
    private final AdaptiveExecutor adaptiveExecutor;

    /**
     * @param clusterFactory
     * @param adaptiveExecutor
     */
    private JedisClusterHelper(Supplier clusterFactory,
            AdaptiveExecutor adaptiveExecutor) {
        this.clusterFactory = clusterFactory;
        this.adaptiveExecutor = adaptiveExecutor;
    }

    public  Map pipeline(Collection keys, BiFunction func) {
        return pipeline(keys, func, Function.identity());
    }

    public  Map pipeline(Collection keys, BiFunction func,
            Function codec) {
        ConcurrentMap result = new ConcurrentHashMap<>();
        adaptiveExecutor.invokeAll(keys,
                key -> result.put(key, codec.apply(func.apply(clusterFactory.get(), key))));
        return result;
    }

    public static final class Builder {

        private AdaptiveExecutor executor;

        public Builder withExecutor(AdaptiveExecutor executor) {
            this.executor = executor;
            return this;
        }

        public JedisClusterHelper build(JedisCluster cluster) {
            ensuer();
            return new JedisClusterHelper(() -> cluster, executor);
        }

        public JedisClusterHelper build(Supplier clusterFactory) {
            ensuer();
            return new JedisClusterHelper(clusterFactory, executor);
        }

        private void ensuer() {
            if (executor == null) {
                throw new NullPointerException("executor is null.");
            }
        }
    }

    public static final Builder newBuilder() {
        return new Builder();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy