Change compression level to 1

master
trivernis 5 years ago
parent 7e699de239
commit 40374f77e7

@ -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<u8> = Vec::new();
compressor.read_to_end(&mut compressed)?;
self.length = compressed.len() as u32;

@ -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<u32, Error> {
if self.head_written {
return Err(Error::new(

@ -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<BDFReader, Error> {
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<BDFWriter, Error> {
let file = File::create(file_name)?;
let f = BufWriter::new(file);
Ok(BDFWriter::new(f, entries, compress))
}
}
pub mod chunks;

Loading…
Cancel
Save