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