bitcask-engine-rs

Crates.iobitcask-engine-rs
lib.rsbitcask-engine-rs
version0.1.0
sourcesrc
created_at2023-12-31 21:23:07.619231
updated_at2023-12-31 21:23:07.619231
descriptionA Rust implementation of Bitcask
homepage
repositoryhttps://github.com/liangrunda/bitcask-engine-rs
max_upload_size
id1085010
size32,664
Liangrun Da (LiangrunDa)

documentation

README

BitCask Engine

A Rust implementation of BitCask, a log-structured storage engine for key/value data.

Apart from the original paper, this implementation also supports the following features:

  1. NX (not exist) for put operation

  2. XX (exist) for put operation

Usage

Add this to your Cargo.toml:

[dependencies]
bitcask-engine-rs = "0.1.0"

To use it in your project, you can initialize a Bitcask instance with a directory path:

use bitcask_engine_rs::Bitcask;

fn main() {
    let mut bitcask = Bitcask::new("/tmp/bitcask").unwrap();
    bitcask.put(&vec![1, 2, 3], &vec![4, 5, 6]).unwrap();
    let res = bitcask.get(&vec![1, 2, 3]);
    assert_eq!(res, Some(vec![4, 5, 6]));
}

The Bitcask instance is thread-safe, so you can share it between threads.

use tokio::io::AsyncReadExt;
use tokio::net::TcpListener;
use bitcask_engine_rs::bitcask::KVStorage;

#[tokio::main]
async fn main() {
    let bitcask = bitcask_engine_rs::bitcask::BitCask::new("./data").unwrap();
    let listener = TcpListener::bind("127.0.0.1:8080").await.unwrap();
    loop {
        let (mut socket, _) = listener.accept().await.unwrap();
        let mut bitcask_clone = bitcask.clone();
        tokio::spawn(async move {
            let mut buf = [0; 1024];
            loop {
                let n = socket.read(&mut buf).await.unwrap();
                if n == 0 {
                    return;
                }
                let key: Vec<u8> = buf[0..n].to_vec();
                let value: Vec<u8> = vec![1, 2];
                let res = bitcask_clone.put(&key, &value);
                println!("res: {:?}", res);
            }
        });
    }
}

Related Projects

TODO

License

The project is under the MIT license.

Commit count: 0

cargo fmt