19
19
import java .io .DataInput ;
20
20
import java .io .DataOutput ;
21
21
import java .io .IOException ;
22
+ import java .util .Map .Entry ;
22
23
24
+ import org .apache .hadoop .io .MapWritable ;
23
25
import org .apache .hadoop .io .Text ;
24
26
import org .apache .hadoop .io .Writable ;
25
27
@@ -28,6 +30,7 @@ public class Inlink implements Writable {
28
30
29
31
private String fromUrl ;
30
32
private String anchor ;
33
+ private MapWritable md = null ;
31
34
32
35
public Inlink () {
33
36
}
@@ -41,6 +44,13 @@ public Inlink(String fromUrl, String anchor) {
41
44
public void readFields (DataInput in ) throws IOException {
42
45
fromUrl = Text .readString (in );
43
46
anchor = Text .readString (in );
47
+ boolean hasMD = in .readBoolean ();
48
+ if (hasMD ) {
49
+ md = new org .apache .hadoop .io .MapWritable ();
50
+ md .readFields (in );
51
+ } else {
52
+ md = null ;
53
+ }
44
54
}
45
55
46
56
/**
@@ -51,12 +61,23 @@ public void readFields(DataInput in) throws IOException {
51
61
public static void skip (DataInput in ) throws IOException {
52
62
Text .skip (in ); // skip fromUrl
53
63
Text .skip (in ); // skip anchor
64
+ boolean hasMD = in .readBoolean ();
65
+ if (hasMD ) {
66
+ MapWritable metadata = new org .apache .hadoop .io .MapWritable ();
67
+ metadata .readFields (in );
68
+ }
54
69
}
55
70
56
71
@ Override
57
72
public void write (DataOutput out ) throws IOException {
58
73
Text .writeString (out , fromUrl );
59
74
Text .writeString (out , anchor );
75
+ if (md != null && md .size () > 0 ) {
76
+ out .writeBoolean (true );
77
+ md .write (out );
78
+ } else {
79
+ out .writeBoolean (false );
80
+ }
60
81
}
61
82
62
83
public static Inlink read (DataInput in ) throws IOException {
@@ -73,6 +94,14 @@ public String getAnchor() {
73
94
return anchor ;
74
95
}
75
96
97
+ public MapWritable getMetadata () {
98
+ return md ;
99
+ }
100
+
101
+ public void setMetadata (MapWritable md ) {
102
+ this .md = md ;
103
+ }
104
+
76
105
@ Override
77
106
public boolean equals (Object o ) {
78
107
if (!(o instanceof Inlink ))
@@ -89,7 +118,16 @@ public int hashCode() {
89
118
90
119
@ Override
91
120
public String toString () {
92
- return "fromUrl: " + fromUrl + " anchor: " + anchor ;
121
+ StringBuilder buffer = new StringBuilder ();
122
+ if (md != null && !md .isEmpty ()) {
123
+ for (Entry <Writable , Writable > e : md .entrySet ()) {
124
+ buffer .append (" " );
125
+ buffer .append (e .getKey ());
126
+ buffer .append (": " );
127
+ buffer .append (e .getValue ());
128
+ }
129
+ }
130
+
131
+ return "fromUrl: " + fromUrl + " anchor: " + anchor + " metadata: " + buffer .toString ();
93
132
}
94
-
95
133
}
0 commit comments