2 releases
0.1.1 | Jul 12, 2022 |
---|---|
0.1.0 | Jul 12, 2022 |
#11 in #tx
83KB
2.5K
SLoC
nikidb
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();
}
Dependencies
~1.7–2.5MB
~45K SLoC