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 static org.apache.hadoop.hbase.io.compress.Compression.Algorithm.NONE;
20
21 import java.io.ByteArrayOutputStream;
22 import java.io.DataOutputStream;
23 import java.io.IOException;
24
25 import org.apache.hadoop.hbase.io.compress.Compression;
26 import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
27 import org.apache.hadoop.hbase.io.hfile.BlockType;
28 import org.apache.hadoop.io.compress.CompressionOutputStream;
29 import org.apache.hadoop.io.compress.Compressor;
30
31 import com.google.common.base.Preconditions;
32
33
34
35
36
37
38
39
40 public class HFileBlockDefaultEncodingContext implements
41 HFileBlockEncodingContext {
42
43 private byte[] onDiskBytesWithHeader;
44 private byte[] uncompressedBytesWithHeader;
45 private BlockType blockType;
46 private final DataBlockEncoding encodingAlgo;
47
48
49 private Compressor compressor;
50
51
52 private CompressionOutputStream compressionStream;
53
54
55 private ByteArrayOutputStream compressedByteStream;
56
57
58 private final Compression.Algorithm compressionAlgorithm;
59
60 private ByteArrayOutputStream encodedStream = new ByteArrayOutputStream();
61 private DataOutputStream dataOut = new DataOutputStream(encodedStream);
62
63 private byte[] dummyHeader;
64
65
66
67
68
69
70 public HFileBlockDefaultEncodingContext(
71 Compression.Algorithm compressionAlgorithm,
72 DataBlockEncoding encoding, byte[] headerBytes) {
73 this.encodingAlgo = encoding;
74 this.compressionAlgorithm =
75 compressionAlgorithm == null ? NONE : compressionAlgorithm;
76 if (this.compressionAlgorithm != NONE) {
77 compressor = compressionAlgorithm.getCompressor();
78 compressedByteStream = new ByteArrayOutputStream();
79 try {
80 compressionStream =
81 compressionAlgorithm.createPlainCompressionStream(
82 compressedByteStream, compressor);
83 } catch (IOException e) {
84 throw new RuntimeException(
85 "Could not create compression stream for algorithm "
86 + compressionAlgorithm, e);
87 }
88 }
89 dummyHeader = Preconditions.checkNotNull(headerBytes,
90 "Please pass HConstants.HFILEBLOCK_DUMMY_HEADER instead of null for param headerBytes");
91 }
92
93 @Override
94 public void setDummyHeader(byte[] headerBytes) {
95 dummyHeader = headerBytes;
96 }
97
98
99
100
101
102 public void prepareEncoding() throws IOException {
103 encodedStream.reset();
104 dataOut.write(dummyHeader);
105 if (encodingAlgo != null
106 && encodingAlgo != DataBlockEncoding.NONE) {
107 encodingAlgo.writeIdInBytes(dataOut);
108 }
109 }
110
111 @Override
112 public void postEncoding(BlockType blockType)
113 throws IOException {
114 dataOut.flush();
115 compressAfterEncodingWithBlockType(encodedStream.toByteArray(), blockType);
116 this.blockType = blockType;
117 }
118
119
120
121
122
123
124 public void compressAfterEncodingWithBlockType(byte[] uncompressedBytesWithHeader,
125 BlockType blockType) throws IOException {
126 compressAfterEncoding(uncompressedBytesWithHeader, blockType, dummyHeader);
127 }
128
129
130
131
132
133
134
135 protected void compressAfterEncoding(byte[] uncompressedBytesWithHeader,
136 BlockType blockType, byte[] headerBytes) throws IOException {
137 this.uncompressedBytesWithHeader = uncompressedBytesWithHeader;
138 if (compressionAlgorithm != NONE) {
139 compressedByteStream.reset();
140 compressedByteStream.write(headerBytes);
141 compressionStream.resetState();
142 compressionStream.write(uncompressedBytesWithHeader,
143 headerBytes.length, uncompressedBytesWithHeader.length
144 - headerBytes.length);
145
146 compressionStream.flush();
147 compressionStream.finish();
148 onDiskBytesWithHeader = compressedByteStream.toByteArray();
149 } else {
150 onDiskBytesWithHeader = uncompressedBytesWithHeader;
151 }
152 this.blockType = blockType;
153 }
154
155 @Override
156 public byte[] getOnDiskBytesWithHeader() {
157 return onDiskBytesWithHeader;
158 }
159
160 @Override
161 public byte[] getUncompressedBytesWithHeader() {
162 return uncompressedBytesWithHeader;
163 }
164
165 @Override
166 public BlockType getBlockType() {
167 return blockType;
168 }
169
170
171
172
173
174 @Override
175 public void close() {
176 if (compressor != null) {
177 compressionAlgorithm.returnCompressor(compressor);
178 compressor = null;
179 }
180 }
181
182 @Override
183 public Algorithm getCompression() {
184 return this.compressionAlgorithm;
185 }
186
187 public DataOutputStream getOutputStreamForEncoder() {
188 return this.dataOut;
189 }
190
191 @Override
192 public DataBlockEncoding getDataBlockEncoding() {
193 return this.encodingAlgo;
194 }
195 }