Skip to content

Commit 26e7467

Browse files
committed
Move the examples back into their folders
1 parent 62d1115 commit 26e7467

15 files changed

+97
-25
lines changed

convert-to-heed3.sh

+12-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ set -e
1414
# the heed folder...
1515
if [[ "$OSTYPE" == "cygwin" || "$OSTYPE" == "msys" ]]; then
1616
cp heed3\\Cargo.toml heed\\Cargo.toml
17+
rm -rf heed\\examples
18+
cp -R heed3\\examples heed\\examples
1719
else
1820
cp heed3/Cargo.toml heed/Cargo.toml
21+
rm -rf heed/examples
22+
cp -R heed3/examples heed/examples
1923
fi
2024

2125
# ...and replaces the `heed::` string by the `heed3::` one.
22-
for file in $(find heed/src -type f -name "*.rs") examples/prev-snapshot.rs; do
26+
for file in $(find heed/src -type f -name "*.rs"); do
2327
if [[ "$OSTYPE" == "darwin"* ]]; then
2428
sed -i '' 's/heed::/heed3::/g' "$file"
2529
else
@@ -31,6 +35,13 @@ done
3135
git config --local user.email "[email protected]"
3236
git config --local user.name "The CI"
3337

38+
# Adds the new examples to the changes
39+
if [[ "$OSTYPE" == "cygwin" || "$OSTYPE" == "msys" ]]; then
40+
git add heed\\examples
41+
else
42+
git add heed/examples
43+
fi
44+
3445
# But only commit if there is something to commit
3546
git diff -q --exit-code || git commit -am 'remove-me: heed3 changes generated by the convert-to-heed3.sh script'
3647

heed/Cargo.toml

-20
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@ keywords = ["lmdb", "database", "storage", "typed"]
99
categories = ["database", "data-structures"]
1010
readme = "../README.md"
1111
edition = "2021"
12-
include = [
13-
"../examples/all-types.rs",
14-
"../examples/clear-database.rs",
15-
"../examples/cursor-append.rs",
16-
"../examples/custom-comparator.rs",
17-
"../examples/multi-env.rs",
18-
"../examples/nested.rs",
19-
"../examples/rmp-serde.rs",
20-
]
2112

2213
[dependencies]
2314
bitflags = { version = "2.9.0", features = ["serde"] }
@@ -113,41 +104,30 @@ longer-keys = ["lmdb-master-sys/longer-keys"]
113104
# <https://github.com/LMDB/lmdb/blob/f20e41de09d97e4461946b7e26ec831d0c24fac7/libraries/liblmdb/mdb.c#L205-L218>
114105
use-valgrind = ["lmdb-master-sys/use-valgrind"]
115106

116-
# Examples are located outside the standard heed/examples directory to prevent
117-
# conflicts between heed3 and heed examples when working on both crates.
118107
[[example]]
119108
name = "all-types"
120-
path = "../examples/all-types.rs"
121109

122110
[[example]]
123111
name = "clear-database"
124-
path = "../examples/clear-database.rs"
125112

126113
[[example]]
127114
name = "cursor-append"
128-
path = "../examples/cursor-append.rs"
129115

130116
[[example]]
131117
name = "custom-comparator"
132-
path = "../examples/custom-comparator.rs"
133118

134119
[[example]]
135120
name = "custom-dupsort-comparator"
136-
path = "../examples/custom-dupsort-comparator.rs"
137121

138122
[[example]]
139123
name = "multi-env"
140-
path = "../examples/multi-env.rs"
141124

142125
[[example]]
143126
name = "nested"
144-
path = "../examples/nested.rs"
145127

146128
[[example]]
147129
name = "prev-snapshot"
148-
path = "../examples/prev-snapshot.rs"
149130

150131
[[example]]
151132
name = "rmp-serde"
152-
path = "../examples/rmp-serde.rs"
153133
required-features = ["serde-rmp"]
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

heed3/Cargo.toml

-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ keywords = ["lmdb", "database", "storage", "typed", "encryption"]
99
categories = ["database", "data-structures"]
1010
readme = "../README.md"
1111
edition = "2021"
12-
include = ["../examples/heed3-encrypted.rs", "../examples/heed3-all-types.rs"]
1312

