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.hfile;
20
21 import java.io.IOException;
22 import java.nio.ByteBuffer;
23
24 import org.apache.hadoop.classification.InterfaceAudience;
25 import org.apache.hadoop.fs.FSDataInputStream;
26 import org.apache.hadoop.fs.Path;
27 import org.apache.hadoop.hbase.KeyValue;
28 import org.apache.hadoop.hbase.fs.HFileSystem;
29 import org.apache.hadoop.hbase.io.compress.Compression;
30 import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
31 import org.apache.hadoop.hbase.io.hfile.HFile.FileInfo;
32 import org.apache.hadoop.io.RawComparator;
33
34
35
36
37 @InterfaceAudience.Private
38 public abstract class AbstractHFileReader implements HFile.Reader {
39
40 protected FSDataInputStream istream;
41
42
43
44 protected FSDataInputStream istreamNoFsChecksum;
45
46
47 protected HFileBlockIndex.BlockIndexReader dataBlockIndexReader;
48
49
50 protected HFileBlockIndex.BlockIndexReader metaBlockIndexReader;
51
52 protected final FixedFileTrailer trailer;
53
54
55 protected final Compression.Algorithm compressAlgo;
56
57
58
59
60
61 protected HFileDataBlockEncoder dataBlockEncoder =
62 NoOpDataBlockEncoder.INSTANCE;
63
64
65 protected byte [] lastKey = null;
66
67
68 protected int avgKeyLen = -1;
69
70
71 protected int avgValueLen = -1;
72
73
74 protected RawComparator<byte []> comparator;
75
76
77 protected final long fileSize;
78
79
80 protected final CacheConfig cacheConf;
81
82
83 protected final Path path;
84
85
86 protected final String name;
87
88 protected FileInfo fileInfo;
89
90
91 protected HFileSystem hfs;
92
93 protected AbstractHFileReader(Path path, FixedFileTrailer trailer,
94 final long fileSize, final CacheConfig cacheConf, final HFileSystem hfs) {
95 this.trailer = trailer;
96 this.compressAlgo = trailer.getCompressionCodec();
97 this.cacheConf = cacheConf;
98 this.fileSize = fileSize;
99 this.path = path;
100 this.name = path.getName();
101 this.hfs = hfs;
102 }
103
104 @SuppressWarnings("serial")
105 public static class BlockIndexNotLoadedException
106 extends IllegalStateException {
107 public BlockIndexNotLoadedException() {
108
109 super("Block index not loaded");
110 }
111 }
112
113 protected String toStringFirstKey() {
114 return KeyValue.keyToString(getFirstKey());
115 }
116
117 protected String toStringLastKey() {
118 return KeyValue.keyToString(getLastKey());
119 }
120
121 public abstract boolean isFileInfoLoaded();
122
123 @Override
124 public String toString() {
125 return "reader=" + path.toString() +
126 (!isFileInfoLoaded()? "":
127 ", compression=" + compressAlgo.getName() +
128 ", cacheConf=" + cacheConf +
129 ", firstKey=" + toStringFirstKey() +
130 ", lastKey=" + toStringLastKey()) +
131 ", avgKeyLen=" + avgKeyLen +
132 ", avgValueLen=" + avgValueLen +
133 ", entries=" + trailer.getEntryCount() +
134 ", length=" + fileSize;
135 }
136
137 @Override
138 public long length() {
139 return fileSize;
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153
154 @Override
155 public HFileScanner getScanner(boolean cacheBlocks, final boolean pread) {
156 return getScanner(cacheBlocks, pread, false);
157 }
158
159
160
161
162
163
164 @Override
165 public byte [] getFirstKey() {
166 if (dataBlockIndexReader == null) {
167 throw new BlockIndexNotLoadedException();
168 }
169 return dataBlockIndexReader.isEmpty() ? null
170 : dataBlockIndexReader.getRootBlockKey(0);
171 }
172
173
174
175
176
177
178
179 @Override
180 public byte[] getFirstRowKey() {
181 byte[] firstKey = getFirstKey();
182 if (firstKey == null)
183 return null;
184 return KeyValue.createKeyValueFromKey(firstKey).getRow();
185 }
186
187
188
189
190
191
192
193 @Override
194 public byte[] getLastRowKey() {
195 byte[] lastKey = getLastKey();
196 if (lastKey == null)
197 return null;
198 return KeyValue.createKeyValueFromKey(lastKey).getRow();
199 }
200
201
202 @Override
203 public long getEntries() {
204 return trailer.getEntryCount();
205 }
206
207
208 @Override
209 public RawComparator<byte []> getComparator() {
210 return comparator;
211 }
212
213
214 @Override
215 public Compression.Algorithm getCompressionAlgorithm() {
216 return compressAlgo;
217 }
218
219
220
221
222
223 public long indexSize() {
224 return (dataBlockIndexReader != null ? dataBlockIndexReader.heapSize() : 0)
225 + ((metaBlockIndexReader != null) ? metaBlockIndexReader.heapSize()
226 : 0);
227 }
228
229 @Override
230 public String getName() {
231 return name;
232 }
233
234 @Override
235 public HFileBlockIndex.BlockIndexReader getDataBlockIndexReader() {
236 return dataBlockIndexReader;
237 }
238
239 @Override
240 public FixedFileTrailer getTrailer() {
241 return trailer;
242 }
243
244 @Override
245 public FileInfo loadFileInfo() throws IOException {
246 return fileInfo;
247 }
248
249
250
251
252
253 @SuppressWarnings("serial")
254 public static class NotSeekedException extends IllegalStateException {
255 public NotSeekedException() {
256 super("Not seeked to a key/value");
257 }
258 }
259
260 protected static abstract class Scanner implements HFileScanner {
261 protected ByteBuffer blockBuffer;
262
263 protected boolean cacheBlocks;
264 protected final boolean pread;
265 protected final boolean isCompaction;
266
267 protected int currKeyLen;
268 protected int currValueLen;
269 protected int currMemstoreTSLen;
270 protected long currMemstoreTS;
271
272 protected int blockFetches;
273
274 protected final HFile.Reader reader;
275
276 public Scanner(final HFile.Reader reader, final boolean cacheBlocks,
277 final boolean pread, final boolean isCompaction) {
278 this.reader = reader;
279 this.cacheBlocks = cacheBlocks;
280 this.pread = pread;
281 this.isCompaction = isCompaction;
282 }
283
284 @Override
285 public boolean isSeeked(){
286 return blockBuffer != null;
287 }
288
289 @Override
290 public String toString() {
291 return "HFileScanner for reader " + String.valueOf(getReader());
292 }
293
294 protected void assertSeeked() {
295 if (!isSeeked())
296 throw new NotSeekedException();
297 }
298
299 @Override
300 public int seekTo(byte[] key) throws IOException {
301 return seekTo(key, 0, key.length);
302 }
303
304 @Override
305 public boolean seekBefore(byte[] key) throws IOException {
306 return seekBefore(key, 0, key.length);
307 }
308
309 @Override
310 public int reseekTo(byte[] key) throws IOException {
311 return reseekTo(key, 0, key.length);
312 }
313
314 @Override
315 public HFile.Reader getReader() {
316 return reader;
317 }
318 }
319
320
321 abstract HFileBlock.FSReader getUncachedBlockReader();
322
323 public Path getPath() {
324 return path;
325 }
326
327 @Override
328 public DataBlockEncoding getEncodingOnDisk() {
329 return dataBlockEncoder.getEncodingOnDisk();
330 }
331 }