This project has retired. For details please refer to its Attic page.
BrachaTouegDeadlockInputFormat xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * 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 and
16   * limitations under the License.
17   */
18  package org.apache.giraph.examples.io.formats;
19  
20  import java.io.IOException;
21  import java.util.List;
22  
23  import org.apache.giraph.edge.Edge;
24  import org.apache.giraph.edge.EdgeFactory;
25  import org.apache.giraph.examples.utils.BrachaTouegDeadlockVertexValue;
26  import org.apache.giraph.graph.Vertex;
27  import org.apache.giraph.io.formats.TextVertexInputFormat;
28  import org.apache.hadoop.io.LongWritable;
29  import org.apache.hadoop.io.Text;
30  import org.apache.hadoop.mapreduce.InputSplit;
31  import org.apache.hadoop.mapreduce.TaskAttemptContext;
32  import org.json.JSONArray;
33  import org.json.JSONException;
34  
35  import com.google.common.collect.Lists;
36  
37  /**
38    * VertexInputFormat for the Bracha Toueg Deadlock Detection algorithm
39    * specified in JSON format.
40    */
41  public class BrachaTouegDeadlockInputFormat extends
42    TextVertexInputFormat<LongWritable, BrachaTouegDeadlockVertexValue,
43      LongWritable> {
44  
45    @Override
46    public TextVertexReader createVertexReader(InputSplit split,
47        TaskAttemptContext context) {
48      return new JsonLongLongLongLongVertexReader();
49    }
50  
51   /**
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 tag
57    * are considered to be combined and, hence, represent to combined requests
58    * 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 4
62    * with tag 1.
63    * [1,[[2,0], [3,1], [4,1]]]
64    */
65    class JsonLongLongLongLongVertexReader
66      extends TextVertexReaderFromEachLineProcessedHandlingExceptions<JSONArray,
67              JSONException> {
68  
69      @Override
70      protected JSONArray preprocessLine(Text line) throws JSONException {
71        return new JSONArray(line.toString());
72      }
73  
74      @Override
75      protected LongWritable getId(JSONArray jsonVertex) throws JSONException,
76                IOException {
77        return new LongWritable(jsonVertex.getLong(0));
78      }
79  
80      @Override
81      protected BrachaTouegDeadlockVertexValue getValue(JSONArray jsonVertex)
82        throws JSONException, IOException {
83  
84        return new BrachaTouegDeadlockVertexValue();
85      }
86  
87      @Override
88      protected Iterable<Edge<LongWritable, LongWritable>>
89      getEdges(JSONArray jsonVertex) throws JSONException, IOException {
90  
91        JSONArray jsonEdgeArray = jsonVertex.getJSONArray(1);
92  
93        /* get the edges */
94        List<Edge<LongWritable, LongWritable>> edges =
95            Lists.newArrayListWithCapacity(jsonEdgeArray.length());
96  
97        for (int i = 0; i < jsonEdgeArray.length(); ++i) {
98          LongWritable targetId;
99          LongWritable tag;
100         JSONArray jsonEdge = jsonEdgeArray.getJSONArray(i);
101 
102         targetId = new LongWritable(jsonEdge.getLong(0));
103         tag = new LongWritable((long) jsonEdge.getLong(1));
104         edges.add(EdgeFactory.create(targetId, tag));
105       }
106       return edges;
107     }
108 
109     @Override
110     protected Vertex<LongWritable, BrachaTouegDeadlockVertexValue,
111       LongWritable> handleException(Text line, JSONArray jsonVertex,
112           JSONException e) {
113 
114       throw new IllegalArgumentException(
115           "Couldn't get vertex from line " + line, e);
116     }
117   }
118 }