Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 35 additions & 16 deletions src/core/block_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t>(CompressionType::kNone)};
}


namespace
{
Expand Down Expand Up @@ -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<char[]> 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<char[]>(static_cast<size_t>(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<uint64_t>(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<uint64_t>(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);
Expand Down
20 changes: 20 additions & 0 deletions src/core/block_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t> Hash(const std::string& file_path);
Expand Down
23 changes: 23 additions & 0 deletions test/cases/test_block_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t>(CompressionType::kLZ4);
constexpr uint64_t kNone = static_cast<uint64_t>(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)
Expand Down
Loading