feat: add interface to retrieve metadata from block decompressor

This commit is contained in:
Marcus Holland-Moritz 2023-12-12 22:20:05 +01:00
parent ce3972f0b4
commit a2e44d13d5
7 changed files with 29 additions and 0 deletions

View File

@ -25,6 +25,7 @@
#include <cstdio> #include <cstdio>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <optional>
#include <span> #include <span>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
@ -128,6 +129,8 @@ class block_decompressor {
compression_type type() const { return impl_->type(); } compression_type type() const { return impl_->type(); }
std::optional<std::string> metadata() const { return impl_->metadata(); }
static std::vector<uint8_t> static std::vector<uint8_t>
decompress(compression_type type, const uint8_t* data, size_t size) { decompress(compression_type type, const uint8_t* data, size_t size) {
std::vector<uint8_t> target; std::vector<uint8_t> target;
@ -142,6 +145,7 @@ class block_decompressor {
virtual bool decompress_frame(size_t frame_size) = 0; virtual bool decompress_frame(size_t frame_size) = 0;
virtual size_t uncompressed_size() const = 0; virtual size_t uncompressed_size() const = 0;
virtual std::optional<std::string> metadata() const = 0;
virtual compression_type type() const = 0; virtual compression_type type() const = 0;
}; };

View File

@ -127,6 +127,8 @@ class brotli_block_decompressor final : public block_decompressor::impl {
compression_type type() const override { return compression_type::BROTLI; } compression_type type() const override { return compression_type::BROTLI; }
std::optional<std::string> metadata() const override { return std::nullopt; }
bool decompress_frame(size_t frame_size) override { bool decompress_frame(size_t frame_size) override {
size_t pos = decompressed_.size(); size_t pos = decompressed_.size();

View File

@ -425,6 +425,21 @@ class flac_block_decompressor final : public block_decompressor::impl {
compression_type type() const override { return compression_type::FLAC; } compression_type type() const override { return compression_type::FLAC; }
std::optional<std::string> metadata() const override {
auto const flags = header_.flags().value();
folly::dynamic meta = folly::dynamic::object
// clang-format off
("endianness", flags & kFlagBigEndian ? "big" : "little")
("signedness", flags & kFlagSigned ? "signed" : "unsigned")
("padding", flags & kFlagLsbPadding ? "lsb" : "msb")
("bytes_per_sample", (flags & kBytesPerSampleMask) + 1)
("bits_per_sample", header_.bits_per_sample().value())
("number_of_channels", header_.num_channels().value())
; // clang-format on
return folly::toJson(meta);
}
bool decompress_frame(size_t frame_size) override { bool decompress_frame(size_t frame_size) override {
size_t pos = decompressed_.size(); size_t pos = decompressed_.size();

View File

@ -125,6 +125,8 @@ class lz4_block_decompressor final : public block_decompressor::impl {
compression_type type() const override { return compression_type::LZ4; } compression_type type() const override { return compression_type::LZ4; }
std::optional<std::string> metadata() const override { return std::nullopt; }
bool decompress_frame(size_t) override { bool decompress_frame(size_t) override {
if (!error_.empty()) { if (!error_.empty()) {
DWARFS_THROW(runtime_error, error_); DWARFS_THROW(runtime_error, error_);

View File

@ -224,6 +224,8 @@ class lzma_block_decompressor final : public block_decompressor::impl {
compression_type type() const override { return compression_type::LZMA; } compression_type type() const override { return compression_type::LZMA; }
std::optional<std::string> metadata() const override { return std::nullopt; }
bool decompress_frame(size_t frame_size) override { bool decompress_frame(size_t frame_size) override {
if (!error_.empty()) { if (!error_.empty()) {
DWARFS_THROW(runtime_error, error_); DWARFS_THROW(runtime_error, error_);

View File

@ -81,6 +81,8 @@ class null_block_decompressor final : public block_decompressor::impl {
compression_type type() const override { return compression_type::NONE; } compression_type type() const override { return compression_type::NONE; }
std::optional<std::string> metadata() const override { return std::nullopt; }
bool decompress_frame(size_t frame_size) override { bool decompress_frame(size_t frame_size) override {
if (decompressed_.size() + frame_size > uncompressed_size_) { if (decompressed_.size() + frame_size > uncompressed_size_) {
frame_size = uncompressed_size_ - decompressed_.size(); frame_size = uncompressed_size_ - decompressed_.size();

View File

@ -145,6 +145,8 @@ class zstd_block_decompressor final : public block_decompressor::impl {
compression_type type() const override { return compression_type::ZSTD; } compression_type type() const override { return compression_type::ZSTD; }
std::optional<std::string> metadata() const override { return std::nullopt; }
bool decompress_frame(size_t /*frame_size*/) override { bool decompress_frame(size_t /*frame_size*/) override {
if (!error_.empty()) { if (!error_.empty()) {
DWARFS_THROW(runtime_error, error_); DWARFS_THROW(runtime_error, error_);