1
|
package eu.dnetlib.data.mapreduce.hbase.dedup.cc;
|
2
|
|
3
|
import java.io.DataInput;
|
4
|
import java.io.DataOutput;
|
5
|
import java.io.IOException;
|
6
|
import java.lang.reflect.Type;
|
7
|
import java.util.TreeSet;
|
8
|
|
9
|
import com.google.gson.*;
|
10
|
import org.apache.hadoop.io.Text;
|
11
|
import org.apache.hadoop.io.Writable;
|
12
|
|
13
|
/**
|
14
|
* Created by claudio on 14/10/15.
|
15
|
*/
|
16
|
public final class VertexWritable implements Writable, Cloneable {
|
17
|
|
18
|
private Text vertexId;
|
19
|
private TreeSet<Text> edges;
|
20
|
private boolean activated;
|
21
|
|
22
|
public VertexWritable() {
|
23
|
super();
|
24
|
}
|
25
|
|
26
|
public VertexWritable(Text minimalVertexId) {
|
27
|
super();
|
28
|
this.vertexId = minimalVertexId;
|
29
|
}
|
30
|
|
31
|
// true if updated
|
32
|
public boolean checkAndSetMinimalVertex(Text id) {
|
33
|
if (vertexId == null) {
|
34
|
vertexId = id;
|
35
|
return true;
|
36
|
} else {
|
37
|
|
38
|
if (id.compareTo(vertexId) < 0) {
|
39
|
vertexId = id;
|
40
|
return true;
|
41
|
}
|
42
|
}
|
43
|
return false;
|
44
|
}
|
45
|
|
46
|
public boolean isMessage() {
|
47
|
return (edges == null);
|
48
|
}
|
49
|
|
50
|
public VertexWritable makeMessage() {
|
51
|
return new VertexWritable(vertexId);
|
52
|
}
|
53
|
|
54
|
public void addVertex(Text id) {
|
55
|
if (edges == null) {
|
56
|
edges = new TreeSet<Text>();
|
57
|
}
|
58
|
edges.add(id);
|
59
|
}
|
60
|
|
61
|
@Override
|
62
|
public void write(DataOutput out) throws IOException {
|
63
|
vertexId.write(out);
|
64
|
if (edges == null) {
|
65
|
out.writeInt(-1);
|
66
|
} else {
|
67
|
out.writeInt(edges.size());
|
68
|
for (Text l : edges) {
|
69
|
l.write(out);
|
70
|
}
|
71
|
}
|
72
|
out.writeBoolean(activated);
|
73
|
}
|
74
|
|
75
|
@Override
|
76
|
public void readFields(DataInput in) throws IOException {
|
77
|
vertexId = new Text();
|
78
|
vertexId.readFields(in);
|
79
|
int length = in.readInt();
|
80
|
if (length > -1) {
|
81
|
edges = new TreeSet<Text>();
|
82
|
for (int i = 0; i < length; i++) {
|
83
|
Text temp = new Text();
|
84
|
temp.readFields(in);
|
85
|
edges.add(temp);
|
86
|
}
|
87
|
} else {
|
88
|
edges = null;
|
89
|
}
|
90
|
activated = in.readBoolean();
|
91
|
}
|
92
|
|
93
|
@Override
|
94
|
public String toString() {
|
95
|
StringBuilder sb = new StringBuilder();
|
96
|
sb.append("VertexWritable [minimalVertexId=").
|
97
|
append(vertexId).
|
98
|
append(", pointsTo=").
|
99
|
append(edges).
|
100
|
append("]");
|
101
|
return sb.toString();
|
102
|
}
|
103
|
|
104
|
public static VertexWritable fromJSON(String json) {
|
105
|
final GsonBuilder builder = new GsonBuilder();
|
106
|
builder.registerTypeAdapter(VertexWritable.class, new JsonDeserializer<VertexWritable>() {
|
107
|
|
108
|
@Override
|
109
|
public VertexWritable deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
|
110
|
|
111
|
JsonObject jo = json.getAsJsonObject();
|
112
|
|
113
|
VertexWritable vertexWritable = new VertexWritable();
|
114
|
vertexWritable.setVertexId(new Text(jo.get("vertexId").getAsString()));
|
115
|
vertexWritable.setActivated(jo.get("activated").getAsBoolean());
|
116
|
|
117
|
TreeSet<Text> edges = new TreeSet<Text>();
|
118
|
|
119
|
for(JsonElement e : jo.get("edges").getAsJsonArray()) {
|
120
|
edges.add(new Text(e.getAsString()));
|
121
|
}
|
122
|
vertexWritable.setEdges(edges);
|
123
|
return vertexWritable;
|
124
|
}
|
125
|
});
|
126
|
return builder.create().fromJson(json, VertexWritable.class);
|
127
|
|
128
|
}
|
129
|
|
130
|
public String toJSON() {
|
131
|
|
132
|
final GsonBuilder builder = new GsonBuilder();
|
133
|
builder.registerTypeAdapter(VertexWritable.class, new JsonSerializer<VertexWritable>() {
|
134
|
@Override
|
135
|
public JsonElement serialize(final VertexWritable src, final Type typeOfSrc, final JsonSerializationContext context) {
|
136
|
JsonObject res = new JsonObject();
|
137
|
res.addProperty("vertexId", getVertexId().toString());
|
138
|
res.addProperty("activated", isActivated());
|
139
|
|
140
|
JsonArray edges = new JsonArray();
|
141
|
if (!isMessage()) {
|
142
|
for (Text edge : getEdges()) {
|
143
|
edges.add(new JsonPrimitive(edge.toString()));
|
144
|
}
|
145
|
}
|
146
|
res.add("edges", edges);
|
147
|
return res;
|
148
|
}
|
149
|
});
|
150
|
|
151
|
return builder.create().toJson(this);
|
152
|
}
|
153
|
|
154
|
@Override
|
155
|
protected VertexWritable clone() {
|
156
|
VertexWritable toReturn = new VertexWritable(new Text(vertexId.toString()));
|
157
|
if (edges != null) {
|
158
|
toReturn.edges = new TreeSet<Text>();
|
159
|
for (Text l : edges) {
|
160
|
toReturn.edges.add(new Text(l.toString()));
|
161
|
}
|
162
|
}
|
163
|
return toReturn;
|
164
|
}
|
165
|
|
166
|
public boolean isActivated() {
|
167
|
return activated;
|
168
|
}
|
169
|
|
170
|
public void setActivated(boolean activated) {
|
171
|
this.activated = activated;
|
172
|
}
|
173
|
|
174
|
public Text getVertexId() {
|
175
|
return vertexId;
|
176
|
}
|
177
|
|
178
|
public void setVertexId(Text vertexId) {
|
179
|
this.vertexId = vertexId;
|
180
|
}
|
181
|
|
182
|
public TreeSet<Text> getEdges() {
|
183
|
return edges;
|
184
|
}
|
185
|
|
186
|
public void setEdges(TreeSet<Text> edges) {
|
187
|
this.edges = edges;
|
188
|
}
|
189
|
|
190
|
}
|