diff --git a/heed/src/db/polymorph.rs b/heed/src/db/polymorph.rs index 5bb73c77..cc918bc8 100644 --- a/heed/src/db/polymorph.rs +++ b/heed/src/db/polymorph.rs @@ -158,7 +158,7 @@ impl PolyDatabase { KC: BytesEncode<'a>, DC: BytesDecode<'txn>, { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; @@ -232,7 +232,7 @@ impl PolyDatabase { KC: BytesEncode<'a> + BytesDecode<'txn>, DC: BytesDecode<'txn>, { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); let mut cursor = RoCursor::new(txn, self.dbi)?; let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; @@ -300,7 +300,7 @@ impl PolyDatabase { KC: BytesEncode<'a> + BytesDecode<'txn>, DC: BytesDecode<'txn>, { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); let mut cursor = RoCursor::new(txn, self.dbi)?; let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; @@ -372,7 +372,7 @@ impl PolyDatabase { KC: BytesEncode<'a> + BytesDecode<'txn>, DC: BytesDecode<'txn>, { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); let mut cursor = RoCursor::new(txn, self.dbi)?; let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; @@ -443,7 +443,7 @@ impl PolyDatabase { KC: BytesEncode<'a> + BytesDecode<'txn>, DC: BytesDecode<'txn>, { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); let mut cursor = RoCursor::new(txn, self.dbi)?; let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; @@ -497,7 +497,7 @@ impl PolyDatabase { KC: BytesDecode<'txn>, DC: BytesDecode<'txn>, { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); let mut cursor = RoCursor::new(txn, self.dbi)?; match cursor.move_on_first() { @@ -550,7 +550,7 @@ impl PolyDatabase { KC: BytesDecode<'txn>, DC: BytesDecode<'txn>, { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); let mut cursor = RoCursor::new(txn, self.dbi)?; match cursor.move_on_last() { @@ -603,7 +603,7 @@ impl PolyDatabase { /// # Ok(()) } /// ``` pub fn len<'txn>(&self, txn: &'txn RoTxn) -> Result { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); let mut db_stat = mem::MaybeUninit::uninit(); let result = unsafe { mdb_result(ffi::mdb_stat(txn.txn, self.dbi, db_stat.as_mut_ptr())) }; @@ -656,7 +656,7 @@ impl PolyDatabase { /// # Ok(()) } /// ``` pub fn is_empty<'txn>(&self, txn: &'txn RoTxn) -> Result { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); let mut cursor = RoCursor::new(txn, self.dbi)?; match cursor.move_on_first()? { @@ -702,7 +702,7 @@ impl PolyDatabase { /// # Ok(()) } /// ``` pub fn iter<'txn, KC, DC>(&self, txn: &'txn RoTxn) -> Result> { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); RoCursor::new(txn, self.dbi).map(|cursor| RoIter::new(cursor)) } @@ -757,7 +757,7 @@ impl PolyDatabase { /// # Ok(()) } /// ``` pub fn iter_mut<'txn, KC, DC>(&self, txn: &'txn mut RwTxn) -> Result> { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); RwCursor::new(txn, self.dbi).map(|cursor| RwIter::new(cursor)) } @@ -799,7 +799,7 @@ impl PolyDatabase { /// # Ok(()) } /// ``` pub fn rev_iter<'txn, KC, DC>(&self, txn: &'txn RoTxn) -> Result> { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); RoCursor::new(txn, self.dbi).map(|cursor| RoRevIter::new(cursor)) } @@ -858,7 +858,7 @@ impl PolyDatabase { &self, txn: &'txn mut RwTxn, ) -> Result> { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); RwCursor::new(txn, self.dbi).map(|cursor| RwRevIter::new(cursor)) } @@ -911,7 +911,7 @@ impl PolyDatabase { KC: BytesEncode<'a>, R: RangeBounds, { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); let start_bound = match range.start_bound() { Bound::Included(bound) => { @@ -1002,7 +1002,7 @@ impl PolyDatabase { KC: BytesEncode<'a>, R: RangeBounds, { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); let start_bound = match range.start_bound() { Bound::Included(bound) => { @@ -1080,7 +1080,7 @@ impl PolyDatabase { KC: BytesEncode<'a>, R: RangeBounds, { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); let start_bound = match range.start_bound() { Bound::Included(bound) => { @@ -1171,7 +1171,7 @@ impl PolyDatabase { KC: BytesEncode<'a>, R: RangeBounds, { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); let start_bound = match range.start_bound() { Bound::Included(bound) => { @@ -1249,7 +1249,7 @@ impl PolyDatabase { where KC: BytesEncode<'a>, { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); let prefix_bytes = KC::bytes_encode(prefix).map_err(Error::Encoding)?; let prefix_bytes = prefix_bytes.into_owned(); @@ -1318,7 +1318,7 @@ impl PolyDatabase { where KC: BytesEncode<'a>, { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); let prefix_bytes = KC::bytes_encode(prefix).map_err(Error::Encoding)?; let prefix_bytes = prefix_bytes.into_owned(); @@ -1374,7 +1374,7 @@ impl PolyDatabase { where KC: BytesEncode<'a>, { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); let prefix_bytes = KC::bytes_encode(prefix).map_err(Error::Encoding)?; let prefix_bytes = prefix_bytes.into_owned(); @@ -1443,7 +1443,7 @@ impl PolyDatabase { where KC: BytesEncode<'a>, { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); let prefix_bytes = KC::bytes_encode(prefix).map_err(Error::Encoding)?; let prefix_bytes = prefix_bytes.into_owned(); @@ -1493,7 +1493,7 @@ impl PolyDatabase { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).map_err(Error::Encoding)?; @@ -1554,7 +1554,7 @@ impl PolyDatabase { KC: BytesEncode<'a>, F: FnMut(&mut ReservedSpace) -> io::Result<()>, { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; let mut key_val = unsafe { crate::into_val(&key_bytes) }; @@ -1620,7 +1620,7 @@ impl PolyDatabase { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).map_err(Error::Encoding)?; @@ -1681,7 +1681,7 @@ impl PolyDatabase { where KC: BytesEncode<'a>, { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; let mut key_val = unsafe { crate::into_val(&key_bytes) }; @@ -1749,7 +1749,7 @@ impl PolyDatabase { KC: BytesEncode<'a> + BytesDecode<'txn>, R: RangeBounds, { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); let mut count = 0; let mut iter = self.range_mut::(txn, range)?; @@ -1806,7 +1806,7 @@ impl PolyDatabase { /// # Ok(()) } /// ``` pub fn clear(&self, txn: &mut RwTxn) -> Result<()> { - assert_matching_env_txn!(self, txn); + assert_eq_env_db_txn!(self, txn); unsafe { mdb_result(ffi::mdb_drop(txn.txn.txn, self.dbi, 0)).map_err(Into::into) } } diff --git a/heed/src/env.rs b/heed/src/env.rs index dc5c20e0..91aacb1c 100644 --- a/heed/src/env.rs +++ b/heed/src/env.rs @@ -22,7 +22,9 @@ use synchronoise::event::SignalEvent; use crate::mdb::error::mdb_result; use crate::mdb::ffi; -use crate::{Database, Error, Flags, PolyDatabase, Result, RoCursor, RoTxn, RwTxn}; +use crate::{ + assert_eq_env_txn, Database, Error, Flags, PolyDatabase, Result, RoCursor, RoTxn, RwTxn, +}; /// The list of opened environments, the value is an optional environment, it is None /// when someone asks to close the environment, closing is a two-phase step, to make sure @@ -297,7 +299,7 @@ impl Drop for EnvInner { unsafe { let _ = ffi::mdb_env_close(self.env); } - // We signal to all the waiters that we have closed the env. + // We signal to all the waiters that the env is closed now. signal_event.signal(); } } @@ -442,6 +444,8 @@ impl Env { KC: 'static, DC: 'static, { + assert_eq_env_txn!(self, rtxn); + let types = (TypeId::of::(), TypeId::of::()); match self.raw_init_database(rtxn.txn, name, Some(types), false) { Ok(dbi) => Ok(Some(Database::new(self.env_mut_ptr() as _, dbi))), @@ -464,6 +468,8 @@ impl Env { rtxn: &RoTxn, name: Option<&str>, ) -> Result> { + assert_eq_env_txn!(self, rtxn); + match self.raw_init_database(rtxn.txn, name, None, false) { Ok(dbi) => Ok(Some(PolyDatabase::new(self.env_mut_ptr() as _, dbi))), Err(Error::Mdb(e)) if e.not_found() => Ok(None), @@ -489,6 +495,8 @@ impl Env { KC: 'static, DC: 'static, { + assert_eq_env_txn!(self, wtxn); + let types = (TypeId::of::(), TypeId::of::()); match self.raw_init_database(wtxn.txn.txn, name, Some(types), true) { Ok(dbi) => Ok(Database::new(self.env_mut_ptr() as _, dbi)), @@ -510,6 +518,8 @@ impl Env { wtxn: &mut RwTxn, name: Option<&str>, ) -> Result { + assert_eq_env_txn!(self, wtxn); + match self.raw_init_database(wtxn.txn.txn, name, None, true) { Ok(dbi) => Ok(PolyDatabase::new(self.env_mut_ptr() as _, dbi)), Err(e) => Err(e), diff --git a/heed/src/lib.rs b/heed/src/lib.rs index cefb56f3..2609b641 100644 --- a/heed/src/lib.rs +++ b/heed/src/lib.rs @@ -146,7 +146,7 @@ mod tests { } } -macro_rules! assert_matching_env_txn { +macro_rules! assert_eq_env_db_txn { ($database:ident, $txn:ident) => { assert!( $database.env_ident == $txn.env_mut_ptr() as usize, @@ -155,4 +155,13 @@ macro_rules! assert_matching_env_txn { }; } -pub(crate) use assert_matching_env_txn; +macro_rules! assert_eq_env_txn { + ($env:ident, $txn:ident) => { + assert!( + $env.env_mut_ptr() == $txn.env_mut_ptr(), + "The environment doesn't match the transaction's environment" + ); + }; +} + +pub(crate) use {assert_eq_env_db_txn, assert_eq_env_txn};