forked from meilisearch/heed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmp-serde.rs
40 lines (30 loc) 路 1016 Bytes
/
rmp-serde.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use std::error::Error;
use std::fs;
use std::path::Path;
use heed::types::{SerdeRmp, Str};
use heed::{Database, EnvOpenOptions};
use serde::{Deserialize, Serialize};
fn main() -> Result<(), Box<dyn Error>> {
let path = Path::new("target").join("heed.mdb");
fs::create_dir_all(&path)?;
let env = unsafe {
EnvOpenOptions::new()
.map_size(10 * 1024 * 1024) // 10MB
.max_dbs(3000)
.open(path)?
};
// you can specify that a database will support some typed key/data
// serde types are also supported!!!
#[derive(Debug, Serialize, Deserialize)]
struct Hello<'a> {
string: &'a str,
}
let mut wtxn = env.write_txn()?;
let db: Database<Str, SerdeRmp<Hello>> = env.create_database(&mut wtxn, Some("serde-rmp"))?;
let hello = Hello { string: "hi" };
db.put(&mut wtxn, "hello", &hello)?;
let ret: Option<Hello> = db.get(&wtxn, "hello")?;
println!("serde-rmp:\t{:?}", ret);
wtxn.commit()?;
Ok(())
}