Crates.io | nikidb |
lib.rs | nikidb |
version | 0.1.1 |
source | src |
created_at | 2022-07-12 15:32:35.461874 |
updated_at | 2022-07-12 16:01:52.521613 |
description | an embedded k-v storage written in rust, similar to boltdb |
homepage | https://github.com/szuwgh/nikidb |
repository | https://github.com/szuwgh/nikidb |
max_upload_size | |
id | 624464 |
size | 113,484 |
An embedded B+ tree-based persistent kv storage written in rust, similar to boltdb
example
use nikidb::db::DB;
use nikidb::db::DEFAULT_OPTIONS;
use nikidb::error::{NKError, NKResult};
use nikidb::tx::Tx;
use std::str;
fn main() {
let db = DB::open("./test.db", DEFAULT_OPTIONS).unwrap();
//create bucket
db.update(Box::new(|tx: &mut Tx| -> NKResult<()> {
match tx.create_bucket("default".as_bytes()) {
Ok(_) => println!("create default bucket success"),
Err(NKError::ErrBucketExists(e)) => println!("{} bucket exist", e),
Err(e) => panic!("create bucket error"),
}
Ok(())
}))
.unwrap();
//set key value
db.update(Box::new(|tx: &mut Tx| -> NKResult<()> {
let b = tx.bucket("default".as_bytes())?;
b.put(b"abc", b"123").unwrap();
Ok(())
}))
.unwrap();
//get key value
db.view(Box::new(|tx: &mut Tx| -> NKResult<()> {
let b = tx.bucket("default".as_bytes())?;
let v = b.get(b"abc").unwrap();
println!("value:{:?}", str::from_utf8(v).unwrap());
Ok(())
}))
.unwrap();
}