1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.comm; |
20 | |
|
21 | |
import java.util.HashMap; |
22 | |
import java.util.Map; |
23 | |
|
24 | |
import org.apache.giraph.graph.Vertex; |
25 | |
import org.apache.giraph.graph.Edge; |
26 | |
import org.apache.giraph.graph.VertexMutations; |
27 | |
import org.apache.hadoop.io.Writable; |
28 | |
import org.apache.hadoop.io.WritableComparable; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
@SuppressWarnings("rawtypes") |
40 | 1 | public class SendMutationsCache<I extends WritableComparable, |
41 | |
V extends Writable, E extends Writable, M extends Writable> { |
42 | |
|
43 | 1 | private Map<Integer, Map<I, VertexMutations<I, V, E, M>>> mutationCache = |
44 | |
new HashMap<Integer, Map<I, VertexMutations<I, V, E, M>>>(); |
45 | |
|
46 | 1 | private final Map<Integer, Integer> mutationCountMap = |
47 | |
new HashMap<Integer, Integer>(); |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
private VertexMutations<I, V, E, M> getVertexMutations( |
58 | |
Integer partitionId, I destVertexId) { |
59 | 0 | Map<I, VertexMutations<I, V, E, M>> idMutations = |
60 | |
mutationCache.get(partitionId); |
61 | 0 | if (idMutations == null) { |
62 | 0 | idMutations = new HashMap<I, VertexMutations<I, V, E, M>>(); |
63 | 0 | mutationCache.put(partitionId, idMutations); |
64 | |
} |
65 | 0 | VertexMutations<I, V, E, M> mutations = idMutations.get(destVertexId); |
66 | 0 | if (mutations == null) { |
67 | 0 | mutations = new VertexMutations<I, V, E, M>(); |
68 | 0 | idMutations.put(destVertexId, mutations); |
69 | |
} |
70 | 0 | return mutations; |
71 | |
} |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
private int incrementPartitionMutationCount(int partitionId) { |
80 | 0 | Integer currentPartitionMutationCount = mutationCountMap.get(partitionId); |
81 | 0 | if (currentPartitionMutationCount == null) { |
82 | 0 | currentPartitionMutationCount = 0; |
83 | |
} |
84 | 0 | Integer updatedPartitionMutationCount = |
85 | |
currentPartitionMutationCount + 1; |
86 | 0 | mutationCountMap.put(partitionId, updatedPartitionMutationCount); |
87 | 0 | return updatedPartitionMutationCount; |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public int addEdgeMutation( |
99 | |
Integer partitionId, I destVertexId, Edge<I, E> edge) { |
100 | |
|
101 | 0 | VertexMutations<I, V, E, M> mutations = |
102 | |
getVertexMutations(partitionId, destVertexId); |
103 | |
|
104 | |
|
105 | 0 | mutations.addEdge(edge); |
106 | |
|
107 | |
|
108 | 0 | return incrementPartitionMutationCount(partitionId); |
109 | |
} |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
public int removeEdgeMutation( |
120 | |
Integer partitionId, I vertexIndex, I destinationVertexIndex) { |
121 | |
|
122 | 0 | VertexMutations<I, V, E, M> mutations = |
123 | |
getVertexMutations(partitionId, vertexIndex); |
124 | |
|
125 | |
|
126 | 0 | mutations.removeEdge(destinationVertexIndex); |
127 | |
|
128 | |
|
129 | 0 | return incrementPartitionMutationCount(partitionId); |
130 | |
} |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
public int addVertexMutation( |
140 | |
Integer partitionId, Vertex<I, V, E, M> vertex) { |
141 | |
|
142 | 0 | VertexMutations<I, V, E, M> mutations = |
143 | |
getVertexMutations(partitionId, vertex.getId()); |
144 | |
|
145 | |
|
146 | 0 | mutations.addVertex(vertex); |
147 | |
|
148 | |
|
149 | 0 | return incrementPartitionMutationCount(partitionId); |
150 | |
} |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
public int removeVertexMutation( |
160 | |
Integer partitionId, I destVertexId) { |
161 | |
|
162 | 0 | VertexMutations<I, V, E, M> mutations = |
163 | |
getVertexMutations(partitionId, destVertexId); |
164 | |
|
165 | |
|
166 | 0 | mutations.removeVertex(); |
167 | |
|
168 | |
|
169 | 0 | return incrementPartitionMutationCount(partitionId); |
170 | |
} |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
public Map<I, VertexMutations<I, V, E, M>> removePartitionMutations( |
179 | |
int partitionId) { |
180 | 0 | Map<I, VertexMutations<I, V, E, M>> idMutations = |
181 | |
mutationCache.remove(partitionId); |
182 | 0 | mutationCountMap.put(partitionId, 0); |
183 | 0 | return idMutations; |
184 | |
} |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
public Map<Integer, Map<I, VertexMutations<I, V, E, M>>> |
192 | |
removeAllPartitionMutations() { |
193 | 24 | Map<Integer, Map<I, VertexMutations<I, V, E, M>>> allMutations = |
194 | |
mutationCache; |
195 | 24 | mutationCache = |
196 | |
new HashMap<Integer, Map<I, VertexMutations<I, V, E, M>>>(); |
197 | 24 | mutationCountMap.clear(); |
198 | 24 | return allMutations; |
199 | |
} |
200 | |
} |