Skip to content

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

License

Notifications You must be signed in to change notification settings

tngsoftware/bitcask-engine-rs

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Crates.io MIT licensed

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.

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 100.0%