blob: f5ccddae33e2a246f4a5b62b1fa15bd4a48a6c2a [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 Jampanif4c88502016-01-21 12:35:36 -080017package org.onosproject.store.primitives.impl;
Madan Jampani09342702015-02-05 23:32:40 -080018
19import static com.google.common.base.Preconditions.checkState;
20
Madan Jampanif1b8e172015-03-23 11:42:02 -070021import java.util.List;
Madan Jampani09342702015-02-05 23:32:40 -080022import com.google.common.base.Charsets;
Madan Jampanif1b8e172015-03-23 11:42:02 -070023import com.google.common.collect.ImmutableList;
Madan Jampani09342702015-02-05 23:32:40 -080024import com.google.common.hash.Hashing;
25
26/**
Madan Jampani7804c992015-07-20 13:20:19 -070027 * Partitioner for mapping map entries to individual database partitions.
Madan Jampani09342702015-02-05 23:32:40 -080028 * <p>
Madan Jampani7804c992015-07-20 13:20:19 -070029 * By default a md5 hash of the hash key (key or map name) is used to pick a
Madan Jampani09342702015-02-05 23:32:40 -080030 * partition.
31 */
32public abstract class DatabasePartitioner implements Partitioner<String> {
33 // Database partitions sorted by their partition name.
Madan Jampanif1b8e172015-03-23 11:42:02 -070034 protected final List<Database> partitions;
Madan Jampani09342702015-02-05 23:32:40 -080035
Madan Jampanif1b8e172015-03-23 11:42:02 -070036 public DatabasePartitioner(List<Database> partitions) {
37 checkState(partitions != null && !partitions.isEmpty(), "Partitions cannot be null or empty");
38 this.partitions = ImmutableList.copyOf(partitions);
Madan Jampani09342702015-02-05 23:32:40 -080039 }
40
41 protected int hash(String key) {
42 return Math.abs(Hashing.md5().newHasher().putBytes(key.getBytes(Charsets.UTF_8)).hash().asInt());
43 }
44
45}