Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce AnyTls #315

Merged
merged 9 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion heed/src/cookbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@
//! }
//!
//! impl<'t> ImmutableMap<'t> {
//! fn from_db<T>(rtxn: &'t RoTxn<T>, db: Database<Str, Str>) -> heed::Result<Self> {
//! fn from_db(rtxn: &'t RoTxn, db: Database<Str, Str>) -> heed::Result<Self> {
//! let mut map = HashMap::new();
//! for result in db.iter(rtxn)? {
//! let (k, v) = result?;
Expand Down
17 changes: 8 additions & 9 deletions heed/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ use crate::mdb::error::mdb_result;
use crate::mdb::ffi;
use crate::*;

pub struct RoCursor<'txn, T> {
pub struct RoCursor<'txn> {
cursor: *mut ffi::MDB_cursor,
_marker: marker::PhantomData<&'txn T>,
_marker: marker::PhantomData<&'txn ()>,
}

impl<'txn, T> RoCursor<'txn, T> {
// TODO should I ask for a &mut RoTxn<'_, T>, here?
pub(crate) fn new(txn: &'txn RoTxn<'_, T>, dbi: ffi::MDB_dbi) -> Result<RoCursor<'txn, T>> {
impl<'txn> RoCursor<'txn> {
pub(crate) fn new<T>(txn: &'txn RoTxn<T>, dbi: ffi::MDB_dbi) -> Result<RoCursor<'txn>> {
let mut cursor: *mut ffi::MDB_cursor = ptr::null_mut();
let mut txn = txn.txn.unwrap();
let mut txn = txn.txn_ptr();
unsafe { mdb_result(ffi::mdb_cursor_open(txn.as_mut(), dbi, &mut cursor))? }
Ok(RoCursor { cursor, _marker: marker::PhantomData })
}
Expand Down Expand Up @@ -237,14 +236,14 @@ impl<'txn, T> RoCursor<'txn, T> {
}
}

impl<T> Drop for RoCursor<'_, T> {
impl Drop for RoCursor<'_> {
fn drop(&mut self) {
unsafe { ffi::mdb_cursor_close(self.cursor) }
}
}

pub struct RwCursor<'txn> {
cursor: RoCursor<'txn, WithoutTls>,
cursor: RoCursor<'txn>,
}

