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