Skip to content

Commit 12fa866

Browse files
committed
Fix all errors and most warnings
1 parent ef5f5bc commit 12fa866

File tree

7 files changed

+50
-100
lines changed

7 files changed

+50
-100
lines changed

heed/src/databases/database.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
341341
/// wtxn.commit()?;
342342
/// # Ok(()) }
343343
/// ```
344-
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
345344
pub fn get<'a, 'txn>(&self, txn: &'txn RoTxn, key: &'a KC::EItem) -> Result<Option<DC::DItem>>
346345
where
347346
KC: BytesEncode<'a>,
@@ -426,7 +425,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
426425
/// wtxn.commit()?;
427426
/// # Ok(()) }
428427
/// ```
429-
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
430428
pub fn get_duplicates<'a, 'txn>(
431429
&self,
432430
txn: &'txn RoTxn,
@@ -490,7 +488,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
490488
/// wtxn.commit()?;
491489
/// # Ok(()) }
492490
/// ```
493-
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
494491
pub fn get_lower_than<'a, 'txn>(
495492
&self,
496493
txn: &'txn RoTxn,
@@ -560,7 +557,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
560557
/// wtxn.commit()?;
561558
/// # Ok(()) }
562559
/// ```
563-
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
564560
pub fn get_lower_than_or_equal_to<'a, 'txn>(
565561
&self,
566562
txn: &'txn RoTxn,
@@ -634,7 +630,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
634630
/// wtxn.commit()?;
635631
/// # Ok(()) }
636632
/// ```
637-
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
638633
pub fn get_greater_than<'a, 'txn>(
639634
&self,
640635
txn: &'txn RoTxn,
@@ -707,7 +702,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
707702
/// wtxn.commit()?;
708703
/// # Ok(()) }
709704
/// ```
710-
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
711705
pub fn get_greater_than_or_equal_to<'a, 'txn>(
712706
&self,
713707
txn: &'txn RoTxn,
@@ -767,7 +761,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
767761
/// wtxn.commit()?;
768762
/// # Ok(()) }
769763
/// ```
770-
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
771764
pub fn first<'txn>(&self, txn: &'txn RoTxn) -> Result<Option<(KC::DItem, DC::DItem)>>
772765
where
773766
KC: BytesDecode<'txn>,
@@ -822,7 +815,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
822815
/// wtxn.commit()?;
823816
/// # Ok(()) }
824817
/// ```
825-
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
826818
pub fn last<'txn>(&self, txn: &'txn RoTxn) -> Result<Option<(KC::DItem, DC::DItem)>>
827819
where
828820
KC: BytesDecode<'txn>,
@@ -880,7 +872,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
880872
/// wtxn.commit()?;
881873
/// # Ok(()) }
882874
/// ```
883-
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
884875
pub fn len(&self, txn: &RoTxn) -> Result<u64> {
885876
self.stat(txn).map(|stat| stat.entries as u64)
886877
}
@@ -924,7 +915,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
924915
/// wtxn.commit()?;
925916
/// # Ok(()) }
926917
/// ```
927-
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
928918
pub fn is_empty(&self, txn: &RoTxn) -> Result<bool> {
929919
self.len(txn).map(|l| l == 0)
930920
}
@@ -967,7 +957,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
967957
/// wtxn.commit()?;
968958
/// # Ok(()) }
969959
/// ```
970-
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
971960
pub fn stat(&self, txn: &RoTxn) -> Result<DatabaseStat> {
972961
assert_eq_env_db_txn!(self, txn);
973962

@@ -1032,7 +1021,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
10321021
/// wtxn.commit()?;
10331022
/// # Ok(()) }
10341023
/// ```
1035-
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
10361024
pub fn iter<'txn>(&self, txn: &'txn RoTxn) -> Result<RoIter<'txn, KC, DC>> {
10371025
assert_eq_env_db_txn!(self, txn);
10381026
RoCursor::new(txn, self.dbi).map(|cursor| RoIter::new(cursor))
@@ -1134,7 +1122,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
11341122
/// wtxn.commit()?;
11351123
/// # Ok(()) }
11361124
/// ```
1137-
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
11381125
pub fn rev_iter<'txn>(&self, txn: &'txn RoTxn) -> Result<RoRevIter<'txn, KC, DC>> {
11391126
assert_eq_env_db_txn!(self, txn);
11401127

@@ -1241,7 +1228,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
12411228
/// wtxn.commit()?;
12421229
/// # Ok(()) }
12431230
/// ```
1244-
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
12451231
pub fn range<'a, 'txn, R>(
12461232
&self,
12471233
txn: &'txn RoTxn,
@@ -1416,7 +1402,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
14161402
/// wtxn.commit()?;
14171403
/// # Ok(()) }
14181404
/// ```
1419-
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
14201405
pub fn rev_range<'a, 'txn, R>(
14211406
&self,
14221407
txn: &'txn RoTxn,
@@ -1592,7 +1577,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
15921577
/// wtxn.commit()?;
15931578
/// # Ok(()) }
15941579
/// ```
1595-
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
15961580
pub fn prefix_iter<'a, 'txn>(
15971581
&self,
15981582
txn: &'txn RoTxn,
@@ -1725,7 +1709,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
17251709
/// wtxn.commit()?;
17261710
/// # Ok(()) }
17271711
/// ```
1728-
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
17291712
pub fn rev_prefix_iter<'a, 'txn>(
17301713
&self,
17311714
txn: &'txn RoTxn,

heed/src/databases/encrypted_database.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
use std::borrow::Cow;
2-
use std::ops::{Bound, RangeBounds};
3-
use std::{any, fmt, marker, mem, ptr};
1+
use std::ops::RangeBounds;
2+
use std::{any, fmt};
43

54
use heed_traits::{Comparator, LexicographicComparator};
6-
use types::{DecodeIgnore, LazyDecode};
5+
use types::LazyDecode;
76

8-
use crate::cursor::MoveOperation;
9-
use crate::env::DefaultComparator;
7+
use crate::envs::DefaultComparator;
108
use crate::iteration_method::MoveOnCurrentKeyDuplicates;
11-
use crate::mdb::error::mdb_result;
12-
use crate::mdb::ffi;
13-
use crate::mdb::lmdb_flags::{AllDatabaseFlags, DatabaseFlags};
9+
use crate::mdb::lmdb_flags::DatabaseFlags;
1410
use crate::*;
1511

1612
/// Options and flags which can be used to configure how a [`Database`] is opened.
@@ -61,8 +57,8 @@ pub struct EncryptedDatabaseOpenOptions<'e, 'n, KC, DC, C = DefaultComparator> {
6157

6258
impl<'e> EncryptedDatabaseOpenOptions<'e, 'static, Unspecified, Unspecified> {
6359
/// Create an options struct to open/create a database with specific flags.
64-
pub fn new(env: &'e Env) -> Self {
65-
EncryptedDatabaseOpenOptions { inner: DatabaseOpenOptions::new(env) }
60+
pub fn new(env: &'e EncryptedEnv) -> Self {
61+
EncryptedDatabaseOpenOptions { inner: DatabaseOpenOptions::new(&env.inner) }
6662
}
6763
}
6864

@@ -119,7 +115,7 @@ impl<'e, 'n, KC, DC, C> EncryptedDatabaseOpenOptions<'e, 'n, KC, DC, C> {
119115
DC: 'static,
120116
C: Comparator + 'static,
121117
{
122-
self.inner.open(rtxn)
118+
self.inner.open(rtxn).map(|opt| opt.map(EncryptedDatabase::new))
123119
}
124120

125121
/// Creates a typed database that can already exist in this environment.
@@ -137,7 +133,7 @@ impl<'e, 'n, KC, DC, C> EncryptedDatabaseOpenOptions<'e, 'n, KC, DC, C> {
137133
DC: 'static,
138134
C: Comparator + 'static,
139135
{
140-
self.inner.create(wtxn)
136+
self.inner.create(wtxn).map(EncryptedDatabase::new)
141137
}
142138
}
143139

@@ -266,8 +262,8 @@ pub struct EncryptedDatabase<KC, DC, C = DefaultComparator> {
266262
}
267263

268264
impl<KC, DC, C> EncryptedDatabase<KC, DC, C> {
269-
pub(crate) fn new(env_ident: usize, dbi: ffi::MDB_dbi) -> Database<KC, DC, C> {
270-
EncryptedDatabase { inner: Database::mew(env_ident, dbi) }
265+
pub(crate) fn new(inner: Database<KC, DC, C>) -> EncryptedDatabase<KC, DC, C> {
266+
EncryptedDatabase { inner }
271267
}
272268

273269
/// Retrieves the value associated with a key.
@@ -2074,7 +2070,7 @@ impl<KC, DC, C> EncryptedDatabase<KC, DC, C> {
20742070
KC: BytesEncode<'a> + BytesDecode<'txn>,
20752071
R: RangeBounds<KC::EItem>,
20762072
{
2077-
self.inner.delete_range(range)
2073+
self.inner.delete_range(txn, range)
20782074
}
20792075

20802076
/// Deletes all key/value pairs in this database.
@@ -2119,7 +2115,7 @@ impl<KC, DC, C> EncryptedDatabase<KC, DC, C> {
21192115
/// # Ok(()) }
21202116
/// ```
21212117
pub fn clear(&self, txn: &mut RwTxn) -> Result<()> {
2122-
self.inner.clear()
2118+
self.inner.clear(txn)
21232119
}
21242120

21252121
/// Change the codec types of this database, specifying the codecs.
@@ -2164,7 +2160,7 @@ impl<KC, DC, C> EncryptedDatabase<KC, DC, C> {
21642160
/// # Ok(()) }
21652161
/// ```
21662162
pub fn remap_types<KC2, DC2>(&self) -> EncryptedDatabase<KC2, DC2, C> {
2167-
EncryptedDatabase::new(self.inner.env_ident, self.inner.dbi)
2163+
EncryptedDatabase::new(self.inner.remap_types::<KC2, DC2>())
21682164
}
21692165

21702166
/// Change the key codec type of this database, specifying the new codec.

heed/src/envs/encrypted_env.rs

Lines changed: 23 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,22 @@
1-
use std::any::TypeId;
2-
use std::ffi::CString;
1+
use std::fmt;
32
use std::fs::File;
4-
use std::path::{Path, PathBuf};
5-
use std::ptr::{self, NonNull};
6-
use std::{fmt, io, mem};
7-
8-
use heed_traits::Comparator;
9-
use lmdb_master_sys::mdb_env_close;
10-
11-
use super::{
12-
custom_key_cmp_wrapper, get_file_fd, metadata_from_fd, DefaultComparator, EnvInfo, FlagSetMode,
13-
OPENED_ENV,
14-
};
15-
use crate::cursor::{MoveOperation, RoCursor};
16-
use crate::mdb::ffi::{self, MDB_env};
17-
use crate::mdb::lmdb_error::mdb_result;
18-
use crate::mdb::lmdb_flags::AllDatabaseFlags;
19-
use crate::{
20-
CompactionOption, Database, DatabaseOpenOptions, EnvFlags, Error, Result, RoTxn, RwTxn,
21-
Unspecified,
22-
};
3+
use std::panic::catch_unwind;
4+
use std::path::Path;
5+
6+
use aead::generic_array::typenum::Unsigned;
7+
use aead::{AeadMutInPlace, Key, KeyInit, Nonce, Tag};
8+
9+
use super::{Env, EnvInfo, FlagSetMode};
10+
use crate::databases::{EncryptedDatabase, EncryptedDatabaseOpenOptions};
11+
use crate::mdb::ffi::{self};
12+
use crate::{CompactionOption, EnvFlags, Result, RoTxn, RwTxn, Unspecified};
2313

2414
/// An environment handle constructed by using [`EnvOpenOptions::open_encrypted`].
2515
pub struct EncryptedEnv {
26-
inner: Env,
16+
pub(crate) inner: Env,
2717
}
2818

29-
impl Env {
30-
pub(crate) fn new(env_ptr: NonNull<MDB_env>, path: PathBuf) -> Env {
31-
Env { env_ptr, path }
32-
}
33-
34-
pub(crate) fn env_mut_ptr(&self) -> NonNull<ffi::MDB_env> {
35-
self.inner.env_mut_ptr()
36-
}
37-
19+
impl EncryptedEnv {
3820
/// The size of the data file on disk.
3921
///
4022
/// # Example
@@ -182,24 +164,6 @@ impl Env {
182164
options.create(wtxn)
183165
}
184166

185-
pub(crate) fn raw_init_database<C: Comparator + 'static>(
186-
&self,
187-
raw_txn: *mut ffi::MDB_txn,
188-
name: Option<&str>,
189-
flags: AllDatabaseFlags,
190-
) -> Result<u32> {
191-
self.inner.raw_init_database(raw_txn, name, flags)
192-
}
193-
194-
fn raw_open_dbi<C: Comparator + 'static>(
195-
&self,
196-
raw_txn: *mut ffi::MDB_txn,
197-
name: Option<&str>,
198-
flags: u32,
199-
) -> std::result::Result<u32, crate::mdb::lmdb_error::Error> {
200-
self.inner.raw_open_dbi(raw_txn, name, flags)
201-
}
202-
203167
/// Create a transaction with read and write access for use with the environment.
204168
///
205169
/// ## LMDB Limitations
@@ -346,7 +310,9 @@ unsafe impl Sync for EncryptedEnv {}
346310

347311
impl fmt::Debug for EncryptedEnv {
348312
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
349-
f.debug_struct("EncryptedEnv").field("path", &self.path.display()).finish_non_exhaustive()
313+
f.debug_struct("EncryptedEnv")
314+
.field("path", &self.inner.path.display())
315+
.finish_non_exhaustive()
350316
}
351317
}
352318

@@ -359,7 +325,7 @@ fn encrypt<A: AeadMutInPlace + KeyInit>(
359325
auth_out: &mut [u8],
360326
) -> aead::Result<()> {
361327
chipertext_out.copy_from_slice(plaintext);
362-
let key: &Key<A> = key.try_into().unwrap();
328+
let key: &Key<A> = key.into();
363329
let nonce: &Nonce<A> = if nonce.len() >= A::NonceSize::USIZE {
364330
nonce[..A::NonceSize::USIZE].into()
365331
} else {
@@ -380,20 +346,20 @@ fn decrypt<A: AeadMutInPlace + KeyInit>(
380346
auth_in: &[u8],
381347
) -> aead::Result<()> {
382348
output.copy_from_slice(chipher_text);
383-
let key: &Key<A> = key.try_into().unwrap();
349+
let key: &Key<A> = key.into();
384350
let nonce: &Nonce<A> = if nonce.len() >= A::NonceSize::USIZE {
385351
nonce[..A::NonceSize::USIZE].into()
386352
} else {
387353
return Err(aead::Error);
388354
};
389-
let tag: &Tag<A> = auth_in.try_into().unwrap();
355+
let tag: &Tag<A> = auth_in.into();
390356
let mut aead = A::new(key);
391357
aead.decrypt_in_place_detached(nonce, aad, output, tag)
392358
}
393359

394360
/// The wrapper function that is called by LMDB that directly calls
395361
/// the Rust idiomatic function internally.
396-
unsafe extern "C" fn encrypt_func_wrapper<E: AeadMutInPlace + KeyInit>(
362+
pub(crate) unsafe extern "C" fn encrypt_func_wrapper<E: AeadMutInPlace + KeyInit>(
397363
src: *const ffi::MDB_val,
398364
dst: *mut ffi::MDB_val,
399365
key_ptr: *const ffi::MDB_val,
@@ -415,16 +381,13 @@ unsafe extern "C" fn encrypt_func_wrapper<E: AeadMutInPlace + KeyInit>(
415381
let aad = [];
416382
let nonce = iv;
417383
let result = if encdec == 1 {
418-
encrypt::<E>(&key, nonce, &aad, input, output, auth)
384+
encrypt::<E>(key, nonce, &aad, input, output, auth)
419385
} else {
420-
decrypt::<E>(&key, nonce, &aad, input, output, auth)
386+
decrypt::<E>(key, nonce, &aad, input, output, auth)
421387
};
422388

423389
result.is_err() as i32
424390
});
425391

426-
match result {
427-
Ok(out) => out,
428-
Err(_) => 1,
429-
}
392+
result.unwrap_or(1)
430393
}

heed/src/envs/env.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::ptr::{self, NonNull};
66
use std::{fmt, io, mem};
77

88
use heed_traits::Comparator;
9-
use lmdb_master_sys::mdb_env_close;
109

1110
use super::{
1211
custom_key_cmp_wrapper, get_file_fd, metadata_from_fd, DefaultComparator, EnvInfo, FlagSetMode,
@@ -24,7 +23,7 @@ use crate::{
2423
/// An environment handle constructed by using [`EnvOpenOptions::open`].
2524
pub struct Env {
2625
env_ptr: NonNull<MDB_env>,
27-
path: PathBuf,
26+
pub(crate) path: PathBuf,
2827
}
2928

3029
impl Env {
@@ -466,7 +465,7 @@ impl fmt::Debug for Env {
466465

467466
impl Drop for Env {
468467
fn drop(&mut self) {
469-
unsafe { mdb_env_close(self.env_ptr.as_mut()) };
468+
unsafe { ffi::mdb_env_close(self.env_ptr.as_mut()) };
470469
let mut lock = OPENED_ENV.write().unwrap();
471470
debug_assert!(lock.remove(&self.path));
472471
}

0 commit comments

Comments
 (0)