Skip to content

Commit

Permalink
Fix all errors and most warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerollmops committed Nov 17, 2024
1 parent ef5f5bc commit 12fa866
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 100 deletions.
17 changes: 0 additions & 17 deletions heed/src/databases/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
pub fn get<'a, 'txn>(&self, txn: &'txn RoTxn, key: &'a KC::EItem) -> Result<Option<DC::DItem>>
where
KC: BytesEncode<'a>,
Expand Down Expand Up @@ -426,7 +425,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
pub fn get_duplicates<'a, 'txn>(
&self,
txn: &'txn RoTxn,
Expand Down Expand Up @@ -490,7 +488,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
pub fn get_lower_than<'a, 'txn>(
&self,
txn: &'txn RoTxn,
Expand Down Expand Up @@ -560,7 +557,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
pub fn get_lower_than_or_equal_to<'a, 'txn>(
&self,
txn: &'txn RoTxn,
Expand Down Expand Up @@ -634,7 +630,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
pub fn get_greater_than<'a, 'txn>(
&self,
txn: &'txn RoTxn,
Expand Down Expand Up @@ -707,7 +702,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
pub fn get_greater_than_or_equal_to<'a, 'txn>(
&self,
txn: &'txn RoTxn,
Expand Down Expand Up @@ -767,7 +761,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
pub fn first<'txn>(&self, txn: &'txn RoTxn) -> Result<Option<(KC::DItem, DC::DItem)>>
where
KC: BytesDecode<'txn>,
Expand Down Expand Up @@ -822,7 +815,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
pub fn last<'txn>(&self, txn: &'txn RoTxn) -> Result<Option<(KC::DItem, DC::DItem)>>
where
KC: BytesDecode<'txn>,
Expand Down Expand Up @@ -880,7 +872,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
pub fn len(&self, txn: &RoTxn) -> Result<u64> {
self.stat(txn).map(|stat| stat.entries as u64)
}
Expand Down Expand Up @@ -924,7 +915,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
pub fn is_empty(&self, txn: &RoTxn) -> Result<bool> {
self.len(txn).map(|l| l == 0)
}
Expand Down Expand Up @@ -967,7 +957,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
pub fn stat(&self, txn: &RoTxn) -> Result<DatabaseStat> {
assert_eq_env_db_txn!(self, txn);

Expand Down Expand Up @@ -1032,7 +1021,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
pub fn iter<'txn>(&self, txn: &'txn RoTxn) -> Result<RoIter<'txn, KC, DC>> {
assert_eq_env_db_txn!(self, txn);
RoCursor::new(txn, self.dbi).map(|cursor| RoIter::new(cursor))
Expand Down Expand Up @@ -1134,7 +1122,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
pub fn rev_iter<'txn>(&self, txn: &'txn RoTxn) -> Result<RoRevIter<'txn, KC, DC>> {
assert_eq_env_db_txn!(self, txn);

Expand Down Expand Up @@ -1241,7 +1228,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
pub fn range<'a, 'txn, R>(
&self,
txn: &'txn RoTxn,
Expand Down Expand Up @@ -1416,7 +1402,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
pub fn rev_range<'a, 'txn, R>(
&self,
txn: &'txn RoTxn,
Expand Down Expand Up @@ -1592,7 +1577,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
pub fn prefix_iter<'a, 'txn>(
&self,
txn: &'txn RoTxn,
Expand Down Expand Up @@ -1725,7 +1709,6 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
#[cfg_attr(master3, heed_master3_proc_macro::mut_read_txn(txn))]
pub fn rev_prefix_iter<'a, 'txn>(
&self,
txn: &'txn RoTxn,
Expand Down
32 changes: 14 additions & 18 deletions heed/src/databases/encrypted_database.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
use std::borrow::Cow;
use std::ops::{Bound, RangeBounds};
use std::{any, fmt, marker, mem, ptr};
use std::ops::RangeBounds;
use std::{any, fmt};

use heed_traits::{Comparator, LexicographicComparator};
use types::{DecodeIgnore, LazyDecode};
use types::LazyDecode;

use crate::cursor::MoveOperation;
use crate::env::DefaultComparator;
use crate::envs::DefaultComparator;
use crate::iteration_method::MoveOnCurrentKeyDuplicates;
use crate::mdb::error::mdb_result;
use crate::mdb::ffi;
use crate::mdb::lmdb_flags::{AllDatabaseFlags, DatabaseFlags};
use crate::mdb::lmdb_flags::DatabaseFlags;
use crate::*;

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

impl<'e> EncryptedDatabaseOpenOptions<'e, 'static, Unspecified, Unspecified> {
/// Create an options struct to open/create a database with specific flags.
pub fn new(env: &'e Env) -> Self {
EncryptedDatabaseOpenOptions { inner: DatabaseOpenOptions::new(env) }
pub fn new(env: &'e EncryptedEnv) -> Self {
EncryptedDatabaseOpenOptions { inner: DatabaseOpenOptions::new(&env.inner) }
}
}

Expand Down Expand Up @@ -119,7 +115,7 @@ impl<'e, 'n, KC, DC, C> EncryptedDatabaseOpenOptions<'e, 'n, KC, DC, C> {
DC: 'static,
C: Comparator + 'static,
{
self.inner.open(rtxn)
self.inner.open(rtxn).map(|opt| opt.map(EncryptedDatabase::new))
}

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

Expand Down Expand Up @@ -266,8 +262,8 @@ pub struct EncryptedDatabase<KC, DC, C = DefaultComparator> {
}

impl<KC, DC, C> EncryptedDatabase<KC, DC, C> {
pub(crate) fn new(env_ident: usize, dbi: ffi::MDB_dbi) -> Database<KC, DC, C> {
EncryptedDatabase { inner: Database::mew(env_ident, dbi) }
pub(crate) fn new(inner: Database<KC, DC, C>) -> EncryptedDatabase<KC, DC, C> {
EncryptedDatabase { inner }
}

/// Retrieves the value associated with a key.
Expand Down Expand Up @@ -2074,7 +2070,7 @@ impl<KC, DC, C> EncryptedDatabase<KC, DC, C> {
KC: BytesEncode<'a> + BytesDecode<'txn>,
R: RangeBounds<KC::EItem>,
{
self.inner.delete_range(range)
self.inner.delete_range(txn, range)
}

/// Deletes all key/value pairs in this database.
Expand Down Expand Up @@ -2119,7 +2115,7 @@ impl<KC, DC, C> EncryptedDatabase<KC, DC, C> {
/// # Ok(()) }
/// ```
pub fn clear(&self, txn: &mut RwTxn) -> Result<()> {
self.inner.clear()
self.inner.clear(txn)
}

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

/// Change the key codec type of this database, specifying the new codec.
Expand Down
83 changes: 23 additions & 60 deletions heed/src/envs/encrypted_env.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,22 @@
use std::any::TypeId;
use std::ffi::CString;
use std::fmt;
use std::fs::File;
use std::path::{Path, PathBuf};
use std::ptr::{self, NonNull};
use std::{fmt, io, mem};

use heed_traits::Comparator;
use lmdb_master_sys::mdb_env_close;

use super::{
custom_key_cmp_wrapper, get_file_fd, metadata_from_fd, DefaultComparator, EnvInfo, FlagSetMode,
OPENED_ENV,
};
use crate::cursor::{MoveOperation, RoCursor};
use crate::mdb::ffi::{self, MDB_env};
use crate::mdb::lmdb_error::mdb_result;
use crate::mdb::lmdb_flags::AllDatabaseFlags;
use crate::{
CompactionOption, Database, DatabaseOpenOptions, EnvFlags, Error, Result, RoTxn, RwTxn,
Unspecified,
};
use std::panic::catch_unwind;
use std::path::Path;

use aead::generic_array::typenum::Unsigned;
use aead::{AeadMutInPlace, Key, KeyInit, Nonce, Tag};

use super::{Env, EnvInfo, FlagSetMode};
use crate::databases::{EncryptedDatabase, EncryptedDatabaseOpenOptions};
use crate::mdb::ffi::{self};
use crate::{CompactionOption, EnvFlags, Result, RoTxn, RwTxn, Unspecified};

/// An environment handle constructed by using [`EnvOpenOptions::open_encrypted`].
pub struct EncryptedEnv {
inner: Env,
pub(crate) inner: Env,
}

impl Env {
pub(crate) fn new(env_ptr: NonNull<MDB_env>, path: PathBuf) -> Env {
Env { env_ptr, path }
}

pub(crate) fn env_mut_ptr(&self) -> NonNull<ffi::MDB_env> {
self.inner.env_mut_ptr()
}

impl EncryptedEnv {
/// The size of the data file on disk.
///
/// # Example
Expand Down Expand Up @@ -182,24 +164,6 @@ impl Env {
options.create(wtxn)
}

pub(crate) fn raw_init_database<C: Comparator + 'static>(
&self,
raw_txn: *mut ffi::MDB_txn,
name: Option<&str>,
flags: AllDatabaseFlags,
) -> Result<u32> {
self.inner.raw_init_database(raw_txn, name, flags)
}

fn raw_open_dbi<C: Comparator + 'static>(
&self,
raw_txn: *mut ffi::MDB_txn,
name: Option<&str>,
flags: u32,
) -> std::result::Result<u32, crate::mdb::lmdb_error::Error> {
self.inner.raw_open_dbi(raw_txn, name, flags)
}

/// Create a transaction with read and write access for use with the environment.
///
/// ## LMDB Limitations
Expand Down Expand Up @@ -346,7 +310,9 @@ unsafe impl Sync for EncryptedEnv {}

impl fmt::Debug for EncryptedEnv {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("EncryptedEnv").field("path", &self.path.display()).finish_non_exhaustive()
f.debug_struct("EncryptedEnv")
.field("path", &self.inner.path.display())
.finish_non_exhaustive()
}
}

Expand All @@ -359,7 +325,7 @@ fn encrypt<A: AeadMutInPlace + KeyInit>(
auth_out: &mut [u8],
) -> aead::Result<()> {
chipertext_out.copy_from_slice(plaintext);
let key: &Key<A> = key.try_into().unwrap();
let key: &Key<A> = key.into();
let nonce: &Nonce<A> = if nonce.len() >= A::NonceSize::USIZE {
nonce[..A::NonceSize::USIZE].into()
} else {
Expand All @@ -380,20 +346,20 @@ fn decrypt<A: AeadMutInPlace + KeyInit>(
auth_in: &[u8],
) -> aead::Result<()> {
output.copy_from_slice(chipher_text);
let key: &Key<A> = key.try_into().unwrap();
let key: &Key<A> = key.into();
let nonce: &Nonce<A> = if nonce.len() >= A::NonceSize::USIZE {
nonce[..A::NonceSize::USIZE].into()
} else {
return Err(aead::Error);
};
let tag: &Tag<A> = auth_in.try_into().unwrap();
let tag: &Tag<A> = auth_in.into();
let mut aead = A::new(key);
aead.decrypt_in_place_detached(nonce, aad, output, tag)
}

/// The wrapper function that is called by LMDB that directly calls
/// the Rust idiomatic function internally.
unsafe extern "C" fn encrypt_func_wrapper<E: AeadMutInPlace + KeyInit>(
pub(crate) unsafe extern "C" fn encrypt_func_wrapper<E: AeadMutInPlace + KeyInit>(
src: *const ffi::MDB_val,
dst: *mut ffi::MDB_val,
key_ptr: *const ffi::MDB_val,
Expand All @@ -415,16 +381,13 @@ unsafe extern "C" fn encrypt_func_wrapper<E: AeadMutInPlace + KeyInit>(
let aad = [];
let nonce = iv;
let result = if encdec == 1 {
encrypt::<E>(&key, nonce, &aad, input, output, auth)
encrypt::<E>(key, nonce, &aad, input, output, auth)
} else {
decrypt::<E>(&key, nonce, &aad, input, output, auth)
decrypt::<E>(key, nonce, &aad, input, output, auth)
};

result.is_err() as i32
});

match result {
Ok(out) => out,
Err(_) => 1,
}
result.unwrap_or(1)
}
5 changes: 2 additions & 3 deletions heed/src/envs/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::ptr::{self, NonNull};
use std::{fmt, io, mem};

use heed_traits::Comparator;
use lmdb_master_sys::mdb_env_close;

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

impl Env {
Expand Down Expand Up @@ -466,7 +465,7 @@ impl fmt::Debug for Env {

impl Drop for Env {
fn drop(&mut self) {
unsafe { mdb_env_close(self.env_ptr.as_mut()) };
unsafe { ffi::mdb_env_close(self.env_ptr.as_mut()) };
let mut lock = OPENED_ENV.write().unwrap();
debug_assert!(lock.remove(&self.path));
}
Expand Down
Loading

0 comments on commit 12fa866

Please sign in to comment.