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.utils;
2021import org.apache.giraph.bsp.BspInputSplit;
22import org.apache.giraph.graph.Vertex;
23import org.apache.giraph.io.VertexInputFormat;
24import org.apache.giraph.io.VertexReader;
25import org.apache.hadoop.conf.Configuration;
26import org.apache.hadoop.io.Writable;
27import org.apache.hadoop.io.WritableComparable;
28import org.apache.hadoop.mapreduce.InputSplit;
29import org.apache.hadoop.mapreduce.JobContext;
30import org.apache.hadoop.mapreduce.TaskAttemptContext;
3132import java.io.IOException;
33import java.util.ArrayList;
34import java.util.Iterator;
35import java.util.List;
3637/**38 * An input format that reads the input graph in memory. Used for unit tests.39 *40 * @param <I> The Input41 * @param <V> The vertex type42 * @param <E> The edge type43 */44publicclass InMemoryVertexInputFormat<I extends WritableComparable,
45 V extends Writable, E extends Writable>
46extends VertexInputFormat<I, V, E> {
47/** The graph */48privatestaticTestGraph GRAPH;
4950publicstaticvoid setGraph(TestGraph graph) {
51 InMemoryVertexInputFormat.GRAPH = graph;
52 }
5354publicstaticTestGraph getGraph() {
55return GRAPH;
56 }
5758 @Override publicvoid checkInputSpecs(Configuration conf) { }
5960 @Override
61public List<InputSplit> getSplits(JobContext context, int minSplitCountHint)
62throws IOException, InterruptedException {
63// This is meaningless, the VertexReader will generate all the test64// data.65 List<InputSplit> inputSplitList = new ArrayList<InputSplit>();
66for (int i = 0; i < minSplitCountHint; ++i) {
67 inputSplitList.add(newBspInputSplit(i, minSplitCountHint));
68 }
69return inputSplitList;
70 }
7172 @Override
73public VertexReader<I, V, E> createVertexReader(InputSplit inputSplit,
74 TaskAttemptContext context) throws IOException {
75returnnewInMemoryVertexReader();
76 }
7778/**79 * Simple in memory reader80 */81privateclassInMemoryVertexReaderextends VertexReader<I, V, E> {
82/** The iterator */83private Iterator<Vertex<I, V, E>> vertexIterator;
84/** Current vertex */85private Vertex<I, V, E> currentVertex;
8687 @Override
88publicvoid initialize(InputSplit inputSplit,
89 TaskAttemptContext context) {
90 vertexIterator = GRAPH.iterator();
91 }
9293 @Override
94publicboolean nextVertex() {
95if (vertexIterator.hasNext()) {
96 currentVertex = vertexIterator.next();
97returntrue;
98 }
99return false;
100 }
101102 @Override
103public Vertex<I, V, E> getCurrentVertex() {
104return currentVertex;
105 }
106107 @Override
108publicvoid close() {
109 }
110111 @Override
112publicfloat getProgress() {
113return 0;
114 }
115 }
116 }