1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */1819package org.apache.giraph.edge;
2021import it.unimi.dsi.fastutil.longs.LongIterator;
22import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
2324import org.apache.giraph.utils.EdgeIterables;
25import org.apache.giraph.utils.Trimmable;
26import org.apache.hadoop.io.LongWritable;
27import org.apache.hadoop.io.NullWritable;
2829import java.io.DataInput;
30import java.io.DataOutput;
31import java.io.IOException;
32import java.util.Iterator;
3334/**35 * {@link OutEdges} implementation with long ids and null edge values,36 * backed by a {@link LongOpenHashSet}.37 * Parallel edges are not allowed.38 * Note: this implementation is optimized for fast random access and mutations,39 * and uses less space than a generic {@link HashMapEdges} (but more than40 * {@link LongNullArrayEdges}.41 */42publicclassLongNullHashSetEdges43implements ReuseObjectsOutEdges<LongWritable, NullWritable>,
44 MutableOutEdges<LongWritable, NullWritable>,
45 StrictRandomAccessOutEdges<LongWritable, NullWritable>,
46Trimmable {
47/** Hash set of target vertex ids. */48private LongOpenHashSet neighbors;
4950 @Override
51publicvoid initialize(Iterable<Edge<LongWritable, NullWritable>> edges) {
52 EdgeIterables.initialize(this, edges);
53 }
5455 @Override
56publicvoid initialize(int capacity) {
57 neighbors = new LongOpenHashSet(capacity);
58 }
5960 @Override
61publicvoid initialize() {
62 neighbors = new LongOpenHashSet();
63 }
6465 @Override
66publicvoid add(Edge<LongWritable, NullWritable> edge) {
67 neighbors.add(edge.getTargetVertexId().get());
68 }
6970 @Override
71publicvoid remove(LongWritable targetVertexId) {
72 neighbors.remove(targetVertexId.get());
73 }
7475 @Override
76publicint size() {
77return neighbors.size();
78 }
7980 @Override
81public Iterator<Edge<LongWritable, NullWritable>> iterator() {
82// Returns an iterator that reuses objects.83// The downcast is fine because all concrete Edge implementations are84// mutable, but we only expose the mutation functionality when appropriate.85return (Iterator) mutableIterator();
86 }
8788 @Override
89public Iterator<MutableEdge<LongWritable, NullWritable>> mutableIterator() {
90returnnew Iterator<MutableEdge<LongWritable, NullWritable>>() {
91/** Wrapped neighbors iterator. */92private LongIterator neighborsIt = neighbors.iterator();
93/** Representative edge object. */94private ReusableEdge<LongWritable, NullWritable> representativeEdge =
95 EdgeFactory.createReusable(new LongWritable());
9697publicboolean hasNext() {
98return neighborsIt.hasNext();
99 }
100101 @Override
102public MutableEdge<LongWritable, NullWritable> next() {
103 representativeEdge.getTargetVertexId().set(neighborsIt.nextLong());
104return representativeEdge;
105 }
106107 @Override
108publicvoid remove() {
109 neighborsIt.remove();
110 }
111 };
112 }
113114 @Override
115publicvoid write(DataOutput out) throws IOException {
116 out.writeInt(neighbors.size());
117 LongIterator neighborsIt = neighbors.iterator();
118while (neighborsIt.hasNext()) {
119 out.writeLong(neighborsIt.nextLong());
120 }
121 }
122123 @Override
124publicvoid readFields(DataInput in) throws IOException {
125int numEdges = in.readInt();
126 initialize(numEdges);
127for (int i = 0; i < numEdges; ++i) {
128 neighbors.add(in.readLong());
129 }
130 }
131132 @Override
133public NullWritable getEdgeValue(LongWritable targetVertexId) {
134if (neighbors.contains(targetVertexId.get())) {
135return NullWritable.get();
136 } else {
137returnnull;
138 }
139 }
140141 @Override
142publicvoid setEdgeValue(LongWritable targetVertexId,
143 NullWritable edgeValue) {
144// No operation.145// Only set value for an existing edge.146// If the edge exist, the Null value is already there.147 }
148149 @Override
150publicvoid trim() {
151 neighbors.trim();
152 }
153 }