1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.io;
20
21 import java.io.BufferedInputStream;
22 import java.io.DataInput;
23 import java.io.DataInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Arrays;
27
28 import org.apache.hadoop.hbase.util.ByteStringer;
29 import org.apache.hadoop.hbase.classification.InterfaceAudience;
30 import org.apache.hadoop.fs.FSDataOutputStream;
31 import org.apache.hadoop.fs.FileSystem;
32 import org.apache.hadoop.fs.Path;
33 import org.apache.hadoop.hbase.KeyValueUtil;
34 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
35 import org.apache.hadoop.hbase.protobuf.generated.FSProtos;
36 import org.apache.hadoop.hbase.util.Bytes;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 @InterfaceAudience.Private
57 public class Reference {
58 private byte [] splitkey;
59 private Range region;
60
61
62
63
64
65 static enum Range {
66
67 top,
68
69 bottom
70 }
71
72
73
74
75
76 public static Reference createTopReference(final byte [] splitRow) {
77 return new Reference(splitRow, Range.top);
78 }
79
80
81
82
83
84 public static Reference createBottomReference(final byte [] splitRow) {
85 return new Reference(splitRow, Range.bottom);
86 }
87
88
89
90
91
92
93 Reference(final byte [] splitRow, final Range fr) {
94 this.splitkey = splitRow == null? null: KeyValueUtil.createFirstOnRow(splitRow).getKey();
95 this.region = fr;
96 }
97
98
99
100
101
102 @Deprecated
103
104
105 public Reference() {
106 this(null, Range.bottom);
107 }
108
109
110
111
112
113 public Range getFileRegion() {
114 return this.region;
115 }
116
117
118
119
120 public byte [] getSplitKey() {
121 return splitkey;
122 }
123
124
125
126
127 @Override
128 public String toString() {
129 return "" + this.region;
130 }
131
132 public static boolean isTopFileRegion(final Range r) {
133 return r.equals(Range.top);
134 }
135
136
137
138
139
140
141 @Deprecated
142 public void readFields(DataInput in) throws IOException {
143 boolean tmp = in.readBoolean();
144
145 this.region = tmp? Range.top: Range.bottom;
146 this.splitkey = Bytes.readByteArray(in);
147 }
148
149 public Path write(final FileSystem fs, final Path p)
150 throws IOException {
151 FSDataOutputStream out = fs.create(p, false);
152 try {
153 out.write(toByteArray());
154 } finally {
155 out.close();
156 }
157 return p;
158 }
159
160
161
162
163
164
165
166
167 public static Reference read(final FileSystem fs, final Path p)
168 throws IOException {
169 InputStream in = fs.open(p);
170 try {
171
172
173 in = in.markSupported()? in: new BufferedInputStream(in);
174 int pblen = ProtobufUtil.lengthOfPBMagic();
175 in.mark(pblen);
176 byte [] pbuf = new byte[pblen];
177 int read = in.read(pbuf);
178 if (read != pblen) throw new IOException("read=" + read + ", wanted=" + pblen);
179
180 if (ProtobufUtil.isPBMagicPrefix(pbuf)) return convert(FSProtos.Reference.parseFrom(in));
181
182
183 in.reset();
184 Reference r = new Reference();
185 DataInputStream dis = new DataInputStream(in);
186
187 in = dis;
188 r.readFields(dis);
189 return r;
190 } finally {
191 in.close();
192 }
193 }
194
195 public FSProtos.Reference convert() {
196 FSProtos.Reference.Builder builder = FSProtos.Reference.newBuilder();
197 builder.setRange(isTopFileRegion(getFileRegion())?
198 FSProtos.Reference.Range.TOP: FSProtos.Reference.Range.BOTTOM);
199 builder.setSplitkey(ByteStringer.wrap(getSplitKey()));
200 return builder.build();
201 }
202
203 public static Reference convert(final FSProtos.Reference r) {
204 Reference result = new Reference();
205 result.splitkey = r.getSplitkey().toByteArray();
206 result.region = r.getRange() == FSProtos.Reference.Range.TOP? Range.top: Range.bottom;
207 return result;
208 }
209
210
211
212
213
214
215
216 byte [] toByteArray() throws IOException {
217 return ProtobufUtil.prependPBMagic(convert().toByteArray());
218 }
219
220 @Override
221 public int hashCode() {
222 return Arrays.hashCode(splitkey) + region.hashCode();
223 }
224
225 public boolean equals(Object o) {
226 if (this == o) return true;
227 if (o == null) return false;
228 if (!(o instanceof Reference)) return false;
229
230 Reference r = (Reference) o;
231 if (splitkey != null && r.splitkey == null) return false;
232 if (splitkey == null && r.splitkey != null) return false;
233 if (splitkey != null && !Arrays.equals(splitkey, r.splitkey)) return false;
234
235 return region.equals(r.region);
236 }
237 }