Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Compressor Settings #50

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 43 additions & 25 deletions brro-compressor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,78 +9,87 @@ use log::debug;
use std::path::Path;
use std::path::PathBuf;

/// Define a data structure for compressor settings
struct CompressorSettings {
// Define fields for compressor settings here
}

/// Processes the given input based on the provided arguments.
/// If `arguments.directory` is true, it processes all files in the directory.
/// Otherwise, it processes the individual file.
fn process_args(arguments: &Args) {
fn process_args(arguments: &Args, compressor_settings: Option<CompressorSettings>) {
// If the input path points to a directory
if arguments.directory {
process_directory(arguments);
process_directory(arguments, compressor_settings);
}
// If the input path points to a single file
else {
process_single_file(arguments);
process_single_file(arguments, compressor_settings);
}
}

/// Processes all files in a given directory.
fn process_directory(arguments: &Args) {
fn process_directory(arguments: &Args, compressor_settings: Option<CompressorSettings>) {
let new_name = format!(
"{}-compressed",
arguments.input.file_name().unwrap().to_string_lossy()
);
let base_dir = arguments.input.with_file_name(new_name);

writer::initialize_directory(&base_dir).expect("Failed to initialize directory");
let files =
reader::stream_reader(&arguments.input).expect("Failed to read files from directory");
let files = reader::stream_reader(&arguments.input).expect("Failed to read files from directory");

for (index, data) in files.contents.iter().enumerate() {
let (vec_data, tag) = data;
let compressed_data = compress_data(vec_data, tag, arguments);
// BRO extension
let file_name = writer::replace_extension(&files.names[index], "bro");
let compressed_data = compress_data(vec_data, tag, arguments, &compressor_settings);

let file_name = writer::replace_extension(&files.names[index], "bin");
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...");
fn process_single_file(arguments: &Args, compressor_settings: Option<CompressorSettings>) {
let (vec, tag) = reader::read_file(&arguments.input).expect("Failed to read file");
let compressed_data = compress_data(&vec, &tag, arguments);
let compressed_data = compress_data(&vec, &tag, arguments, &compressor_settings);

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(), "bro");
let new_filename_string = writer::replace_extension(&filename_str.to_string(), "bin");
let new_path = arguments.input.parent().unwrap().join(new_filename_string);
write_compressed_data_to_path(&compressed_data, &new_path);
}
}
}

/// 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!");
fn compress_data(vec: &Vec<f64>, tag: &MetricTag, arguments: &Args, compressor_settings: &Option<CompressorSettings>) -> Vec<u8> {
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(vec, Compressor::Constant);
cs.compress_chunk_with(&optimizer_results_f, Compressor::Constant);
cs.to_bytes()
} else {
cs.compress_chunk_with(vec, Compressor::Noop);
// Check if compressor settings were provided and use them
if let Some(_settings) = compressor_settings {
// Use the provided settings
// You may need to replace `Compressor::Noop` with an actual compressor based on the settings
cs.compress_chunk_with(&optimizer_results_f, Compressor::Noop);
} else {
// Use default settings or handle this case as needed
cs.compress_chunk_with(&optimizer_results_f, Compressor::Noop);
}
cs.to_bytes()
}
}


/// Writes the compressed data to the specified path.
fn write_compressed_data_to_path(compressed: &[u8], path: &Path) {
let mut file =
writer::create_streaming_writer(path).expect("Failed to create a streaming writer");
let mut file = writer::create_streaming_writer(path).expect("Failed to create a streaming writer");
writer::write_data_to_stream(&mut file, compressed).expect("Failed to write compressed data");
}

Expand All @@ -90,7 +99,6 @@ struct Args {
/// input file
input: PathBuf,

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

Expand All @@ -107,5 +115,15 @@ fn main() {
env_logger::init();
let arguments = Args::parse();
debug!("{:?}", arguments);
process_args(&arguments);
}

// Create an optional CompressorSettings based on user input
let compressor_settings = if arguments.noop {
None // Use default settings or handle this case as needed
} else {
Some(CompressorSettings {
// Define settings based on user input here
})
};

process_args(&arguments, compressor_settings);
}