This project has retired. For details please refer to its Attic page.
LongByteHashMapEdgesTest 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.edge;
19  
20  import com.google.common.collect.Lists;
21  import org.apache.giraph.utils.ExtendedDataOutput;
22  import org.apache.giraph.utils.UnsafeByteArrayInputStream;
23  import org.apache.giraph.utils.UnsafeByteArrayOutputStream;
24  import org.apache.hadoop.io.ByteWritable;
25  import org.apache.hadoop.io.LongWritable;
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  import java.io.DataInput;
30  import java.io.IOException;
31  import java.util.Iterator;
32  import java.util.List;
33  
34  import static org.junit.Assert.assertEquals;
35  import static org.junit.Assert.assertNotNull;
36  
37  
38  public class LongByteHashMapEdgesTest {
39    private static Edge<LongWritable, ByteWritable> createEdge(long id, byte value) {
40      return EdgeFactory.create(new LongWritable(id), new ByteWritable(value));
41    }
42  
43    private static void assertEdges(LongByteHashMapEdges edges, long[] expectedIds,
44                                    byte[] expectedValues) {
45      Assert.assertEquals(expectedIds.length, edges.size());
46      for (int i = 0; i< expectedIds.length; i++) {
47         ByteWritable value = edges.getEdgeValue(new LongWritable(expectedIds[i]));
48         assertNotNull(value);
49        assertEquals(expectedValues[i], value.get());
50      }
51    }
52  
53    @Test
54    public void testEdges() {
55      LongByteHashMapEdges edges = new LongByteHashMapEdges();
56  
57      List<Edge<LongWritable, ByteWritable>> initialEdges = Lists.newArrayList(
58        createEdge(1, (byte) 99), createEdge(2, (byte) 77), createEdge(4, (byte) 66));
59  
60      edges.initialize(initialEdges);
61      assertEdges(edges, new long[]{1, 2, 4}, new byte[]{99, 77, 66});
62  
63      edges.add(EdgeFactory.createReusable(new LongWritable(3), new ByteWritable((byte) 55)));
64      assertEdges(edges, new long[]{1, 2, 3, 4}, new byte[]{99, 77, 55, 66});
65  
66      edges.remove(new LongWritable(2));
67      assertEdges(edges, new long[]{1, 3, 4}, new byte[]{99, 55, 66});
68    }
69  
70    @Test
71    public void testMutateEdges() {
72      LongByteHashMapEdges edges = new LongByteHashMapEdges();
73  
74      edges.initialize();
75  
76      // Add 10 edges with id and value set to i, for i = 0..9
77      for (int i = 0; i < 10; ++i) {
78        edges.add(createEdge(i, (byte) i));
79      }
80  
81      // Use the mutable iterator to remove edges with even id
82      Iterator<MutableEdge<LongWritable, ByteWritable>> edgeIt =
83          edges.mutableIterator();
84      while (edgeIt.hasNext()) {
85        if (edgeIt.next().getTargetVertexId().get() % 2 == 0) {
86          edgeIt.remove();
87        }
88      }
89  
90      // We should now have 5 edges
91      assertEquals(5, edges.size());
92      // The edge ids should be all odd
93      for (Edge<LongWritable, ByteWritable> edge : edges) {
94        assertEquals(1, edge.getTargetVertexId().get() % 2);
95        assertEquals(1, edge.getValue().get() % 2);
96      }
97    }
98  
99    @Test
100   public void testSerialization() throws IOException {
101     LongByteHashMapEdges edges = new LongByteHashMapEdges();
102 
103     edges.initialize();
104 
105     // Add 10 edges with id and value set to i, for i = 0..9
106     for (int i = 0; i < 10; ++i) {
107       edges.add(createEdge(i, (byte) i));
108     }
109 
110     edges.trim();
111 
112     // Use the mutable iterator to remove edges with even id
113     Iterator<MutableEdge<LongWritable, ByteWritable>> edgeIt =
114         edges.mutableIterator();
115     while (edgeIt.hasNext()) {
116       if (edgeIt.next().getTargetVertexId().get() % 2 == 0) {
117         edgeIt.remove();
118       }
119     }
120 
121     // We should now have 5 edges
122     assertEdges(edges, new long[]{1, 3, 5, 7, 9}, new byte[]{1, 3, 5, 7, 9});
123 
124     ExtendedDataOutput tempBuffer = new UnsafeByteArrayOutputStream();
125 
126     edges.write(tempBuffer);
127 
128     DataInput input = new UnsafeByteArrayInputStream(
129       tempBuffer.getByteArray(), 0, tempBuffer.getPos());
130 
131     edges = new LongByteHashMapEdges();
132     edges.readFields(input);
133 
134     assertEquals(5, edges.size());
135 
136     for (Edge<LongWritable, ByteWritable> edge : edges) {
137       assertEquals(1, edge.getTargetVertexId().get() % 2);
138       assertEquals(1, edge.getValue().get() % 2);
139     }
140   }
141 
142   /**
143    * This implementation does not allow parallel edges.
144    */
145   @Test
146   public void testParallelEdges() {
147     LongByteHashMapEdges edges = new LongByteHashMapEdges();
148 
149     List<Edge<LongWritable, ByteWritable>> initialEdges = Lists.newArrayList(
150       createEdge(2, (byte) 1), createEdge(2, (byte) 2), createEdge(2, (byte) 3));
151 
152     edges.initialize(initialEdges);
153     assertEquals(1, edges.size());
154 
155     edges.remove(new LongWritable(2));
156     assertEquals(0, edges.size());
157 
158     edges.add(EdgeFactory.create(new LongWritable(2), new ByteWritable((byte) 4)));
159     assertEquals(1, edges.size());
160 
161     edges.trim();
162     assertEquals(1, edges.size());
163   }
164 }