1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */1819package org.apache.giraph.partition;
2021import org.apache.giraph.conf.GiraphConstants;
22import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
23import org.apache.hadoop.io.LongWritable;
24import org.apache.hadoop.io.Writable;
2526/**27 * Factory for simple range-based partitioners based on long vertex ids.28 * Workers are assigned equal-sized ranges of partitions,29 * and partitions are assigned equal-sized ranges of vertices.30 *31 * @param <V> Vertex value type32 * @param <E> Edge value type33 */34publicclass SimpleLongRangePartitionerFactory<V extends Writable,
35 E extends Writable> extends GraphPartitionerFactory<LongWritable, V, E> {
3637/** Vertex key space size. */38privatelong keySpaceSize;
3940 @Override
41publicint getPartition(LongWritable id, int partitionCount,
42int workerCount) {
43return getPartition(id, partitionCount);
44 }
4546/**47 * Calculates in which partition current vertex belongs to,48 * from interval [0, partitionCount).49 *50 * @param id Vertex id51 * @param partitionCount Number of partitions52 * @return partition53 */54protectedint getPartition(LongWritable id, int partitionCount) {
55return getPartitionInRange(id.get(), keySpaceSize, partitionCount);
56 }
5758 @Override
59publicint getWorker(int partition, int partitionCount, int workerCount) {
60return getPartitionInRange(partition, partitionCount, workerCount);
61 }
6263 @Override
64publicvoid setConf(ImmutableClassesGiraphConfiguration conf) {
65super.setConf(conf);
66 keySpaceSize =
67 conf.getLong(GiraphConstants.PARTITION_VERTEX_KEY_SPACE_SIZE, -1);
68if (keySpaceSize == -1) {
69thrownew IllegalStateException("Need to specify " +
70 GiraphConstants.PARTITION_VERTEX_KEY_SPACE_SIZE +
71" when using SimpleLongRangePartitionerFactory");
72 }
73 }
74 }