-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Expand file tree
/
Copy pathCompressionMethod.cpp
More file actions
362 lines (317 loc) · 12.9 KB
/
Copy pathCompressionMethod.cpp
File metadata and controls
362 lines (317 loc) · 12.9 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include <IO/CompressionMethod.h>
#include <IO/BrotliReadBuffer.h>
#include <IO/BrotliWriteBuffer.h>
#include <IO/LZMADeflatingWriteBuffer.h>
#include <IO/LZMAInflatingReadBuffer.h>
#include <IO/ReadBuffer.h>
#include <IO/WriteBuffer.h>
#include <IO/ZlibDeflatingWriteBuffer.h>
#include <IO/ZlibInflatingReadBuffer.h>
#include <IO/LibdeflateDeflatingWriteBuffer.h>
#include <IO/LibdeflateInflatingReadBuffer.h>
#include <IO/ZstdDeflatingWriteBuffer.h>
#include <IO/ZstdInflatingReadBuffer.h>
#include <IO/Lz4DeflatingWriteBuffer.h>
#include <IO/Lz4InflatingReadBuffer.h>
#include <IO/Bzip2ReadBuffer.h>
#include <IO/Bzip2WriteBuffer.h>
#include <IO/HadoopSnappyReadBuffer.h>
#include <IO/HadoopSnappyWriteBuffer.h>
#include <IO/SnappyFramedReadBuffer.h>
#include <IO/SnappyFramedWriteBuffer.h>
#include "config.h"
#include <boost/algorithm/string/case_conv.hpp>
#include <charconv>
#include <Poco/String.h>
#include <string_view>
#include <Common/StringUtils.h>
namespace DB
{
namespace ErrorCodes
{
extern const int NOT_IMPLEMENTED;
}
std::string toContentEncodingName(CompressionMethod method)
{
switch (method)
{
case CompressionMethod::Gzip:
return "gzip";
case CompressionMethod::Zlib:
return "deflate";
case CompressionMethod::Brotli:
return "br";
case CompressionMethod::Xz:
return "xz";
case CompressionMethod::Zstd:
return "zstd";
case CompressionMethod::Lz4:
return "lz4";
case CompressionMethod::Bzip2:
return "bz2";
case CompressionMethod::Snappy:
return "snappy";
case CompressionMethod::None:
return "";
}
}
CompressionMethod chooseHTTPCompressionMethod(const std::string & list)
{
struct Entry
{
std::string_view coding;
double q_value = 1.0;
};
std::vector<Entry> entries; // STYLE_CHECK_ALLOW_STD_CONTAINERS
size_t pos = 0;
while (pos < list.size())
{
while (pos < list.size() && isWhitespaceASCII(list[pos]))
++pos;
if (pos >= list.size())
break;
size_t comma = list.find(',', pos);
if (comma == std::string::npos)
comma = list.size();
std::string_view token(list.data() + pos, comma - pos);
while (!token.empty() && isWhitespaceASCII(token.back()))
token.remove_suffix(1);
pos = comma + 1;
auto semicolon = token.find(';');
if (semicolon == std::string_view::npos)
{
entries.push_back({token, 1.0});
continue;
}
std::string_view coding = token.substr(0, semicolon);
while (!coding.empty() && isWhitespaceASCII(coding.back()))
coding.remove_suffix(1);
double q = 1.0;
std::string_view params = token.substr(semicolon + 1);
auto qpos = params.find("q=");
if (qpos == std::string_view::npos)
qpos = params.find("Q=");
if (qpos != std::string_view::npos)
{
auto qval = params.substr(qpos + 2);
while (!qval.empty() && isWhitespaceASCII(qval.front()))
qval.remove_prefix(1);
auto [_, ec] = std::from_chars(qval.data(), qval.data() + qval.size(), q);
if (ec != std::errc{})
q = 1.0;
}
entries.push_back({coding, q});
}
static constexpr std::pair<std::string_view, CompressionMethod> preferred[] = {
{"zstd", CompressionMethod::Zstd},
#if USE_BROTLI
{"br", CompressionMethod::Brotli},
#endif
{"lz4", CompressionMethod::Lz4},
#if USE_SNAPPY
{"snappy", CompressionMethod::Snappy},
#endif
{"gzip", CompressionMethod::Gzip},
{"deflate", CompressionMethod::Zlib},
{"xz", CompressionMethod::Xz},
#if USE_BZIP2
{"bz2", CompressionMethod::Bzip2},
#endif
};
/// `*` is the wildcard: matches every content-coding not explicitly listed (RFC 9110 §12.5.3).
double star_q = -1.0;
for (const auto & entry : entries)
{
if (Poco::icompare(entry.coding, std::string_view("*")) == 0)
star_q = entry.q_value;
}
for (const auto & [name, method] : preferred)
{
bool listed = false;
for (const auto & entry : entries)
{
if (Poco::icompare(entry.coding, name) == 0)
{
listed = true;
if (entry.q_value > 0.0)
return method;
break;
}
}
/// `*;q=N` (N > 0) covers every coding the client did not explicitly list.
if (!listed && star_q > 0.0)
return method;
}
return CompressionMethod::None;
}
CompressionMethod chooseCompressionMethod(const std::string & path, const std::string & hint)
{
/// Both the autodetection gate below and the uncompressed fallback must agree on the spelling.
std::string hint_lower = hint;
boost::algorithm::to_lower(hint_lower);
std::string file_extension;
if (hint_lower.empty() || hint_lower == "auto")
{
auto pos = path.find_last_of('.');
if (pos != std::string::npos)
file_extension = path.substr(pos + 1, std::string::npos);
}
std::string method_str;
if (file_extension.empty())
method_str = hint_lower;
else
method_str = std::move(file_extension);
boost::algorithm::to_lower(method_str);
if (method_str == "gzip" || method_str == "gz")
return CompressionMethod::Gzip;
if (method_str == "deflate")
return CompressionMethod::Zlib;
if (method_str == "brotli" || method_str == "br")
return CompressionMethod::Brotli;
if (method_str == "lzma" || method_str == "xz")
return CompressionMethod::Xz;
if (method_str == "zstd" || method_str == "zst")
return CompressionMethod::Zstd;
if (method_str == "lz4")
return CompressionMethod::Lz4;
if (method_str == "bz2")
return CompressionMethod::Bzip2;
if (method_str == "snappy")
return CompressionMethod::Snappy;
if (hint_lower.empty() || hint_lower == "auto" || hint_lower == "none")
return CompressionMethod::None;
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Unknown compression method '{}'. "
"Only 'auto', 'none', 'gzip', 'deflate', 'br', 'xz', 'zstd', 'lz4', 'bz2', 'snappy' are supported as compression methods", hint);
}
std::pair<uint64_t, uint64_t> getCompressionLevelRange(const CompressionMethod & method)
{
switch (method)
{
case CompressionMethod::Zstd:
return {1, 22};
case CompressionMethod::Lz4:
return {1, 12};
#if USE_LIBDEFLATE
case CompressionMethod::Gzip:
case CompressionMethod::Zlib:
/// libdeflate compresses up to level 12; keep the `INTO OUTFILE ... COMPRESSION ... LEVEL`
/// validation in line with the writer in `createWriteCompressedWrapper` and with the
/// `output_format_compression_level` / `http_zlib_compression_level` paths.
return {1, 12};
#endif
default:
return {1, 9};
}
}
static std::unique_ptr<CompressedReadBufferWrapper> createCompressedWrapper(
std::unique_ptr<ReadBuffer> nested, CompressionMethod method, size_t buf_size, char * existing_memory, size_t alignment, int zstd_window_log_max, [[maybe_unused]] SnappyMode snappy_mode)
{
if (method == CompressionMethod::Gzip || method == CompressionMethod::Zlib)
{
#if USE_LIBDEFLATE
/// libdeflate is faster than zlib for decompression.
return std::make_unique<LibdeflateInflatingReadBuffer>(std::move(nested), method, buf_size, existing_memory, alignment);
#else
return std::make_unique<ZlibInflatingReadBuffer>(std::move(nested), method, buf_size, existing_memory, alignment);
#endif
}
#if USE_BROTLI
if (method == CompressionMethod::Brotli)
return std::make_unique<BrotliReadBuffer>(std::move(nested), buf_size, existing_memory, alignment);
#endif
if (method == CompressionMethod::Xz)
return std::make_unique<LZMAInflatingReadBuffer>(std::move(nested), buf_size, existing_memory, alignment);
if (method == CompressionMethod::Zstd)
return std::make_unique<ZstdInflatingReadBuffer>(std::move(nested), buf_size, existing_memory, alignment, zstd_window_log_max);
if (method == CompressionMethod::Lz4)
return std::make_unique<Lz4InflatingReadBuffer>(std::move(nested), buf_size, existing_memory, alignment);
#if USE_BZIP2
if (method == CompressionMethod::Bzip2)
return std::make_unique<Bzip2ReadBuffer>(std::move(nested), buf_size, existing_memory, alignment);
#endif
#if USE_SNAPPY
if (method == CompressionMethod::Snappy)
{
if (snappy_mode == SnappyMode::Framed)
return std::make_unique<SnappyFramedReadBuffer>(std::move(nested), buf_size, existing_memory, alignment);
return std::make_unique<HadoopSnappyReadBuffer>(std::move(nested), buf_size, existing_memory, alignment);
}
#endif
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Unsupported compression method");
}
std::unique_ptr<ReadBuffer> wrapReadBufferWithCompressionMethod(
std::unique_ptr<ReadBuffer> nested, CompressionMethod method, int zstd_window_log_max, SnappyMode snappy_mode, size_t buf_size, char * existing_memory, size_t alignment)
{
if (method == CompressionMethod::None)
return nested;
return createCompressedWrapper(std::move(nested), method, buf_size, existing_memory, alignment, zstd_window_log_max, snappy_mode);
}
template<typename WriteBufferT>
std::unique_ptr<WriteBuffer> createWriteCompressedWrapper(
WriteBufferT && nested, CompressionMethod method, int level, int zstd_window_log, [[maybe_unused]] SnappyMode snappy_mode, size_t buf_size, char * existing_memory, size_t alignment, bool compress_empty)
{
if (method == DB::CompressionMethod::Gzip || method == CompressionMethod::Zlib)
{
#if USE_LIBDEFLATE
/// libdeflate is faster and compresses better; it produces a single valid gzip/zlib member.
/// Levels outside libdeflate's [1, 12] range (e.g. 0 = store) keep using zlib.
if (level >= 1 && level <= 12)
return std::make_unique<LibdeflateDeflatingWriteBuffer>(
std::forward<WriteBufferT>(nested), method, level, buf_size, existing_memory, alignment, compress_empty);
#endif
return std::make_unique<ZlibDeflatingWriteBuffer>(std::forward<WriteBufferT>(nested), method, level, buf_size, existing_memory, alignment, compress_empty);
}
#if USE_BROTLI
if (method == DB::CompressionMethod::Brotli)
return std::make_unique<BrotliWriteBuffer>(std::forward<WriteBufferT>(nested), level, buf_size, existing_memory, alignment, compress_empty);
#endif
if (method == CompressionMethod::Xz)
return std::make_unique<LZMADeflatingWriteBuffer>(std::forward<WriteBufferT>(nested), level, buf_size, existing_memory, alignment, compress_empty);
if (method == CompressionMethod::Zstd)
return std::make_unique<ZstdDeflatingWriteBuffer>(std::forward<WriteBufferT>(nested), level, zstd_window_log, buf_size, existing_memory, alignment, compress_empty);
if (method == CompressionMethod::Lz4)
return std::make_unique<Lz4DeflatingWriteBuffer>(std::forward<WriteBufferT>(nested), level, buf_size, existing_memory, alignment, compress_empty);
#if USE_BZIP2
if (method == CompressionMethod::Bzip2)
return std::make_unique<Bzip2WriteBuffer>(std::forward<WriteBufferT>(nested), level, buf_size, existing_memory, alignment, compress_empty);
#endif
#if USE_SNAPPY
if (method == CompressionMethod::Snappy)
{
if (snappy_mode == SnappyMode::Framed)
return std::make_unique<SnappyFramedWriteBuffer>(std::forward<WriteBufferT>(nested), buf_size, existing_memory, alignment, compress_empty);
return std::make_unique<HadoopSnappyWriteBuffer>(std::forward<WriteBufferT>(nested), buf_size, existing_memory, alignment, compress_empty);
}
#endif
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Unsupported compression method");
}
std::unique_ptr<WriteBuffer> wrapWriteBufferWithCompressionMethod(
std::unique_ptr<WriteBuffer> nested,
CompressionMethod method,
int level,
int zstd_window_log,
SnappyMode snappy_mode,
size_t buf_size,
char * existing_memory,
size_t alignment,
bool compress_empty)
{
if (method == CompressionMethod::None)
return nested;
return createWriteCompressedWrapper(nested, method, level, zstd_window_log, snappy_mode, buf_size, existing_memory, alignment, compress_empty);
}
std::unique_ptr<WriteBuffer> wrapWriteBufferWithCompressionMethod(
WriteBuffer * nested,
CompressionMethod method,
int level,
int zstd_window_log,
SnappyMode snappy_mode,
size_t buf_size,
char * existing_memory,
size_t alignment,
bool compress_empty)
{
chassert(method != CompressionMethod::None);
return createWriteCompressedWrapper(nested, method, level, zstd_window_log, snappy_mode, buf_size, existing_memory, alignment, compress_empty);
}
}