blob: ef4ece781b437bbd20ac831c8525a80b1c1ccd14 [file] [log] [blame]
Madan Jampani09342702015-02-05 23:32:40 -08001package org.onosproject.store.consistent.impl;
2
3import static com.google.common.base.Preconditions.checkState;
4
5import java.util.Map;
6
7import com.google.common.base.Charsets;
8import com.google.common.collect.ImmutableSortedMap;
9import com.google.common.hash.Hashing;
10
11/**
12 * Partitioner for mapping table entries to individual database partitions.
13 * <p>
14 * By default a md5 hash of the hash key (key or table name) is used to pick a
15 * partition.
16 */
17public abstract class DatabasePartitioner implements Partitioner<String> {
18 // Database partitions sorted by their partition name.
19 protected final Database[] sortedPartitions;
20
21 public DatabasePartitioner(Map<String, Database> partitionMap) {
22 checkState(partitionMap != null && !partitionMap.isEmpty(), "Partition map cannot be null or empty");
23 sortedPartitions = ImmutableSortedMap.<String, Database>copyOf(partitionMap).values().toArray(new Database[]{});
24 }
25
26 protected int hash(String key) {
27 return Math.abs(Hashing.md5().newHasher().putBytes(key.getBytes(Charsets.UTF_8)).hash().asInt());
28 }
29
30}