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

com.github.risedragone.jedis.impl.JedisClientShardingImpl Maven / Gradle / Ivy

The newest version!
package com.github.risedragone.jedis.impl;

import static redis.clients.util.Hashing.MURMUR_HASH;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.github.risedragone.jedis.JedisClient;
import com.github.risedragone.jedis.JedisClientException;
import com.github.risedragone.jedis.JedisClientSharding;
import com.github.risedragone.jedis.ShardInfo;

public class JedisClientShardingImpl implements JedisClientSharding {

	final List shards;
	final TreeMap clients = new TreeMap();

	public JedisClientShardingImpl(List shards) throws JedisClientException {
		this.shards = shards;

		if (shards != null) {
			for (ShardInfo info : shards) {
				if (info.getHash() == null) {
					if (info.getRate() < 0 || info.getRate() > 100) {
						throw new JedisClientException("Invalid shard rate: " + info.getRate());
					}
					info.setHash(convRateToLong(info.getRate()));
				}
				if (clients.put(info.getHash(), new JedisClientImpl(info.getJedisPool())) != null) {
					throw new JedisClientException("Duplicate shard hash: " + info.getHash());
				}
			}
		}
	}

	private static long convRateToLong(byte rate) {
		BigDecimal min = BigDecimal.valueOf(Integer.MIN_VALUE);
		return BigDecimal.valueOf(Integer.MAX_VALUE).subtract(min).divide(BigDecimal.valueOf(100)).multiply(BigDecimal.valueOf(rate)).add(min).longValue();
	}

	@Override
	public JedisClient shard(long key) {
		Map.Entry entry = clients.floorEntry(key);
		if (entry == null) {
			entry = clients.firstEntry();
		}
		return entry.getValue();
	}

	@Override
	public JedisClient shard(String key) {
		return shard(MURMUR_HASH.hash(key));
	}

	@Override
	public JedisClient shard(byte[] key) {
		return shard(MURMUR_HASH.hash(key));
	}

	@Override
	public Collection getAllShardInfo() {
		return shards;
	}

	@Override
	public Collection getAllShard() {
		return clients.values();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy