Skip to content
This repository was archived by the owner on Oct 30, 2023. It is now read-only.

Commit ea7753f

Browse files
dlogothetisMaja Kabiljo
authored andcommitted
JIRA-1149
closes #40
1 parent e2f82b2 commit ea7753f

File tree

4 files changed

+500
-0
lines changed

4 files changed

+500
-0
lines changed

giraph-core/src/main/java/org/apache/giraph/edge/IdAndNullArrayEdges.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.DataOutput;
2222
import java.io.IOException;
2323
import java.util.Iterator;
24+
import java.util.NoSuchElementException;
2425

2526
import org.apache.giraph.conf.ImmutableClassesGiraphConfigurable;
2627
import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
@@ -75,6 +76,7 @@ public void initialize(Iterable<Edge<I, NullWritable>> edges) {
7576

7677
@Override
7778
public void initialize(int capacity) {
79+
neighbors.clear();
7880
neighbors.setCapacity(capacity);
7981
}
8082

@@ -158,6 +160,9 @@ public boolean hasNext() {
158160

159161
@Override
160162
public MutableEdge<I, NullWritable> next() {
163+
if (!hasNext()) {
164+
throw new NoSuchElementException();
165+
}
161166
neighbors.getIntoW(offset++, representativeEdge.getTargetVertexId());
162167
return representativeEdge;
163168
}

giraph-core/src/main/java/org/apache/giraph/edge/IdAndValueArrayEdges.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.DataOutput;
2222
import java.io.IOException;
2323
import java.util.Iterator;
24+
import java.util.NoSuchElementException;
2425

2526
import org.apache.giraph.conf.ImmutableClassesGiraphConfigurable;
2627
import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
@@ -78,7 +79,9 @@ public void initialize(Iterable<Edge<I, E>> edges) {
7879

7980
@Override
8081
public void initialize(int capacity) {
82+
neighborIds.clear();
8183
neighborIds.setCapacity(capacity);
84+
neighborEdgeValues.clear();
8285
neighborEdgeValues.setCapacity(capacity);
8386
}
8487

@@ -162,6 +165,9 @@ public boolean hasNext() {
162165

163166
@Override
164167
public Edge<I, E> next() {
168+
if (!hasNext()) {
169+
throw new NoSuchElementException();
170+
}
165171
neighborIds.getIntoW(index, representativeEdge.getTargetVertexId());
166172
neighborEdgeValues.getIntoW(index, representativeEdge.getValue());
167173
index++;
@@ -220,6 +226,9 @@ public boolean hasNext() {
220226

221227
@Override
222228
public MutableEdge<I, E> next() {
229+
if (!hasNext()) {
230+
throw new NoSuchElementException();
231+
}
223232
representativeEdge.setIndex(index++);
224233
return representativeEdge;
225234
}
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
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.edge;
19+
20+
import com.google.common.collect.Lists;
21+
import org.apache.giraph.conf.GiraphConfiguration;
22+
import org.apache.giraph.conf.GiraphConstants;
23+
import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
24+
import org.apache.hadoop.io.LongWritable;
25+
import org.apache.hadoop.io.NullWritable;
26+
import org.apache.hadoop.io.Writable;
27+
import org.junit.Assert;
28+
import org.junit.Test;
29+
30+
import java.io.ByteArrayInputStream;
31+
import java.io.ByteArrayOutputStream;
32+
import java.io.DataInputStream;
33+
import java.io.DataOutputStream;
34+
import java.io.IOException;
35+
import java.util.ArrayList;
36+
import java.util.HashSet;
37+
import java.util.Iterator;
38+
import java.util.List;
39+
import java.util.Set;
40+
41+
import static org.junit.Assert.assertEquals;
42+
import static org.junit.Assert.assertTrue;
43+
44+
45+
public class IdAndNullArrayEdgesTest {
46+
private static Edge<LongWritable, NullWritable> createEdge(long id) {
47+
return EdgeFactory.create(new LongWritable(id));
48+
}
49+
50+
private static void assertEdges(IdAndNullArrayEdges edges, long... expected) {
51+
int index = 0;
52+
for (Edge<LongWritable, NullWritable> edge :
53+
(Iterable<Edge<LongWritable, NullWritable>>) edges) {
54+
Assert.assertEquals(expected[index], edge.getTargetVertexId().get());
55+
index++;
56+
}
57+
Assert.assertEquals(expected.length, index);
58+
}
59+
60+
private IdAndNullArrayEdges getEdges() {
61+
GiraphConfiguration gc = new GiraphConfiguration();
62+
GiraphConstants.VERTEX_ID_CLASS.set(gc, LongWritable.class);
63+
GiraphConstants.EDGE_VALUE_CLASS.set(gc, NullWritable.class);
64+
ImmutableClassesGiraphConfiguration<LongWritable, Writable, NullWritable> conf =
65+
new ImmutableClassesGiraphConfiguration<LongWritable, Writable, NullWritable>(gc);
66+
IdAndNullArrayEdges ret = new IdAndNullArrayEdges();
67+
ret.setConf(
68+
new ImmutableClassesGiraphConfiguration<LongWritable, Writable, NullWritable>(conf));
69+
return ret;
70+
}
71+
72+
@Test
73+
public void testEdges() {
74+
IdAndNullArrayEdges edges = getEdges();
75+
76+
List<Edge<LongWritable, NullWritable>> initialEdges = Lists.newArrayList(
77+
createEdge(1), createEdge(2), createEdge(4));
78+
79+
edges.initialize(initialEdges);
80+
assertEdges(edges, 1, 2, 4);
81+
82+
edges.add(EdgeFactory.createReusable(new LongWritable(3)));
83+
assertEdges(edges, 1, 2, 4, 3); // order matters, it's an array
84+
85+
edges.remove(new LongWritable(2));
86+
assertEdges(edges, 1, 3, 4);
87+
}
88+
89+
@Test
90+
public void testInitialize() {
91+
IdAndNullArrayEdges edges = getEdges();
92+
93+
List<Edge<LongWritable, NullWritable>> initialEdges = Lists.newArrayList(
94+
createEdge(1), createEdge(2), createEdge(4));
95+
96+
edges.initialize(initialEdges);
97+
assertEdges(edges, 1, 2, 4);
98+
99+
edges.add(EdgeFactory.createReusable(new LongWritable(3)));
100+
assertEdges(edges, 1, 2, 4, 3);
101+
102+
edges.initialize();
103+
assertEquals(0, edges.size());
104+
}
105+
106+
@Test
107+
public void testMutateEdges() {
108+
IdAndNullArrayEdges edges = getEdges();
109+
110+
edges.initialize();
111+
112+
// Add 10 edges with id i, for i = 0..9
113+
for (int i = 0; i < 10; ++i) {
114+
edges.add(createEdge(i));
115+
}
116+
117+
// Use the mutable iterator to remove edges with even id
118+
Iterator<MutableEdge<LongWritable, NullWritable>> edgeIt =
119+
edges.mutableIterator();
120+
while (edgeIt.hasNext()) {
121+
if (edgeIt.next().getTargetVertexId().get() % 2 == 0) {
122+
edgeIt.remove();
123+
}
124+
}
125+
126+
// We should now have 5 edges
127+
assertEquals(5, edges.size());
128+
// The edge ids should be all odd
129+
for (Edge<LongWritable, NullWritable> edge :
130+
(Iterable<Edge<LongWritable, NullWritable>>) edges) {
131+
assertEquals(1, edge.getTargetVertexId().get() % 2);
132+
}
133+
}
134+
135+
@Test
136+
public void testSerialization() throws IOException {
137+
IdAndNullArrayEdges edges = getEdges();
138+
139+
edges.initialize();
140+
141+
// Add 10 edges with id i, for i = 0..9
142+
for (int i = 0; i < 10; ++i) {
143+
edges.add(createEdge(i));
144+
}
145+
146+
// Use the mutable iterator to remove edges with even id
147+
Iterator<MutableEdge<LongWritable, NullWritable>> edgeIt =
148+
edges.mutableIterator();
149+
while (edgeIt.hasNext()) {
150+
if (edgeIt.next().getTargetVertexId().get() % 2 == 0) {
151+
edgeIt.remove();
152+
}
153+
}
154+
155+
// We should now have 5 edges
156+
assertEdges(edges, 9, 1, 7, 3, 5); // id order matter because of the implementation
157+
158+
ByteArrayOutputStream arrayStream = new ByteArrayOutputStream();
159+
DataOutputStream tempBuffer = new DataOutputStream(arrayStream);
160+
161+
edges.write(tempBuffer);
162+
tempBuffer.close();
163+
164+
byte[] binary = arrayStream.toByteArray();
165+
166+
assertTrue("Serialized version should not be empty ", binary.length > 0);
167+
168+
edges = getEdges();
169+
edges.readFields(new DataInputStream(new ByteArrayInputStream(binary)));
170+
171+
assertEquals(5, edges.size());
172+
173+
long[] ids = new long[]{9, 1, 7, 3, 5};
174+
int index = 0;
175+
176+
for (Edge<LongWritable, NullWritable> edge :
177+
(Iterable<Edge<LongWritable, NullWritable>>) edges) {
178+
assertEquals(ids[index], edge.getTargetVertexId().get());
179+
index++;
180+
}
181+
assertEquals(ids.length, index);
182+
}
183+
184+
@Test
185+
public void testParallelEdges() {
186+
IdAndNullArrayEdges edges = getEdges();
187+
188+
List<Edge<LongWritable, NullWritable>> initialEdges = Lists.newArrayList(
189+
createEdge(2), createEdge(2), createEdge(2));
190+
191+
edges.initialize(initialEdges);
192+
assertEquals(3, edges.size());
193+
194+
edges.remove(new LongWritable(2));
195+
assertEquals(0, edges.size());
196+
197+
edges.add(EdgeFactory.create(new LongWritable(2)));
198+
assertEquals(1, edges.size());
199+
200+
assertEquals(1, edges.size());
201+
}
202+
203+
@Test
204+
public void testEdgeValues() {
205+
IdAndNullArrayEdges edges = getEdges();
206+
Set<Long> testValues = new HashSet<Long>();
207+
testValues.add(0L);
208+
testValues.add((long) Integer.MAX_VALUE);
209+
testValues.add(Long.MAX_VALUE);
210+
211+
List<Edge<LongWritable, NullWritable>> initialEdges =
212+
new ArrayList<Edge<LongWritable, NullWritable>>();
213+
for(Long id : testValues) {
214+
initialEdges.add(createEdge(id));
215+
}
216+
217+
edges.initialize(initialEdges);
218+
219+
Iterator<MutableEdge<LongWritable, NullWritable>> edgeIt =
220+
edges.mutableIterator();
221+
while (edgeIt.hasNext()) {
222+
long value = edgeIt.next().getTargetVertexId().get();
223+
assertTrue("Unknown edge found " + value, testValues.remove(value));
224+
}
225+
}
226+
227+
@Test
228+
public void testAddedSmallerValues() {
229+
IdAndNullArrayEdges edges = getEdges();
230+
231+
List<Edge<LongWritable, NullWritable>> initialEdges = Lists.newArrayList(
232+
createEdge(100));
233+
234+
edges.initialize(initialEdges);
235+
236+
for (int i=0; i<16; i++) {
237+
edges.add(createEdge(i));
238+
}
239+
240+
assertEquals(17, edges.size());
241+
}
242+
}

0 commit comments

Comments
 (0)