-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example to expose the new possibilities of no more RwTxn
- Loading branch information
1 parent
277deea
commit 94a5608
Showing
1 changed file
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use std::error::Error; | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
use heed::types::*; | ||
use heed::{Database, EnvOpenOptions}; | ||
|
||
/// This example exposes some of the possibilities using | ||
/// a non-mutable RwTxn to iterate on a database permits. | ||
fn main() -> Result<(), Box<dyn Error>> { | ||
let path = Path::new("target").join("heed.mdb"); | ||
|
||
fs::create_dir_all(&path)?; | ||
|
||
let env = 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 one: Database<Str, ByteSlice> = env.create_database(&mut wtxn, Some("one"))?; | ||
let two: Database<Str, ByteSlice> = env.create_database(&mut wtxn, Some("two"))?; | ||
|
||
// clear db | ||
one.clear(&wtxn)?; | ||
two.clear(&wtxn)?; | ||
wtxn.commit()?; | ||
|
||
// ----- | ||
|
||
let wtxn = env.write_txn()?; | ||
for number in 0u32..1000 { | ||
let k = number.to_string(); | ||
let v = number.to_be_bytes(); | ||
one.put(&wtxn, &k, &v)?; | ||
} | ||
|
||
for result in one.iter(&wtxn)? { | ||
let (k, v) = result?; | ||
two.put(&wtxn, k, v)?; | ||
} | ||
|
||
for (res1, res2) in one.iter(&wtxn)?.zip(two.iter(&wtxn)?) { | ||
let (k1, v1) = res1?; | ||
let (k2, v2) = res2?; | ||
assert_eq!(k1, k2); | ||
assert_eq!(v1, v2); | ||
} | ||
|
||
wtxn.commit()?; | ||
|
||
// ----- | ||
|
||
let wtxn = env.write_txn()?; | ||
for result in one.iter(&wtxn)? { | ||
let (k, v) = result?; | ||
if k == "10" { | ||
let n: u32 = 11; | ||
one.put(&wtxn, &n.to_string(), &[])?; | ||
} | ||
if k == "11" { | ||
assert!(v.is_empty()); | ||
} | ||
} | ||
|
||
wtxn.commit()?; | ||
|
||
// ----- | ||
|
||
let wtxn = env.write_txn()?; | ||
let n: u32 = 100_000; | ||
one.put(&wtxn, &n.to_string(), &n.to_be_bytes())?; | ||
let v = one.get(&wtxn, &n.to_string())?.unwrap(); | ||
one.put(&wtxn, &n.to_string(), v)?; | ||
|
||
let v = one.get(&wtxn, &n.to_string())?.unwrap(); | ||
assert_eq!(v, n.to_be_bytes()); | ||
|
||
wtxn.commit()?; | ||
|
||
// ----- | ||
|
||
// What happen when we clear the database while iterating on it? | ||
let wtxn = env.write_txn()?; | ||
for result in one.remap_data_type::<DecodeIgnore>().iter(&wtxn)? { | ||
let (k, _) = result?; | ||
if k == "10" { | ||
one.clear(&wtxn)?; | ||
} | ||
} | ||
|
||
assert!(one.is_empty(&wtxn)?); | ||
wtxn.commit()?; | ||
|
||
Ok(()) | ||
} |