dermesser / leveldb-rs

A reimplementation of LevelDB in Rust (no bindings).
Other
521 stars 59 forks source link

Feature Request: Custom Compression Method #32

Closed KAIYOHUGO closed 1 year ago

KAIYOHUGO commented 1 year ago

In current version is hardcode in rusty_leveldb::CompressionType.

I propose a Compressor trait

pub trait Compressor {
    fn encode(&self, block: Vec<u8>) -> rusty_leveldb::Result<Vec<u8>>;

    fn decode(&self, block: Vec<u8>) -> rusty_leveldb::Result<Vec<u8>>;
}

then change rusty_leveldb::Options to this

pub struct Options {
    ...
    compressor: Box<dyn Compressor>,
}

this should fit in here and here

KAIYOHUGO commented 1 year ago

I can make PR if this idea is acceptable.

dermesser commented 1 year ago

Thank you for the request! Do you have a specific use case where snappy compression doesn't cut it? The downside of introducing such a compressor is obviously losing compatibility, although I agree it has the potential of slightly more elegant code as well.

KAIYOHUGO commented 1 year ago

Thanks for your reply! My use case is weird. I want to read Minecraft Bedrock world format and it is base on leveldb with zlib compression.

Introducing such a compressor indeed losing compatibility between Google's implementation, but for user it is no different. They only need to change CompressionType::CompressionNone to something like compressor::NoneCompressor::new(). It can also make config compression more easy. e.g. change compression level

By the way. Google's implementation add zstd now, maybe it is a good time to add zstd?

dermesser commented 1 year ago

Thank you for explaining, and in fact I didn't know that Google has added zstd by now! In that case I will try my hand at building the foundations for this, and then adding different compression algorithms should become fairly simple.

KAIYOHUGO commented 1 year ago

I have made a basic implementation, maybe it is worth to take a look?

I change Options to this. Options.compressor is the index in Options.compressor_list, and the index also represents the magic number of compression.

struct Options {
    ...
    compressor: u8,
    compressor_list: [Option<Box<dyn Compressor>>; 256]
}