This project has retired. For details please refer to its Attic page.
TestCreateSourceVertex 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  
19  package org.apache.giraph.io;
20  
21  import com.google.common.collect.Maps;
22  import org.apache.giraph.conf.GiraphConfiguration;
23  import org.apache.giraph.conf.GiraphConstants;
24  import org.apache.giraph.edge.ByteArrayEdges;
25  import org.apache.giraph.edge.DefaultCreateSourceVertexCallback;
26  import org.apache.giraph.io.formats.IdWithValueTextOutputFormat;
27  import org.apache.giraph.io.formats.IntIntNullTextVertexInputFormat;
28  import org.apache.giraph.io.formats.IntNullTextEdgeInputFormat;
29  import org.apache.giraph.utils.ComputationCountEdges;
30  import org.apache.giraph.utils.IntIntNullNoOpComputation;
31  import org.apache.giraph.utils.InternalVertexRunner;
32  import org.apache.hadoop.io.IntWritable;
33  import org.junit.Test;
34  
35  import java.util.Map;
36  
37  import static org.junit.Assert.assertEquals;
38  import static org.junit.Assert.assertTrue;
39  
40  /**
41   * Test createSourceVertex configuration option
42   */
43  public class TestCreateSourceVertex {
44    @Test
45    public void testPositiveCreateSourceVertex() throws Exception {
46      String [] vertices = new String[] {
47          "1 0",
48          "2 0",
49          "3 0",
50          "4 0",
51      };
52      String [] edges = new String[] {
53          "1 2",
54          "1 5",
55          "2 4",
56          "2 1",
57          "3 4",
58          "4 1",
59          "4 5",
60          "6 2",
61          "7 8",
62          "4 8",
63      };
64  
65      GiraphConfiguration conf = getConf();
66      conf.setCreateSourceVertex(false);
67  
68      Iterable<String> results = InternalVertexRunner.run(conf, vertices, edges);
69      Map<Integer, Integer> values = parseResults(results);
70  
71      // Check that only vertices from vertex input are present in output graph
72      assertEquals(4, values.size());
73      // Check that the ids of vertices in output graph exactly match vertex input
74      assertTrue(values.containsKey(1));
75      assertTrue(values.containsKey(2));
76      assertTrue(values.containsKey(3));
77      assertTrue(values.containsKey(4));
78  
79      conf.setComputationClass(ComputationCountEdges.class);
80      results = InternalVertexRunner.run(conf, vertices, edges);
81      values = parseResults(results);
82  
83      // Check the number of edges of each vertex
84      assertEquals(2, (int) values.get(1));
85      assertEquals(2, (int) values.get(2));
86      assertEquals(1, (int) values.get(3));
87      assertEquals(3, (int) values.get(4));
88    }
89  
90    @Test
91    public void testNegativeCreateSourceVertex() throws Exception {
92      String [] vertices = new String[] {
93          "1 0",
94          "2 0",
95          "3 0",
96          "4 0",
97      };
98      String [] edges = new String[] {
99          "1 2",
100         "1 5",
101         "2 4",
102         "2 1",
103         "3 4",
104         "4 1",
105         "4 5",
106         "6 2",
107         "7 8",
108         "4 8",
109     };
110 
111     GiraphConfiguration conf = getConf();
112 
113     Iterable<String> results = InternalVertexRunner.run(conf, vertices, edges);
114     Map<Integer, Integer> values = parseResults(results);
115 
116     // Check that only vertices from vertex input are present in output graph
117     assertEquals(6, values.size());
118     // Check that the ids of vertices in output graph exactly match vertex input
119     assertTrue(values.containsKey(1));
120     assertTrue(values.containsKey(2));
121     assertTrue(values.containsKey(3));
122     assertTrue(values.containsKey(4));
123     assertTrue(values.containsKey(6));
124     assertTrue(values.containsKey(7));
125 
126     conf.setComputationClass(ComputationCountEdges.class);
127     results = InternalVertexRunner.run(conf, vertices, edges);
128     values = parseResults(results);
129 
130     // Check the number of edges of each vertex
131     assertEquals(2, (int) values.get(1));
132     assertEquals(2, (int) values.get(2));
133     assertEquals(1, (int) values.get(3));
134     assertEquals(3, (int) values.get(4));
135     assertEquals(1, (int) values.get(6));
136     assertEquals(1, (int) values.get(7));
137   }
138 
139   @Test
140   public void testCustomCreateSourceVertex() throws Exception {
141     String [] vertices = new String[] {
142         "1 0",
143         "2 0",
144         "3 0",
145         "4 0",
146     };
147     String [] edges = new String[] {
148         "1 2",
149         "1 5",
150         "2 4",
151         "2 1",
152         "3 4",
153         "4 1",
154         "4 5",
155         "6 2",
156         "7 8",
157         "4 8",
158     };
159 
160     GiraphConfiguration conf = getConf();
161     GiraphConstants.CREATE_EDGE_SOURCE_VERTICES_CALLBACK.set(conf,
162         CreateEvenSourceVerticesCallback.class);
163 
164     Iterable<String> results = InternalVertexRunner.run(conf, vertices, edges);
165     Map<Integer, Integer> values = parseResults(results);
166 
167     // Check that only vertices from vertex input are present in output graph
168     assertEquals(5, values.size());
169     // Check that the ids of vertices in output graph exactly match vertex input
170     assertTrue(values.containsKey(1));
171     assertTrue(values.containsKey(2));
172     assertTrue(values.containsKey(3));
173     assertTrue(values.containsKey(4));
174     assertTrue(values.containsKey(6));
175 
176     conf.setComputationClass(ComputationCountEdges.class);
177     results = InternalVertexRunner.run(conf, vertices, edges);
178     values = parseResults(results);
179 
180     // Check the number of edges of each vertex
181     assertEquals(2, (int) values.get(1));
182     assertEquals(2, (int) values.get(2));
183     assertEquals(1, (int) values.get(3));
184     assertEquals(3, (int) values.get(4));
185     assertEquals(1, (int) values.get(6));
186   }
187 
188   /**
189    * Only allows to create vertices with even ids.
190    */
191   public static class CreateEvenSourceVerticesCallback extends
192       DefaultCreateSourceVertexCallback<IntWritable> {
193 
194     @Override
195     public boolean shouldCreateSourceVertex(IntWritable vertexId) {
196       return vertexId.get()  % 2 == 0;
197     }
198   }
199 
200 
201   private GiraphConfiguration getConf() {
202     GiraphConfiguration conf = new GiraphConfiguration();
203     conf.setComputationClass(IntIntNullNoOpComputation.class);
204     conf.setOutEdgesClass(ByteArrayEdges.class);
205     conf.setVertexInputFormatClass(IntIntNullTextVertexInputFormat.class);
206     conf.setEdgeInputFormatClass(IntNullTextEdgeInputFormat.class);
207     conf.setVertexOutputFormatClass(IdWithValueTextOutputFormat.class);
208     return conf;
209   }
210 
211   private static Map<Integer, Integer> parseResults(Iterable<String> results) {
212     Map<Integer, Integer> values = Maps.newHashMap();
213     for (String line : results) {
214       String[] tokens = line.split("\\s+");
215       int id = Integer.valueOf(tokens[0]);
216       int value = Integer.valueOf(tokens[1]);
217       values.put(id, value);
218     }
219     return values;
220   }
221 }