From d080ededfd321f6f6f41b093b9ef1327f249e46b Mon Sep 17 00:00:00 2001 From: blocksmith314 Date: Mon, 23 Nov 2026 22:18:07 +0800 Subject: [PATCH] test: cover the metadata compression fallback decision Factor the compressed-vs-raw metadata choice into PlanMetadataStorage() and unit-test the compressed, zero-compressed and no-shrink cases, including the header size/type recorded for the fallback path. Refs #96 --- src/core/block_store.cpp | 51 ++++++++++++++++++++++----------- src/core/block_store.h | 20 +++++++++++++ test/cases/test_block_store.cpp | 23 +++++++++++++++ 3 files changed, 78 insertions(+), 16 deletions(-) diff --git a/src/core/block_store.cpp b/src/core/block_store.cpp index 266e3a1..6766bf0 100644 --- a/src/core/block_store.cpp +++ b/src/core/block_store.cpp @@ -67,6 +67,22 @@ namespace ctar return XXH64(buf.data(), buf.size(), kHashSeed); } + MetadataStoragePlan PlanMetadataStorage(const uint64_t raw_size, const uint64_t compressed_size, + const uint64_t compressed_type) + { + if (compressed_size > 0 && compressed_size < raw_size) + { + return {.use_compressed = true, + .stored_size = compressed_size, + .header_size = compressed_size, + .header_type = compressed_type}; + } + return {.use_compressed = false, + .stored_size = raw_size, + .header_size = raw_size, + .header_type = static_cast(CompressionType::kNone)}; + } + namespace { @@ -789,26 +805,29 @@ namespace ctar // zero-filling a std::string of the worst-case size committed ~29 MiB of // metadata to RSS even though the compressed output was only ~7 MiB (#93). std::unique_ptr compressed_meta; - const char* stored_metadata = meta_buffer.data(); - uint64_t stored_metadata_size = meta_buffer.size(); + uint64_t compressed_size = 0; if (compress_metadata) { compressed_meta = std::make_unique_for_overwrite(static_cast(max_metadata_size)); - const uint64_t compressed_size = compress::Compress(metadata_type, meta_buffer.data(), compressed_meta.get(), - raw_meta_size, compression_param); - if (compressed_size > 0 && compressed_size < raw_meta_size) - { - stored_metadata = compressed_meta.get(); - stored_metadata_size = compressed_size; - metadata_compressed_size_ = compressed_size; - metadata_compressed_type_ = static_cast(metadata_type); - } - else - { - // Compression did not help; write the uncompressed bytes. - compressed_meta.reset(); - } + compressed_size = + compress::Compress(metadata_type, meta_buffer.data(), compressed_meta.get(), raw_meta_size, + compression_param); + } + const MetadataStoragePlan metadata_plan = + PlanMetadataStorage(raw_meta_size, compressed_size, static_cast(metadata_type)); + const char* stored_metadata = meta_buffer.data(); + if (metadata_plan.use_compressed) + { + stored_metadata = compressed_meta.get(); + } + else + { + // Compression was not used or did not help; write the raw bytes. + compressed_meta.reset(); } + const uint64_t stored_metadata_size = metadata_plan.stored_size; + metadata_compressed_size_ = metadata_plan.header_size; + metadata_compressed_type_ = metadata_plan.header_type; const uint64_t metadata_offset = file_data_offset_ + block_region_size_; const uint64_t pack_file_size = metadata_offset + metadata_compressed_size_ + sizeof(kMagicNumber); diff --git a/src/core/block_store.h b/src/core/block_store.h index 1aa20c0..5efe0aa 100644 --- a/src/core/block_store.h +++ b/src/core/block_store.h @@ -30,6 +30,26 @@ namespace ctar Extract }; + /// @brief Where the serialized metadata is stored and what the header records. + struct MetadataStoragePlan + { + bool use_compressed = false; + uint64_t stored_size = 0; // bytes written to the archive + uint64_t header_size = 0; // metadata_compressed_size + uint64_t header_type = 0; // metadata_compressed_type + }; + + /// @brief Choose between the compressed and raw metadata bytes. + /// + /// Compression is used only when it succeeded and produced a smaller buffer. + /// Otherwise the raw bytes are stored and the header records kNone, so the + /// fallback path is explicit and testable. + /// @param raw_size uncompressed metadata size + /// @param compressed_size result from Compress(); 0 means failure + /// @param compressed_type compression type that was attempted + [[nodiscard]] MetadataStoragePlan PlanMetadataStorage(uint64_t raw_size, uint64_t compressed_size, + uint64_t compressed_type); + /// @brief XXH64 of a file's contents, using the archive hash seed. ResultStatus Hash(const std::string& file_path); diff --git a/test/cases/test_block_store.cpp b/test/cases/test_block_store.cpp index da05a72..29fc176 100644 --- a/test/cases/test_block_store.cpp +++ b/test/cases/test_block_store.cpp @@ -1029,6 +1029,29 @@ namespace ctar fs::remove_all(root); } + TEST(PackMetadata, PlanUsesCompressedOnlyWhenSmaller) + { + constexpr uint64_t kRaw = 4 * 1024 * 1024; + constexpr uint64_t kLz4 = static_cast(CompressionType::kLZ4); + constexpr uint64_t kNone = static_cast(CompressionType::kNone); + + const auto compressed = PlanMetadataStorage(kRaw, kRaw - 1, kLz4); + EXPECT_TRUE(compressed.use_compressed); + EXPECT_EQ(compressed.stored_size, kRaw - 1); + EXPECT_EQ(compressed.header_size, kRaw - 1); + EXPECT_EQ(compressed.header_type, kLz4); + + // Compression failure (0) and no-shrink results fall back to the raw bytes. + for (const uint64_t size : {uint64_t{0}, kRaw, kRaw + 1}) + { + const auto fallback = PlanMetadataStorage(kRaw, size, kLz4); + EXPECT_FALSE(fallback.use_compressed); + EXPECT_EQ(fallback.stored_size, kRaw); + EXPECT_EQ(fallback.header_size, kRaw); + EXPECT_EQ(fallback.header_type, kNone); + } + } + } // namespace ctar int main(int argc, char** argv)