@@ -20,6 +20,49 @@ namespace sparrow_ipc
2020// }
2121// }
2222
23+ std::vector<std::uint8_t > lz4_compress (std::span<const std::uint8_t > data)
24+ {
25+ const std::int64_t uncompressed_size = data.size ();
26+ const size_t max_compressed_size = LZ4F_compressFrameBound (uncompressed_size, nullptr );
27+ std::vector<std::uint8_t > compressed_data (max_compressed_size);
28+ const size_t compressed_size = LZ4F_compressFrame (compressed_data.data (), max_compressed_size, data.data (), uncompressed_size, nullptr );
29+ if (LZ4F_isError (compressed_size))
30+ {
31+ throw std::runtime_error (" Failed to compress data with LZ4 frame format" );
32+ }
33+ compressed_data.resize (compressed_size);
34+ return compressed_data;
35+ }
36+
37+ std::vector<std::uint8_t > lz4_decompress (std::span<const std::uint8_t > data)
38+ {
39+ if (data.size () < 8 )
40+ {
41+ throw std::runtime_error (" Invalid compressed data: missing decompressed size" );
42+ }
43+ const std::int64_t decompressed_size = *reinterpret_cast <const std::int64_t *>(data.data ());
44+ const auto compressed_data = data.subspan (8 );
45+
46+ if (decompressed_size == -1 )
47+ {
48+ // TODO think of avoiding copy here
49+ return {compressed_data.begin (), compressed_data.end ()};
50+ }
51+
52+ std::vector<std::uint8_t > decompressed_data (decompressed_size);
53+ LZ4F_dctx* dctx = nullptr ;
54+ LZ4F_createDecompressionContext (&dctx, LZ4F_VERSION);
55+ size_t compressed_size_in_out = compressed_data.size ();
56+ size_t decompressed_size_in_out = decompressed_size;
57+ size_t result = LZ4F_decompress (dctx, decompressed_data.data (), &decompressed_size_in_out, compressed_data.data (), &compressed_size_in_out, nullptr );
58+ if (LZ4F_isError (result) || (decompressed_size_in_out != (size_t )decompressed_size))
59+ {
60+ throw std::runtime_error (" Failed to decompress data with LZ4 frame format" );
61+ }
62+ LZ4F_freeDecompressionContext (dctx);
63+ return decompressed_data;
64+ }
65+
2366 std::vector<std::uint8_t > compress (const org::apache::arrow::flatbuf::CompressionType compression_type, std::span<const std::uint8_t > data)
2467 {
2568 if (data.empty ())
@@ -30,18 +73,14 @@ namespace sparrow_ipc
3073 {
3174 case org::apache::arrow::flatbuf::CompressionType::LZ4_FRAME:
3275 {
33- const std::int64_t uncompressed_size = data.size ();
34- const size_t max_compressed_size = LZ4F_compressFrameBound (uncompressed_size, nullptr );
35- std::vector<std::uint8_t > compressed_data (max_compressed_size);
36- const size_t compressed_size = LZ4F_compressFrame (compressed_data.data (), max_compressed_size, data.data (), uncompressed_size, nullptr );
37- if (LZ4F_isError (compressed_size))
38- {
39- throw std::runtime_error (" Failed to compress data with LZ4 frame format" );
40- }
41- compressed_data.resize (compressed_size);
42- return compressed_data;
76+ return lz4_compress (data);
77+ }
78+ case org::apache::arrow::flatbuf::CompressionType::ZSTD:
79+ {
80+ throw std::runtime_error (" Compression using zstd is not supported yet." );
4381 }
4482 default :
83+ // TODO think of avoiding copy here
4584 return {data.begin (), data.end ()};
4685 }
4786 }
@@ -56,32 +95,14 @@ namespace sparrow_ipc
5695 {
5796 case org::apache::arrow::flatbuf::CompressionType::LZ4_FRAME:
5897 {
59- if (data.size () < 8 )
60- {
61- throw std::runtime_error (" Invalid compressed data: missing decompressed size" );
62- }
63- const std::int64_t decompressed_size = *reinterpret_cast <const std::int64_t *>(data.data ());
64- const auto compressed_data = data.subspan (8 );
65-
66- if (decompressed_size == -1 )
67- {
68- return {compressed_data.begin (), compressed_data.end ()};
69- }
70-
71- std::vector<std::uint8_t > decompressed_data (decompressed_size);
72- LZ4F_dctx* dctx = nullptr ;
73- LZ4F_createDecompressionContext (&dctx, LZ4F_VERSION);
74- size_t compressed_size_in_out = compressed_data.size ();
75- size_t decompressed_size_in_out = decompressed_size;
76- size_t result = LZ4F_decompress (dctx, decompressed_data.data (), &decompressed_size_in_out, compressed_data.data (), &compressed_size_in_out, nullptr );
77- if (LZ4F_isError (result))
78- {
79- throw std::runtime_error (" Failed to decompress data with LZ4 frame format" );
80- }
81- LZ4F_freeDecompressionContext (dctx);
82- return decompressed_data;
98+ return lz4_decompress (data);
99+ }
100+ case org::apache::arrow::flatbuf::CompressionType::ZSTD:
101+ {
102+ throw std::runtime_error (" Decompression using zstd is not supported yet." );
83103 }
84104 default :
105+ // TODO think of avoiding copy here
85106 return {data.begin (), data.end ()};
86107 }
87108 }
0 commit comments