Reformat all files in the project according to java conventions.
This commit is contained in:
@@ -26,91 +26,93 @@ public class Sharded<R, S extends ShardInfo<R>> {
|
||||
private Pattern tagPattern = null;
|
||||
// the tag is anything between {}
|
||||
public static final Pattern DEFAULT_KEY_TAG_PATTERN = Pattern
|
||||
.compile("\\{(.+?)\\}");
|
||||
.compile("\\{(.+?)\\}");
|
||||
|
||||
public Sharded(List<S> shards) {
|
||||
this(shards, Hashing.MURMUR_HASH); // MD5 is really not good as we works
|
||||
// with 64-bits not 128
|
||||
this(shards, Hashing.MURMUR_HASH); // MD5 is really not good as we works
|
||||
// with 64-bits not 128
|
||||
}
|
||||
|
||||
public Sharded(List<S> shards, Hashing algo) {
|
||||
this.algo = algo;
|
||||
initialize(shards);
|
||||
this.algo = algo;
|
||||
initialize(shards);
|
||||
}
|
||||
|
||||
public Sharded(List<S> shards, Pattern tagPattern) {
|
||||
this(shards, Hashing.MURMUR_HASH, tagPattern); // MD5 is really not good
|
||||
// as we works with
|
||||
// 64-bits not 128
|
||||
this(shards, Hashing.MURMUR_HASH, tagPattern); // MD5 is really not good
|
||||
// as we works with
|
||||
// 64-bits not 128
|
||||
}
|
||||
|
||||
public Sharded(List<S> shards, Hashing algo, Pattern tagPattern) {
|
||||
this.algo = algo;
|
||||
this.tagPattern = tagPattern;
|
||||
initialize(shards);
|
||||
this.algo = algo;
|
||||
this.tagPattern = tagPattern;
|
||||
initialize(shards);
|
||||
}
|
||||
|
||||
private void initialize(List<S> shards) {
|
||||
nodes = new TreeMap<Long, S>();
|
||||
nodes = new TreeMap<Long, S>();
|
||||
|
||||
for (int i = 0; i != shards.size(); ++i) {
|
||||
final S shardInfo = shards.get(i);
|
||||
if (shardInfo.getName() == null)
|
||||
for (int n = 0; n < 160 * shardInfo.getWeight(); n++) {
|
||||
nodes.put(this.algo.hash("SHARD-" + i + "-NODE-" + n), shardInfo);
|
||||
}
|
||||
else
|
||||
for (int n = 0; n < 160 * shardInfo.getWeight(); n++) {
|
||||
nodes.put(this.algo.hash(shardInfo.getName() + "*" + shardInfo.getWeight() + n), shardInfo);
|
||||
}
|
||||
resources.put(shardInfo, shardInfo.createResource());
|
||||
}
|
||||
for (int i = 0; i != shards.size(); ++i) {
|
||||
final S shardInfo = shards.get(i);
|
||||
if (shardInfo.getName() == null)
|
||||
for (int n = 0; n < 160 * shardInfo.getWeight(); n++) {
|
||||
nodes.put(this.algo.hash("SHARD-" + i + "-NODE-" + n),
|
||||
shardInfo);
|
||||
}
|
||||
else
|
||||
for (int n = 0; n < 160 * shardInfo.getWeight(); n++) {
|
||||
nodes.put(
|
||||
this.algo.hash(shardInfo.getName() + "*"
|
||||
+ shardInfo.getWeight() + n), shardInfo);
|
||||
}
|
||||
resources.put(shardInfo, shardInfo.createResource());
|
||||
}
|
||||
}
|
||||
|
||||
public R getShard(byte[] key) {
|
||||
return resources.get(getShardInfo(key));
|
||||
return resources.get(getShardInfo(key));
|
||||
}
|
||||
|
||||
public R getShard(String key) {
|
||||
return resources.get(getShardInfo(key));
|
||||
return resources.get(getShardInfo(key));
|
||||
}
|
||||
|
||||
public S getShardInfo(byte[] key) {
|
||||
SortedMap<Long, S> tail = nodes.tailMap(algo.hash(key));
|
||||
if (tail.isEmpty()) {
|
||||
return nodes.get(nodes.firstKey());
|
||||
}
|
||||
return tail.get(tail.firstKey());
|
||||
SortedMap<Long, S> tail = nodes.tailMap(algo.hash(key));
|
||||
if (tail.isEmpty()) {
|
||||
return nodes.get(nodes.firstKey());
|
||||
}
|
||||
return tail.get(tail.firstKey());
|
||||
}
|
||||
|
||||
public S getShardInfo(String key) {
|
||||
return getShardInfo(SafeEncoder.encode(getKeyTag(key)));
|
||||
return getShardInfo(SafeEncoder.encode(getKeyTag(key)));
|
||||
}
|
||||
|
||||
/**
|
||||
* A key tag is a special pattern inside a key that, if preset, is the only
|
||||
* part of the key hashed in order to select the server for this key.
|
||||
*
|
||||
*
|
||||
* @see http://code.google.com/p/redis/wiki/FAQ#I
|
||||
* 'm_using_some_form_of_key_hashing_for_partitioning,_but_wh
|
||||
* @param key
|
||||
* @return The tag if it exists, or the original key
|
||||
*/
|
||||
public String getKeyTag(String key) {
|
||||
if (tagPattern != null) {
|
||||
Matcher m = tagPattern.matcher(key);
|
||||
if (m.find())
|
||||
return m.group(1);
|
||||
}
|
||||
return key;
|
||||
if (tagPattern != null) {
|
||||
Matcher m = tagPattern.matcher(key);
|
||||
if (m.find())
|
||||
return m.group(1);
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
public Collection<S> getAllShardInfo() {
|
||||
return Collections.unmodifiableCollection(nodes.values());
|
||||
return Collections.unmodifiableCollection(nodes.values());
|
||||
}
|
||||
|
||||
public Collection<R> getAllShards() {
|
||||
return Collections.unmodifiableCollection(resources.values());
|
||||
return Collections.unmodifiableCollection(resources.values());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user