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 org.apache.giraph.conf.ImmutableClassesGiraphConfiguration; |
22 | |
import org.apache.hadoop.io.Writable; |
23 | |
import org.apache.hadoop.io.WritableComparable; |
24 | |
|
25 | |
import java.io.DataInput; |
26 | |
import java.io.DataOutput; |
27 | |
import java.io.IOException; |
28 | |
import java.util.Iterator; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public class MutableEdgesWrapper<I extends WritableComparable, |
43 | |
E extends Writable> implements OutEdges<I, E> { |
44 | |
|
45 | |
private final OutEdges<I, E> newEdges; |
46 | |
|
47 | |
private final Iterator<Edge<I, E>> oldEdgesIterator; |
48 | |
|
49 | |
private MutableEdge<I, E> currentEdge; |
50 | |
|
51 | |
private int numEdges; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
private MutableEdgesWrapper(OutEdges<I, E> oldEdges, |
61 | 0 | OutEdges<I, E> newEdges) { |
62 | 0 | oldEdgesIterator = oldEdges.iterator(); |
63 | 0 | this.newEdges = newEdges; |
64 | 0 | numEdges = oldEdges.size(); |
65 | 0 | } |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public static <I extends WritableComparable, E extends Writable> |
77 | |
MutableEdgesWrapper<I, E> wrap( |
78 | |
OutEdges<I, E> edges, |
79 | |
ImmutableClassesGiraphConfiguration<I, ?, E> conf) { |
80 | 0 | MutableEdgesWrapper<I, E> wrapper = new MutableEdgesWrapper<I, E>( |
81 | 0 | edges, conf.createAndInitializeOutEdges(edges.size())); |
82 | 0 | return wrapper; |
83 | |
} |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public OutEdges<I, E> unwrap() { |
91 | 0 | if (currentEdge != null) { |
92 | 0 | newEdges.add(currentEdge); |
93 | 0 | currentEdge = null; |
94 | |
} |
95 | 0 | while (oldEdgesIterator.hasNext()) { |
96 | 0 | newEdges.add(oldEdgesIterator.next()); |
97 | |
} |
98 | 0 | return newEdges; |
99 | |
} |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public OutEdges<I, E> getNewEdges() { |
107 | 0 | return newEdges; |
108 | |
} |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public Iterator<Edge<I, E>> getOldEdgesIterator() { |
116 | 0 | return oldEdgesIterator; |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
public MutableEdge<I, E> getCurrentEdge() { |
125 | 0 | return currentEdge; |
126 | |
} |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
public void setCurrentEdge(MutableEdge<I, E> edge) { |
134 | 0 | currentEdge = edge; |
135 | 0 | } |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public void decrementEdges() { |
142 | 0 | --numEdges; |
143 | 0 | } |
144 | |
|
145 | |
@Override |
146 | |
public void initialize(Iterable<Edge<I, E>> edges) { |
147 | 0 | throw new IllegalStateException("initialize: MutableEdgesWrapper should " + |
148 | |
"never be initialized."); |
149 | |
} |
150 | |
|
151 | |
@Override |
152 | |
public void initialize(int capacity) { |
153 | 0 | throw new IllegalStateException("initialize: MutableEdgesWrapper should " + |
154 | |
"never be initialized."); |
155 | |
} |
156 | |
|
157 | |
@Override |
158 | |
public void initialize() { |
159 | 0 | throw new IllegalStateException("initialize: MutableEdgesWrapper should " + |
160 | |
"never be initialized."); |
161 | |
} |
162 | |
|
163 | |
@Override |
164 | |
public void add(Edge<I, E> edge) { |
165 | 0 | unwrap().add(edge); |
166 | 0 | } |
167 | |
|
168 | |
@Override |
169 | |
public void remove(I targetVertexId) { |
170 | 0 | unwrap().remove(targetVertexId); |
171 | 0 | } |
172 | |
|
173 | |
@Override |
174 | |
public int size() { |
175 | 0 | return numEdges; |
176 | |
} |
177 | |
|
178 | |
@Override |
179 | |
public Iterator<Edge<I, E>> iterator() { |
180 | 0 | return unwrap().iterator(); |
181 | |
} |
182 | |
|
183 | |
@Override |
184 | |
public void write(DataOutput out) throws IOException { |
185 | 0 | throw new IllegalStateException("write: MutableEdgesWrapper should " + |
186 | |
"never be serialized."); |
187 | |
} |
188 | |
|
189 | |
@Override |
190 | |
public void readFields(DataInput in) throws IOException { |
191 | 0 | throw new IllegalStateException("readFields: MutableEdgesWrapper should " + |
192 | |
"never be deserialized."); |
193 | |
} |
194 | |
} |