Repository: giraph Updated Branches: refs/heads/trunk a1a236fa6 -> 572ca0630
GIRAPH-1015: Support vertex combiner in TestGraph Summary: TestGraph should use vertex combiner which is specified in the conf passed, instead of replacing the vertex with latest added. Test Plan: Added a test, mvn clean verify Reviewers: sergey.edunov, ikabiljo Differential Revision: https://reviews.facebook.net/D40227 Project: http://git-wip-us.apache.org/repos/asf/giraph/repo Commit: http://git-wip-us.apache.org/repos/asf/giraph/commit/572ca063 Tree: http://git-wip-us.apache.org/repos/asf/giraph/tree/572ca063 Diff: http://git-wip-us.apache.org/repos/asf/giraph/diff/572ca063 Branch: refs/heads/trunk Commit: 572ca06300c04a3b8abee4da1fdaaef7056bb9b9 Parents: a1a236f Author: Maja Kabiljo <[email protected]> Authored: Tue Jun 16 17:30:22 2015 -0700 Committer: Maja Kabiljo <[email protected]> Committed: Wed Jun 17 09:53:55 2015 -0700 ---------------------------------------------------------------------- .../java/org/apache/giraph/utils/TestGraph.java | 41 ++++++++- .../org/apache/giraph/utils/TestTestGraph.java | 93 ++++++++++++++++++++ 2 files changed, 131 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/giraph/blob/572ca063/giraph-core/src/main/java/org/apache/giraph/utils/TestGraph.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/main/java/org/apache/giraph/utils/TestGraph.java b/giraph-core/src/main/java/org/apache/giraph/utils/TestGraph.java index 6bdd4b9..2865a53 100644 --- a/giraph-core/src/main/java/org/apache/giraph/utils/TestGraph.java +++ b/giraph-core/src/main/java/org/apache/giraph/utils/TestGraph.java @@ -28,6 +28,7 @@ import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration; import org.apache.giraph.edge.Edge; import org.apache.giraph.edge.EdgeFactory; import org.apache.giraph.graph.Vertex; +import org.apache.giraph.graph.VertexValueCombiner; import org.apache.hadoop.io.Writable; import org.apache.hadoop.io.WritableComparable; @@ -46,6 +47,8 @@ public class TestGraph<I extends WritableComparable, V extends Writable, E extends Writable> implements Iterable<Vertex<I, V, E>> { + /** Vertex value combiner */ + protected final VertexValueCombiner<V> vertexValueCombiner; /** The vertex values */ protected HashMap<I, Vertex<I, V, E>> vertices = Maps.newHashMap(); /** The configuration */ @@ -58,6 +61,7 @@ public class TestGraph<I extends WritableComparable, */ public TestGraph(GiraphConfiguration conf) { this.conf = new ImmutableClassesGiraphConfiguration(conf); + vertexValueCombiner = this.conf.createVertexValueCombiner(); } public HashMap<I, Vertex<I, V, E>> getVertices() { @@ -83,7 +87,15 @@ public class TestGraph<I extends WritableComparable, * @return this */ public TestGraph<I, V, E> addVertex(Vertex<I, V, E> vertex) { - vertices.put(vertex.getId(), vertex); + Vertex<I, V, E> previousVertex = vertices.get(vertex.getId()); + if (previousVertex != null) { + vertexValueCombiner.combine(previousVertex.getValue(), vertex.getValue()); + for (Edge<I, E> edge : vertex.getEdges()) { + previousVertex.addEdge(edge); + } + } else { + vertices.put(vertex.getId(), vertex); + } return this; } @@ -97,8 +109,31 @@ public class TestGraph<I extends WritableComparable, */ public TestGraph<I, V, E> addVertex(I id, V value, Entry<I, E>... edges) { - Vertex<I, V, E> v = makeVertex(id, value, edges); - vertices.put(id, v); + addVertex(makeVertex(id, value, edges)); + return this; + } + + /** + * Set vertex, replace if there was already a vertex with same id added + * + * @param vertex Vertex + * @return this + */ + public TestGraph<I, V, E> setVertex(Vertex<I, V, E> vertex) { + vertices.put(vertex.getId(), vertex); + return this; + } + + /** + * Set vertex, replace if there was already a vertex with same id added + * + * @param id the index + * @param value the value + * @param edges all edges + * @return this + */ + public TestGraph<I, V, E> setVertex(I id, V value, Entry<I, E>... edges) { + setVertex(makeVertex(id, value, edges)); return this; } http://git-wip-us.apache.org/repos/asf/giraph/blob/572ca063/giraph-core/src/test/java/org/apache/giraph/utils/TestTestGraph.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/test/java/org/apache/giraph/utils/TestTestGraph.java b/giraph-core/src/test/java/org/apache/giraph/utils/TestTestGraph.java new file mode 100644 index 0000000..afb56fd --- /dev/null +++ b/giraph-core/src/test/java/org/apache/giraph/utils/TestTestGraph.java @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.giraph.utils; + +import org.apache.giraph.conf.GiraphConfiguration; +import org.apache.giraph.conf.GiraphConstants; +import org.apache.giraph.edge.HashMapEdges; +import org.apache.giraph.graph.Vertex; +import org.apache.giraph.graph.VertexValueCombiner; +import org.apache.hadoop.io.LongWritable; +import org.apache.hadoop.io.NullWritable; +import org.junit.Assert; +import org.junit.Test; + +import java.util.AbstractMap; +import java.util.Map; + +/** + * Test TestGraph + */ +public class TestTestGraph { + @Test + public void testTestGraph() { + GiraphConfiguration conf = new GiraphConfiguration(); + GiraphConstants.VERTEX_ID_CLASS.set(conf, LongWritable.class); + GiraphConstants.VERTEX_VALUE_CLASS.set(conf, LongWritable.class); + GiraphConstants.EDGE_VALUE_CLASS.set(conf, NullWritable.class); + conf.setVertexValueCombinerClass(SumLongVertexValueCombiner.class); + conf.setOutEdgesClass(HashMapEdges.class); + TestGraph<LongWritable, LongWritable, NullWritable> testGraph = + new TestGraph<>(conf); + addVertex(testGraph, 1, 10, 2, 3); + addVertex(testGraph, 2, 20, 1, 3); + addVertex(testGraph, 3, 30, 1, 2); + addVertex(testGraph, 1, 100, 3, 4); + addVertex(testGraph, 2, 200, 5, 1, 6); + + Vertex<LongWritable, LongWritable, NullWritable> vertex1 = + testGraph.getVertex(new LongWritable(1)); + Assert.assertEquals(110, vertex1.getValue().get()); + Assert.assertEquals(3, vertex1.getNumEdges()); + + Vertex<LongWritable, LongWritable, NullWritable> vertex2 = + testGraph.getVertex(new LongWritable(2)); + Assert.assertEquals(220, vertex2.getValue().get()); + Assert.assertEquals(4, vertex2.getNumEdges()); + + Vertex<LongWritable, LongWritable, NullWritable> vertex3 = + testGraph.getVertex(new LongWritable(3)); + Assert.assertEquals(30, vertex3.getValue().get()); + Assert.assertEquals(2, vertex3.getNumEdges()); + } + + public static void addVertex( + TestGraph<LongWritable, LongWritable, NullWritable> graph, long id, + long value, long... neighbors) { + Map.Entry<LongWritable, NullWritable> edges[] = + new Map.Entry[neighbors.length]; + for (int i = 0; i < neighbors.length; i++) { + edges[i] = new AbstractMap.SimpleEntry<>( + new LongWritable(neighbors[i]), NullWritable.get()); + } + graph.addVertex(new LongWritable(id), new LongWritable(value), edges); + } + + /** + * Vertex value combiner that sums up long vertex values + */ + public static class SumLongVertexValueCombiner + implements VertexValueCombiner<LongWritable> { + @Override + public void combine(LongWritable originalVertexValue, + LongWritable vertexValue) { + originalVertexValue.set(originalVertexValue.get() + vertexValue.get()); + } + } +}
