1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.regionserver.wal;
21
22 import java.io.DataInput;
23 import java.io.DataOutput;
24 import java.io.EOFException;
25 import java.io.IOException;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.UUID;
29
30 import org.apache.hadoop.hbase.HConstants;
31 import org.apache.hadoop.hbase.util.Bytes;
32 import org.apache.hadoop.io.WritableComparable;
33 import org.apache.hadoop.io.WritableUtils;
34
35
36
37
38
39
40
41
42
43
44
45 public class HLogKey implements WritableComparable<HLogKey> {
46
47 private static final int VERSION = -1;
48
49
50 private byte [] encodedRegionName;
51 private byte [] tablename;
52 private long logSeqNum;
53
54 private long writeTime;
55
56 private UUID clusterId;
57
58
59 public HLogKey() {
60 this(null, null, 0L, HConstants.LATEST_TIMESTAMP,
61 HConstants.DEFAULT_CLUSTER_ID);
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public HLogKey(final byte [] encodedRegionName, final byte [] tablename,
77 long logSeqNum, final long now, UUID clusterId) {
78 this.encodedRegionName = encodedRegionName;
79 this.tablename = tablename;
80 this.logSeqNum = logSeqNum;
81 this.writeTime = now;
82 this.clusterId = clusterId;
83 }
84
85
86 public byte [] getEncodedRegionName() {
87 return encodedRegionName;
88 }
89
90
91 public byte [] getTablename() {
92 return tablename;
93 }
94
95
96 public long getLogSeqNum() {
97 return logSeqNum;
98 }
99
100 void setLogSeqNum(long logSeqNum) {
101 this.logSeqNum = logSeqNum;
102 }
103
104
105
106
107 public long getWriteTime() {
108 return this.writeTime;
109 }
110
111
112
113
114
115 public UUID getClusterId() {
116 return clusterId;
117 }
118
119
120
121
122
123 public void setClusterId(UUID clusterId) {
124 this.clusterId = clusterId;
125 }
126
127 @Override
128 public String toString() {
129 return Bytes.toString(tablename) + "/" + Bytes.toString(encodedRegionName) + "/" +
130 logSeqNum;
131 }
132
133
134
135
136
137
138
139
140 public Map<String, Object> toStringMap() {
141 Map<String, Object> stringMap = new HashMap<String, Object>();
142 stringMap.put("table", Bytes.toStringBinary(tablename));
143 stringMap.put("region", Bytes.toStringBinary(encodedRegionName));
144 stringMap.put("sequence", logSeqNum);
145 return stringMap;
146 }
147
148 @Override
149 public boolean equals(Object obj) {
150 if (this == obj) {
151 return true;
152 }
153 if (obj == null || getClass() != obj.getClass()) {
154 return false;
155 }
156 return compareTo((HLogKey)obj) == 0;
157 }
158
159 @Override
160 public int hashCode() {
161 int result = Bytes.hashCode(this.encodedRegionName);
162 result ^= this.logSeqNum;
163 result ^= this.writeTime;
164 result ^= this.clusterId.hashCode();
165 return result;
166 }
167
168 public int compareTo(HLogKey o) {
169 int result = Bytes.compareTo(this.encodedRegionName, o.encodedRegionName);
170 if (result == 0) {
171 if (this.logSeqNum < o.logSeqNum) {
172 result = -1;
173 } else if (this.logSeqNum > o.logSeqNum) {
174 result = 1;
175 }
176 if (result == 0) {
177 if (this.writeTime < o.writeTime) {
178 result = -1;
179 } else if (this.writeTime > o.writeTime) {
180 return 1;
181 }
182 }
183 }
184
185 return result;
186 }
187
188
189
190
191
192
193
194 void internTableName(byte []tablename) {
195
196
197 assert Bytes.equals(tablename, this.tablename);
198 this.tablename = tablename;
199 }
200
201
202
203
204
205
206
207 void internEncodedRegionName(byte []encodedRegionName) {
208
209
210 assert Bytes.equals(this.encodedRegionName, encodedRegionName);
211 this.encodedRegionName = encodedRegionName;
212 }
213
214 @Override
215 public void write(DataOutput out) throws IOException {
216 WritableUtils.writeVInt(out, VERSION);
217 Bytes.writeByteArray(out, this.encodedRegionName);
218 Bytes.writeByteArray(out, this.tablename);
219 out.writeLong(this.logSeqNum);
220 out.writeLong(this.writeTime);
221
222 if (this.clusterId == HConstants.DEFAULT_CLUSTER_ID) {
223 out.writeBoolean(false);
224 } else {
225 out.writeBoolean(true);
226 out.writeLong(this.clusterId.getMostSignificantBits());
227 out.writeLong(this.clusterId.getLeastSignificantBits());
228 }
229 }
230
231 @Override
232 public void readFields(DataInput in) throws IOException {
233 int version = 0;
234
235
236
237
238
239
240
241
242 int len = WritableUtils.readVInt(in);
243 if (len < 0) {
244
245 version = len;
246 len = WritableUtils.readVInt(in);
247 }
248 this.encodedRegionName = new byte[len];
249 in.readFully(this.encodedRegionName);
250 this.tablename = Bytes.readByteArray(in);
251 this.logSeqNum = in.readLong();
252 this.writeTime = in.readLong();
253 this.clusterId = HConstants.DEFAULT_CLUSTER_ID;
254 if (version < 0) {
255 if (in.readBoolean()) {
256 this.clusterId = new UUID(in.readLong(), in.readLong());
257 }
258 } else {
259 try {
260
261 in.readByte();
262 } catch(EOFException e) {
263
264 }
265 }
266 }
267 }