blob: cd0525e501e4f82863e45059c41bdf3b67928dae [file] [log] [blame]
Madan Jampani25461112015-02-17 14:17:29 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Madan Jampani09342702015-02-05 23:32:40 -080017package org.onosproject.store.consistent.impl;
18
19import static com.google.common.base.Preconditions.checkState;
20
21import java.util.Map;
22
23import com.google.common.base.Charsets;
24import com.google.common.collect.ImmutableSortedMap;
25import com.google.common.hash.Hashing;
26
27/**
28 * Partitioner for mapping table entries to individual database partitions.
29 * <p>
30 * By default a md5 hash of the hash key (key or table name) is used to pick a
31 * partition.
32 */
33public abstract class DatabasePartitioner implements Partitioner<String> {
34 // Database partitions sorted by their partition name.
35 protected final Database[] sortedPartitions;
36
37 public DatabasePartitioner(Map<String, Database> partitionMap) {
38 checkState(partitionMap != null && !partitionMap.isEmpty(), "Partition map cannot be null or empty");
39 sortedPartitions = ImmutableSortedMap.<String, Database>copyOf(partitionMap).values().toArray(new Database[]{});
40 }
41
42 protected int hash(String key) {
43 return Math.abs(Hashing.md5().newHasher().putBytes(key.getBytes(Charsets.UTF_8)).hash().asInt());
44 }
45
46}