Skip to content

Commit

Permalink
feat(objectarium): add lzma compression algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikssonJoakim committed Aug 2, 2023
1 parent 0c133fc commit b3392f7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
29 changes: 29 additions & 0 deletions contracts/okp4-objectarium/src/compress.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::io;

use enum_iterator::Sequence;
use lzma_rs;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use snap;
Expand All @@ -14,6 +15,8 @@ pub enum CompressionAlgorithm {
Passthrough,
/// Represents the Snappy algorithm.
Snappy,
/// Represents the LZMA algorithm.
Lzma,
}

impl CompressionAlgorithm {
Expand All @@ -22,6 +25,7 @@ impl CompressionAlgorithm {
let compressor = match self {
CompressionAlgorithm::Passthrough => passthrough,
CompressionAlgorithm::Snappy => snappy_compress,
CompressionAlgorithm::Lzma => lzma_compress,
};
compressor(data)
}
Expand All @@ -32,6 +36,7 @@ impl CompressionAlgorithm {
let decompressor = match self {
CompressionAlgorithm::Passthrough => passthrough,
CompressionAlgorithm::Snappy => snappy_decompress,
CompressionAlgorithm::Lzma => lzma_decompress,
};
decompressor(data)
}
Expand All @@ -49,6 +54,12 @@ impl From<std::io::Error> for CompressionError {
}
}

impl From<lzma_rs::error::Error> for CompressionError {
fn from(err: lzma_rs::error::Error) -> Self {
CompressionError::Error(err.to_string())
}
}

/// pass_through returns the data as is.
#[inline]
fn passthrough(data: &[u8]) -> Result<Vec<u8>, CompressionError> {
Expand Down Expand Up @@ -77,6 +88,24 @@ fn snappy_decompress(data: &[u8]) -> Result<Vec<u8>, CompressionError> {
Ok(writer)
}

// lzma_compress returns the LZMA compressed data.
#[inline]
fn lzma_compress(data: &[u8]) -> Result<Vec<u8>, CompressionError> {
let mut reader = io::Cursor::new(data);
let mut writer = Vec::new();
lzma_rs::lzma_compress(&mut reader, &mut writer)?;
Ok(writer)
}

// lzma_decompress returns the LZMA decompressed data.
#[inline]
fn lzma_decompress(data: &[u8]) -> Result<Vec<u8>, CompressionError> {
let mut reader = io::Cursor::new(data);
let mut writer = Vec::new();
lzma_rs::lzma_decompress(&mut reader, &mut writer)?;
Ok(writer)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
6 changes: 6 additions & 0 deletions contracts/okp4-objectarium/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ pub enum CompressionAlgorithm {
///
/// See [the snappy web page](https://google.github.io/snappy/) for more information.
Snappy,
/// # Lzma
/// Represents the LZMA algorithm.
/// LZMA is a lossless data compression/decompression algorithm that features a high compression ratio and a variable compression-dictionary size up to 4 GB.
///
/// See [the LZMA wiki page](https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Markov_chain_algorithm) for more information.
Lzma,
}

/// HashAlgorithm is an enumeration that defines the different hash algorithms
Expand Down
2 changes: 2 additions & 0 deletions contracts/okp4-objectarium/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ impl From<msg::CompressionAlgorithm> for CompressionAlgorithm {
match algorithm {
msg::CompressionAlgorithm::Passthrough => CompressionAlgorithm::Passthrough,
msg::CompressionAlgorithm::Snappy => CompressionAlgorithm::Snappy,
msg::CompressionAlgorithm::Lzma => CompressionAlgorithm::Lzma,
}
}
}
Expand All @@ -125,6 +126,7 @@ impl From<CompressionAlgorithm> for msg::CompressionAlgorithm {
match algorithm {
CompressionAlgorithm::Passthrough => msg::CompressionAlgorithm::Passthrough,
CompressionAlgorithm::Snappy => msg::CompressionAlgorithm::Snappy,
CompressionAlgorithm::Lzma => msg::CompressionAlgorithm::Lzma,
}
}
}
Expand Down

0 comments on commit b3392f7

Please sign in to comment.