-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from KAIYOHUGO/master
Add Compressor
- Loading branch information
Showing
15 changed files
with
291 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "mcpe" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
miniz_oxide = "0.7.1" | ||
rusty-leveldb = { path = "../../" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# MCPE | ||
|
||
This example show how to customize compression method. | ||
|
||
This setup is compatible to [Mojang's leveldb](https://github.com/Mojang/leveldb-mcpe). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use miniz_oxide::deflate::{compress_to_vec, compress_to_vec_zlib}; | ||
use miniz_oxide::inflate::{decompress_to_vec, decompress_to_vec_zlib}; | ||
use rusty_leveldb::{Compressor, CompressorList, Options, DB}; | ||
use std::rc::Rc; | ||
|
||
struct ZlibCompressor(u8); | ||
|
||
impl ZlibCompressor { | ||
/// level 0-10 | ||
pub fn new(level: u8) -> Self { | ||
assert!(level <= 10); | ||
Self(level) | ||
} | ||
} | ||
|
||
impl Compressor for ZlibCompressor { | ||
fn encode(&self, block: Vec<u8>) -> rusty_leveldb::Result<Vec<u8>> { | ||
Ok(compress_to_vec_zlib(&block, self.0)) | ||
} | ||
|
||
fn decode(&self, block: Vec<u8>) -> rusty_leveldb::Result<Vec<u8>> { | ||
decompress_to_vec_zlib(&block).map_err(|e| rusty_leveldb::Status { | ||
code: rusty_leveldb::StatusCode::CompressionError, | ||
err: e.to_string(), | ||
}) | ||
} | ||
} | ||
|
||
struct RawZlibCompressor(u8); | ||
|
||
impl RawZlibCompressor { | ||
/// level 0-10 | ||
pub fn new(level: u8) -> Self { | ||
assert!(level <= 10); | ||
Self(level) | ||
} | ||
} | ||
|
||
impl Compressor for RawZlibCompressor { | ||
fn encode(&self, block: Vec<u8>) -> rusty_leveldb::Result<Vec<u8>> { | ||
Ok(compress_to_vec(&block, self.0)) | ||
} | ||
|
||
fn decode(&self, block: Vec<u8>) -> rusty_leveldb::Result<Vec<u8>> { | ||
decompress_to_vec(&block).map_err(|e| rusty_leveldb::Status { | ||
code: rusty_leveldb::StatusCode::CompressionError, | ||
err: e.to_string(), | ||
}) | ||
} | ||
} | ||
|
||
pub fn mcpe_options(compression_level: u8) -> Options { | ||
let mut opt = Options::default(); | ||
opt.compressor = 0; | ||
let mut list = CompressorList::new(); | ||
list.set_with_id(0, RawZlibCompressor::new(compression_level)); | ||
list.set_with_id(1, ZlibCompressor::new(compression_level)); | ||
opt.compressor_list = Rc::new(list); | ||
opt | ||
} | ||
|
||
fn main() { | ||
let path = "mcpe_db"; | ||
let compression_level = 10; | ||
let opt = mcpe_options(compression_level); | ||
let mut db = DB::open(path, opt).unwrap(); | ||
db.put(b"~local_player", b"NBT data goes here").unwrap(); | ||
let value = db.get(b"~local_player").unwrap(); | ||
assert_eq!(&value, b"NBT data goes here") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/// Custom compression method | ||
/// | ||
/// ``` | ||
/// # use rusty_leveldb::{Compressor, CompressorId}; | ||
/// | ||
/// #[derive(Debug, Clone, Copy, Default)] | ||
/// pub struct CustomCompressor; | ||
/// | ||
/// impl CompressorId for CustomCompressor { | ||
/// // a unique id to identify what compressor should DB use | ||
/// const ID: u8 = 42; | ||
/// } | ||
/// | ||
/// impl Compressor for CustomCompressor { | ||
/// fn encode(&self, block: Vec<u8>) -> rusty_leveldb::Result<Vec<u8>> { | ||
/// // Do something | ||
/// Ok(block) | ||
/// } | ||
/// | ||
/// fn decode(&self, block: Vec<u8>) -> rusty_leveldb::Result<Vec<u8>> { | ||
/// // Do something | ||
/// Ok(block) | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// See [crate::CompressorList] for usage | ||
pub trait Compressor { | ||
fn encode(&self, block: Vec<u8>) -> crate::Result<Vec<u8>>; | ||
|
||
fn decode(&self, block: Vec<u8>) -> crate::Result<Vec<u8>>; | ||
} | ||
|
||
/// Set default compressor id | ||
pub trait CompressorId { | ||
const ID: u8; | ||
} | ||
|
||
/// A compressor that do **Nothing** | ||
/// | ||
/// It default id is `0` | ||
#[derive(Debug, Clone, Copy, Default)] | ||
pub struct NoneCompressor; | ||
|
||
impl CompressorId for NoneCompressor { | ||
const ID: u8 = 0; | ||
} | ||
|
||
impl Compressor for NoneCompressor { | ||
fn encode(&self, block: Vec<u8>) -> crate::Result<Vec<u8>> { | ||
Ok(block) | ||
} | ||
|
||
fn decode(&self, block: Vec<u8>) -> crate::Result<Vec<u8>> { | ||
Ok(block) | ||
} | ||
} | ||
|
||
/// A compressor that compress data with Google's Snappy | ||
/// | ||
/// It default id is `1` | ||
#[derive(Debug, Clone, Copy, Default)] | ||
pub struct SnappyCompressor; | ||
|
||
impl CompressorId for SnappyCompressor { | ||
const ID: u8 = 1; | ||
} | ||
|
||
impl Compressor for SnappyCompressor { | ||
fn encode(&self, block: Vec<u8>) -> crate::Result<Vec<u8>> { | ||
Ok(snap::raw::Encoder::new().compress_vec(&block)?) | ||
} | ||
|
||
fn decode(&self, block: Vec<u8>) -> crate::Result<Vec<u8>> { | ||
Ok(snap::raw::Decoder::new().decompress_vec(&block)?) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.