Compress raw bytes with zstd, hash, and write as a section. Used for GRAPH and SEMANTIC sections where the caller provides pre-serialized (but uncompressed) postcard bytes.
(
&mut self,
section_type: SectionType,
uncompressed: &[u8],
)
| 131 | /// Used for GRAPH and SEMANTIC sections where the caller provides |
| 132 | /// pre-serialized (but uncompressed) postcard bytes. |
| 133 | pub(super) fn write_raw_section( |
| 134 | &mut self, |
| 135 | section_type: SectionType, |
| 136 | uncompressed: &[u8], |
| 137 | ) -> FormatResult<()> { |
| 138 | // Compress with zstd |
| 139 | let compressed = zstd::encode_all(uncompressed, self.options.compression_level()) |
| 140 | .map_err(|e| FormatError::Compress(e.to_string()))?; |
| 141 | |
| 142 | // Write section header |
| 143 | let section_header = SectionHeader::new(section_type, compressed.len() as u32); |
| 144 | let header_bytes = section_header.to_bytes(); |
| 145 | self.writer.write_all(&header_bytes)?; |
| 146 | |
| 147 | // Write compressed payload |
| 148 | self.writer.write_all(&compressed)?; |
| 149 | |
| 150 | // Feed through hasher |
| 151 | if section_type.is_hashed() { |
| 152 | self.hasher.update(&header_bytes); |
| 153 | self.hasher.update(&compressed); |
| 154 | } |
| 155 | |
| 156 | // Update stats |
| 157 | self.stats.sections_written += 1; |
| 158 | self.stats.total_uncompressed += uncompressed.len() as u64; |
| 159 | self.stats.total_compressed += compressed.len() as u64; |
| 160 | self.stats.total_bytes_written += (SectionHeader::SIZE + compressed.len()) as u64; |
| 161 | |
| 162 | self.last_section_ordering = Some(section_type.ordering()); |
| 163 | Ok(()) |
| 164 | } |
| 165 | } |