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