Skip to content

Commit

Permalink
Decompression methods
Browse files Browse the repository at this point in the history
  • Loading branch information
cjrolo committed Sep 29, 2023
1 parent 5e365e6 commit c230f10
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
28 changes: 24 additions & 4 deletions brro-compressor/src/compressor/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const CONSTANT_COMPRESSOR_ID: u8 = 0;

/// This is a temporary implementation, other implementations (FFT, Polynomial) might provide the same result
/// as going through the data anyway.
#[derive(Encode, Decode, PartialEq, Debug)]
#[derive(Encode, Decode, PartialEq, Debug, Clone)]
pub struct Constant {
pub id: u8,
pub constant: i64,
Expand Down Expand Up @@ -52,7 +52,6 @@ impl Constant {
}

/// Compresses the data. Walks the data array and sets one value as the constant.
/// TODO: Fix residuals positions
/// Performance consideration, we do O(3*n) in the worst case, best case is O(n).
pub fn compress(&mut self, data: &[i64]) {
// Count occurrences of each value in the data
Expand Down Expand Up @@ -83,8 +82,17 @@ impl Constant {
}
}

/// This function transforms the structure into a Binary stream to be appended to the frame
pub fn to_bytes(self) -> Vec<u8> {
/// Receives a data stream and generates a Constant
pub fn decompress(data: &Vec<u8>) -> Self {
let config = BinConfig::get();
match bincode::decode_from_slice(&data, config) {
Ok((constant, _)) => constant,
Err(e) => panic!("{e}")
}
}

/// This function transforms the structure into a Binary stream
pub fn to_bytes(&self) -> Vec<u8> {
// Use Bincode and flate2-rs? Do this at the Stream Level?
let config = BinConfig::get();
bincode::encode_to_vec(self, config).unwrap()
Expand All @@ -111,6 +119,18 @@ mod tests {
assert_eq!(constant(&vector1), [0, 2, 0]);
}

#[test]
fn test_compression() {
let vector1 = vec![1.0, 1.0, 1.0, 1.0, 1.0];
let mut c = Constant::new(vector1.len());
c.compress(&Constant::optimize(&vector1));
let bin_data = c.to_bytes();
let c2 = Constant::decompress(&bin_data);

assert_eq!(bin_data, [0, 2, 0]);
assert_eq!(c.clone(), c2);
}

#[test]
fn test_define_constant() {
let vector1 = vec![1.0, 1.0, 1.0, 1.0, 1.0];
Expand Down
26 changes: 23 additions & 3 deletions brro-compressor/src/compressor/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bincode::{Decode, Encode};

// 250 to optimize bincode encoding, since it checks for <251 for u8
const NOOP_COMPRESSOR_ID:u8 = 250;
#[derive(Encode, Decode, PartialEq, Debug)]
#[derive(Encode, Decode, PartialEq, Debug, Clone)]
pub struct Noop {
pub id: u8,
pub data: Vec<i64>,
Expand Down Expand Up @@ -32,8 +32,17 @@ impl Noop {
self.data = Noop::optimize(data);
}

/// Receives a data stream and generates a Constant
pub fn decompress(data: &Vec<u8>) -> Self {
let config = BinConfig::get();
match bincode::decode_from_slice(&data, config) {
Ok((constant, _)) => constant,
Err(e) => panic!("{e}")
}
}

/// This function transforms the structure in a Binary stream to be appended to the frame
pub fn to_bytes(self) -> Vec<u8> {
pub fn to_bytes(&self) -> Vec<u8> {
let config = BinConfig::get();
bincode::encode_to_vec(self, config).unwrap()
}
Expand All @@ -52,8 +61,19 @@ mod tests {
use super::*;

#[test]
fn test_constant() {
fn test_noop() {
let vector1 = vec![1.0, 1.0, 1.0, 1.0, 1.0];
assert_eq!(noop(&vector1), [250, 5, 2, 2, 2, 2, 2]);
}

#[test]
fn test_compression() {
let vector1 = vec![1.0, 1.0, 1.0, 1.0, 1.0];
let mut c = Noop::new(vector1.len());
c.compress(&vector1);
let bin_data = c.to_bytes();
let c2 = Noop::decompress(&bin_data);

assert_eq!(c.clone(), c2);
}
}

0 comments on commit c230f10

Please sign in to comment.