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.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 current.tagsLength = currentBuffer.getShort();
71 ByteBufferUtils.skip(currentBuffer, current.tagsLength);
72 }
73 if (includesMvcc()) {
74 current.memstoreTS = ByteBufferUtils.readVLong(currentBuffer);
75 } else {
76 current.memstoreTS = 0;
77 }
78 current.nextKvOffset = currentBuffer.position();
79 }
80
81 @Override
82 protected void decodeFirst() {
83 ByteBufferUtils.skip(currentBuffer, Bytes.SIZEOF_INT);
84 current.lastCommonPrefix = 0;
85 decodeNext();
86 }
87 };
88 }
89
90 @Override
91 protected ByteBuffer internalDecodeKeyValues(DataInputStream source, int allocateHeaderLength,
92 int skipLastBytes, HFileBlockDefaultDecodingContext decodingCtx) throws IOException {
93 int decompressedSize = source.readInt();
94 ByteBuffer buffer = ByteBuffer.allocate(decompressedSize +
95 allocateHeaderLength);
96 buffer.position(allocateHeaderLength);
97 ByteBufferUtils.copyFromStreamToBuffer(buffer, source, decompressedSize);
98
99 return buffer;
100 }
101
102 }