1413
[dependencies]
1514
aead = { version = "0.5.2", default-features = false }
@@ -113,12 +112,9 @@ use-valgrind = ["lmdb-master3-sys/use-valgrind"]
113112
# conflicts between heed3 and heed examples when working on both crates.
114113
[[example]]
115114
name = "prev-snapshot"
116-
path = "../examples/prev-snapshot.rs"
117115

118116
[[example]]
119117
name = "heed3-encrypted"
120-
path = "../examples/heed3-encrypted.rs"
121118

122119
[[example]]
123120
name = "heed3-all-types"
124-
path = "../examples/heed3-all-types.rs"
File renamed without changes.
File renamed without changes.

heed3/examples/prev-snapshot.rs

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use std::error::Error;
2+
3+
use heed3::types::*;
4+
use heed3::{Database, EnvFlags, EnvOpenOptions};
5+
6+
// In this test we are checking that we can move to a previous environement snapshot.
7+
fn main() -> Result<(), Box<dyn Error>> {
8+
let env_path = tempfile::tempdir()?;
9+
10+
let env = unsafe {
11+
EnvOpenOptions::new()
12+
.map_size(10 * 1024 * 1024) // 10MB
13+
.max_dbs(3)
14+
.open(&env_path)?
15+
};
16+
17+
let mut wtxn = env.write_txn()?;
18+
let db: Database<Str, Str> = env.create_database(&mut wtxn, None)?;
19+
20+
// We fill the db database with entries.
21+
db.put(&mut wtxn, "I am here", "to test things")?;
22+
db.put(&mut wtxn, "I am here too", "for the same purpose")?;
23+
24+
wtxn.commit()?;
25+
26+
env.prepare_for_closing().wait();
27+
28+
// We can get the env state from before the last commit
29+
// and therefore see an empty env.
30+
let env = unsafe {
31+
EnvOpenOptions::new()
32+
.map_size(10 * 1024 * 1024) // 10MB
33+
.max_dbs(3)
34+
.flags(EnvFlags::PREV_SNAPSHOT)
35+
.open(&env_path)?
36+
};
37+
38+
let mut wtxn = env.write_txn()?;
39+
let db: Database<Str, Str> = env.create_database(&mut wtxn, None)?;
40+
41+
assert!(db.is_empty(&wtxn)?);
42+
43+
wtxn.abort();
44+
env.prepare_for_closing().wait();
45+
46+
// However, if we don't commit we can still get
47+
// back the latest version of the env.
48+
let env = unsafe {
49+
EnvOpenOptions::new()
50+
.map_size(10 * 1024 * 1024) // 10MB
51+
.max_dbs(3)
52+
.open(&env_path)?
53+
};
54+
55+
let mut wtxn = env.write_txn()?;
56+
let db: Database<Str, Str> = env.create_database(&mut wtxn, None)?;
57+
58+
assert_eq!(db.get(&wtxn, "I am here")?, Some("to test things"));
59+
assert_eq!(db.get(&wtxn, "I am here too")?, Some("for the same purpose"));
60+
61+
// And write new stuff in the env.
62+
db.put(&mut wtxn, "I will fade away", "I am so sad")?;
63+
64+
wtxn.commit()?;
65+
env.prepare_for_closing().wait();
66+
67+
// Once again we can get back the previous version
68+
// of the env and see some entries disappear.
69+
let env = unsafe {
70+
EnvOpenOptions::new()
71+
.map_size(10 * 1024 * 1024) // 10MB
72+
.max_dbs(3)
73+
.flags(EnvFlags::PREV_SNAPSHOT)
74+
.open(&env_path)?
75+
};
76+
77+
let rtxn = env.read_txn()?;
78+
let db: Database<Str, Str> = env.open_database(&rtxn, None)?.unwrap();
79+
80+
assert_eq!(db.get(&rtxn, "I am here")?, Some("to test things"));
81+
assert_eq!(db.get(&rtxn, "I am here too")?, Some("for the same purpose"));
82+
assert_eq!(db.get(&rtxn, "I will fade away")?, None);
83+
84+
Ok(())
85+
}

0 commit comments

Comments
 (0)