1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.graph; |
20 | |
|
21 | |
import org.apache.giraph.utils.UnmodifiableIntArrayIterator; |
22 | |
import org.apache.hadoop.io.IntWritable; |
23 | |
import org.apache.hadoop.io.NullWritable; |
24 | |
|
25 | |
import com.google.common.collect.Iterables; |
26 | |
|
27 | |
import java.io.DataInput; |
28 | |
import java.io.DataOutput; |
29 | |
import java.io.IOException; |
30 | |
import java.util.Iterator; |
31 | |
import java.util.Map; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 327 | public abstract class IntIntNullIntVertex extends |
39 | |
SimpleVertex<IntWritable, IntWritable, IntWritable> { |
40 | |
|
41 | |
private int[] neighbors; |
42 | |
|
43 | |
private int[] messages; |
44 | |
|
45 | |
@Override |
46 | |
public void initialize(IntWritable id, IntWritable value, |
47 | |
Map<IntWritable, NullWritable> edges, |
48 | |
Iterable<IntWritable> messages) { |
49 | 48 | super.initialize(id, value); |
50 | 48 | this.neighbors = new int[(edges != null) ? edges.size() : 0]; |
51 | 48 | int n = 0; |
52 | 48 | if (edges != null) { |
53 | 27 | for (Map.Entry<IntWritable, NullWritable> edge : edges.entrySet()) { |
54 | 66 | this.neighbors[n++] = edge.getKey().get(); |
55 | |
} |
56 | |
} |
57 | 48 | this.messages = new int[(messages != null) ? Iterables.size(messages) : 0]; |
58 | 48 | if (messages != null) { |
59 | 27 | n = 0; |
60 | 27 | for (IntWritable message : messages) { |
61 | 2 | this.messages[n++] = message.get(); |
62 | |
} |
63 | |
} |
64 | 48 | } |
65 | |
|
66 | |
@Override |
67 | |
public Iterable<IntWritable> getNeighbors() { |
68 | 62 | return new Iterable<IntWritable>() { |
69 | |
@Override |
70 | |
public Iterator<IntWritable> iterator() { |
71 | 62 | return new UnmodifiableIntArrayIterator(neighbors); |
72 | |
} |
73 | |
}; |
74 | |
} |
75 | |
|
76 | |
@Override |
77 | |
public boolean hasEdge(IntWritable targetVertexId) { |
78 | 0 | for (int neighbor : neighbors) { |
79 | 0 | if (neighbor == targetVertexId.get()) { |
80 | 0 | return true; |
81 | |
} |
82 | |
} |
83 | 0 | return false; |
84 | |
} |
85 | |
|
86 | |
@Override |
87 | |
public int getNumEdges() { |
88 | 209 | return neighbors.length; |
89 | |
} |
90 | |
|
91 | |
@Override |
92 | |
public Iterable<IntWritable> getMessages() { |
93 | 175 | return new Iterable<IntWritable>() { |
94 | |
@Override |
95 | |
public Iterator<IntWritable> iterator() { |
96 | 149 | return new UnmodifiableIntArrayIterator(messages); |
97 | |
} |
98 | |
}; |
99 | |
} |
100 | |
|
101 | |
@Override |
102 | |
public void putMessages(Iterable<IntWritable> newMessages) { |
103 | 44 | messages = new int[Iterables.size(newMessages)]; |
104 | 44 | int n = 0; |
105 | 44 | for (IntWritable message : newMessages) { |
106 | 44 | messages[n++] = message.get(); |
107 | |
} |
108 | 44 | } |
109 | |
|
110 | |
@Override |
111 | |
public int getNumMessages() { |
112 | 0 | return messages.length; |
113 | |
} |
114 | |
|
115 | |
@Override |
116 | |
void releaseResources() { |
117 | 70 | messages = new int[0]; |
118 | 70 | } |
119 | |
|
120 | |
@Override |
121 | |
public void write(final DataOutput out) throws IOException { |
122 | 28 | out.writeInt(getId().get()); |
123 | 28 | out.writeInt(getValue().get()); |
124 | 28 | out.writeInt(neighbors.length); |
125 | 30 | for (int n = 0; n < neighbors.length; n++) { |
126 | 2 | out.writeInt(neighbors[n]); |
127 | |
} |
128 | 28 | out.writeInt(messages.length); |
129 | 30 | for (int n = 0; n < messages.length; n++) { |
130 | 2 | out.writeInt(messages[n]); |
131 | |
} |
132 | 28 | out.writeBoolean(isHalted()); |
133 | 28 | } |
134 | |
|
135 | |
@Override |
136 | |
public void readFields(DataInput in) throws IOException { |
137 | 21 | int id = in.readInt(); |
138 | 21 | int value = in.readInt(); |
139 | 21 | super.initialize(new IntWritable(id), new IntWritable(value)); |
140 | 21 | int numEdges = in.readInt(); |
141 | 21 | neighbors = new int[numEdges]; |
142 | 23 | for (int n = 0; n < numEdges; n++) { |
143 | 2 | neighbors[n] = in.readInt(); |
144 | |
} |
145 | 21 | int numMessages = in.readInt(); |
146 | 21 | messages = new int[numMessages]; |
147 | 23 | for (int n = 0; n < numMessages; n++) { |
148 | 2 | messages[n] = in.readInt(); |
149 | |
} |
150 | 21 | boolean halt = in.readBoolean(); |
151 | 21 | if (halt) { |
152 | 1 | voteToHalt(); |
153 | |
} else { |
154 | 20 | wakeUp(); |
155 | |
} |
156 | 21 | } |
157 | |
} |