Skip to content

Commit 4cfb845

Browse files
committed
Fix the tests and examples to match the new open/create_database API
1 parent e0bf124 commit 4cfb845

File tree

11 files changed

+192
-171
lines changed

11 files changed

+192
-171
lines changed

heed/examples/all-types-poly.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ fn main() -> Result<(), Box<dyn Error>> {
2222
//
2323
// like here we specify that the key will be an array of two i32
2424
// and the data will be an str
25-
let db = env.create_poly_database(Some("kikou"))?;
26-
2725
let mut wtxn = env.write_txn()?;
26+
let db = env.create_poly_database(&mut wtxn, Some("kikou"))?;
27+
2828
db.put::<OwnedType<[i32; 2]>, Str>(&mut wtxn, &[2, 3], "what's up?")?;
2929
let ret = db.get::<OwnedType<[i32; 2]>, Str>(&wtxn, &[2, 3])?;
3030

3131
println!("{:?}", ret);
3232
wtxn.commit()?;
3333

3434
// here the key will be an str and the data will be a slice of u8
35-
let db = env.create_poly_database(Some("kiki"))?;
36-
3735
let mut wtxn = env.write_txn()?;
36+
let db = env.create_poly_database(&mut wtxn, Some("kiki"))?;
37+
3838
db.put::<Str, ByteSlice>(&mut wtxn, "hello", &[2, 3][..])?;
3939
let ret = db.get::<Str, ByteSlice>(&wtxn, "hello")?;
4040

@@ -47,9 +47,8 @@ fn main() -> Result<(), Box<dyn Error>> {
4747
string: &'a str,
4848
}
4949

50-
let db = env.create_poly_database(Some("serde"))?;
51-
5250
let mut wtxn = env.write_txn()?;
51+
let db = env.create_poly_database(&mut wtxn, Some("serde"))?;
5352

5453
let hello = Hello { string: "hi" };
5554
db.put::<Str, SerdeBincode<Hello>>(&mut wtxn, "hello", &hello)?;
@@ -71,9 +70,8 @@ fn main() -> Result<(), Box<dyn Error>> {
7170
bytes: [u8; 12],
7271
}
7372

74-
let db = env.create_poly_database(Some("nocopy-struct"))?;
75-
7673
let mut wtxn = env.write_txn()?;
74+
let db = env.create_poly_database(&mut wtxn, Some("nocopy-struct"))?;
7775

7876
let zerobytes = ZeroBytes { bytes: [24; 12] };
7977
db.put::<Str, UnalignedType<ZeroBytes>>(&mut wtxn, "zero", &zerobytes)?;
@@ -84,9 +82,9 @@ fn main() -> Result<(), Box<dyn Error>> {
8482
wtxn.commit()?;
8583

8684
// you can ignore the data
87-
let db = env.create_poly_database(Some("ignored-data"))?;
88-
8985
let mut wtxn = env.write_txn()?;
86+
let db = env.create_poly_database(&mut wtxn, Some("ignored-data"))?;
87+
9088
db.put::<Str, Unit>(&mut wtxn, "hello", &())?;
9189
let ret = db.get::<Str, Unit>(&wtxn, "hello")?;
9290

@@ -97,26 +95,26 @@ fn main() -> Result<(), Box<dyn Error>> {
9795
println!("{:?}", ret);
9896
wtxn.commit()?;
9997

100-
// database opening and types are tested in a way
98+
// database opening and types are tested in a safe way
10199
//
102100
// we try to open a database twice with the same types
103-
let _db = env.create_poly_database(Some("ignored-data"))?;
101+
let mut wtxn = env.write_txn()?;
102+
let _db = env.create_poly_database(&mut wtxn, Some("ignored-data"))?;
104103

105104
// and here we try to open it with other types
106105
// asserting that it correctly returns an error
107106
//
108107
// NOTE that those types are not saved upon runs and
109108
// therefore types cannot be checked upon different runs,
110109
// the first database opening fix the types for this run.
111-
let result = env.create_database::<OwnedType<BEI64>, Unit>(Some("ignored-data"));
110+
let result = env.create_database::<OwnedType<BEI64>, Unit>(&mut wtxn, Some("ignored-data"));
112111
assert!(result.is_err());
113112

114113
// you can iterate over keys in order
115114
type BEI64 = I64<BE>;
116115

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

119-
let mut wtxn = env.write_txn()?;
120118
db.put::<OwnedType<BEI64>, Unit>(&mut wtxn, &BEI64::new(0), &())?;
121119
db.put::<OwnedType<BEI64>, Unit>(&mut wtxn, &BEI64::new(68), &())?;
122120
db.put::<OwnedType<BEI64>, Unit>(&mut wtxn, &BEI64::new(35), &())?;

heed/examples/all-types.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ fn main() -> Result<(), Box<dyn Error>> {
2222
//
2323
// like here we specify that the key will be an array of two i32
2424
// and the data will be an str
25-
let db: Database<OwnedType<[i32; 2]>, Str> = env.create_database(Some("kikou"))?;
26-
2725
let mut wtxn = env.write_txn()?;
26+
let db: Database<OwnedType<[i32; 2]>, Str> = env.create_database(&mut wtxn, Some("kikou"))?;
27+
2828
let _ret = db.put(&mut wtxn, &[2, 3], "what's up?")?;
2929
let ret: Option<&str> = db.get(&wtxn, &[2, 3])?;
3030

3131
println!("{:?}", ret);
3232
wtxn.commit()?;
3333

3434
// here the key will be an str and the data will be a slice of u8
35-
let db: Database<Str, ByteSlice> = env.create_database(Some("kiki"))?;
36-
3735
let mut wtxn = env.write_txn()?;
36+
let db: Database<Str, ByteSlice> = env.create_database(&mut wtxn, Some("kiki"))?;
37+
3838
let _ret = db.put(&mut wtxn, "hello", &[2, 3][..])?;
3939
let ret: Option<&[u8]> = db.get(&wtxn, "hello")?;
4040

@@ -47,9 +47,9 @@ fn main() -> Result<(), Box<dyn Error>> {
4747
string: &'a str,
4848
}
4949

50-
let db: Database<Str, SerdeBincode<Hello>> = env.create_database(Some("serde-bincode"))?;
51-
5250
let mut wtxn = env.write_txn()?;
51+
let db: Database<Str, SerdeBincode<Hello>> =
52+
env.create_database(&mut wtxn, Some("serde-bincode"))?;
5353

5454
let hello = Hello { string: "hi" };
5555
db.put(&mut wtxn, "hello", &hello)?;
@@ -59,9 +59,8 @@ fn main() -> Result<(), Box<dyn Error>> {
5959

6060
wtxn.commit()?;
6161

62-
let db: Database<Str, SerdeJson<Hello>> = env.create_database(Some("serde-json"))?;
63-
6462
let mut wtxn = env.write_txn()?;
63+
let db: Database<Str, SerdeJson<Hello>> = env.create_database(&mut wtxn, Some("serde-json"))?;
6564

6665
let hello = Hello { string: "hi" };
6766
db.put(&mut wtxn, "hello", &hello)?;
@@ -78,9 +77,9 @@ fn main() -> Result<(), Box<dyn Error>> {
7877
bytes: [u8; 12],
7978
}
8079

81-
let db: Database<Str, UnalignedType<ZeroBytes>> = env.create_database(Some("simple-struct"))?;
82-
8380
let mut wtxn = env.write_txn()?;
81+
let db: Database<Str, UnalignedType<ZeroBytes>> =
82+
env.create_database(&mut wtxn, Some("simple-struct"))?;
8483

8584
let zerobytes = ZeroBytes { bytes: [24; 12] };
8685
db.put(&mut wtxn, "zero", &zerobytes)?;
@@ -91,9 +90,9 @@ fn main() -> Result<(), Box<dyn Error>> {
9190
wtxn.commit()?;
9291

9392
// you can ignore the data
94-
let db: Database<Str, Unit> = env.create_database(Some("ignored-data"))?;
95-
9693
let mut wtxn = env.write_txn()?;
94+
let db: Database<Str, Unit> = env.create_database(&mut wtxn, Some("ignored-data"))?;
95+
9796
let _ret = db.put(&mut wtxn, "hello", &())?;
9897
let ret: Option<()> = db.get(&wtxn, "hello")?;
9998

@@ -104,26 +103,27 @@ fn main() -> Result<(), Box<dyn Error>> {
104103
println!("{:?}", ret);
105104
wtxn.commit()?;
106105

107-
// database opening and types are tested in a way
106+
// database opening and types are tested in a safe way
108107
//
109108
// we try to open a database twice with the same types
110-
let _db: Database<Str, Unit> = env.create_database(Some("ignored-data"))?;
109+
let mut wtxn = env.write_txn()?;
110+
let _db: Database<Str, Unit> = env.create_database(&mut wtxn, Some("ignored-data"))?;
111111

112112
// and here we try to open it with other types
113113
// asserting that it correctly returns an error
114114
//
115115
// NOTE that those types are not saved upon runs and
116116
// therefore types cannot be checked upon different runs,
117117
// the first database opening fix the types for this run.
118-
let result = env.create_database::<Str, OwnedSlice<i32>>(Some("ignored-data"));
118+
let result = env.create_database::<Str, OwnedSlice<i32>>(&mut wtxn, Some("ignored-data"));
119119
assert!(result.is_err());
120120

121121
// you can iterate over keys in order
122122
type BEI64 = I64<BE>;
123123

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

126-
let mut wtxn = env.write_txn()?;
127127
let _ret = db.put(&mut wtxn, &BEI64::new(0), &())?;
128128
let _ret = db.put(&mut wtxn, &BEI64::new(68), &())?;
129129
let _ret = db.put(&mut wtxn, &BEI64::new(35), &())?;

heed/examples/clear-database.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ fn main() -> Result<(), Box<dyn Error>> {
1818
.max_dbs(3)
1919
.open(env_path)?;
2020

21-
let db: Database<Str, Str> = env.create_database(Some("first"))?;
2221
let mut wtxn = env.write_txn()?;
22+
let db: Database<Str, Str> = env.create_database(&mut wtxn, Some("first"))?;
2323

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

heed/examples/cursor-append.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ fn main() -> Result<(), Box<dyn Error>> {
1818
.max_dbs(3)
1919
.open(env_path)?;
2020

21-
let first: Database<Str, Str> = env.create_database(Some("first"))?;
22-
let second: Database<Str, Str> = env.create_database(Some("second"))?;
23-
2421
let mut wtxn = env.write_txn()?;
22+
let first: Database<Str, Str> = env.create_database(&mut wtxn, Some("first"))?;
23+
let second: Database<Str, Str> = env.create_database(&mut wtxn, Some("second"))?;
2524

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

heed/examples/multi-env.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ fn main() -> Result<(), Box<dyn Error>> {
2121
.max_dbs(3000)
2222
.open(env2_path)?;
2323

24-
let db1: Database<Str, ByteSlice> = env1.create_database(Some("hello"))?;
25-
let db2: Database<OwnedType<u32>, OwnedType<u32>> = env2.create_database(Some("hello"))?;
24+
let mut wtxn = env1.write_txn()?;
25+
let db1: Database<Str, ByteSlice> = env1.create_database(&mut wtxn, Some("hello"))?;
26+
let db2: Database<OwnedType<u32>, OwnedType<u32>> =
27+
env2.create_database(&mut wtxn, Some("hello"))?;
2628

2729
// clear db
28-
let mut wtxn = env1.write_txn()?;
2930
db1.clear(&mut wtxn)?;
3031
wtxn.commit()?;
3132

heed/examples/nested.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@ fn main() -> Result<(), Box<dyn Error>> {
1616
.open(path)?;
1717

1818
// here the key will be an str and the data will be a slice of u8
19-
let db: Database<Str, ByteSlice> = env.create_database(None)?;
19+
let mut wtxn = env.write_txn()?;
20+
let db: Database<Str, ByteSlice> = env.create_database(&mut wtxn, None)?;
2021

2122
// clear db
22-
let mut wtxn = env.write_txn()?;
2323
db.clear(&mut wtxn)?;
2424
wtxn.commit()?;
2525

2626
// -----
2727

2828
let mut wtxn = env.write_txn()?;
29-
3029
let mut nwtxn = env.nested_write_txn(&mut wtxn)?;
3130

3231
db.put(&mut nwtxn, "what", &[4, 5][..])?;

0 commit comments

Comments
 (0)