1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.hadoop.hbase.io.encoding;
18
19 import java.io.DataInputStream;
20 import java.io.DataOutputStream;
21 import java.io.IOException;
22 import java.nio.ByteBuffer;
23
24 import org.apache.hadoop.hbase.classification.InterfaceAudience;
25 import org.apache.hadoop.hbase.KeyValue.KVComparator;
26 import org.apache.hadoop.hbase.util.ByteBufferUtils;
27 import org.apache.hadoop.hbase.util.Bytes;
28
29
30
31
32
33 @InterfaceAudience.Private
34 public class CopyKeyDataBlockEncoder extends BufferedDataBlockEncoder {
35 @Override
36 public void internalEncodeKeyValues(DataOutputStream out,
37 ByteBuffer in, HFileBlockDefaultEncodingContext encodingCtx) throws IOException {
38 in.rewind();
39 ByteBufferUtils.putInt(out, in.limit());
40 ByteBufferUtils.moveBufferToStream(out, in, in.limit());
41 }
42
43
44 @Override
45 public ByteBuffer getFirstKeyInBlock(ByteBuffer block) {
46 int keyLength = block.getInt(Bytes.SIZEOF_INT);
47 return ByteBuffer.wrap(block.array(),
48 block.arrayOffset() + 3 * Bytes.SIZEOF_INT, keyLength).slice();
49 }
50
51
52 @Override
53 public String toString() {
54 return CopyKeyDataBlockEncoder.class.getSimpleName();
55 }
56
57 @Override
58 public EncodedSeeker createSeeker(KVComparator comparator,
59 final HFileBlockDecodingContext decodingCtx) {
60 return new BufferedEncodedSeeker<SeekerState>(comparator, decodingCtx) {
61 @Override
62 protected void decodeNext() {
63 current.keyLength = currentBuffer.getInt();
64 current.valueLength = currentBuffer.getInt();
65 current.ensureSpaceForKey();
66 currentBuffer.get(current.keyBuffer, 0, current.keyLength);
67 current.valueOffset = currentBuffer.position();
68 ByteBufferUtils.skip(currentBuffer, current.valueLength);
69 if (includesTags()) {
70
71 current.tagsLength = ((currentBuffer.get() & 0xff) << 8) ^ (currentBuffer.get() & 0xff);
72 ByteBufferUtils.skip(currentBuffer, current.tagsLength);
73 }
74 if (includesMvcc()) {
75 current.memstoreTS = ByteBufferUtils.readVLong(currentBuffer);
76 } else {
77 current.memstoreTS = 0;
78 }
79 current.nextKvOffset = currentBuffer.position();
80 }
81
82 @Override
83 protected void decodeFirst() {
84 ByteBufferUtils.skip(currentBuffer, Bytes.SIZEOF_INT);
85 current.lastCommonPrefix = 0;
86 decodeNext();
87 }
88 };
89 }
90
91 @Override
92 protected ByteBuffer internalDecodeKeyValues(DataInputStream source, int allocateHeaderLength,
93 int skipLastBytes, HFileBlockDefaultDecodingContext decodingCtx) throws IOException {
94 int decompressedSize = source.readInt();
95 ByteBuffer buffer = ByteBuffer.allocate(decompressedSize +
96 allocateHeaderLength);
97 buffer.position(allocateHeaderLength);
98 ByteBufferUtils.copyFromStreamToBuffer(buffer, source, decompressedSize);
99
100 return buffer;
101 }
102
103 }