1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.regionserver.wal;
20
21 import java.io.DataInput;
22 import java.io.DataOutput;
23 import java.io.IOException;
24
25 import org.apache.hadoop.hbase.classification.InterfaceAudience;
26 import org.apache.hadoop.hbase.KeyValue;
27 import org.apache.hadoop.hbase.util.Bytes;
28 import org.apache.hadoop.io.WritableUtils;
29
30
31
32
33
34
35
36
37
38
39 @Deprecated
40 @InterfaceAudience.Private
41 class KeyValueCompression {
42
43
44
45
46
47
48
49
50
51 public static KeyValue readKV(DataInput in, CompressionContext readContext)
52 throws IOException {
53 int keylength = WritableUtils.readVInt(in);
54 int vlength = WritableUtils.readVInt(in);
55 int tagsLength = WritableUtils.readVInt(in);
56 int length = (int) KeyValue.getKeyValueDataStructureSize(keylength, vlength, tagsLength);
57
58 byte[] backingArray = new byte[length];
59 int pos = 0;
60 pos = Bytes.putInt(backingArray, pos, keylength);
61 pos = Bytes.putInt(backingArray, pos, vlength);
62
63
64 int elemLen = Compressor.uncompressIntoArray(backingArray,
65 pos + Bytes.SIZEOF_SHORT, in, readContext.rowDict);
66 checkLength(elemLen, Short.MAX_VALUE);
67 pos = Bytes.putShort(backingArray, pos, (short)elemLen);
68 pos += elemLen;
69
70
71 elemLen = Compressor.uncompressIntoArray(backingArray,
72 pos + Bytes.SIZEOF_BYTE, in, readContext.familyDict);
73 checkLength(elemLen, Byte.MAX_VALUE);
74 pos = Bytes.putByte(backingArray, pos, (byte)elemLen);
75 pos += elemLen;
76
77
78 elemLen = Compressor.uncompressIntoArray(backingArray, pos, in,
79 readContext.qualifierDict);
80 pos += elemLen;
81
82
83 in.readFully(backingArray, pos, length - pos);
84
85 return new KeyValue(backingArray, 0, length);
86 }
87
88 private static void checkLength(int len, int max) throws IOException {
89 if (len < 0 || len > max) {
90 throw new IOException(
91 "Invalid length for compresesed portion of keyvalue: " + len);
92 }
93 }
94
95
96
97
98
99
100
101
102
103 public static void writeKV(final DataOutput out, KeyValue keyVal,
104 CompressionContext writeContext) throws IOException {
105 byte[] backingArray = keyVal.getBuffer();
106 int offset = keyVal.getOffset();
107
108
109 WritableUtils.writeVInt(out, keyVal.getKeyLength());
110 WritableUtils.writeVInt(out, keyVal.getValueLength());
111 WritableUtils.writeVInt(out, keyVal.getTagsLength());
112
113
114
115 Compressor.writeCompressed(keyVal.getBuffer(), keyVal.getRowOffset(),
116 keyVal.getRowLength(), out, writeContext.rowDict);
117
118
119
120 Compressor.writeCompressed(keyVal.getBuffer(), keyVal.getFamilyOffset(),
121 keyVal.getFamilyLength(), out, writeContext.familyDict);
122
123
124 Compressor.writeCompressed(keyVal.getBuffer(), keyVal.getQualifierOffset(),
125 keyVal.getQualifierLength(), out,
126 writeContext.qualifierDict);
127
128
129 int pos = keyVal.getTimestampOffset();
130 int remainingLength = keyVal.getLength() + offset - (pos);
131 out.write(backingArray, pos, remainingLength);
132 }
133 }