1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.edge; |
20 | |
|
21 | |
import it.unimi.dsi.fastutil.longs.Long2ByteMap; |
22 | |
import it.unimi.dsi.fastutil.longs.Long2ByteOpenHashMap; |
23 | |
import it.unimi.dsi.fastutil.objects.ObjectIterator; |
24 | |
|
25 | |
import java.io.DataInput; |
26 | |
import java.io.DataOutput; |
27 | |
import java.io.IOException; |
28 | |
import java.util.Iterator; |
29 | |
|
30 | |
import org.apache.giraph.utils.EdgeIterables; |
31 | |
import org.apache.giraph.utils.Trimmable; |
32 | |
import org.apache.hadoop.io.ByteWritable; |
33 | |
import org.apache.hadoop.io.LongWritable; |
34 | |
|
35 | |
import com.google.common.collect.UnmodifiableIterator; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | public class LongByteHashMapEdges |
45 | |
implements StrictRandomAccessOutEdges<LongWritable, ByteWritable>, |
46 | |
ReuseObjectsOutEdges<LongWritable, ByteWritable>, |
47 | |
MutableOutEdges<LongWritable, ByteWritable>, Trimmable { |
48 | |
|
49 | |
private Long2ByteOpenHashMap edgeMap; |
50 | |
|
51 | |
private ByteWritable representativeEdgeValue; |
52 | |
|
53 | |
@Override |
54 | |
public void initialize(Iterable<Edge<LongWritable, ByteWritable>> edges) { |
55 | 0 | EdgeIterables.initialize(this, edges); |
56 | 0 | } |
57 | |
|
58 | |
@Override |
59 | |
public void initialize(int capacity) { |
60 | 0 | edgeMap = new Long2ByteOpenHashMap(capacity); |
61 | 0 | } |
62 | |
|
63 | |
@Override |
64 | |
public void initialize() { |
65 | 0 | edgeMap = new Long2ByteOpenHashMap(); |
66 | 0 | } |
67 | |
|
68 | |
@Override |
69 | |
public void add(Edge<LongWritable, ByteWritable> edge) { |
70 | 0 | edgeMap.put(edge.getTargetVertexId().get(), edge.getValue().get()); |
71 | 0 | } |
72 | |
|
73 | |
@Override |
74 | |
public void remove(LongWritable targetVertexId) { |
75 | 0 | edgeMap.remove(targetVertexId.get()); |
76 | 0 | } |
77 | |
|
78 | |
@Override |
79 | |
public ByteWritable getEdgeValue(LongWritable targetVertexId) { |
80 | 0 | if (!edgeMap.containsKey(targetVertexId.get())) { |
81 | 0 | return null; |
82 | |
} |
83 | 0 | if (representativeEdgeValue == null) { |
84 | 0 | representativeEdgeValue = new ByteWritable(); |
85 | |
} |
86 | 0 | representativeEdgeValue.set(edgeMap.get(targetVertexId.get())); |
87 | 0 | return representativeEdgeValue; |
88 | |
} |
89 | |
|
90 | |
@Override |
91 | |
public void setEdgeValue(LongWritable targetVertexId, |
92 | |
ByteWritable edgeValue) { |
93 | 0 | if (edgeMap.containsKey(targetVertexId.get())) { |
94 | 0 | edgeMap.put(targetVertexId.get(), edgeValue.get()); |
95 | |
} |
96 | 0 | } |
97 | |
|
98 | |
@Override |
99 | |
public int size() { |
100 | 0 | return edgeMap.size(); |
101 | |
} |
102 | |
|
103 | |
@Override |
104 | |
public Iterator<Edge<LongWritable, ByteWritable>> iterator() { |
105 | |
|
106 | 0 | return new UnmodifiableIterator<Edge<LongWritable, ByteWritable>>() { |
107 | |
|
108 | 0 | private final ObjectIterator<Long2ByteMap.Entry> mapIterator = |
109 | 0 | edgeMap.long2ByteEntrySet().fastIterator(); |
110 | |
|
111 | 0 | private final ReusableEdge<LongWritable, ByteWritable> |
112 | |
representativeEdge = |
113 | 0 | EdgeFactory.createReusable(new LongWritable(), new ByteWritable()); |
114 | |
|
115 | |
@Override |
116 | |
public boolean hasNext() { |
117 | 0 | return mapIterator.hasNext(); |
118 | |
} |
119 | |
|
120 | |
@Override |
121 | |
public Edge<LongWritable, ByteWritable> next() { |
122 | 0 | Long2ByteMap.Entry nextEntry = mapIterator.next(); |
123 | 0 | representativeEdge.getTargetVertexId().set(nextEntry.getLongKey()); |
124 | 0 | representativeEdge.getValue().set(nextEntry.getByteValue()); |
125 | 0 | return representativeEdge; |
126 | |
} |
127 | |
}; |
128 | |
} |
129 | |
|
130 | |
@Override |
131 | |
public void trim() { |
132 | 0 | edgeMap.trim(); |
133 | 0 | } |
134 | |
|
135 | |
|
136 | 0 | private static class LongByteHashMapMutableEdge |
137 | |
extends DefaultEdge<LongWritable, ByteWritable> { |
138 | |
|
139 | |
private Long2ByteMap.Entry entry; |
140 | |
|
141 | |
|
142 | |
public LongByteHashMapMutableEdge() { |
143 | 0 | super(new LongWritable(), new ByteWritable()); |
144 | 0 | } |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
public void setEntry(Long2ByteMap.Entry entry) { |
152 | |
|
153 | 0 | getTargetVertexId().set(entry.getLongKey()); |
154 | 0 | getValue().set(entry.getByteValue()); |
155 | |
|
156 | 0 | this.entry = entry; |
157 | 0 | } |
158 | |
|
159 | |
@Override |
160 | |
public void setValue(ByteWritable value) { |
161 | |
|
162 | 0 | getValue().set(value.get()); |
163 | |
|
164 | 0 | entry.setValue(value.get()); |
165 | 0 | } |
166 | |
} |
167 | |
|
168 | |
@Override |
169 | |
public Iterator<MutableEdge<LongWritable, ByteWritable>> mutableIterator() { |
170 | 0 | return new Iterator<MutableEdge<LongWritable, ByteWritable>>() { |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | 0 | private final ObjectIterator<Long2ByteMap.Entry> mapIterator = |
177 | 0 | edgeMap.long2ByteEntrySet().iterator(); |
178 | |
|
179 | 0 | private final LongByteHashMapMutableEdge representativeEdge = |
180 | |
new LongByteHashMapMutableEdge(); |
181 | |
|
182 | |
@Override |
183 | |
public boolean hasNext() { |
184 | 0 | return mapIterator.hasNext(); |
185 | |
} |
186 | |
|
187 | |
@Override |
188 | |
public MutableEdge<LongWritable, ByteWritable> next() { |
189 | 0 | representativeEdge.setEntry(mapIterator.next()); |
190 | 0 | return representativeEdge; |
191 | |
} |
192 | |
|
193 | |
@Override |
194 | |
public void remove() { |
195 | 0 | mapIterator.remove(); |
196 | 0 | } |
197 | |
}; |
198 | |
} |
199 | |
|
200 | |
@Override |
201 | |
public void write(DataOutput out) throws IOException { |
202 | 0 | out.writeInt(edgeMap.size()); |
203 | 0 | for (Long2ByteMap.Entry entry : edgeMap.long2ByteEntrySet()) { |
204 | 0 | out.writeLong(entry.getLongKey()); |
205 | 0 | out.writeByte(entry.getByteValue()); |
206 | 0 | } |
207 | 0 | } |
208 | |
|
209 | |
@Override |
210 | |
public void readFields(DataInput in) throws IOException { |
211 | 0 | int numEdges = in.readInt(); |
212 | 0 | initialize(numEdges); |
213 | 0 | for (int i = 0; i < numEdges; ++i) { |
214 | 0 | edgeMap.put(in.readLong(), in.readByte()); |
215 | |
} |
216 | 0 | } |
217 | |
} |