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