-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathnested.rs
77 lines (54 loc) · 1.85 KB
/
nested.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use std::error::Error;
use heed::types::*;
use heed::{Database, EnvOpenOptions};
fn main() -> Result<(), Box<dyn Error>> {
let path = tempfile::tempdir()?;
let env = unsafe {
EnvOpenOptions::new()
.map_size(10 * 1024 * 1024) // 10MB
.max_dbs(3000)
.open(path)?
};
// here the key will be an str and the data will be a slice of u8
let mut wtxn = env.write_txn()?;
let db: Database<Str, Bytes> = env.create_database(&mut wtxn, None)?;
// clear db
db.clear(&mut wtxn)?;
wtxn.commit()?;
// -----
let mut wtxn = env.write_txn()?;
let mut nwtxn = env.nested_write_txn(&mut wtxn)?;
db.put(&mut nwtxn, "what", &[4, 5][..])?;
let ret = db.get(&nwtxn, "what")?;
println!("nested(1) \"what\": {:?}", ret);
println!("nested(1) abort");
nwtxn.abort();
let ret = db.get(&wtxn, "what")?;
println!("parent \"what\": {:?}", ret);
// ------
println!();
// also try with multiple levels of nesting
let mut nwtxn = env.nested_write_txn(&mut wtxn)?;
let mut nnwtxn = env.nested_write_txn(&mut nwtxn)?;
db.put(&mut nnwtxn, "humm...", &[6, 7][..])?;
let ret = db.get(&nnwtxn, "humm...")?;
println!("nested(2) \"humm...\": {:?}", ret);
println!("nested(2) commit");
nnwtxn.commit()?;
nwtxn.commit()?;
let ret = db.get(&wtxn, "humm...")?;
println!("parent \"humm...\": {:?}", ret);
db.put(&mut wtxn, "hello", &[2, 3][..])?;
let ret = db.get(&wtxn, "hello")?;
println!("parent \"hello\": {:?}", ret);
println!("parent commit");
wtxn.commit()?;
// ------
println!();
let rtxn = env.read_txn()?;
let ret = db.get(&rtxn, "hello")?;
println!("parent (reader) \"hello\": {:?}", ret);
let ret = db.get(&rtxn, "humm...")?;
println!("parent (reader) \"humm...\": {:?}", ret);
Ok(())
}