Debian Codegraph @

lib/compressor.c

raw symbols SCIP
1 /*
2 Copyright (C) 2009-2018 Brazil
3 Copyright (C) 2020-2025 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "grn_compressor.h"
21
22 #include "grn.h"
23 #include "grn_ctx.h"
24
25 #ifdef GRN_WITH_ZLIB
26 # include <zlib.h>
27 #endif
28
29 #ifdef GRN_WITH_LZ4
30 # include <lz4.h>
31 #endif
32
33 #ifdef GRN_WITH_ZSTD
34 # include <zstd.h>
35 #endif
36
37 #ifdef GRN_WITH_BLOSC
38 # include <blosc2.h>
39 # include <blosc2/filters-registry.h>
40
41 # define GRN_BLOSC_META_HEADER "header"
42 # define GRN_BLOSC_META_FOOTER "footer"
43 #endif
44
45 #ifdef GRN_WITH_OPENZL
46 # include <openzl/openzl.h>
47 #endif
48
49 /*
50 * Compressed value uses the following format:
51 *
52 * +-------------------+------+
53 * | uint64_t metadata | data |
54 * +-------------------+------+
55 *
56 * Metadata:
57 * * Upper 32bit is flags
58 * * Lower 32bit is data length in byte of the original data
59 *
60 * Available flags:
61 *
62 * * COMPRESSED_VALUE_META_FLAG_RAW: Data is raw data (not compressed
63 * original data). If raw data is small (< COMPRESS_THRESHOLD_BYTE),
64 * compression is meaningless. So raw data is stored as-is.
65 *
66 * If COMPRESSED_VALUE_META_FLAG_RAW isn't used, data is compressed
67 * data.
68 */
69
70 #define COMPRESSED_VALUE_LEN(data_len) (sizeof(uint64_t) + (data_len))
71 #define COMPRESSED_VALUE_GET_METADATA(value) (*((uint64_t *)(value)))
72 #define COMPRESSED_VALUE_SET_METADATA(value, metadata) \
73 *((uint64_t *)(value)) = (metadata)
74 #define COMPRESSED_VALUE_GET_DATA(value) ((void *)(((uint64_t *)(value)) + 1))
75 #define COMPRESSED_VALUE_GET_DATA_LEN(value_len) (value_len - sizeof(uint64_t))
76
77 #define COMPRESSED_VALUE_METADATA_PACK(original_len, flags) \
78 ((original_len) | (flags))
79 #define COMPRESSED_VALUE_METADATA_FLAG(metadata) \
80 ((metadata) & 0xf000000000000000)
81 #define COMPRESSED_VALUE_METADATA_FLAG_RAW 0x1000000000000000
82 #define COMPRESSED_VALUE_METADATA_ORIGINAL_LEN(metadata) \
83 ((metadata) & 0x0fffffffffffffff)
84
85 #define COMPRESS_THRESHOLD_BYTE 256
86
87 /*
88 * Create a compressed value format data for small data. It has
89 * the COMPRESSED_VALUE_META_FLAG_RAW flag.
90 */
91 static inline grn_rc
92 grn_compressor_pack(grn_ctx *ctx, grn_compress_data *data)
93 {
94 uint64_t input_len = data->header_len + data->body_len + data->footer_len;
95 data->packed_value_len = COMPRESSED_VALUE_LEN(input_len);
96 uint64_t packed_value_metadata =
97 COMPRESSED_VALUE_METADATA_PACK(input_len,
98 COMPRESSED_VALUE_METADATA_FLAG_RAW);
99
100 COMPRESSED_VALUE_SET_METADATA(data->packed_value, packed_value_metadata);
101 char *packed_value_data = COMPRESSED_VALUE_GET_DATA(data->packed_value);
102 size_t offset = 0;
103 if (data->header_len > 0) {
104 grn_memcpy(packed_value_data + offset, data->header, data->header_len);
105 offset += data->header_len;
106 }
107 if (data->body_len > 0) {
108 grn_memcpy(packed_value_data + offset, data->body, data->body_len);
109 offset += data->body_len;
110 }
111 if (data->footer_len > 0) {
112 grn_memcpy(packed_value_data + offset, data->footer, data->footer_len);
113 offset += data->footer_len;
114 }
115
116 return ctx->rc;
117 }
118
119 /*
120 * Parse a compressed value format data. If it has the
121 * COMPRESSED_VALUE_METADATA_FLAG_RAW flag, data is stored in
122 * data->unpacked_value and data->unpacked_value_len. Otherwise, data
123 * is stored in data->compressed_value and data->compressed_value_len.
124 */
125 static inline grn_rc
126 grn_compressor_unpack(grn_ctx *ctx, grn_decompress_data *data)
127 {
128 uint64_t compressed_value_metadata =
129 COMPRESSED_VALUE_GET_METADATA(data->raw_value);
130 data->compressed_value = COMPRESSED_VALUE_GET_DATA(data->raw_value);
131 data->compressed_value_len =
132 COMPRESSED_VALUE_GET_DATA_LEN(data->raw_value_len);
133
134 data->decompressed_value_len =
135 (uint32_t)COMPRESSED_VALUE_METADATA_ORIGINAL_LEN(compressed_value_metadata);
136 if (COMPRESSED_VALUE_METADATA_FLAG(compressed_value_metadata) &
137 COMPRESSED_VALUE_METADATA_FLAG_RAW) {
138 data->unpacked_value = data->compressed_value;
139 data->compressed_value = NULL;
140 data->unpacked_value_len = data->compressed_value_len;
141 data->compressed_value_len = 0;
142 data->decompressed_value_len = 0;
143 }
144
145 return ctx->rc;
146 }
147
148 #ifdef GRN_WITH_ZLIB
149 static inline const char *
150 grn_zrc_to_string(int zrc)
151 {
152 switch (zrc) {
153 case Z_OK:
154 return "OK";
155 case Z_STREAM_END:
156 return "Stream is end";
157 case Z_NEED_DICT:
158 return "Need dictionary";
159 case Z_ERRNO:
160 return "See errno";
161 case Z_STREAM_ERROR:
162 return "Stream error";
163 case Z_DATA_ERROR:
164 return "Data error";
165 case Z_MEM_ERROR:
166 return "Memory error";
167 case Z_BUF_ERROR:
168 return "Buffer error";
169 case Z_VERSION_ERROR:
170 return "Version error";
171 default:
172 return "Unknown";
173 }
174 }
175
176 static inline grn_rc
177 grn_compressor_compress_zlib(grn_ctx *ctx, grn_compress_data *data)
178 {
179 const char *tag = "[compressor][compress][zlib] failed to compress:";
180
181 z_stream zstream = {0};
182 zstream.zalloc = Z_NULL;
183 zstream.zfree = Z_NULL;
184 int zwindow_bits = 15;
185 int zmem_level = 8;
186 int zrc = deflateInit2(&zstream,
187 Z_DEFAULT_COMPRESSION,
188 Z_DEFLATED,
189 zwindow_bits,
190 zmem_level,
191 Z_DEFAULT_STRATEGY);
192 if (zrc != Z_OK) {
193 ERR(GRN_ZLIB_ERROR, "%s initialize: %s", tag, grn_zrc_to_string(zrc));
194 return ctx->rc;
195 }
196
197 uint64_t input_len = data->header_len + data->body_len + data->footer_len;
198 zstream.avail_out = deflateBound(&zstream, input_len);
199 data->compressed_value = GRN_MALLOC(COMPRESSED_VALUE_LEN(zstream.avail_out));
200 if (!data->compressed_value) {
201 char message[GRN_CTX_MSGSIZE];
202 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
203 ERR(GRN_ZLIB_ERROR, "%s allocate buffer: %s", tag, message);
204 deflateEnd(&zstream);
205 return ctx->rc;
206 }
207 COMPRESSED_VALUE_SET_METADATA(data->compressed_value,
208 COMPRESSED_VALUE_METADATA_PACK(input_len, 0));
209 zstream.next_out = COMPRESSED_VALUE_GET_DATA(data->compressed_value);
210
211 if (data->header_len == 0 && data->footer_len == 0) {
212 zstream.next_in = data->body;
213 zstream.avail_in = data->body_len;
214 zrc = deflate(&zstream, Z_FINISH);
215 if (zrc != Z_STREAM_END) {
216 ERR(GRN_ZLIB_ERROR, "%s finish: %s", tag, grn_zrc_to_string(zrc));
217 deflateEnd(&zstream);
218 GRN_FREE(data->compressed_value);
219 data->compressed_value = NULL;
220 return ctx->rc;
221 }
222 } else {
223 if (data->header_len > 0) {
224 zstream.next_in = data->header;
225 zstream.avail_in = data->header_len;
226 /* TODO: Z_NO_FLUSH here may not be safe. All zstream.next_in
227 * may not be processed. */
228 zrc = deflate(&zstream, Z_NO_FLUSH);
229 if (zrc != Z_OK) {
230 ERR(GRN_ZLIB_ERROR, "%s header: %s", tag, grn_zrc_to_string(zrc));
231 deflateEnd(&zstream);
232 GRN_FREE(data->compressed_value);
233 data->compressed_value = NULL;
234 return ctx->rc;
235 }
236 }
237
238 if (data->body_len > 0) {
239 zstream.next_in = data->body;
240 zstream.avail_in = data->body_len;
241 /* TODO: Z_NO_FLUSH here may not be safe. All zstream.next_in
242 * may not be processed. */
243 zrc = deflate(&zstream, Z_NO_FLUSH);
244 if (zrc != Z_OK) {
245 ERR(GRN_ZLIB_ERROR, "%s body: %s", tag, grn_zrc_to_string(zrc));
246 deflateEnd(&zstream);
247 GRN_FREE(data->compressed_value);
248 data->compressed_value = NULL;
249 return ctx->rc;
250 }
251 }
252
253 if (data->footer_len > 0) {
254 zstream.next_in = data->footer;
255 zstream.avail_in = data->footer_len;
256 /* TODO: Z_NO_FLUSH here may not be safe. All zstream.next_in
257 * may not be processed. */
258 zrc = deflate(&zstream, Z_NO_FLUSH);
259 if (zrc != Z_OK) {
260 ERR(GRN_ZLIB_ERROR, "%s footer: %s", tag, grn_zrc_to_string(zrc));
261 deflateEnd(&zstream);
262 GRN_FREE(data->compressed_value);
263 data->compressed_value = NULL;
264 return ctx->rc;
265 }
266 }
267
268 zrc = deflate(&zstream, Z_FINISH);
269 if (zrc != Z_STREAM_END) {
270 ERR(GRN_ZLIB_ERROR, "%s finish: %s", tag, grn_zrc_to_string(zrc));
271 deflateEnd(&zstream);
272 GRN_FREE(data->compressed_value);
273 data->compressed_value = NULL;
274 return ctx->rc;
275 }
276 }
277 data->compressed_value_len = COMPRESSED_VALUE_LEN(zstream.total_out);
278
279 zrc = deflateEnd(&zstream);
280 if (zrc != Z_OK) {
281 ERR(GRN_ZLIB_ERROR, "%s end: %s", tag, grn_zrc_to_string(zrc));
282 GRN_FREE(data->compressed_value);
283 data->compressed_value = NULL;
284 data->compressed_value_len = 0;
285 return ctx->rc;
286 }
287
288 return ctx->rc;
289 }
290
291 static inline grn_rc
292 grn_compressor_decompress_zlib(grn_ctx *ctx, grn_decompress_data *data)
293 {
294 const char *tag = "[compressor][decompress][zlib] failed to decompress:";
295
296 z_stream zstream = {0};
297 zstream.next_in = (Bytef *)(data->compressed_value);
298 zstream.avail_in = data->compressed_value_len;
299 zstream.zalloc = Z_NULL;
300 zstream.zfree = Z_NULL;
301 int zrc = inflateInit2(&zstream, 15 /* windowBits */);
302 if (zrc != Z_OK) {
303 ERR(GRN_ZLIB_ERROR, "%s initialize: %s", tag, grn_zrc_to_string(zrc));
304 return ctx->rc;
305 }
306 data->decompressed_value = GRN_MALLOC(data->decompressed_value_len);
307 if (!data->decompressed_value) {
308 char message[GRN_CTX_MSGSIZE];
309 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
310 ERR(GRN_ZLIB_ERROR, "%s allocate buffer: %s", tag, message);
311 inflateEnd(&zstream);
312 data->decompressed_value_len = 0;
313 return ctx->rc;
314 }
315 zstream.next_out = (Bytef *)(data->decompressed_value);
316 zstream.avail_out = data->decompressed_value_len;
317 zrc = inflate(&zstream, Z_FINISH);
318 if (zrc != Z_STREAM_END) {
319 ERR(GRN_ZLIB_ERROR, "%s finish: %s", tag, grn_zrc_to_string(zrc));
320 inflateEnd(&zstream);
321 GRN_FREE(data->decompressed_value);
322 data->decompressed_value = NULL;
323 data->decompressed_value_len = 0;
324 return ctx->rc;
325 }
326 data->decompressed_value_len = zstream.total_out;
327 zrc = inflateEnd(&zstream);
328 if (zrc != Z_OK) {
329 ERR(GRN_ZLIB_ERROR, "%s end: %s", tag, grn_zrc_to_string(zrc));
330 GRN_FREE(data->decompressed_value);
331 data->decompressed_value = NULL;
332 data->decompressed_value_len = 0;
333 return ctx->rc;
334 }
335 return ctx->rc;
336 }
337 #endif /* GRN_WITH_ZLIB */
338
339 #ifdef GRN_WITH_LZ4
340 static inline grn_rc
341 grn_compressor_compress_lz4(grn_ctx *ctx, grn_compress_data *data)
342 {
343 const char *tag = "[compressor][compress][lz4] failed to compress:";
344
345 size_t input_len = data->header_len + data->body_len + data->footer_len;
346 if (input_len > (size_t)LZ4_MAX_INPUT_SIZE) {
347 data->packed_value = GRN_MALLOC(COMPRESSED_VALUE_LEN(input_len));
348 if (!data->packed_value) {
349 char message[GRN_CTX_MSGSIZE];
350 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
351 ERR(GRN_LZ4_ERROR, "%s allocate packed buffer: %s", tag, message);
352 return ctx->rc;
353 }
354 grn_rc rc = grn_compressor_pack(ctx, data);
355 if (rc != GRN_SUCCESS) {
356 GRN_FREE(data->packed_value);
357 data->packed_value = NULL;
358 data->packed_value_len = 0;
359 }
360 return rc;
361 }
362
363 int lz4_value_len_max = LZ4_compressBound((int)input_len);
364 data->compressed_value = GRN_MALLOC(COMPRESSED_VALUE_LEN(lz4_value_len_max));
365 if (!data->compressed_value) {
366 char message[GRN_CTX_MSGSIZE];
367 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
368 ERR(GRN_LZ4_ERROR, "%s allocate compressed buffer: %s", tag, message);
369 return ctx->rc;
370 }
371 COMPRESSED_VALUE_SET_METADATA(data->compressed_value,
372 COMPRESSED_VALUE_METADATA_PACK(input_len, 0));
373
374 char *lz4_value = COMPRESSED_VALUE_GET_DATA(data->compressed_value);
375 grn_obj value;
376 if (data->header_len == 0 && data->footer_len == 0) {
377 GRN_TEXT_INIT(&value, GRN_OBJ_DO_SHALLOW_COPY);
378 GRN_TEXT_SET(ctx, &value, data->body, data->body_len);
379 } else {
380 GRN_TEXT_INIT(&value, 0);
381 if (data->header_len > 0) {
382 GRN_TEXT_PUT(ctx, &value, data->header, data->header_len);
383 }
384 if (data->body_len > 0) {
385 GRN_TEXT_PUT(ctx, &value, data->body, data->body_len);
386 }
387 if (data->footer_len > 0) {
388 GRN_TEXT_PUT(ctx, &value, data->footer, data->footer_len);
389 }
390 }
391 int lz4_value_len_real = LZ4_compress_default(GRN_TEXT_VALUE(&value),
392 lz4_value,
393 GRN_TEXT_LEN(&value),
394 lz4_value_len_max);
395 GRN_OBJ_FIN(ctx, &value);
396 if (lz4_value_len_real <= 0) {
397 ERR(GRN_LZ4_ERROR, "%s compress", tag);
398 GRN_FREE(data->compressed_value);
399 data->compressed_value = NULL;
400 return ctx->rc;
401 }
402 data->compressed_value_len = COMPRESSED_VALUE_LEN(lz4_value_len_real);
403
404 return ctx->rc;
405 }
406
407 static inline grn_rc
408 grn_compressor_decompress_lz4(grn_ctx *ctx, grn_decompress_data *data)
409 {
410 const char *tag = "[compressor][decompress][lz4] failed to decompress:";
411
412 data->decompressed_value = GRN_MALLOC(data->decompressed_value_len);
413 if (!data->decompressed_value) {
414 char message[GRN_CTX_MSGSIZE];
415 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
416 ERR(GRN_LZ4_ERROR, "%s allocate buffer: %s", tag, message);
417 data->decompressed_value_len = 0;
418 return ctx->rc;
419 }
420 if (LZ4_decompress_safe((const char *)(data->compressed_value),
421 (char *)(data->decompressed_value),
422 data->compressed_value_len,
423 data->decompressed_value_len) < 0) {
424 ERR(GRN_LZ4_ERROR, "%s decompress", tag);
425 GRN_FREE(data->decompressed_value);
426 data->decompressed_value = NULL;
427 data->decompressed_value_len = 0;
428 return ctx->rc;
429 }
430
431 return ctx->rc;
432 }
433 #endif /* GRN_WITH_LZ4 */
434
435 #ifdef GRN_WITH_ZSTD
436 static inline grn_rc
437 grn_compressor_compress_zstd(grn_ctx *ctx, grn_compress_data *data)
438 {
439 const char *tag = "[compressor][compress][zstd] failed to compress:";
440
441 const int zstd_compression_level = ZSTD_CLEVEL_DEFAULT;
442
443 ZSTD_CCtx *zstd_cctx = ZSTD_createCCtx();
444 if (!zstd_cctx) {
445 ERR(GRN_ZSTD_ERROR, "%s allocate compress context", tag);
446 return ctx->rc;
447 }
448
449 size_t input_len = data->header_len + data->body_len + data->footer_len;
450 size_t zstd_value_len_max = ZSTD_compressBound(input_len);
451 data->compressed_value = GRN_MALLOC(COMPRESSED_VALUE_LEN(zstd_value_len_max));
452 if (!data->compressed_value) {
453 char message[GRN_CTX_MSGSIZE];
454 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
455 ERR(GRN_ZSTD_ERROR, "%s allocate compress buffer: %s", tag, message);
456 ZSTD_freeCCtx(zstd_cctx);
457 return ctx->rc;
458 }
459 void *zstd_value = COMPRESSED_VALUE_GET_DATA(data->compressed_value);
460 COMPRESSED_VALUE_SET_METADATA(data->compressed_value,
461 COMPRESSED_VALUE_METADATA_PACK(input_len, 0));
462
463 if (data->header_len == 0 && data->footer_len == 0) {
464 size_t zstd_value_len_real = ZSTD_compressCCtx(zstd_cctx,
465 zstd_value,
466 zstd_value_len_max,
467 data->body,
468 data->body_len,
469 zstd_compression_level);
470 if (ZSTD_isError(zstd_value_len_real)) {
471 ERR(GRN_ZSTD_ERROR,
472 "%s compress: %s",
473 tag,
474 ZSTD_getErrorName(zstd_value_len_real));
475 ZSTD_freeCCtx(zstd_cctx);
476 GRN_FREE(data->compressed_value);
477 data->compressed_value = NULL;
478 return ctx->rc;
479 }
480 data->compressed_value_len = COMPRESSED_VALUE_LEN(zstd_value_len_real);
481 } else {
482 ZSTD_outBuffer zstd_output = {0};
483 zstd_output.dst = zstd_value;
484 zstd_output.size = zstd_value_len_max;
485
486 if (data->header_len > 0) {
487 ZSTD_inBuffer zstd_input = {0};
488 zstd_input.src = data->header;
489 zstd_input.size = data->header_len;
490 zstd_input.pos = 0;
491 size_t zstd_result = ZSTD_compressStream2(zstd_cctx,
492 &zstd_output,
493 &zstd_input,
494 ZSTD_e_flush);
495 if (ZSTD_isError(zstd_result)) {
496 ERR(GRN_ZSTD_ERROR,
497 "%s compress header: %s",
498 tag,
499 ZSTD_getErrorName(zstd_result));
500 ZSTD_freeCCtx(zstd_cctx);
501 GRN_FREE(data->compressed_value);
502 data->compressed_value = NULL;
503 return ctx->rc;
504 }
505 }
506
507 if (data->body_len > 0) {
508 ZSTD_inBuffer zstd_input = {0};
509 zstd_input.src = data->body;
510 zstd_input.size = data->body_len;
511 zstd_input.pos = 0;
512 size_t zstd_result = ZSTD_compressStream2(zstd_cctx,
513 &zstd_output,
514 &zstd_input,
515 ZSTD_e_flush);
516 if (ZSTD_isError(zstd_result)) {
517 ERR(GRN_ZSTD_ERROR,
518 "%s compress body: %s",
519 tag,
520 ZSTD_getErrorName(zstd_result));
521 ZSTD_freeCCtx(zstd_cctx);
522 GRN_FREE(data->compressed_value);
523 data->compressed_value = NULL;
524 return ctx->rc;
525 }
526 }
527
528 if (data->footer_len > 0) {
529 ZSTD_inBuffer zstd_input = {0};
530 zstd_input.src = data->footer;
531 zstd_input.size = data->footer_len;
532 zstd_input.pos = 0;
533 size_t zstd_result = ZSTD_compressStream2(zstd_cctx,
534 &zstd_output,
535 &zstd_input,
536 ZSTD_e_flush);
537 if (ZSTD_isError(zstd_result)) {
538 ERR(GRN_ZSTD_ERROR,
539 "%s compress footer: %s",
540 tag,
541 ZSTD_getErrorName(zstd_result));
542 ZSTD_freeCCtx(zstd_cctx);
543 GRN_FREE(data->compressed_value);
544 data->compressed_value = NULL;
545 return ctx->rc;
546 }
547 }
548
549 {
550 ZSTD_inBuffer zstd_input = {0};
551 size_t zstd_result =
552 ZSTD_compressStream2(zstd_cctx, &zstd_output, &zstd_input, ZSTD_e_end);
553 if (ZSTD_isError(zstd_result)) {
554 ERR(GRN_ZSTD_ERROR, "%s end: %s", tag, ZSTD_getErrorName(zstd_result));
555 ZSTD_freeCCtx(zstd_cctx);
556 GRN_FREE(data->compressed_value);
557 data->compressed_value = NULL;
558 return ctx->rc;
559 }
560 }
561 data->compressed_value_len = COMPRESSED_VALUE_LEN(zstd_output.pos);
562 }
563 ZSTD_freeCCtx(zstd_cctx);
564
565 return ctx->rc;
566 }
567
568 static inline grn_rc
569 grn_compressor_decompress_zstd(grn_ctx *ctx, grn_decompress_data *data)
570 {
571 const char *tag = "[compressor][decompress][zstd] failed to decompress:";
572
573 data->decompressed_value = GRN_MALLOC(data->decompressed_value_len);
574 if (!data->decompressed_value) {
575 char message[GRN_CTX_MSGSIZE];
576 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
577 ERR(GRN_ZSTD_ERROR, "%s allocate buffer: %s", tag, message);
578 data->decompressed_value_len = 0;
579 return ctx->rc;
580 }
581
582 size_t written_len = ZSTD_decompress((char *)(data->decompressed_value),
583 data->decompressed_value_len,
584 data->compressed_value,
585 data->compressed_value_len);
586 if (ZSTD_isError(written_len)) {
587 ERR(GRN_ZSTD_ERROR,
588 "%s decompress: %s",
589 tag,
590 ZSTD_getErrorName(written_len));
591 GRN_FREE(data->decompressed_value);
592 data->decompressed_value = NULL;
593 data->decompressed_value_len = 0;
594 return ctx->rc;
595 }
596
597 return ctx->rc;
598 }
599 #endif /* GRN_WITH_ZSTD */
600
601 #ifdef GRN_WITH_BLOSC
602 static inline blosc2_schunk *
603 grn_compressor_compress_blosc_create_schunk(grn_ctx *ctx,
604 grn_compress_data *data,
605 blosc2_cparams *cparams,
606 blosc2_storage *storage)
607 {
608 *cparams = BLOSC2_CPARAMS_DEFAULTS;
609 switch (data->body_column_flags & GRN_OBJ_COMPRESS_MASK) {
610 # ifdef GRN_WITH_ZLIB
611 case GRN_OBJ_COMPRESS_ZLIB:
612 cparams->compcode = BLOSC_ZLIB;
613 break;
614 # endif
615 # ifdef GRN_WITH_LZ4
616 case GRN_OBJ_COMPRESS_LZ4:
617 cparams->compcode = BLOSC_LZ4;
618 break;
619 # endif
620 # ifdef GRN_WITH_ZSTD
621 case GRN_OBJ_COMPRESS_ZSTD:
622 cparams->compcode = BLOSC_ZSTD;
623 break;
624 # endif
625 default:
626 break;
627 }
628 if (grn_type_id_is_text_family(ctx, data->body_range)) {
629 cparams->typesize = sizeof(char);
630 } else {
631 cparams->typesize = grn_type_id_size(ctx, data->body_range);
632 if (data->body_column_flags & GRN_OBJ_WITH_WEIGHT) {
633 if (data->body_column_flags & GRN_OBJ_WEIGHT_BFLOAT16) {
634 # ifdef GRN_HAVE_BFLOAT16
635 cparams->typesize += sizeof(grn_bfloat16);
636 # else
637 cparams->typesize += sizeof(float);
638 # endif
639 } else if (data->body_column_flags & GRN_OBJ_WEIGHT_FLOAT32) {
640 cparams->typesize += sizeof(float);
641 } else {
642 cparams->typesize += sizeof(double);
643 }
644 }
645 }
646 size_t current_filter_id = BLOSC2_MAX_FILTERS - 1;
647 cparams->filters[current_filter_id] = BLOSC_NOFILTER;
648 if (data->body_n_elements > 1) {
649 if (data->body_column_flags & GRN_OBJ_COMPRESS_FILTER_BYTE_DELTA) {
650 cparams->filters[current_filter_id] = BLOSC_FILTER_BYTEDELTA;
651 cparams->filters_meta[current_filter_id] = cparams->typesize;
652 current_filter_id--;
653 }
654 if (cparams->typesize > (int32_t)sizeof(char)) {
655 if (data->body_column_flags & GRN_OBJ_COMPRESS_FILTER_SHUFFLE) {
656 cparams->filters[current_filter_id] = BLOSC_SHUFFLE;
657 current_filter_id--;
658 }
659 }
660 if (data->body_range == GRN_DB_FLOAT ||
661 data->body_range == GRN_DB_FLOAT32) {
662 if (data->body_column_flags &
663 GRN_OBJ_COMPRESS_FILTER_TRUNCATE_PRECISION_2BYTES) {
664 cparams->filters[current_filter_id] = BLOSC_TRUNC_PREC;
665 cparams->filters_meta[current_filter_id] = -16;
666 current_filter_id--;
667 } else if (data->body_column_flags &
668 GRN_OBJ_COMPRESS_FILTER_TRUNCATE_PRECISION_1BYTE) {
669 cparams->filters[current_filter_id] = BLOSC_TRUNC_PREC;
670 cparams->filters_meta[current_filter_id] = -8;
671 current_filter_id--;
672 }
673 }
674 }
675
676 *storage = BLOSC2_STORAGE_DEFAULTS;
677 storage->contiguous = true;
678 storage->cparams = cparams;
679
680 return blosc2_schunk_new(storage);
681 }
682
683 static inline grn_rc
684 grn_compressor_compress_blosc(grn_ctx *ctx, grn_compress_data *data)
685 {
686 const char *tag = "[compressor][compress][blosc] failed to compress:";
687
688 blosc2_cparams cparams;
689 blosc2_storage storage;
690 blosc2_schunk *schunk =
691 grn_compressor_compress_blosc_create_schunk(ctx, data, &cparams, &storage);
692
693 if (data->header_len > 0) {
694 int meta_index = blosc2_meta_add(schunk,
695 GRN_BLOSC_META_HEADER,
696 data->header,
697 data->header_len);
698 if (meta_index < 0) {
699 ERR(GRN_BLOSC_ERROR, "%s set header: %s", tag, print_error(meta_index));
700 blosc2_schunk_free(schunk);
701 return ctx->rc;
702 }
703 }
704
705 if (data->footer_len > 0) {
706 int meta_index = blosc2_meta_add(schunk,
707 GRN_BLOSC_META_FOOTER,
708 data->footer,
709 data->footer_len);
710 if (meta_index < 0) {
711 ERR(GRN_BLOSC_ERROR, "%s set footer: %s", tag, print_error(meta_index));
712 blosc2_schunk_free(schunk);
713 return ctx->rc;
714 }
715 }
716
717 if (data->body_len > 0) {
718 int64_t n_chunks =
719 blosc2_schunk_append_buffer(schunk, data->body, data->body_len);
720 if (n_chunks < 0) {
721 ERR(GRN_BLOSC_ERROR, "%s compress body: %s", tag, print_error(n_chunks));
722 blosc2_schunk_free(schunk);
723 return ctx->rc;
724 }
725 }
726
727 uint8_t *blosc_value = NULL;
728 bool blosc_value_need_free = false;
729 int64_t blosc_value_len =
730 blosc2_schunk_to_buffer(schunk, &blosc_value, &blosc_value_need_free);
731 if (blosc_value_len < 0) {
732 ERR(GRN_BLOSC_ERROR,
733 "%s serialize compressed value: %s",
734 tag,
735 print_error(blosc_value_len));
736 blosc2_schunk_free(schunk);
737 return ctx->rc;
738 }
739
740 data->compressed_value_len = COMPRESSED_VALUE_LEN(blosc_value_len);
741 data->compressed_value = GRN_MALLOC(data->compressed_value_len);
742 if (!data->compressed_value) {
743 ERR(GRN_BLOSC_ERROR, "%s allocate packed value buffer", tag);
744 if (blosc_value_need_free) {
745 free(blosc_value);
746 }
747 blosc2_schunk_free(schunk);
748 data->compressed_value_len = 0;
749 return ctx->rc;
750 }
751 size_t input_len = data->header_len + data->body_len + data->footer_len;
752 COMPRESSED_VALUE_SET_METADATA(data->compressed_value,
753 COMPRESSED_VALUE_METADATA_PACK(input_len, 0));
754 grn_memcpy(COMPRESSED_VALUE_GET_DATA(data->compressed_value),
755 blosc_value,
756 blosc_value_len);
757 if (blosc_value_need_free) {
758 free(blosc_value);
759 }
760 blosc2_schunk_free(schunk);
761
762 return ctx->rc;
763 }
764
765 static inline bool
766 grn_compressor_decompress_blosc_copy_meta(grn_ctx *ctx,
767 blosc2_schunk *schunk,
768 uint8_t **destination,
769 int32_t *destination_len,
770 const char *name,
771 const char *tag)
772 {
773 int i = 0;
774 for (i = 0; i < schunk->nmetalayers; i++) {
775 blosc2_metalayer *metalayer = schunk->metalayers[i];
776 if (strcmp(metalayer->name, name) == 0) {
777 if (metalayer->content_len > *destination_len) {
778 ERR(GRN_BLOSC_ERROR,
779 "%s %s is too large: %d > %d",
780 tag,
781 name,
782 metalayer->content_len,
783 *destination_len);
784 return false;
785 }
786 memcpy(*destination, metalayer->content, metalayer->content_len);
787 *destination += metalayer->content_len;
788 *destination_len -= metalayer->content_len;
789 break;
790 }
791 }
792 return true;
793 }
794
795 static grn_rc
796 grn_compressor_decompress_blosc(grn_ctx *ctx, grn_decompress_data *data)
797 {
798 const char *tag = "[compressor][decompress][blosc] failed to decompress:";
799
800 data->decompressed_value = GRN_MALLOC(data->decompressed_value_len);
801 if (!data->decompressed_value) {
802 char message[GRN_CTX_MSGSIZE];
803 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
804 ERR(GRN_BLOSC_ERROR, "%s allocate buffer: %s", tag, message);
805 data->decompressed_value_len = 0;
806 return ctx->rc;
807 }
808
809 blosc2_schunk *schunk = blosc2_schunk_from_buffer(data->compressed_value,
810 data->compressed_value_len,
811 false);
812 if (!schunk) {
813 ERR(GRN_BLOSC_ERROR, "%s failed to load super chunk", tag);
814 GRN_FREE(data->decompressed_value);
815 data->decompressed_value = NULL;
816 data->decompressed_value_len = 0;
817 return ctx->rc;
818 }
819 uint8_t *destination = data->decompressed_value;
820 int32_t destination_len = data->decompressed_value_len;
821 if (!grn_compressor_decompress_blosc_copy_meta(ctx,
822 schunk,
823 &destination,
824 &destination_len,
825 GRN_BLOSC_META_HEADER,
826 tag)) {
827 blosc2_schunk_free(schunk);
828 GRN_FREE(data->decompressed_value);
829 data->decompressed_value = NULL;
830 data->decompressed_value_len = 0;
831 return ctx->rc;
832 }
833 {
834 int64_t i;
835 for (i = 0; i < schunk->nchunks; i++) {
836 int decompressed_len =
837 blosc2_schunk_decompress_chunk(schunk, i, destination, destination_len);
838 if (decompressed_len <= 0) {
839 ERR(GRN_BLOSC_ERROR,
840 "%s failed to decode a chunk: %" GRN_FMT_INT64D ": %s",
841 tag,
842 i,
843 print_error(decompressed_len));
844 blosc2_schunk_free(schunk);
845 GRN_FREE(data->decompressed_value);
846 data->decompressed_value = NULL;
847 data->decompressed_value_len = 0;
848 return ctx->rc;
849 }
850 destination += decompressed_len;
851 destination_len -= decompressed_len;
852 }
853 }
854 if (!grn_compressor_decompress_blosc_copy_meta(ctx,
855 schunk,
856 &destination,
857 &destination_len,
858 GRN_BLOSC_META_FOOTER,
859 tag)) {
860 blosc2_schunk_free(schunk);
861 GRN_FREE(data->decompressed_value);
862 data->decompressed_value = NULL;
863 data->decompressed_value_len = 0;
864 return ctx->rc;
865 }
866 blosc2_schunk_free(schunk);
867 if (destination_len != 0) {
868 ERR(GRN_BLOSC_ERROR,
869 "%s decompressed size isn't match: expected:%u actual:%d",
870 tag,
871 data->decompressed_value_len,
872 destination_len);
873 GRN_FREE(data->decompressed_value);
874 data->decompressed_value = NULL;
875 data->decompressed_value_len = 0;
876 return ctx->rc;
877 }
878
879 return ctx->rc;
880 }
881 #endif
882
883 #ifdef GRN_WITH_OPENZL
884 static inline ZL_GraphID
885 openzl_build_float32_graph_id(ZL_Compressor *zl_compressor)
886 {
887 /*
888 ZL_NODE_FLOAT32_DECONSTRUCT outputs:
889 * fixed-size-fields stream containing sign and fraction bits
890 * serial stream containing exponent bits
891 See also:
892 https://github.com/facebook/openzl/blob/v0.1.0/include/openzl/codecs/zl_float_deconstruct.h#L12-L18
893 */
894 const ZL_GraphID successor_graphs[] = {ZL_GRAPH_STORE, ZL_GRAPH_FSE};
895 /*
896 n_successor_graphs must be equal to the number of
897 ZL_NODE_FLOAT32_DECONSTRUCT output items defined in the API documentation
898 for ZL_Compressor_buildStaticGraph().
899
900 See also:
901 https://openzl.org/api/c/compressor/#ZL_Compressor_buildStaticGraph
902 */
903 const size_t n_successor_graphs =
904 sizeof(successor_graphs) / sizeof(ZL_GraphID);
905 ZL_RESULT_OF(ZL_GraphID)
906 result = ZL_Compressor_buildStaticGraph(zl_compressor,
907 ZL_NODE_FLOAT32_DECONSTRUCT,
908 successor_graphs,
909 n_successor_graphs,
910 NULL);
911 if (ZL_RES_isError(result)) {
912 return ZL_GRAPH_ILLEGAL;
913 }
914 return ZL_RES_value(result);
915 }
916
917 static inline bool
918 openzl_is_plain_vector_column(grn_compress_data *data)
919 {
920 bool is_vector_column = ((data->body_column_flags &
921 GRN_OBJ_COLUMN_TYPE_MASK) == GRN_OBJ_COLUMN_VECTOR);
922 bool is_with_weight = ((data->body_column_flags & GRN_OBJ_WITH_WEIGHT) != 0);
923 return is_vector_column && !is_with_weight;
924 }
925
926 static inline ZL_GraphID
927 openzl_build_graph_id(ZL_Compressor *zl_compressor, grn_compress_data *data)
928 {
929 if (openzl_is_plain_vector_column(data)) {
930 switch (data->body_range) {
931 case GRN_DB_FLOAT32:
932 return openzl_build_float32_graph_id(zl_compressor);
933 default:
934 break;
935 }
936 }
937 return ZL_GRAPH_ZSTD;
938 }
939
940 static inline void
941 openzl_compress(grn_ctx *ctx,
942 const char *tag,
943 grn_compress_data *data,
944 ZL_CCtx *zl_cctx,
945 ZL_Compressor *zl_compressor,
946 void *zl_value,
947 const size_t zl_value_len_max,
948 const ZL_TypedRef *inputs[],
949 size_t n_inputs)
950 {
951 ZL_Report zl_report = ZL_CCtx_refCompressor(zl_cctx, zl_compressor);
952 if (ZL_isError(zl_report)) {
953 ERR(GRN_OPENZL_ERROR,
954 "%s failed to reference compressor: %s",
955 tag,
956 ZL_ErrorCode_toString(ZL_errorCode(zl_report)));
957 return;
958 }
959 zl_report = ZL_CCtx_compressMultiTypedRef(zl_cctx,
960 zl_value,
961 zl_value_len_max,
962 inputs,
963 n_inputs);
964 if (ZL_isError(zl_report)) {
965 ERR(GRN_OPENZL_ERROR,
966 "%s failed to compress data: %s",
967 tag,
968 ZL_ErrorCode_toString(ZL_errorCode(zl_report)));
969 return;
970 }
971 size_t zl_compressed_size = ZL_validResult(zl_report);
972 data->compressed_value_len = COMPRESSED_VALUE_LEN(zl_compressed_size);
973 }
974
975 static inline ZL_TypedRef *
976 openzl_create_type_ref(grn_ctx *ctx, const char *tag, grn_compress_data *data)
977 {
978 ZL_TypedRef *typed_ref = NULL;
979 if (openzl_is_plain_vector_column(data)) {
980 switch (data->body_range) {
981 case GRN_DB_FLOAT32:
982 typed_ref = ZL_TypedRef_createNumeric(data->body,
983 data->body_element_size,
984 data->body_n_elements);
985 break;
986 default:
987 typed_ref = ZL_TypedRef_createSerial(data->body, data->body_len);
988 break;
989 }
990 } else {
991 typed_ref = ZL_TypedRef_createSerial(data->body, data->body_len);
992 }
993
994 if (!typed_ref) {
995 ERR(GRN_OPENZL_ERROR,
996 "%s failed to create OpenZL input type reference",
997 tag);
998 }
999 return typed_ref;
1000 }
1001
1002 static inline void
1003 openzl_compress_only_body(grn_ctx *ctx,
1004 const char *tag,
1005 grn_compress_data *data,
1006 ZL_CCtx *zl_cctx,
1007 ZL_Compressor *zl_compressor,
1008 void *zl_value,
1009 const size_t zl_value_len_max)
1010 {
1011 ZL_GraphID zl_graph_id = openzl_build_graph_id(zl_compressor, data);
1012 if (!ZL_GraphID_isValid(zl_graph_id)) {
1013 ERR(GRN_OPENZL_ERROR,
1014 "%s Invalid API parameter passing for input data type: %s(%u)",
1015 tag,
1016 grn_type_id_to_string_builtin(ctx, data->body_range),
1017 data->body_range);
1018 return;
1019 }
1020
1021 ZL_TypedRef *typed_ref = openzl_create_type_ref(ctx, tag, data);
1022 if (!typed_ref) {
1023 return;
1024 }
1025
1026 ZL_Report zl_report =
1027 ZL_Compressor_selectStartingGraphID(zl_compressor, zl_graph_id);
1028 if (ZL_isError(zl_report)) {
1029 ERR(GRN_OPENZL_ERROR,
1030 "%s failed to select starting graph ID: %s",
1031 tag,
1032 ZL_ErrorCode_toString(ZL_errorCode(zl_report)));
1033 ZL_TypedRef_free(typed_ref);
1034 return;
1035 }
1036 /*
1037 * "ZL_CCtx_compressMultiTypedRef()" requires a value of type "const
1038 * ZL_TypedRef *[]" as its fourth argument.
1039 * Ref: https://openzl.org/api/c/compress/#ZL_CCtx_compressMultiTypedRef
1040 * Therefore, we prepare a "const ZL_TypedRef *[]" value here and pass it to
1041 * "openzl_compress()".
1042 * "ZL_CCtx_compressMultiTypedRef()" is called inside "openzl_compress()".
1043 *
1044 * Since the OpenZL tests uses the same approach, we are following it here as
1045 * well.
1046 * (Ref:
1047 * https://github.com/facebook/openzl/blob/dev/tests/round_trip/test_n_to_n.cpp#L89-L109)
1048 */
1049 const ZL_TypedRef *inputs[] = {typed_ref};
1050 openzl_compress(ctx,
1051 tag,
1052 data,
1053 zl_cctx,
1054 zl_compressor,
1055 zl_value,
1056 zl_value_len_max,
1057 inputs,
1058 1);
1059 ZL_TypedRef_free(typed_ref);
1060 }
1061
1062 static inline grn_rc
1063 grn_compressor_compress_openzl(grn_ctx *ctx, grn_compress_data *data)
1064 {
1065 const char *tag = "[compressor][compress][openzl]";
1066 ZL_CCtx *zl_cctx = ZL_CCtx_create();
1067 if (!zl_cctx) {
1068 ERR(GRN_OPENZL_ERROR, "%s failed to allocate compress context", tag);
1069 return ctx->rc;
1070 }
1071
1072 ZL_Compressor *zl_compressor = ZL_Compressor_create();
1073 if (!zl_compressor) {
1074 ERR(GRN_OPENZL_ERROR, "%s failed to allocate compressor", tag);
1075 ZL_CCtx_free(zl_cctx);
1076 return ctx->rc;
1077 }
1078
1079 const size_t input_len = data->header_len + data->body_len + data->footer_len;
1080 const size_t zl_value_len_max = ZL_compressBound(input_len);
1081 data->compressed_value = GRN_MALLOC(COMPRESSED_VALUE_LEN(zl_value_len_max));
1082 if (!data->compressed_value) {
1083 char message[GRN_CTX_MSGSIZE];
1084 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
1085 ERR(GRN_OPENZL_ERROR,
1086 "%s failed to allocate compress buffer: %s",
1087 tag,
1088 message);
1089 ZL_Compressor_free(zl_compressor);
1090 ZL_CCtx_free(zl_cctx);
1091 return ctx->rc;
1092 }
1093
1094 void *zl_value = COMPRESSED_VALUE_GET_DATA(data->compressed_value);
1095 COMPRESSED_VALUE_SET_METADATA(data->compressed_value,
1096 COMPRESSED_VALUE_METADATA_PACK(input_len, 0));
1097
1098 ZL_Report zl_report = ZL_Compressor_setParameter(zl_compressor,
1099 ZL_CParam_formatVersion,
1100 ZL_MAX_FORMAT_VERSION);
1101 if (ZL_isError(zl_report)) {
1102 ERR(GRN_OPENZL_ERROR,
1103 "%s failed to set compressor parameter: %s",
1104 tag,
1105 ZL_ErrorCode_toString(ZL_errorCode(zl_report)));
1106 GRN_FREE(data->compressed_value);
1107 data->compressed_value = NULL;
1108 data->compressed_value_len = 0;
1109 ZL_Compressor_free(zl_compressor);
1110 ZL_CCtx_free(zl_cctx);
1111 return ctx->rc;
1112 }
1113
1114 if (data->header_len == 0 && data->footer_len == 0) {
1115 openzl_compress_only_body(ctx,
1116 tag,
1117 data,
1118 zl_cctx,
1119 zl_compressor,
1120 zl_value,
1121 zl_value_len_max);
1122 if (ctx->rc != GRN_SUCCESS) {
1123 GRN_FREE(data->compressed_value);
1124 data->compressed_value = NULL;
1125 data->compressed_value_len = 0;
1126 }
1127 ZL_Compressor_free(zl_compressor);
1128 ZL_CCtx_free(zl_cctx);
1129 } else {
1130 /*
1131 * We don't implement compression of header, body, and footer yet.
1132 * So, if a header or footer exists, we don't compress data currently.
1133 */
1134 ERR(GRN_FUNCTION_NOT_IMPLEMENTED,
1135 "%s compress of vector columns is not implemented yet",
1136 tag);
1137 GRN_FREE(data->compressed_value);
1138 data->compressed_value = NULL;
1139 data->compressed_value_len = 0;
1140 ZL_Compressor_free(zl_compressor);
1141 ZL_CCtx_free(zl_cctx);
1142 }
1143 return ctx->rc;
1144 }
1145
1146 static inline grn_rc
1147 grn_compressor_decompress_openzl(grn_ctx *ctx, grn_decompress_data *data)
1148 {
1149 const char *tag = "[compressor][decompress][openzl]";
1150 data->decompressed_value = GRN_MALLOC(data->decompressed_value_len);
1151 if (!data->decompressed_value) {
1152 char message[GRN_CTX_MSGSIZE];
1153 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
1154 ERR(GRN_OPENZL_ERROR, "%s failed to allocate buffer: %s", tag, message);
1155 data->decompressed_value_len = 0;
1156 return ctx->rc;
1157 }
1158
1159 ZL_TypedBuffer *outputs[1] = {ZL_TypedBuffer_create()};
1160 if (!outputs[0]) {
1161 ERR(GRN_OPENZL_ERROR, "%s failed to allocate output buffer", tag);
1162 GRN_FREE(data->decompressed_value);
1163 data->decompressed_value = NULL;
1164 data->decompressed_value_len = 0;
1165 return ctx->rc;
1166 }
1167
1168 ZL_DCtx *zl_dctx = ZL_DCtx_create();
1169 if (!zl_dctx) {
1170 ERR(GRN_OPENZL_ERROR, "%s failed to allocate decompress context", tag);
1171 ZL_TypedBuffer_free(outputs[0]);
1172 GRN_FREE(data->decompressed_value);
1173 data->decompressed_value = NULL;
1174 data->decompressed_value_len = 0;
1175 return ctx->rc;
1176 }
1177 ZL_Report zl_decompress =
1178 ZL_DCtx_decompressMultiTBuffer(zl_dctx,
1179 outputs,
1180 1,
1181 data->compressed_value,
1182 data->compressed_value_len);
1183 if (ZL_isError(zl_decompress)) {
1184 ERR(GRN_OPENZL_ERROR,
1185 "%s failed to decompress: %s",
1186 tag,
1187 ZL_ErrorCode_toString(ZL_errorCode(zl_decompress)));
1188 ZL_DCtx_free(zl_dctx);
1189 ZL_TypedBuffer_free(outputs[0]);
1190 GRN_FREE(data->decompressed_value);
1191 data->decompressed_value = NULL;
1192 data->decompressed_value_len = 0;
1193 return ctx->rc;
1194 }
1195
1196 size_t decompressed_size = ZL_TypedBuffer_byteSize(outputs[0]);
1197 if (data->decompressed_value_len != (uint32_t)decompressed_size) {
1198 ERR(GRN_OPENZL_ERROR,
1199 "%s unexpected decompressed size: expected %" GRN_FMT_INT32U " bytes"
1200 " but got %" GRN_FMT_SIZE " bytes",
1201 tag,
1202 data->decompressed_value_len,
1203 decompressed_size);
1204 ZL_DCtx_free(zl_dctx);
1205 ZL_TypedBuffer_free(outputs[0]);
1206 GRN_FREE(data->decompressed_value);
1207 data->decompressed_value = NULL;
1208 data->decompressed_value_len = 0;
1209 return ctx->rc;
1210 }
1211
1212 memcpy(data->decompressed_value,
1213 ZL_TypedBuffer_rPtr(outputs[0]),
1214 data->decompressed_value_len);
1215
1216 ZL_DCtx_free(zl_dctx);
1217 ZL_TypedBuffer_free(outputs[0]);
1218 return ctx->rc;
1219 }
1220 #endif /* GRN_WITH_OPENZL */
1221
1222 grn_rc
1223 grn_compressor_compress(grn_ctx *ctx, grn_compress_data *data)
1224 {
1225 size_t input_len = data->header_len + data->body_len + data->footer_len;
1226 if (input_len < COMPRESS_THRESHOLD_BYTE) {
1227 data->packed_value = GRN_MALLOC(COMPRESSED_VALUE_LEN(input_len));
1228 if (!data->packed_value) {
1229 char message[GRN_CTX_MSGSIZE];
1230 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
1231 grn_rc rc;
1232 switch (data->type) {
1233 case GRN_COMPRESSION_TYPE_ZLIB:
1234 rc = GRN_ZLIB_ERROR;
1235 break;
1236 case GRN_COMPRESSION_TYPE_LZ4:
1237 rc = GRN_LZ4_ERROR;
1238 break;
1239 case GRN_COMPRESSION_TYPE_ZSTD:
1240 rc = GRN_ZSTD_ERROR;
1241 break;
1242 case GRN_COMPRESSION_TYPE_BLOSC:
1243 rc = GRN_BLOSC_ERROR;
1244 break;
1245 case GRN_COMPRESSION_TYPE_OPENZL:
1246 rc = GRN_OPENZL_ERROR;
1247 break;
1248 default:
1249 rc = ctx->rc == GRN_SUCCESS ? GRN_UNKNOWN_ERROR : ctx->rc;
1250 break;
1251 }
1252 ERR(rc,
1253 "[compressor][compress] failed to allocate packed buffer: %s",
1254 message);
1255 return ctx->rc;
1256 }
1257
1258 grn_rc rc = grn_compressor_pack(ctx, data);
1259 if (rc != GRN_SUCCESS) {
1260 GRN_FREE(data->packed_value);
1261 data->packed_value = NULL;
1262 data->packed_value_len = 0;
1263 }
1264 return rc;
1265 }
1266
1267 switch (data->type) {
1268 #ifdef GRN_WITH_ZLIB
1269 case GRN_COMPRESSION_TYPE_ZLIB:
1270 return grn_compressor_compress_zlib(ctx, data);
1271 #endif /* GRN_WITH_ZLIB */
1272 #ifdef GRN_WITH_LZ4
1273 case GRN_COMPRESSION_TYPE_LZ4:
1274 return grn_compressor_compress_lz4(ctx, data);
1275 #endif /* GRN_WITH_LZ4 */
1276 #ifdef GRN_WITH_ZSTD
1277 case GRN_COMPRESSION_TYPE_ZSTD:
1278 return grn_compressor_compress_zstd(ctx, data);
1279 #endif /* GRN_WITH_ZSTD */
1280 #ifdef GRN_WITH_BLOSC
1281 case GRN_COMPRESSION_TYPE_BLOSC:
1282 return grn_compressor_compress_blosc(ctx, data);
1283 #endif
1284 #ifdef GRN_WITH_OPENZL
1285 case GRN_COMPRESSION_TYPE_OPENZL:
1286 return grn_compressor_compress_openzl(ctx, data);
1287 #endif /* GRN_WITH_OPENZL */
1288 default:
1289 ERR(GRN_FUNCTION_NOT_IMPLEMENTED,
1290 "[compressor][compress] unsupported type: %d",
1291 data->type);
1292 return ctx->rc;
1293 }
1294 }
1295
1296 grn_rc
1297 grn_compressor_decompress(grn_ctx *ctx, grn_decompress_data *data)
1298 {
1299 grn_rc rc = grn_compressor_unpack(ctx, data);
1300 if (rc != GRN_SUCCESS) {
1301 return rc;
1302 }
1303 if (data->unpacked_value) {
1304 return ctx->rc;
1305 }
1306
1307 switch (data->type) {
1308 #ifdef GRN_WITH_ZLIB
1309 case GRN_COMPRESSION_TYPE_ZLIB:
1310 return grn_compressor_decompress_zlib(ctx, data);
1311 #endif /* GRN_WITH_ZLIB */
1312 #ifdef GRN_WITH_LZ4
1313 case GRN_COMPRESSION_TYPE_LZ4:
1314 return grn_compressor_decompress_lz4(ctx, data);
1315 #endif /* GRN_WITH_LZ4 */
1316 #ifdef GRN_WITH_ZSTD
1317 case GRN_COMPRESSION_TYPE_ZSTD:
1318 return grn_compressor_decompress_zstd(ctx, data);
1319 #endif /* GRN_WITH_ZSTD */
1320 #ifdef GRN_WITH_BLOSC
1321 case GRN_COMPRESSION_TYPE_BLOSC:
1322 return grn_compressor_decompress_blosc(ctx, data);
1323 #endif
1324 #ifdef GRN_WITH_OPENZL
1325 case GRN_COMPRESSION_TYPE_OPENZL:
1326 return grn_compressor_decompress_openzl(ctx, data);
1327 #endif /* GRN_WITH_OPENZL */
1328 default:
1329 ERR(GRN_FUNCTION_NOT_IMPLEMENTED,
1330 "[compressor][decompress] unsupported type: %d",
1331 data->type);
1332 return ctx->rc;
1333 }
1334 }