Skip to content

Commit 975e730

Browse files
committed
Reintroduce the Simplified Open Options
1 parent bb8153c commit 975e730

File tree

5 files changed

+64
-24
lines changed

5 files changed

+64
-24
lines changed

examples/heed3-encryption.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn main() -> Result<(), Box<dyn Error>> {
2121
Argon2::default().hash_password_into(password.as_bytes(), salt.as_bytes(), &mut key)?;
2222

2323
// We open the environment
24-
let mut options = EnvOpenOptions::new().encrypt_with::<ChaCha20Poly1305>(key);
24+
let mut options = EnvOpenOptions::<ChaCha20Poly1305>::new_encrypted_with(key);
2525
let env = unsafe {
2626
options
2727
.map_size(10 * 1024 * 1024) // 10MB

heed/src/env/clear.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,17 @@ use synchronoise::SignalEvent;
1313

1414
#[cfg(windows)]
1515
use crate::env::OsStrExtLmdb as _;
16-
use crate::env::{canonicalize_path, Env, EnvEntry, EnvFlags, EnvInner, OPENED_ENV};
16+
use crate::env::{canonicalize_path, Env, EnvFlags, EnvInner, OPENED_ENV};
1717
use crate::mdb::ffi;
1818
use crate::mdb::lmdb_error::mdb_result;
1919
use crate::{Error, Result};
2020

21+
pub struct EnvEntry {
22+
pub(super) env: Option<Env>,
23+
pub(super) signal_event: Arc<SignalEvent>,
24+
pub(super) options: EnvOpenOptions,
25+
}
26+
2127
/// Options and flags which can be used to configure how an environment is opened.
2228
#[derive(Clone, Debug, PartialEq, Eq)]
2329
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]

heed/src/env/encrypted.rs

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,48 @@ use std::path::Path;
1010
use std::sync::Arc;
1111
use std::{fmt, io, ptr};
1212

13-
use aead::consts::U0;
1413
use aead::generic_array::typenum::Unsigned;
15-
use aead::generic_array::GenericArray;
16-
use aead::{AeadCore, AeadMutInPlace, Key, KeyInit, KeySizeUser, Nonce, Tag};
14+
use aead::{AeadCore, AeadMutInPlace, Key, KeyInit, Nonce, Tag};
1715
use synchronoise::SignalEvent;
1816

1917
#[cfg(windows)]
2018
use crate::env::OsStrExtLmdb as _;
21-
use crate::env::{canonicalize_path, Env, EnvEntry, EnvFlags, EnvInner, OPENED_ENV};
19+
use crate::env::{canonicalize_path, Env, EnvFlags, EnvInner, OPENED_ENV};
2220
use crate::mdb::ffi;
2321
use crate::mdb::lmdb_error::mdb_result;
2422
use crate::{Error, Result};
2523

24+
pub struct EnvEntry {
25+
pub(super) env: Option<Env>,
26+
pub(super) signal_event: Arc<SignalEvent>,
27+
pub(super) options: SimplifiedEncryptedOpenOptions,
28+
}
29+
30+
/// A simplified version of the options that were used to open a given [`Env`].
31+
#[derive(Debug, Clone, PartialEq, Eq)]
32+
pub struct SimplifiedEncryptedOpenOptions {
33+
/// The maximum size this [`Env`] with take in bytes or [`None`] if it was not specified.
34+
pub map_size: Option<usize>,
35+
/// The maximum number of concurrent readers or [`None`] if it was not specified.
36+
pub max_readers: Option<u32>,
37+
/// The maximum number of opened database or [`None`] if it was not specified.
38+
pub max_dbs: Option<u32>,
39+
/// The raw flags enabled for this [`Env`] or [`None`] if it was not specified.
40+
pub flags: u32,
41+
}
42+
43+
impl<E: AeadMutInPlace + KeyInit> From<&EnvOpenOptions<E>> for SimplifiedEncryptedOpenOptions {
44+
fn from(eoo: &EnvOpenOptions<E>) -> SimplifiedEncryptedOpenOptions {
45+
let EnvOpenOptions { encrypt: _, map_size, max_readers, max_dbs, flags } = eoo;
46+
SimplifiedEncryptedOpenOptions {
47+
map_size: *map_size,
48+
max_readers: *max_readers,
49+
max_dbs: *max_dbs,
50+
flags: flags.bits(),
51+
}
52+
}
53+
}
54+
2655
/// Options and flags which can be used to configure how an environment is opened.
2756
#[derive(Clone, PartialEq, Eq)]
2857
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
@@ -55,8 +84,8 @@ impl<E: AeadMutInPlace + KeyInit> EnvOpenOptions<E> {
5584
/// use std::path::Path;
5685
/// use argon2::Argon2;
5786
/// use chacha20poly1305::{ChaCha20Poly1305, Key};
58-
/// use heed3::types::*;
59-
/// use heed3::{EnvOpenOptions, Database};
87+
/// use heed3_encryption::types::*;
88+
/// use heed3_encryption::{EnvOpenOptions, Database};
6089
///
6190
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
6291
/// let env_path = Path::new("target").join("encrypt.mdb");
@@ -70,7 +99,7 @@ impl<E: AeadMutInPlace + KeyInit> EnvOpenOptions<E> {
7099
/// Argon2::default().hash_password_into(password.as_bytes(), salt.as_bytes(), &mut key)?;
71100
///
72101
/// // We open the environment
73-
/// let mut options = EnvOpenOptions::new().encrypt_with::<ChaCha20Poly1305>(key);
102+
/// let mut options = EnvOpenOptions::<ChaCha20Poly1305>::new_encrypted_with(key);
74103
/// let env = unsafe {
75104
/// options
76105
/// .map_size(10 * 1024 * 1024) // 10MB
@@ -94,13 +123,13 @@ impl<E: AeadMutInPlace + KeyInit> EnvOpenOptions<E> {
94123
///
95124
/// ## Example Showing limitations
96125
///
97-
/// ```compile_fail
126+
/// ```compile_fail,E0499
98127
/// use std::fs;
99128
/// use std::path::Path;
100129
/// use argon2::Argon2;
101130
/// use chacha20poly1305::{ChaCha20Poly1305, Key};
102-
/// use heed3::types::*;
103-
/// use heed3::{EnvOpenOptions, Database};
131+
/// use heed3_encryption::types::*;
132+
/// use heed3_encryption::{EnvOpenOptions, Database};
104133
///
105134
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
106135
/// let env_path = Path::new("target").join("encrypt.mdb");
@@ -114,7 +143,7 @@ impl<E: AeadMutInPlace + KeyInit> EnvOpenOptions<E> {
114143
/// Argon2::default().hash_password_into(password.as_bytes(), salt.as_bytes(), &mut key)?;
115144
///
116145
/// // We open the environment
117-
/// let mut options = EnvOpenOptions::new().encrypt_with::<ChaCha20Poly1305>(key);
146+
/// let mut options = EnvOpenOptions::<ChaCha20Poly1305>::new_encrypted_with(key);
118147
/// let env = unsafe {
119148
/// options
120149
/// .map_size(10 * 1024 * 1024) // 10MB
@@ -125,6 +154,11 @@ impl<E: AeadMutInPlace + KeyInit> EnvOpenOptions<E> {
125154
/// let key1 = "first-key";
126155
/// let key2 = "second-key";
127156
///
157+
/// // We create the database
158+
/// let mut wtxn = env.write_txn()?;
159+
/// let db: Database<Str, Str> = env.create_database(&mut wtxn, Some("first"))?;
160+
/// wtxn.commit()?;
161+
///
128162
/// // Declare the read transaction as mutable because LMDB, when using encryption,
129163
/// // does not allow keeping keys between reads due to the use of an internal cache.
130164
/// let mut rtxn = env.read_txn()?;
@@ -263,7 +297,7 @@ impl<E: AeadMutInPlace + KeyInit> EnvOpenOptions<E> {
263297
Ok(path) => path,
264298
};
265299

266-
let original_options = SimplifiedOpenOptions::from(self);
300+
let original_options = SimplifiedEncryptedOpenOptions::from(self);
267301
match lock.entry(path) {
268302
Entry::Occupied(entry) => {
269303
let env = entry.get().env.clone().ok_or(Error::DatabaseClosing)?;
@@ -282,7 +316,7 @@ impl<E: AeadMutInPlace + KeyInit> EnvOpenOptions<E> {
282316
let mut env: *mut ffi::MDB_env = ptr::null_mut();
283317
mdb_result(ffi::mdb_env_create(&mut env))?;
284318

285-
let encrypt_key = crate::into_val(self.encrypt);
319+
let encrypt_key = crate::into_val(&self.encrypt);
286320
mdb_result(ffi::mdb_env_set_encrypt(
287321
env,
288322
Some(encrypt_func_wrapper::<E>),

heed/src/env/mod.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,15 @@ use clear as sub_env;
3838
mod encrypted;
3939
#[cfg(all(master3, encryption))]
4040
use encrypted as sub_env;
41+
#[cfg(all(master3, encryption))]
42+
pub use encrypted::SimplifiedEncryptedOpenOptions;
4143

4244
/// The list of opened environments, the value is an optional environment, it is None
4345
/// when someone asks to close the environment, closing is a two-phase step, to make sure
4446
/// no one tries to open the same environment between these two phases.
4547
///
4648
/// Trying to open a None marked environment returns an error to the user trying to open it.
47-
static OPENED_ENV: Lazy<RwLock<HashMap<PathBuf, EnvEntry>>> = Lazy::new(RwLock::default);
48-
49-
pub struct EnvEntry {
50-
env: Option<Env>,
51-
signal_event: Arc<SignalEvent>,
52-
options: EnvOpenOptions,
53-
}
49+
static OPENED_ENV: Lazy<RwLock<HashMap<PathBuf, sub_env::EnvEntry>>> = Lazy::new(RwLock::default);
5450

5551
// Thanks to the mozilla/rkv project
5652
// Workaround the UNC path on Windows, see https://github.com/rust-lang/rust/issues/42869.
@@ -139,7 +135,7 @@ impl Drop for EnvInner {
139135

140136
match lock.remove(&self.path) {
141137
None => panic!("It seems another env closed this env before"),
142-
Some(EnvEntry { signal_event, .. }) => {
138+
Some(sub_env::EnvEntry { signal_event, .. }) => {
143139
unsafe {
144140
ffi::mdb_env_close(self.env);
145141
}
@@ -644,7 +640,7 @@ impl Env {
644640
let mut lock = OPENED_ENV.write().unwrap();
645641
match lock.get_mut(self.path()) {
646642
None => panic!("cannot find the env that we are trying to close"),
647-
Some(EnvEntry { env, signal_event, .. }) => {
643+
Some(sub_env::EnvEntry { env, signal_event, .. }) => {
648644
// We remove the env from the global list and replace it with a None.
649645
let _env = env.take();
650646
let signal_event = signal_event.clone();

heed/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,11 @@ pub enum Error {
153153
DatabaseClosing,
154154
/// Attempt to open [`Env`] with different options.
155155
BadOpenOptions {
156+
/// The simplified options that were used to originally open this env.
157+
#[cfg(all(master3, encryption))]
158+
options: env::SimplifiedEncryptedOpenOptions,
156159
/// The options that were used to originally open this env.
160+
#[cfg(not(all(master3, encryption)))]
157161
options: EnvOpenOptions,
158162
/// The env opened with the original options.
159163
env: Env,

0 commit comments

Comments
 (0)