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, boolean includesMemstoreTS) throws IOException {
38 in.rewind();
39 ByteBufferUtils.putInt(out, in.limit());
40 ByteBufferUtils.moveBufferToStream(out, in, in.limit());
41 }
42
43 @Override
44 public ByteBuffer decodeKeyValues(DataInputStream source,
45 int preserveHeaderLength, int skipLastBytes, boolean includesMemstoreTS)
46 throws IOException {
47 int decompressedSize = source.readInt();
48 ByteBuffer buffer = ByteBuffer.allocate(decompressedSize +
49 preserveHeaderLength);
50 buffer.position(preserveHeaderLength);
51 ByteBufferUtils.copyFromStreamToBuffer(buffer, source, decompressedSize);
52
53 return buffer;
54 }
55
56 @Override
57 public ByteBuffer getFirstKeyInBlock(ByteBuffer block) {
58 int keyLength = block.getInt(Bytes.SIZEOF_INT);
59 return ByteBuffer.wrap(block.array(),
60 block.arrayOffset() + 3 * Bytes.SIZEOF_INT, keyLength).slice();
61 }
62
63
64 @Override
65 public String toString() {
66 return CopyKeyDataBlockEncoder.class.getSimpleName();
67 }
68
69 @Override
70 public EncodedSeeker createSeeker(KVComparator comparator,
71 final boolean includesMemstoreTS) {
72 return new BufferedEncodedSeeker<SeekerState>(comparator) {
73 @Override
74 protected void decodeNext() {
75 current.keyLength = currentBuffer.getInt();
76 current.valueLength = currentBuffer.getInt();
77 current.ensureSpaceForKey();
78 currentBuffer.get(current.keyBuffer, 0, current.keyLength);
79 current.valueOffset = currentBuffer.position();
80 ByteBufferUtils.skip(currentBuffer, current.valueLength);
81 if (includesMemstoreTS) {
82 current.memstoreTS = ByteBufferUtils.readVLong(currentBuffer);
83 } else {
84 current.memstoreTS = 0;
85 }
86 current.nextKvOffset = currentBuffer.position();
87 }
88
89 @Override
90 protected void decodeFirst() {
91 ByteBufferUtils.skip(currentBuffer, Bytes.SIZEOF_INT);
92 current.lastCommonPrefix = 0;
93 decodeNext();
94 }
95 };
96 }
97
98 }