an efficient binary archive format
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

implement add using streaming under the hood

zach a2f0cdbd 1b4a0b5d

+5 -31
+5 -31
src/lib.rs
··· 276 276 } 277 277 278 278 pub fn add(&mut self, name: &str, data: &[u8], compress: Compress) -> io::Result<()> { 279 - let compress = self.should_auto_compress(compress, data.len()); 280 - let (processed, c_type) = if compress { 281 - (Cow::Owned(zstd::encode_all(data, 3)?), Compress::Zstd) 282 - } else { 283 - (Cow::Borrowed(data), Compress::None) 284 - }; 285 - 286 - self.file.seek(SeekFrom::Start(self.data_end))?; 287 - self.file.write_all(&processed)?; 288 - 289 - let offset = self.data_end; 290 - let c_size = processed.len() as u64; 291 - let pad = pad::<8, u64>(c_size); 292 - if pad > 0 { 293 - self.file.write_all(&vec![0u8; pad as usize])?; 294 - } 295 - 296 - self.data_end = offset + c_size + pad; 297 - 298 - let entry = Entry { 299 - offset: offset.to_le_bytes(), 300 - compressed_size: c_size.to_le_bytes(), 301 - uncompressed_size: (data.len() as u64).to_le_bytes(), 302 - compression_type: c_type as u8, 303 - name_len: (name.len() as u16).to_le_bytes(), 304 - ..Default::default() 305 - }; 306 - 307 - self.index.insert(name.to_string(), entry); 279 + let mut stream = self.stream(name, compress)?; 280 + stream.write_all(data)?; 281 + stream.finish()?; 308 282 Ok(()) 309 283 } 310 284 ··· 428 402 let data = 429 403 mmap.get(entry.offset() as usize..(entry.offset() + entry.compressed_size()) as usize)?; 430 404 431 - if entry.compression_type == 1 { 405 + if entry.compression_type == Compress::Zstd as u8 { 432 406 let mut out = Vec::with_capacity(entry.uncompressed_size() as usize); 433 407 zstd::Decoder::new(data).ok()?.read_to_end(&mut out).ok()?; 434 408 Some(Cow::Owned(out)) ··· 450 424 .get(entry.offset() as usize..(entry.offset() + entry.compressed_size()) as usize) 451 425 .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "Invalid mmap offset"))?; 452 426 453 - if entry.compression_type == 1 { 427 + if entry.compression_type == Compress::Zstd as u8 { 454 428 std::io::copy( 455 429 &mut zstd::Decoder::new(data) 456 430 .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?,