1 unstable release

0.1.0 Dec 31, 2023

#1276 in Database interfaces

MIT license

29KB
659 lines

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);
            }
        });
    }
}

TODO

License

The project is under the MIT license.

Dependencies

~4MB
~71K SLoC