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 tagsLength = WritableUtils.readVInt(in);
54 int length = (int) KeyValue.getKeyValueDataStructureSize(keylength, vlength, tagsLength);
55
56 byte[] backingArray = new byte[length];
57 int pos = 0;
58 pos = Bytes.putInt(backingArray, pos, keylength);
59 pos = Bytes.putInt(backingArray, pos, vlength);
60
61
62 int elemLen = Compressor.uncompressIntoArray(backingArray,
63 pos + Bytes.SIZEOF_SHORT, in, readContext.rowDict);
64 checkLength(elemLen, Short.MAX_VALUE);
65 pos = Bytes.putShort(backingArray, pos, (short)elemLen);
66 pos += elemLen;
67
68
69 elemLen = Compressor.uncompressIntoArray(backingArray,
70 pos + Bytes.SIZEOF_BYTE, in, readContext.familyDict);
71 checkLength(elemLen, Byte.MAX_VALUE);
72 pos = Bytes.putByte(backingArray, pos, (byte)elemLen);
73 pos += elemLen;
74
75
76 elemLen = Compressor.uncompressIntoArray(backingArray, pos, in,
77 readContext.qualifierDict);
78 pos += elemLen;
79
80
81 in.readFully(backingArray, pos, length - pos);
82
83 return new KeyValue(backingArray, 0, length);
84 }
85
86 private static void checkLength(int len, int max) throws IOException {
87 if (len < 0 || len > max) {
88 throw new IOException(
89 "Invalid length for compresesed portion of keyvalue: " + len);
90 }
91 }
92
93
94
95
96
97
98
99
100
101 public static void writeKV(final DataOutput out, KeyValue keyVal,
102 CompressionContext writeContext) throws IOException {
103 byte[] backingArray = keyVal.getBuffer();
104 int offset = keyVal.getOffset();
105
106
107 WritableUtils.writeVInt(out, keyVal.getKeyLength());
108 WritableUtils.writeVInt(out, keyVal.getValueLength());
109 WritableUtils.writeVInt(out, keyVal.getTagsLengthUnsigned());
110
111
112
113 Compressor.writeCompressed(keyVal.getBuffer(), keyVal.getRowOffset(),
114 keyVal.getRowLength(), out, writeContext.rowDict);
115
116
117
118 Compressor.writeCompressed(keyVal.getBuffer(), keyVal.getFamilyOffset(),
119 keyVal.getFamilyLength(), out, writeContext.familyDict);
120
121
122 Compressor.writeCompressed(keyVal.getBuffer(), keyVal.getQualifierOffset(),
123 keyVal.getQualifierLength(), out,
124 writeContext.qualifierDict);
125
126
127 int pos = keyVal.getTimestampOffset();
128 int remainingLength = keyVal.getLength() + offset - (pos);
129 out.write(backingArray, pos, remainingLength);
130 }
131 }