From 40374f77e7d0d97bea54ec58ac346a7f0062b6ba Mon Sep 17 00:00:00 2001 From: trivernis Date: Fri, 13 Mar 2020 16:31:16 +0100 Subject: [PATCH] Change compression level to 1 --- src/chunks.rs | 2 +- src/io.rs | 2 +- src/lib.rs | 50 +++++++++++++++++++++++++++++++------------------- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/chunks.rs b/src/chunks.rs index d5adaea..473e1df 100644 --- a/src/chunks.rs +++ b/src/chunks.rs @@ -146,7 +146,7 @@ impl GenericChunk { /// Compresses the data of the chunk using lzma with a level of 6 pub fn compress(&mut self) -> Result<(), Error> { let data = self.data.as_slice(); - let mut compressor = XzEncoder::new(data, 6); + let mut compressor = XzEncoder::new(data, 1); let mut compressed: Vec = Vec::new(); compressor.read_to_end(&mut compressed)?; self.length = compressed.len() as u32; diff --git a/src/io.rs b/src/io.rs index 1bc1108..d3d78c3 100644 --- a/src/io.rs +++ b/src/io.rs @@ -40,7 +40,7 @@ impl BDFWriter { } /// Adds an entry to the hash lookup table - /// If the lookup table has already been written to the file, an error ris returned + /// If the lookup table has already been written to the file, an error is returned pub fn add_lookup_entry(&mut self, mut entry: HashEntry) -> Result { if self.head_written { return Err(Error::new( diff --git a/src/lib.rs b/src/lib.rs index c12a4ba..cecff83 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,9 @@ #[cfg(test)] mod tests { + use super::io::BDFWriter; use std::io::{BufWriter, Error, BufReader}; - use std::fs::File; + use std::fs::{File, remove_file}; use crate::chunks::{HashEntry, DataEntry}; use crate::io::BDFReader; @@ -11,9 +12,7 @@ mod tests { #[test] fn it_writes_uncompressed() -> Result<(), Error> { - let file = File::create("tmp.bdf")?; - let f = BufWriter::new(file); - let mut writer = BDFWriter::new(f, 2, false); + let mut writer = new_writer("tmp1.bdf", 2, false)?; writer.add_lookup_entry(HashEntry::new(BAR.to_string(), 5))?; writer.add_lookup_entry(HashEntry::new(FOO.to_string(), 4))?; @@ -30,15 +29,14 @@ mod tests { writer.flush()?; writer.flush_writer()?; + remove_file("tmp1.bdf")?; Ok(()) } #[test] fn it_writes_compressed() -> Result<(), Error> { - let file = File::create("tmp-compressed.bdf")?; - let f = BufWriter::new(file); - let mut writer = BDFWriter::new(f, 2, true); + let mut writer = new_writer("tmp2.bdf", 2, true)?; writer.add_lookup_entry(HashEntry::new(FOO.to_string(), 4))?; writer.add_lookup_entry(HashEntry::new(BAR.to_string(), 5))?; @@ -56,43 +54,43 @@ mod tests { writer.flush()?; writer.flush_writer()?; + remove_file("tmp2.bdf")?; + Ok(()) } #[test] fn it_reads() -> Result<(), Error> { - create_simple_file(false)?; - let file = File::open("tmp2.bdf")?; - let f = BufReader::new(file); - let mut reader = BDFReader::new(f); + create_simple_file("tmp3.bdf", false)?; + let mut reader = new_reader("tmp3.bdf")?; reader.read_metadata()?; let lookup_table = &reader.read_lookup_table()?.clone(); let mut next_chunk = reader.next_chunk()?; let data_entries = next_chunk.data_entries(lookup_table)?; assert_eq!(data_entries[0].plain, "lol".to_string()); + remove_file("tmp3.bdf")?; + Ok(()) } #[test] fn it_reads_compressed() -> Result<(), Error> { - create_simple_file(true)?; - let file = File::open("tmp2.bdf")?; - let f = BufReader::new(file); - let mut reader = BDFReader::new(f); + create_simple_file("tmp4.bdf", true)?; + let mut reader = new_reader("tmp4.bdf")?; reader.read_metadata()?; let lookup_table = &reader.read_lookup_table()?.clone(); let mut next_chunk = reader.next_chunk()?; let data_entries = next_chunk.data_entries(lookup_table)?; assert_eq!(data_entries[0].plain, "lol".to_string()); + remove_file("tmp4.bdf")?; + Ok(()) } - fn create_simple_file(compressed: bool) -> Result<(), Error>{ - let file = File::create("tmp2.bdf")?; - let f = BufWriter::new(file); - let mut writer = BDFWriter::new(f, 1, compressed); + fn create_simple_file(name: &str, compressed: bool) -> Result<(), Error>{ + let mut writer = new_writer(name, 1, compressed)?; writer.add_lookup_entry(HashEntry::new(FOO.to_string(), 4))?; let mut entry_1 = DataEntry::new("lol".to_string()); @@ -104,6 +102,20 @@ mod tests { Ok(()) } + + fn new_reader(file_name: &str) -> Result { + let file = File::open(file_name)?; + let f = BufReader::new(file); + + Ok(BDFReader::new(f)) + } + + fn new_writer(file_name: &str, entries: u64, compress: bool) -> Result { + let file = File::create(file_name)?; + let f = BufWriter::new(file); + + Ok(BDFWriter::new(f, entries, compress)) + } } pub mod chunks;