Skip to content

Commit

Permalink
Merge pull request #47 from instaclustr/optimizer-optimization
Browse files Browse the repository at this point in the history
Compressors working!
  • Loading branch information
cjrolo authored Oct 16, 2023
2 parents cd09030 + edaeb86 commit 5d810b8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
1 change: 1 addition & 0 deletions brro-compressor/src/compressor/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ impl Constant {
.map(|(k, v)| (k.try_into().unwrap(), *v))
.collect();
}
debug!("Compressed {} elements into {} elements!",data.len(), self.residuals.len()+1);
}

/// Receives a data stream and generates a Constant
Expand Down
3 changes: 2 additions & 1 deletion brro-compressor/src/compressor/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl Noop {
/// "Compress"
pub fn compress(&mut self, data: &[f64]) {
self.data = Noop::optimize(data);
debug!("Compressed {} elements into {} elements!",data.len(), self.data.len());
}

/// Receives a data stream and generates a Noop
Expand All @@ -55,7 +56,7 @@ impl Noop {
}

pub fn noop(data: &[f64]) -> Vec<u8> {
info!("[Compressor] Initializing Noop Compressor");
info!("Initializing Noop Compressor");
let mut c = Noop::new(data.len());
c.compress(data);
c.to_bytes()
Expand Down
17 changes: 10 additions & 7 deletions brro-compressor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,23 @@ fn process_directory(arguments: &Args) {
for (index, data) in files.contents.iter().enumerate() {
let (vec_data, tag) = data;
let compressed_data = compress_data(vec_data, tag, arguments);

let file_name = writer::replace_extension(&files.names[index], "bin");
// BRO extension
let file_name = writer::replace_extension(&files.names[index], "bro");
let new_path = base_dir.join(&file_name);
write_compressed_data_to_path(&compressed_data, &new_path);
}
}

/// Processes a single file.
fn process_single_file(arguments: &Args) {
debug!("Processing single file...");
let (vec, tag) = reader::read_file(&arguments.input).expect("Failed to read file");
let compressed_data = compress_data(&vec, &tag, arguments);

if let Some(filename_osstr) = arguments.input.file_name() {
if let Some(filename_str) = filename_osstr.to_str() {
// BRO extension
let new_filename_string =
writer::replace_extension(&filename_str.to_string(), "bin");
writer::replace_extension(&filename_str.to_string(), "bro");
let new_path = arguments.input.parent().unwrap().join(new_filename_string);
write_compressed_data_to_path(&compressed_data, &new_path);
}
Expand All @@ -62,15 +63,16 @@ fn process_single_file(arguments: &Args) {

/// Compresses the data based on the provided tag and arguments.
fn compress_data(vec: &Vec<f64>, tag: &MetricTag, arguments: &Args) -> Vec<u8> {
debug!("Compressing data!");
let optimizer_results = optimizer::process_data(vec, tag);
let optimizer_results_f: Vec<f64> = optimizer_results.iter().map(|&x| x as f64).collect();
let _optimizer_results_f: Vec<f64> = optimizer_results.iter().map(|&x| x as f64).collect();

let mut cs = CompressedStream::new();
if arguments.constant {
cs.compress_chunk_with(&optimizer_results_f, Compressor::Constant);
cs.compress_chunk_with(vec, Compressor::Constant);
cs.to_bytes()
} else {
cs.compress_chunk_with(&optimizer_results_f, Compressor::Noop);
cs.compress_chunk_with(vec, Compressor::Noop);
cs.to_bytes()
}
}
Expand All @@ -88,6 +90,7 @@ struct Args {
/// input file
input: PathBuf,

/// Processes a directory instead of a single file
#[arg(short, action)]
directory: bool,

Expand Down
4 changes: 3 additions & 1 deletion brro-compressor/src/optimizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ fn to_median_filter(data: &Vec<f64>) -> Vec<i64> {
filtered
}


/// This should look at the data and return an optimized dataset for a specific compressor,
/// If a compressor is hand picked, this should be skipped.
/// TODO: Make it do that
pub fn process_data(wav_data: &Vec<f64>, tag: &MetricTag) -> Vec<i64> {
let mut _bitdepth = 64;
let mut _dc_component: i64 = 0;
Expand Down
6 changes: 6 additions & 0 deletions brro-compressor/src/utils/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fn process_wav_file(file_path: &Path) -> io::Result<(Vec<f64>, MetricTag)> {
let full_path_str = file_path.to_str().unwrap_or("");
debug!("File: {} ,", full_path_str);
let wav_data = read_metrics_from_wav(full_path_str);
debug!("Data Len: {}", wav_data.len());
// Depending on Metric Tag, apply a transformation
let tag = tag_metric(full_path_str);
Ok((wav_data, tag))
Expand All @@ -34,6 +35,11 @@ fn process_raw_file(file_path: &Path) -> io::Result<(Vec<f64>, MetricTag)> {
todo!("Handle RAW file processing here (for example, decoding or encoding): {file_path:?}");
}

// Function to process a BRRO (Compressed) file
fn process_brro_file(file_path: &Path) -> io::Result<(Vec<f64>, MetricTag)> {
todo!("Handle BRRO file processing here (for example, decoding or encoding): {file_path:?}");
}

pub struct Files {
pub contents: Vec<(Vec<f64>, MetricTag)>,
pub names: Vec<String>,
Expand Down

0 comments on commit 5d810b8

Please sign in to comment.