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.io.formats;
2021import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
22import org.apache.giraph.partition.PartitionUtils;
23import org.apache.giraph.worker.WorkerInfo;
2425import java.util.Collections;
26import java.util.List;
27import java.util.Random;
2829/**30 * Helper class to generate pseudo-random local edges.31 * Like {@link PseudoRandomLocalEdgesHelper}, but for graphs where vertex ids32 * are integers.33 */34publicclassPseudoRandomIntNullLocalEdgesHelper {
35/** Minimum ratio of partition-local edges. */36privatefloat minLocalEdgesRatio;
37/** Total number of vertices. */38privateint numVertices;
39/** Total number of partitions. */40privateint numPartitions;
41/** Average partition size. */42privateint partitionSize;
4344/**45 * Constructor.46 *47 * @param numVertices Total number of vertices.48 * @param conf Configuration.49 */50publicPseudoRandomIntNullLocalEdgesHelper(int numVertices,
51ImmutableClassesGiraphConfiguration conf) {
52this.minLocalEdgesRatio = conf.getFloat(
53 PseudoRandomInputFormatConstants.LOCAL_EDGES_MIN_RATIO,
54 PseudoRandomInputFormatConstants.LOCAL_EDGES_MIN_RATIO_DEFAULT);
55this.numVertices = numVertices;
56int numWorkers = conf.getMaxWorkers();
57 List<WorkerInfo> workerInfos = Collections.nCopies(numWorkers,
58newWorkerInfo());
59 numPartitions = PartitionUtils.computePartitionCount(
60 workerInfos.size(), conf);
61 partitionSize = numVertices / numPartitions;
62 }
6364/**65 * Generate a destination vertex id for the given source vertex,66 * using the desired configuration for edge locality and the provided67 * pseudo-random generator.68 *69 * @param sourceVertexId Source vertex id.70 * @param rand Pseudo-random generator.71 * @return Destination vertex id.72 */73publicint generateDestVertex(int sourceVertexId, Random rand) {
74if (rand.nextFloat() < minLocalEdgesRatio) {
75int partitionId = sourceVertexId % numPartitions;
76return partitionId + numPartitions * rand.nextInt(partitionSize);
77 } else {
78return rand.nextInt(numVertices);
79 }
80 }
81 }