Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace the restrictive &mut RwTxn by less restrictive &RwTxn #190

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions heed/examples/all-types-poly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut wtxn = env.write_txn()?;
let db = env.create_poly_database(&mut wtxn, Some("kikou"))?;

db.put::<OwnedType<[i32; 2]>, Str>(&mut wtxn, &[2, 3], "what's up?")?;
db.put::<OwnedType<[i32; 2]>, Str>(&wtxn, &[2, 3], "what's up?")?;
let ret = db.get::<OwnedType<[i32; 2]>, Str>(&wtxn, &[2, 3])?;

println!("{:?}", ret);
Expand All @@ -35,7 +35,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut wtxn = env.write_txn()?;
let db = env.create_poly_database(&mut wtxn, Some("kiki"))?;

db.put::<Str, ByteSlice>(&mut wtxn, "hello", &[2, 3][..])?;
db.put::<Str, ByteSlice>(&wtxn, "hello", &[2, 3][..])?;
let ret = db.get::<Str, ByteSlice>(&wtxn, "hello")?;

println!("{:?}", ret);
Expand All @@ -51,13 +51,13 @@ fn main() -> Result<(), Box<dyn Error>> {
let db = env.create_poly_database(&mut wtxn, Some("serde"))?;

let hello = Hello { string: "hi" };
db.put::<Str, SerdeBincode<Hello>>(&mut wtxn, "hello", &hello)?;
db.put::<Str, SerdeBincode<Hello>>(&wtxn, "hello", &hello)?;

let ret = db.get::<Str, SerdeBincode<Hello>>(&wtxn, "hello")?;
println!("serde-bincode:\t{:?}", ret);

let hello = Hello { string: "hi" };
db.put::<Str, SerdeJson<Hello>>(&mut wtxn, "hello", &hello)?;
db.put::<Str, SerdeJson<Hello>>(&wtxn, "hello", &hello)?;

let ret = db.get::<Str, SerdeJson<Hello>>(&wtxn, "hello")?;
println!("serde-json:\t{:?}", ret);
Expand All @@ -74,7 +74,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let db = env.create_poly_database(&mut wtxn, Some("nocopy-struct"))?;

let zerobytes = ZeroBytes { bytes: [24; 12] };
db.put::<Str, UnalignedType<ZeroBytes>>(&mut wtxn, "zero", &zerobytes)?;
db.put::<Str, UnalignedType<ZeroBytes>>(&wtxn, "zero", &zerobytes)?;

let ret = db.get::<Str, UnalignedType<ZeroBytes>>(&wtxn, "zero")?;

Expand All @@ -85,7 +85,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut wtxn = env.write_txn()?;
let db = env.create_poly_database(&mut wtxn, Some("ignored-data"))?;

db.put::<Str, Unit>(&mut wtxn, "hello", &())?;
db.put::<Str, Unit>(&wtxn, "hello", &())?;
let ret = db.get::<Str, Unit>(&wtxn, "hello")?;

println!("{:?}", ret);
Expand Down Expand Up @@ -115,10 +115,10 @@ fn main() -> Result<(), Box<dyn Error>> {

let db = env.create_poly_database(&mut wtxn, Some("big-endian-iter"))?;

db.put::<BEI64, Unit>(&mut wtxn, &0, &())?;
db.put::<BEI64, Unit>(&mut wtxn, &68, &())?;
db.put::<BEI64, Unit>(&mut wtxn, &35, &())?;
db.put::<BEI64, Unit>(&mut wtxn, &42, &())?;
db.put::<BEI64, Unit>(&wtxn, &0, &())?;
db.put::<BEI64, Unit>(&wtxn, &68, &())?;
db.put::<BEI64, Unit>(&wtxn, &35, &())?;
db.put::<BEI64, Unit>(&wtxn, &42, &())?;

let rets: Result<Vec<(i64, _)>, _> = db.iter::<BEI64, Unit>(&wtxn)?.collect();

Expand All @@ -132,7 +132,7 @@ fn main() -> Result<(), Box<dyn Error>> {

// delete a range of key
let range = 35..=42;
let deleted: usize = db.delete_range::<BEI64, _>(&mut wtxn, &range)?;
let deleted: usize = db.delete_range::<BEI64, _>(&wtxn, &range)?;

let rets: Result<Vec<(i64, _)>, _> = db.iter::<BEI64, Unit>(&wtxn)?.collect();

Expand Down
22 changes: 11 additions & 11 deletions heed/examples/all-types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut wtxn = env.write_txn()?;
let db: Database<OwnedType<[i32; 2]>, Str> = env.create_database(&mut wtxn, Some("kikou"))?;

db.put(&mut wtxn, &[2, 3], "what's up?")?;
db.put(&wtxn, &[2, 3], "what's up?")?;
let ret: Option<&str> = db.get(&wtxn, &[2, 3])?;

println!("{:?}", ret);
Expand All @@ -35,7 +35,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut wtxn = env.write_txn()?;
let db: Database<Str, ByteSlice> = env.create_database(&mut wtxn, Some("kiki"))?;

db.put(&mut wtxn, "hello", &[2, 3][..])?;
db.put(&wtxn, "hello", &[2, 3][..])?;
let ret: Option<&[u8]> = db.get(&wtxn, "hello")?;

println!("{:?}", ret);
Expand All @@ -52,7 +52,7 @@ fn main() -> Result<(), Box<dyn Error>> {
env.create_database(&mut wtxn, Some("serde-bincode"))?;

let hello = Hello { string: "hi" };
db.put(&mut wtxn, "hello", &hello)?;
db.put(&wtxn, "hello", &hello)?;

let ret: Option<Hello> = db.get(&wtxn, "hello")?;
println!("serde-bincode:\t{:?}", ret);
Expand All @@ -63,7 +63,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let db: Database<Str, SerdeJson<Hello>> = env.create_database(&mut wtxn, Some("serde-json"))?;

let hello = Hello { string: "hi" };
db.put(&mut wtxn, "hello", &hello)?;
db.put(&wtxn, "hello", &hello)?;

let ret: Option<Hello> = db.get(&wtxn, "hello")?;
println!("serde-json:\t{:?}", ret);
Expand All @@ -82,7 +82,7 @@ fn main() -> Result<(), Box<dyn Error>> {
env.create_database(&mut wtxn, Some("simple-struct"))?;

let zerobytes = ZeroBytes { bytes: [24; 12] };
db.put(&mut wtxn, "zero", &zerobytes)?;
db.put(&wtxn, "zero", &zerobytes)?;

let ret = db.get(&wtxn, "zero")?;

Expand All @@ -93,7 +93,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut wtxn = env.write_txn()?;
let db: Database<Str, Unit> = env.create_database(&mut wtxn, Some("ignored-data"))?;

db.put(&mut wtxn, "hello", &())?;
db.put(&wtxn, "hello", &())?;
let ret: Option<()> = db.get(&wtxn, "hello")?;

println!("{:?}", ret);
Expand Down Expand Up @@ -123,10 +123,10 @@ fn main() -> Result<(), Box<dyn Error>> {

let db: Database<BEI64, Unit> = env.create_database(&mut wtxn, Some("big-endian-iter"))?;

db.put(&mut wtxn, &0, &())?;
db.put(&mut wtxn, &68, &())?;
db.put(&mut wtxn, &35, &())?;
db.put(&mut wtxn, &42, &())?;
db.put(&wtxn, &0, &())?;
db.put(&wtxn, &68, &())?;
db.put(&wtxn, &35, &())?;
db.put(&wtxn, &42, &())?;

let rets: Result<Vec<(i64, _)>, _> = db.iter(&wtxn)?.collect();

Expand All @@ -140,7 +140,7 @@ fn main() -> Result<(), Box<dyn Error>> {

// delete a range of key
let range = 35..=42;
let deleted: usize = db.delete_range(&mut wtxn, &range)?;
let deleted: usize = db.delete_range(&wtxn, &range)?;

let rets: Result<Vec<(i64, _)>, _> = db.iter(&wtxn)?.collect();

Expand Down
10 changes: 5 additions & 5 deletions heed/examples/clear-database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ fn main() -> Result<(), Box<dyn Error>> {
let db: Database<Str, Str> = env.create_database(&mut wtxn, Some("first"))?;

// We fill the db database with entries.
db.put(&mut wtxn, "I am here", "to test things")?;
db.put(&mut wtxn, "I am here too", "for the same purpose")?;
db.put(&wtxn, "I am here", "to test things")?;
db.put(&wtxn, "I am here too", "for the same purpose")?;

wtxn.commit()?;

let mut wtxn = env.write_txn()?;
db.clear(&mut wtxn)?;
db.put(&mut wtxn, "And I come back", "to test things")?;
let wtxn = env.write_txn()?;
db.clear(&wtxn)?;
db.put(&wtxn, "And I come back", "to test things")?;

let mut iter = db.iter(&wtxn)?;
assert_eq!(iter.next().transpose()?, Some(("And I come back", "to test things")));
Expand Down
6 changes: 3 additions & 3 deletions heed/examples/cursor-append.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ fn main() -> Result<(), Box<dyn Error>> {
let second: Database<Str, Str> = env.create_database(&mut wtxn, Some("second"))?;

// We fill the first database with entries.
first.put(&mut wtxn, "I am here", "to test things")?;
first.put(&mut wtxn, "I am here too", "for the same purpose")?;
first.put(&wtxn, "I am here", "to test things")?;
first.put(&wtxn, "I am here too", "for the same purpose")?;

// We try to append ordered entries in the second database.
let mut iter = second.iter_mut(&mut wtxn)?;
let mut iter = second.iter_mut(&wtxn)?;

unsafe { iter.append("aaaa", "lol")? };
unsafe { iter.append("abcd", "lol")? };
Expand Down
8 changes: 4 additions & 4 deletions heed/examples/multi-env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ fn main() -> Result<(), Box<dyn Error>> {
env2.create_database(&mut wtxn2, Some("hello"))?;

// clear db
db1.clear(&mut wtxn1)?;
db1.clear(&wtxn1)?;
wtxn1.commit()?;

// clear db
db2.clear(&mut wtxn2)?;
db2.clear(&wtxn2)?;
wtxn2.commit()?;

// -----

let mut wtxn1 = env1.write_txn()?;
let wtxn1 = env1.write_txn()?;

db1.put(&mut wtxn1, "what", &[4, 5][..])?;
db1.put(&wtxn1, "what", &[4, 5][..])?;
db1.get(&wtxn1, "what")?;
wtxn1.commit()?;

Expand Down
12 changes: 6 additions & 6 deletions heed/examples/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ fn main() -> Result<(), Box<dyn Error>> {
let db: Database<Str, ByteSlice> = env.create_database(&mut wtxn, None)?;

// clear db
db.clear(&mut wtxn)?;
db.clear(&wtxn)?;
wtxn.commit()?;

// -----

let mut wtxn = env.write_txn()?;
let mut nwtxn = env.nested_write_txn(&mut wtxn)?;
let nwtxn = env.nested_write_txn(&mut wtxn)?;

db.put(&mut nwtxn, "what", &[4, 5][..])?;
db.put(&nwtxn, "what", &[4, 5][..])?;
let ret = db.get(&nwtxn, "what")?;
println!("nested(1) \"what\": {:?}", ret);

Expand All @@ -43,9 +43,9 @@ fn main() -> Result<(), Box<dyn Error>> {

// 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)?;
let nnwtxn = env.nested_write_txn(&mut nwtxn)?;

db.put(&mut nnwtxn, "humm...", &[6, 7][..])?;
db.put(&nnwtxn, "humm...", &[6, 7][..])?;
let ret = db.get(&nnwtxn, "humm...")?;
println!("nested(2) \"humm...\": {:?}", ret);

Expand All @@ -56,7 +56,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let ret = db.get(&wtxn, "humm...")?;
println!("parent \"humm...\": {:?}", ret);

db.put(&mut wtxn, "hello", &[2, 3][..])?;
db.put(&wtxn, "hello", &[2, 3][..])?;

let ret = db.get(&wtxn, "hello")?;
println!("parent \"hello\": {:?}", ret);
Expand Down
113 changes: 113 additions & 0 deletions heed/examples/non-mutable-rwtxn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
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"))?;
let numbers: Database<U8, Unit> = env.create_database(&mut wtxn, Some("numbers"))?;

// 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()?;

// -----

// We can also insert values along the way, while we are iterating
let wtxn = env.write_txn()?;
numbers.put(&wtxn, &0, &())?;
for (result, expected) in numbers.iter(&wtxn)?.zip(0..=u8::MAX) {
let (i, ()) = result?;
assert_eq!(i, expected);
if let Some(next) = i.checked_add(1) {
numbers.put(&wtxn, &next, &())?;
}
}

wtxn.commit()?;

Ok(())
}
Loading