-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathQatCompressionMode.java
206 lines (165 loc) · 7.47 KB
/
QatCompressionMode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.index.codec.customcodecs;
import org.apache.lucene.codecs.compressing.CompressionMode;
import org.apache.lucene.codecs.compressing.Compressor;
import org.apache.lucene.codecs.compressing.Decompressor;
import org.apache.lucene.store.ByteBuffersDataInput;
import org.apache.lucene.store.DataInput;
import org.apache.lucene.store.DataOutput;
import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.BytesRef;
import java.io.IOException;
import java.util.function.Supplier;
import com.intel.qat.QatZipper;
import static org.opensearch.index.codec.customcodecs.backward_codecs.lucene99.Lucene99QatCodec.DEFAULT_COMPRESSION_LEVEL;
import static org.opensearch.index.codec.customcodecs.backward_codecs.lucene99.Lucene99QatCodec.DEFAULT_QAT_MODE;
/** QatCompressionMode offers QAT_LZ4 and QAT_DEFLATE compressors. */
public class QatCompressionMode extends CompressionMode {
private static final int NUM_SUB_BLOCKS = 10;
private final QatZipper.Algorithm algorithm;
private final int compressionLevel;
private final Supplier<QatZipper.Mode> supplier;
/** default constructor */
protected QatCompressionMode() {
this(QatZipper.Algorithm.LZ4, DEFAULT_COMPRESSION_LEVEL, () -> { return DEFAULT_QAT_MODE; });
}
/**
* Creates a new instance.
*
* @param algorithm The compression algorithm (LZ4 or DEFLATE)
*/
protected QatCompressionMode(QatZipper.Algorithm algorithm) {
this(algorithm, DEFAULT_COMPRESSION_LEVEL, () -> { return DEFAULT_QAT_MODE; });
}
/**
* Creates a new instance.
*
* @param algorithm The compression algorithm (LZ4 or DEFLATE)
* @param compressionLevel The compression level to use.
*/
protected QatCompressionMode(QatZipper.Algorithm algorithm, int compressionLevel) {
this(algorithm, compressionLevel, () -> { return DEFAULT_QAT_MODE; });
}
/**
* Creates a new instance.
*
* @param algorithm The compression algorithm (LZ4 or DEFLATE)
* @param compressionLevel The compression level to use.
* @param supplier a supplier for QAT acceleration mode.
*/
protected QatCompressionMode(QatZipper.Algorithm algorithm, int compressionLevel, Supplier<QatZipper.Mode> supplier) {
this.algorithm = algorithm;
this.compressionLevel = compressionLevel;
this.supplier = supplier;
}
@Override
public Compressor newCompressor() {
return new QatCompressor(algorithm, compressionLevel, supplier.get());
}
@Override
public Decompressor newDecompressor() {
return new QatDecompressor(algorithm, supplier.get());
}
public int getCompressionLevel() {
return compressionLevel;
}
/** The QatCompressor. */
private static final class QatCompressor extends Compressor {
private byte[] compressedBuffer;
private final QatZipper qatZipper;
/** compressor with a given compresion level */
public QatCompressor(QatZipper.Algorithm algorithm, int compressionLevel, QatZipper.Mode qatMode) {
compressedBuffer = BytesRef.EMPTY_BYTES;
qatZipper = QatZipperFactory.createInstance(algorithm, compressionLevel, qatMode, QatZipper.PollingMode.PERIODICAL);
}
private void compress(byte[] bytes, int offset, int length, DataOutput out) throws IOException {
assert offset >= 0 : "Offset value must be greater than 0.";
int blockLength = (length + NUM_SUB_BLOCKS - 1) / NUM_SUB_BLOCKS;
out.writeVInt(blockLength);
final int end = offset + length;
assert end >= 0 : "Buffer read size must be greater than 0.";
for (int start = offset; start < end; start += blockLength) {
int l = Math.min(blockLength, end - start);
if (l == 0) {
out.writeVInt(0);
return;
}
final int maxCompressedLength = qatZipper.maxCompressedLength(l);
compressedBuffer = ArrayUtil.grow(compressedBuffer, maxCompressedLength);
int compressedSize = qatZipper.compress(bytes, start, l, compressedBuffer, 0, compressedBuffer.length);
out.writeVInt(compressedSize);
out.writeBytes(compressedBuffer, compressedSize);
}
}
@Override
public void compress(ByteBuffersDataInput buffersInput, DataOutput out) throws IOException {
final int length = (int) buffersInput.length();
byte[] bytes = new byte[length];
buffersInput.readBytes(bytes, 0, length);
compress(bytes, 0, length, out);
}
@Override
public void close() throws IOException {}
}
/** QAT_DEFLATE decompressor */
private static final class QatDecompressor extends Decompressor {
private byte[] compressed;
private final QatZipper qatZipper;
private final QatZipper.Mode qatMode;
private final QatZipper.Algorithm algorithm;
/** default decompressor */
public QatDecompressor(QatZipper.Algorithm algorithm, QatZipper.Mode qatMode) {
this.algorithm = algorithm;
this.qatMode = qatMode;
compressed = BytesRef.EMPTY_BYTES;
qatZipper = QatZipperFactory.createInstance(algorithm, qatMode, QatZipper.PollingMode.PERIODICAL);
}
/*resuable decompress function*/
@Override
public void decompress(DataInput in, int originalLength, int offset, int length, BytesRef bytes) throws IOException {
assert offset + length <= originalLength : "Buffer read size must be within limit.";
if (length == 0) {
bytes.length = 0;
return;
}
final int blockLength = in.readVInt();
bytes.offset = bytes.length = 0;
int offsetInBlock = 0;
int offsetInBytesRef = offset;
// Skip unneeded blocks
while (offsetInBlock + blockLength < offset) {
final int compressedLength = in.readVInt();
in.skipBytes(compressedLength);
offsetInBlock += blockLength;
offsetInBytesRef -= blockLength;
}
// Read blocks that intersect with the interval we need
while (offsetInBlock < offset + length) {
final int compressedLength = in.readVInt();
if (compressedLength == 0) {
return;
}
compressed = ArrayUtil.grow(compressed, compressedLength);
in.readBytes(compressed, 0, compressedLength);
int l = Math.min(blockLength, originalLength - offsetInBlock);
bytes.bytes = ArrayUtil.grow(bytes.bytes, bytes.length + l);
final int uncompressed = qatZipper.decompress(compressed, 0, compressedLength, bytes.bytes, bytes.length, l);
bytes.length += uncompressed;
offsetInBlock += blockLength;
}
bytes.offset = offsetInBytesRef;
bytes.length = length;
assert bytes.isValid() : "Decompression output is corrupted.";
}
@Override
public Decompressor clone() {
return new QatDecompressor(algorithm, qatMode);
}
}
}