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.bsp.ImmutableOutputCommitter;
22import org.apache.giraph.conf.GiraphConfiguration;
23import org.apache.giraph.graph.Vertex;
24import org.apache.giraph.io.VertexOutputFormat;
25import org.apache.giraph.io.VertexWriter;
26import org.apache.giraph.utils.TestGraph;
27import org.apache.hadoop.io.Writable;
28import org.apache.hadoop.io.WritableComparable;
29import org.apache.hadoop.mapreduce.JobContext;
30import org.apache.hadoop.mapreduce.OutputCommitter;
31import org.apache.hadoop.mapreduce.TaskAttemptContext;
3233import java.io.IOException;
3435/**36 * VertexOutputFormat which stores all vertices in memory37 *38 * @param <I> Vertex id39 * @param <V> Vertex data40 * @param <E> Edge data41 */42publicclass InMemoryVertexOutputFormat<I extends WritableComparable,
43 V extends Writable, E extends Writable> extends44 VertexOutputFormat<I, V, E> {
45/** Graph where we store all vertices */46privatestaticTestGraph OUTPUT_GRAPH;
4748/**49 * Initialize this output format - needs to be called before running the50 * application. Creates new instance of TestGraph51 *52 * @param conf Configuration53 */54publicstaticvoid initializeOutputGraph(GiraphConfiguration conf) {
55 OUTPUT_GRAPH = newTestGraph(conf);
56 }
5758/**59 * Get graph containing all the vertices60 *61 * @param <I> Vertex id62 * @param <V> Vertex data63 * @param <E> Edge data64 * @return Output graph65 */66publicstatic <I extends WritableComparable, V extends Writable,
67 E extends Writable> TestGraph<I, V, E> getOutputGraph() {
68return OUTPUT_GRAPH;
69 }
7071 @Override
72public VertexWriter<I, V, E> createVertexWriter(
73 TaskAttemptContext context) throws IOException, InterruptedException {
74returnnew VertexWriter<I, V, E>() {
75 @Override
76publicvoid initialize(
77 TaskAttemptContext context) throws IOException, InterruptedException {
78 }
7980 @Override
81publicvoid close(
82 TaskAttemptContext context) throws IOException, InterruptedException {
83 }
8485 @Override
86publicvoid writeVertex(
87 Vertex<I, V, E> vertex) throws IOException, InterruptedException {
88synchronized (OUTPUT_GRAPH) {
89 OUTPUT_GRAPH.addVertex(vertex);
90 }
91 }
92 };
93 }
9495 @Override
96publicvoid checkOutputSpecs(
97 JobContext context) throws IOException, InterruptedException {
98 }
99100 @Override
101public OutputCommitter getOutputCommitter(
102 TaskAttemptContext context) throws IOException, InterruptedException {
103returnnewImmutableOutputCommitter();
104 }
105 }