impl<'txn> RwCursor<'txn> {
Expand Down Expand Up @@ -404,7 +403,7 @@ impl<'txn> RwCursor<'txn> {
}

impl<'txn> Deref for RwCursor<'txn> {
type Target = RoCursor<'txn, WithoutTls>;
type Target = RoCursor<'txn>;

fn deref(&self) -> &Self::Target {
&self.cursor
Expand Down
93 changes: 44 additions & 49 deletions heed/src/databases/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ impl<'e, 'n, T, KC, DC, C> DatabaseOpenOptions<'e, 'n, T, KC, DC, C> {
///
/// If not done, you might raise `Io(Os { code: 22, kind: InvalidInput, message: "Invalid argument" })`
/// known as `EINVAL`.
pub fn open(&self, rtxn: &RoTxn<T>) -> Result<Option<Database<KC, DC, C>>>
pub fn open(&self, rtxn: &RoTxn) -> Result<Option<Database<KC, DC, C>>>
where
KC: 'static,
DC: 'static,
C: Comparator + 'static,
{
assert_eq_env_txn!(self.env, rtxn);

match self.env.raw_init_database::<C>(rtxn.txn.unwrap(), self.name, self.flags) {
match self.env.raw_init_database::<C>(rtxn.txn_ptr(), self.name, self.flags) {
Ok(dbi) => Ok(Some(Database::new(self.env.env_mut_ptr().as_ptr() as _, dbi))),
Err(Error::Mdb(e)) if e.not_found() => Ok(None),
Err(e) => Err(e),
Expand All @@ -165,7 +165,7 @@ impl<'e, 'n, T, KC, DC, C> DatabaseOpenOptions<'e, 'n, T, KC, DC, C> {
assert_eq_env_txn!(self.env, wtxn);

let flags = self.flags | AllDatabaseFlags::CREATE;
match self.env.raw_init_database::<C>(wtxn.txn.txn.unwrap(), self.name, flags) {
match self.env.raw_init_database::<C>(wtxn.txn_ptr(), self.name, flags) {
Ok(dbi) => Ok(Database::new(self.env.env_mut_ptr().as_ptr() as _, dbi)),
Err(e) => Err(e),
}
Expand Down Expand Up @@ -340,11 +340,7 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
pub fn get<'a, 'txn, T>(
&self,
txn: &'txn RoTxn<T>,
key: &'a KC::EItem,
) -> Result<Option<DC::DItem>>
pub fn get<'a, 'txn>(&self, txn: &'txn RoTxn, key: &'a KC::EItem) -> Result<Option<DC::DItem>>
where
KC: BytesEncode<'a>,
DC: BytesDecode<'txn>,
Expand All @@ -358,7 +354,7 @@ impl<KC, DC, C> Database<KC, DC, C> {

let result = unsafe {
mdb_result(ffi::mdb_get(
txn.txn.unwrap().as_mut(),
txn.txn_ptr().as_mut(),
self.dbi,
&mut key_val,
data_val.as_mut_ptr(),
Expand Down Expand Up @@ -429,11 +425,11 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
pub fn get_duplicates<'a, 'txn, T>(
pub fn get_duplicates<'a, 'txn>(
&self,
txn: &'txn RoTxn<T>,
txn: &'txn RoTxn,
key: &'a KC::EItem,
) -> Result<Option<RoIter<'txn, T, KC, DC, MoveOnCurrentKeyDuplicates>>>
) -> Result<Option<RoIter<'txn, KC, DC, MoveOnCurrentKeyDuplicates>>>
where
KC: BytesEncode<'a>,
{
Expand Down Expand Up @@ -492,9 +488,9 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
pub fn get_lower_than<'a, 'txn, T>(
pub fn get_lower_than<'a, 'txn>(
&self,
txn: &'txn RoTxn<T>,
txn: &'txn RoTxn,
key: &'a KC::EItem,
) -> Result<Option<(KC::DItem, DC::DItem)>>
where
Expand Down Expand Up @@ -561,9 +557,9 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
pub fn get_lower_than_or_equal_to<'a, 'txn, T>(
pub fn get_lower_than_or_equal_to<'a, 'txn>(
&self,
txn: &'txn RoTxn<T>,
txn: &'txn RoTxn,
key: &'a KC::EItem,
) -> Result<Option<(KC::DItem, DC::DItem)>>
where
Expand Down Expand Up @@ -634,9 +630,9 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
pub fn get_greater_than<'a, 'txn, T>(
pub fn get_greater_than<'a, 'txn>(
&self,
txn: &'txn RoTxn<T>,
txn: &'txn RoTxn,
key: &'a KC::EItem,
) -> Result<Option<(KC::DItem, DC::DItem)>>
where
Expand Down Expand Up @@ -706,9 +702,9 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
pub fn get_greater_than_or_equal_to<'a, 'txn, T>(
pub fn get_greater_than_or_equal_to<'a, 'txn>(
&self,
txn: &'txn RoTxn<T>,
txn: &'txn RoTxn,
key: &'a KC::EItem,
) -> Result<Option<(KC::DItem, DC::DItem)>>
where
Expand Down Expand Up @@ -765,7 +761,7 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
pub fn first<'txn, T>(&self, txn: &'txn RoTxn<T>) -> Result<Option<(KC::DItem, DC::DItem)>>
pub fn first<'txn>(&self, txn: &'txn RoTxn) -> Result<Option<(KC::DItem, DC::DItem)>>
where
KC: BytesDecode<'txn>,
DC: BytesDecode<'txn>,
Expand Down Expand Up @@ -819,7 +815,7 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
pub fn last<'txn, T>(&self, txn: &'txn RoTxn<T>) -> Result<Option<(KC::DItem, DC::DItem)>>
pub fn last<'txn>(&self, txn: &'txn RoTxn) -> Result<Option<(KC::DItem, DC::DItem)>>
where
KC: BytesDecode<'txn>,
DC: BytesDecode<'txn>,
Expand Down Expand Up @@ -876,7 +872,7 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
pub fn len<T>(&self, txn: &RoTxn<T>) -> Result<u64> {
pub fn len(&self, txn: &RoTxn) -> Result<u64> {
self.stat(txn).map(|stat| stat.entries as u64)
}

Expand Down Expand Up @@ -919,7 +915,7 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
pub fn is_empty<T>(&self, txn: &RoTxn<T>) -> Result<bool> {
pub fn is_empty(&self, txn: &RoTxn) -> Result<bool> {
self.len(txn).map(|l| l == 0)
}

Expand Down Expand Up @@ -961,12 +957,12 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
pub fn stat<T>(&self, txn: &RoTxn<T>) -> Result<DatabaseStat> {
pub fn stat(&self, txn: &RoTxn) -> Result<DatabaseStat> {
assert_eq_env_db_txn!(self, txn);

let mut db_stat = mem::MaybeUninit::uninit();
let result = unsafe {
mdb_result(ffi::mdb_stat(txn.txn.unwrap().as_mut(), self.dbi, db_stat.as_mut_ptr()))
mdb_result(ffi::mdb_stat(txn.txn_ptr().as_mut(), self.dbi, db_stat.as_mut_ptr()))
};

match result {
Expand Down Expand Up @@ -1026,7 +1022,7 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
pub fn iter<'txn, T>(&self, txn: &'txn RoTxn<T>) -> Result<RoIter<'txn, T, KC, DC>> {
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 @@ -1128,7 +1124,7 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
pub fn rev_iter<'txn, T>(&self, txn: &'txn RoTxn<T>) -> Result<RoRevIter<'txn, T, KC, DC>> {
pub fn rev_iter<'txn>(&self, txn: &'txn RoTxn) -> Result<RoRevIter<'txn, KC, DC>> {
assert_eq_env_db_txn!(self, txn);

RoCursor::new(txn, self.dbi).map(|cursor| RoRevIter::new(cursor))
Expand Down Expand Up @@ -1235,11 +1231,11 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
pub fn range<'a, 'txn, R, T>(
pub fn range<'a, 'txn, R>(
&self,
txn: &'txn RoTxn<T>,
txn: &'txn RoTxn,
range: &'a R,
) -> Result<RoRange<'txn, T, KC, DC, C>>
) -> Result<RoRange<'txn, KC, DC, C>>
where
KC: BytesEncode<'a>,
R: RangeBounds<KC::EItem>,
Expand Down Expand Up @@ -1408,11 +1404,11 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
pub fn rev_range<'a, 'txn, R, T>(
pub fn rev_range<'a, 'txn, R>(
&self,
txn: &'txn RoTxn<T>,
txn: &'txn RoTxn,
range: &'a R,
) -> Result<RoRevRange<'txn, T, KC, DC, C>>
) -> Result<RoRevRange<'txn, KC, DC, C>>
where
KC: BytesEncode<'a>,
R: RangeBounds<KC::EItem>,
Expand Down Expand Up @@ -1583,11 +1579,11 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
pub fn prefix_iter<'a, 'txn, T>(
pub fn prefix_iter<'a, 'txn>(
&self,
txn: &'txn RoTxn<T>,
txn: &'txn RoTxn,
prefix: &'a KC::EItem,
) -> Result<RoPrefix<'txn, T, KC, DC, C>>
) -> Result<RoPrefix<'txn, KC, DC, C>>
where
KC: BytesEncode<'a>,
C: LexicographicComparator,
Expand Down Expand Up @@ -1716,11 +1712,11 @@ impl<KC, DC, C> Database<KC, DC, C> {
/// wtxn.commit()?;
/// # Ok(()) }
/// ```
pub fn rev_prefix_iter<'a, 'txn, T>(
pub fn rev_prefix_iter<'a, 'txn>(
&self,
txn: &'txn RoTxn<'_, T>,
txn: &'txn RoTxn,
prefix: &'a KC::EItem,
) -> Result<RoRevPrefix<'txn, T, KC, DC, C>>
) -> Result<RoRevPrefix<'txn, KC, DC, C>>
where
KC: BytesEncode<'a>,
C: LexicographicComparator,
Expand Down Expand Up @@ -1854,7 +1850,7 @@ impl<KC, DC, C> Database<KC, DC, C> {

unsafe {
mdb_result(ffi::mdb_put(
txn.txn.txn.unwrap().as_mut(),
txn.txn_ptr().as_mut(),
self.dbi,
&mut key_val,
&mut data_val,
Expand Down Expand Up @@ -1921,7 +1917,7 @@ impl<KC, DC, C> Database<KC, DC, C> {

unsafe {
mdb_result(ffi::mdb_put(
txn.txn.txn.unwrap().as_mut(),
txn.txn_ptr().as_mut(),
self.dbi,
&mut key_val,
&mut reserved,
Expand Down Expand Up @@ -2019,7 +2015,7 @@ impl<KC, DC, C> Database<KC, DC, C> {

unsafe {
mdb_result(ffi::mdb_put(
txn.txn.txn.unwrap().as_mut(),
txn.txn_ptr().as_mut(),
self.dbi,
&mut key_val,
&mut data_val,
Expand Down Expand Up @@ -2132,7 +2128,7 @@ impl<KC, DC, C> Database<KC, DC, C> {

let result = unsafe {
mdb_result(ffi::mdb_put(
txn.txn.txn.unwrap().as_mut(),
txn.txn_ptr().as_mut(),
self.dbi,
&mut key_val,
&mut data_val,
Expand Down Expand Up @@ -2288,7 +2284,7 @@ impl<KC, DC, C> Database<KC, DC, C> {

let result = unsafe {
mdb_result(ffi::mdb_put(
txn.txn.txn.unwrap().as_mut(),
txn.txn.txn_ptr().as_mut(),
self.dbi,
&mut key_val,
&mut reserved,
Expand Down Expand Up @@ -2371,7 +2367,7 @@ impl<KC, DC, C> Database<KC, DC, C> {

let result = unsafe {
mdb_result(ffi::mdb_del(
txn.txn.txn.unwrap().as_mut(),
txn.txn.txn_ptr().as_mut(),
self.dbi,
&mut key_val,
ptr::null_mut(),
Expand Down Expand Up @@ -2463,7 +2459,7 @@ impl<KC, DC, C> Database<KC, DC, C> {

let result = unsafe {
mdb_result(ffi::mdb_del(
txn.txn.txn.unwrap().as_mut(),
txn.txn.txn_ptr().as_mut(),
self.dbi,
&mut key_val,
&mut data_val,
Expand Down Expand Up @@ -2593,8 +2589,7 @@ impl<KC, DC, C> Database<KC, DC, C> {
assert_eq_env_db_txn!(self, txn);

unsafe {
mdb_result(ffi::mdb_drop(txn.txn.txn.unwrap().as_mut(), self.dbi, 0))
.map_err(Into::into)
mdb_result(ffi::mdb_drop(txn.txn.txn_ptr().as_mut(), self.dbi, 0)).map_err(Into::into)
}
}

Expand Down
Loading
Loading