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 */18package org.apache.giraph.examples.io.formats;
1920import java.io.IOException;
21import java.util.List;
2223import org.apache.giraph.edge.Edge;
24import org.apache.giraph.edge.EdgeFactory;
25import org.apache.giraph.examples.utils.BrachaTouegDeadlockVertexValue;
26import org.apache.giraph.graph.Vertex;
27import org.apache.giraph.io.formats.TextVertexInputFormat;
28import org.apache.hadoop.io.LongWritable;
29import org.apache.hadoop.io.Text;
30import org.apache.hadoop.mapreduce.InputSplit;
31import org.apache.hadoop.mapreduce.TaskAttemptContext;
32import org.json.JSONArray;
33import org.json.JSONException;
3435import com.google.common.collect.Lists;
3637/**38 * VertexInputFormat for the Bracha Toueg Deadlock Detection algorithm39 * specified in JSON format.40 */41publicclassBrachaTouegDeadlockInputFormatextends42 TextVertexInputFormat<LongWritable, BrachaTouegDeadlockVertexValue,
43 LongWritable> {
4445 @Override
46publicTextVertexReader createVertexReader(InputSplit split,
47 TaskAttemptContext context) {
48returnnewJsonLongLongLongLongVertexReader();
49 }
5051/**52 * VertexReader use for the Bracha Toueg Deadlock Detection Algorithm.53 * The files should be in the following JSON format:54 * JSONArray(<vertex id>,55 * JSONArray(JSONArray(<dest vertex id>, <edge tag>), ...))56 * The tag is use for the N-out-of-M semantics. Two edges with the same tag57 * are considered to be combined and, hence, represent to combined requests58 * that need to be both satisfied to continue execution.59 * Here is an example with vertex id 1, and three edges (requests).60 * First edge has a destination vertex 2 with tag 0.61 * Second and third edge have a destination vertex respectively of 3 and 462 * with tag 1.63 * [1,[[2,0], [3,1], [4,1]]]64 */65classJsonLongLongLongLongVertexReader66extends TextVertexReaderFromEachLineProcessedHandlingExceptions<JSONArray,
67 JSONException> {
6869 @Override
70protected JSONArray preprocessLine(Text line) throws JSONException {
71returnnew JSONArray(line.toString());
72 }
7374 @Override
75protected LongWritable getId(JSONArray jsonVertex) throws JSONException,
76 IOException {
77returnnew LongWritable(jsonVertex.getLong(0));
78 }
7980 @Override
81protectedBrachaTouegDeadlockVertexValue getValue(JSONArray jsonVertex)
82throws JSONException, IOException {
8384returnnewBrachaTouegDeadlockVertexValue();
85 }
8687 @Override
88protected Iterable<Edge<LongWritable, LongWritable>>
89 getEdges(JSONArray jsonVertex) throws JSONException, IOException {
9091 JSONArray jsonEdgeArray = jsonVertex.getJSONArray(1);
9293/* get the edges */94 List<Edge<LongWritable, LongWritable>> edges =
95 Lists.newArrayListWithCapacity(jsonEdgeArray.length());
9697for (int i = 0; i < jsonEdgeArray.length(); ++i) {
98 LongWritable targetId;
99 LongWritable tag;
100 JSONArray jsonEdge = jsonEdgeArray.getJSONArray(i);
101102 targetId = new LongWritable(jsonEdge.getLong(0));
103 tag = new LongWritable((long) jsonEdge.getLong(1));
104 edges.add(EdgeFactory.create(targetId, tag));
105 }
106return edges;
107 }
108109 @Override
110protected Vertex<LongWritable, BrachaTouegDeadlockVertexValue,
111 LongWritable> handleException(Text line, JSONArray jsonVertex,
112 JSONException e) {
113114thrownew IllegalArgumentException(
115"Couldn't get vertex from line " + line, e);
116 }
117 }
118 }