4 个版本
0.1.3 | 2021 年 5 月 15 日 |
---|---|
0.1.2 | 2021 年 5 月 15 日 |
0.1.1 | 2021 年 5 月 15 日 |
0.1.0 | 2021 年 5 月 15 日 |
#2889 在 数据库接口
18KB
234 行
扩展基本键值存储
键/值存储非常快速且高效,Sled 是其中之一。
这个 crate 试图更好地组织基本的 K/V 数据,使其具有更多的意义。更接近于文档存储中的组织方式,但运行在 Sled 之上,以实现最大速度。
以下示例尝试将 日本机器 组织成一个车辆和电子设备的数据集。
pub mod db;
use blazeup::kv;
use std::{error::Error, path::Path, result::Result };
fn main() -> Result<(), Box<dyn Error>> {
// path where your database is to be stored
// Note the folder is created if missing
let path = Path::new("./db");
// reset
// Note reset deletes the entire KV directory
kv::reset(Some(&path))?;
//init with index path...
kv::init(Some(&path))?;
// set bucket name.
let bucket = "Japanese-Machines";
//create your value
//the value includes a name as well as a vector of the kv::Types enum values
let vehicles = kv::Record{
name:"models".into(), //should be a string
values: vec![
kv::Types::String("Toyota".into()),
kv::Types::String("Subaru".into()),
]
};
// set value by passing key & value
kv::set(&bucket, "vehicles", vehicles.clone())?;
let tvs = kv::Record{
name:"models".into(), //should be a string
values: vec![
kv::Types::String("Sony".into()),
kv::Types::String("Sharp".into()),
]
};
kv::set(&bucket, "electronics-tvs", tvs.clone())?;
let radios = kv::Record{
name:"old-models".into(), //should be a string
values: vec![
kv::Types::String("Hitachi".into()),
kv::Types::String("Toshiba".into()),
]
};
kv::set(&bucket, "electronics-radios", radios.clone())?;
//Use Transaction to set multiple values
// We use the convenient kv::tx! macro
// all values are entered as key => Record
let ts: HashMap<_, _> = kv::tx! {
"tx-vehicles" => vehicles ,
"tx-radios" => radios ,
"tx-tvs" => tvs
};
//commit transaction
kv::transaction(bucket, ts)?;
// get single value
let value = kv::get(&bucket, "electronics-radios");
println!("{:#?}", value);
// get all values with filter
// here we filter for all electronics that are old models
// Because the Filter struct takes Options as values, you can also enter None
// Note: filters ise the WildMatch crate (https://docs.rs/wildmatch/2.1.0/wildmatch/) so all patterns supported by WildMatch will work fine.
let filter = kv::Filter{
key : Some("electronics*"),
name:Some("old*")
// name:None
};
// now fetch
let all = kv::get_all(&bucket, Some(filter));
println!("{:#?}", all);
}
依赖关系
~2.4–3.5MB
~70K SLoC