Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame^] | 1 | package org.onosproject.store.consistent.impl; |
| 2 | |
| 3 | import static com.google.common.base.Preconditions.checkState; |
| 4 | |
| 5 | import java.util.Map; |
| 6 | |
| 7 | import com.google.common.base.Charsets; |
| 8 | import com.google.common.collect.ImmutableSortedMap; |
| 9 | import 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 | */ |
| 17 | public 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 | } |