diff --git a/.github/workflows/test.yml b/.github/workflows/rust.yml similarity index 58% rename from .github/workflows/test.yml rename to .github/workflows/rust.yml index f602afee..818ea4ba 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/rust.yml @@ -1,7 +1,7 @@ --- on: [pull_request] -name: Cargo test +name: Rust jobs: check: name: Test the heed project @@ -16,6 +16,8 @@ jobs: steps: - uses: actions/checkout@v2 + with: + submodules: recursive - uses: actions-rs/toolchain@v1 with: profile: minimal @@ -26,6 +28,30 @@ jobs: cargo clean cargo test + examples: + name: Run the heed examples + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, macos-latest] + include: + - os: ubuntu-latest + - os: macos-latest + + steps: + - uses: actions/checkout@v2 + with: + submodules: recursive + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + - name: Run the examples + run: | + cargo clean + cargo run --example 2>&1 | grep -E '^ ' | xargs -n1 cargo run --example + fmt: name: Ensure the heed project is formatted runs-on: ubuntu-latest diff --git a/.gitignore b/.gitignore index 1919b36c..35efa2b0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ heed/target **/*.rs.bk Cargo.lock -/*.mdb +*.mdb diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..e946c61f --- /dev/null +++ b/.gitmodules @@ -0,0 +1,4 @@ +[submodule "lmdb-master-sys/lmdb"] + path = lmdb-master-sys/lmdb + url = https://github.com/LMDB/lmdb + branch = mdb.master diff --git a/Cargo.toml b/Cargo.toml index b8066c4f..f88355d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,2 +1,2 @@ [workspace] -members = ["heed", "heed-traits", "heed-types"] +members = ["lmdb-master-sys", "heed", "heed-traits", "heed-types"] diff --git a/README.md b/README.md index 16bdbcf2..1fb16414 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # heed -A fully typed [LMDB](https://en.wikipedia.org/wiki/Lightning_Memory-Mapped_Database) wrapper with minimum overhead, uses zerocopy internally. +A fully typed [LMDB](https://en.wikipedia.org/wiki/Lightning_Memory-Mapped_Database) wrapper with minimum overhead, uses bytemuck internally. [![License](https://img.shields.io/badge/license-MIT-green)](#LICENSE) [![Crates.io](https://img.shields.io/crates/v/heed)](https://crates.io/crates/heed) @@ -11,36 +11,22 @@ A fully typed [LMDB](https://en.wikipedia.org/wiki/Lightning_Memory-Mapped_Datab This library is able to serialize all kind of types, not just bytes slices, even _Serde_ types are supported. -## Example Usage +Go check out [the examples](heed/examples/). -```rust -fs::create_dir_all("target/heed.mdb")?; -let env = EnvOpenOptions::new().open("target/heed.mdb")?; +## Building from Source -// We open the default unamed database. -// Specifying the type of the newly created database. -// Here we specify that the key is an str and the value a simple integer. -let db: Database> = env.create_database(None)?; +### Using the system LMDB if available -// We then open a write transaction and start writing into the database. -// All of those puts are type checked at compile time, -// therefore you cannot write an integer instead of a string. -let mut wtxn = env.write_txn()?; -db.put(&mut wtxn, "seven", &7)?; -db.put(&mut wtxn, "zero", &0)?; -db.put(&mut wtxn, "five", &5)?; -db.put(&mut wtxn, "three", &3)?; -wtxn.commit()?; +If you don't already have clone the repository you can use this command: -// We open a read transaction to check if those values are available. -// When we read we also type check at compile time. -let rtxn = env.read_txn()?; +```bash +git clone --recursive https://github.com/meilisearch/heed.git +cd heed +cargo build +``` -let ret = db.get(&rtxn, "zero")?; -assert_eq!(ret, Some(0)); +However, if you already cloned it and forgot about the initialising the submodules: -let ret = db.get(&rtxn, "five")?; -assert_eq!(ret, Some(5)); +```bash +git submodule update --init ``` - -You want to see more about all the possibilities? Go check out [the examples](heed/examples/). diff --git a/heed-traits/Cargo.toml b/heed-traits/Cargo.toml index 679cd997..97397d4a 100644 --- a/heed-traits/Cargo.toml +++ b/heed-traits/Cargo.toml @@ -1,11 +1,11 @@ [package] name = "heed-traits" -version = "0.7.0" +version = "0.20.0-alpha.0" authors = ["Kerollmops "] description = "The traits used inside of the fully typed LMDB wrapper, heed" license = "MIT" repository = "https://github.com/Kerollmops/heed" readme = "../README.md" -edition = "2018" +edition = "2021" [dependencies] diff --git a/heed-traits/src/lib.rs b/heed-traits/src/lib.rs index 4f1b9227..f1eb67a4 100644 --- a/heed-traits/src/lib.rs +++ b/heed-traits/src/lib.rs @@ -1,13 +1,19 @@ use std::borrow::Cow; +use std::error::Error as StdError; +/// A boxed `Send + Sync + 'static` error. +pub type BoxedError = Box; + +/// A trait that represents an encoding structure. pub trait BytesEncode<'a> { type EItem: ?Sized + 'a; - fn bytes_encode(item: &'a Self::EItem) -> Option>; + fn bytes_encode(item: &'a Self::EItem) -> Result, BoxedError>; } +/// A trait that represents a decoding structure. pub trait BytesDecode<'a> { type DItem: 'a; - fn bytes_decode(bytes: &'a [u8]) -> Option; + fn bytes_decode(bytes: &'a [u8]) -> Result; } diff --git a/heed-types/Cargo.toml b/heed-types/Cargo.toml index 541b5bbd..5cc2c1b4 100644 --- a/heed-types/Cargo.toml +++ b/heed-types/Cargo.toml @@ -1,19 +1,23 @@ [package] name = "heed-types" -version = "0.7.2" +version = "0.20.0-alpha.0" authors = ["Kerollmops "] description = "The types used with the fully typed LMDB wrapper, heed" license = "MIT" repository = "https://github.com/Kerollmops/heed" readme = "../README.md" -edition = "2018" +edition = "2021" [dependencies] -bincode = { version = "1.2.1", optional = true } -heed-traits = { version = "0.7.0", path = "../heed-traits" } -serde = { version = "1.0.117", optional = true } -serde_json = { version = "1.0.59", optional = true } -zerocopy = "0.3.0" +bincode = { version = "1.3.3", optional = true } +bytemuck = { version = "1.12.3", features = ["extern_crate_alloc", "extern_crate_std"] } +byteorder = "1.4.3" +heed-traits = { version = "0.20.0-alpha.0", path = "../heed-traits" } +serde = { version = "1.0.151", optional = true } +serde_json = { version = "1.0.91", optional = true } + +[dev-dependencies] +rand = "0.8.5" [features] default = ["serde-bincode", "serde-json"] diff --git a/heed-types/src/cow_slice.rs b/heed-types/src/cow_slice.rs index a13341c2..48f30b19 100644 --- a/heed-types/src/cow_slice.rs +++ b/heed-types/src/cow_slice.rs @@ -1,10 +1,7 @@ use std::borrow::Cow; -use std::{mem, ptr}; -use heed_traits::{BytesDecode, BytesEncode}; -use zerocopy::{AsBytes, FromBytes, LayoutVerified}; - -use crate::aligned_to; +use bytemuck::{pod_collect_to_vec, try_cast_slice, AnyBitPattern, NoUninit, PodCastError}; +use heed_traits::{BoxedError, BytesDecode, BytesEncode}; /// Describes a slice that must be [memory aligned] and /// will be reallocated if it is not. @@ -23,47 +20,22 @@ use crate::aligned_to; /// [`OwnedSlice`]: crate::OwnedSlice pub struct CowSlice(std::marker::PhantomData); -impl<'a, T: 'a> BytesEncode<'a> for CowSlice -where - T: AsBytes, -{ +impl<'a, T: NoUninit> BytesEncode<'a> for CowSlice { type EItem = [T]; - fn bytes_encode(item: &'a Self::EItem) -> Option> { - Some(Cow::Borrowed(<[T] as AsBytes>::as_bytes(item))) + fn bytes_encode(item: &'a Self::EItem) -> Result, BoxedError> { + try_cast_slice(item).map(Cow::Borrowed).map_err(Into::into) } } -impl<'a, T: 'a> BytesDecode<'a> for CowSlice -where - T: FromBytes + Copy, -{ +impl<'a, T: AnyBitPattern + NoUninit> BytesDecode<'a> for CowSlice { type DItem = Cow<'a, [T]>; - fn bytes_decode(bytes: &'a [u8]) -> Option { - match LayoutVerified::<_, [T]>::new_slice(bytes) { - Some(layout) => Some(Cow::Borrowed(layout.into_slice())), - None => { - let len = bytes.len(); - let elem_size = mem::size_of::(); - - // ensure that it is the alignment that is wrong - // and the length is valid - if len % elem_size == 0 && !aligned_to(bytes, mem::align_of::()) { - let elems = len / elem_size; - let mut vec = Vec::::with_capacity(elems); - - unsafe { - let dst = vec.as_mut_ptr() as *mut u8; - ptr::copy_nonoverlapping(bytes.as_ptr(), dst, len); - vec.set_len(elems); - } - - return Some(Cow::Owned(vec)); - } - - None - } + fn bytes_decode(bytes: &'a [u8]) -> Result { + match try_cast_slice(bytes) { + Ok(items) => Ok(Cow::Borrowed(items)), + Err(PodCastError::AlignmentMismatch) => Ok(Cow::Owned(pod_collect_to_vec(bytes))), + Err(error) => Err(error.into()), } } } diff --git a/heed-types/src/cow_type.rs b/heed-types/src/cow_type.rs index 4ace47b5..7f4ff151 100644 --- a/heed-types/src/cow_type.rs +++ b/heed-types/src/cow_type.rs @@ -1,10 +1,7 @@ use std::borrow::Cow; -use std::{mem, ptr}; -use heed_traits::{BytesDecode, BytesEncode}; -use zerocopy::{AsBytes, FromBytes, LayoutVerified}; - -use crate::aligned_to; +use bytemuck::{bytes_of, bytes_of_mut, try_from_bytes, AnyBitPattern, NoUninit, PodCastError}; +use heed_traits::{BoxedError, BytesDecode, BytesEncode}; /// Describes a type that must be [memory aligned] and /// will be reallocated if it is not. @@ -29,44 +26,26 @@ use crate::aligned_to; /// [`CowSlice`]: crate::CowSlice pub struct CowType(std::marker::PhantomData); -impl<'a, T: 'a> BytesEncode<'a> for CowType -where - T: AsBytes, -{ +impl<'a, T: NoUninit> BytesEncode<'a> for CowType { type EItem = T; - fn bytes_encode(item: &'a Self::EItem) -> Option> { - Some(Cow::Borrowed(::as_bytes(item))) + fn bytes_encode(item: &'a Self::EItem) -> Result, BoxedError> { + Ok(Cow::Borrowed(bytes_of(item))) } } -impl<'a, T: 'a> BytesDecode<'a> for CowType -where - T: FromBytes + Copy, -{ +impl<'a, T: AnyBitPattern + NoUninit> BytesDecode<'a> for CowType { type DItem = Cow<'a, T>; - fn bytes_decode(bytes: &'a [u8]) -> Option { - match LayoutVerified::<_, T>::new(bytes) { - Some(layout) => Some(Cow::Borrowed(layout.into_ref())), - None => { - let len = bytes.len(); - let elem_size = mem::size_of::(); - - // ensure that it is the alignment that is wrong - // and the length is valid - if len == elem_size && !aligned_to(bytes, mem::align_of::()) { - let mut data = mem::MaybeUninit::::uninit(); - - unsafe { - let dst = data.as_mut_ptr() as *mut u8; - ptr::copy_nonoverlapping(bytes.as_ptr(), dst, len); - return Some(Cow::Owned(data.assume_init())); - } - } - - None + fn bytes_decode(bytes: &'a [u8]) -> Result { + match try_from_bytes(bytes) { + Ok(item) => Ok(Cow::Borrowed(item)), + Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned) => { + let mut item = T::zeroed(); + bytes_of_mut(&mut item).copy_from_slice(bytes); + Ok(Cow::Owned(item)) } + Err(error) => Err(error.into()), } } } diff --git a/heed-types/src/integer.rs b/heed-types/src/integer.rs new file mode 100644 index 00000000..214d39f2 --- /dev/null +++ b/heed-types/src/integer.rs @@ -0,0 +1,75 @@ +use std::borrow::Cow; +use std::marker::PhantomData; +use std::mem::size_of; + +use byteorder::{ByteOrder, ReadBytesExt}; +use heed_traits::{BoxedError, BytesDecode, BytesEncode}; + +pub struct U8; + +impl BytesEncode<'_> for U8 { + type EItem = u8; + + fn bytes_encode(item: &Self::EItem) -> Result, BoxedError> { + Ok(Cow::from([*item].to_vec())) + } +} + +impl BytesDecode<'_> for U8 { + type DItem = u8; + + fn bytes_decode(mut bytes: &'_ [u8]) -> Result { + bytes.read_u8().map_err(Into::into) + } +} + +pub struct I8; + +impl BytesEncode<'_> for I8 { + type EItem = i8; + + fn bytes_encode(item: &Self::EItem) -> Result, BoxedError> { + Ok(Cow::from([*item as u8].to_vec())) + } +} + +impl BytesDecode<'_> for I8 { + type DItem = i8; + + fn bytes_decode(mut bytes: &'_ [u8]) -> Result { + bytes.read_i8().map_err(Into::into) + } +} + +macro_rules! define_type { + ($name:ident, $native:ident, $read_method:ident, $write_method:ident) => { + pub struct $name(PhantomData); + + impl BytesEncode<'_> for $name { + type EItem = $native; + + fn bytes_encode(item: &Self::EItem) -> Result, BoxedError> { + let mut buf = [0; size_of::()]; + O::$write_method(&mut buf, *item); + Ok(Cow::from(buf.to_vec())) + } + } + + impl BytesDecode<'_> for $name { + type DItem = $native; + + fn bytes_decode(mut bytes: &'_ [u8]) -> Result { + bytes.$read_method::().map_err(Into::into) + } + } + }; +} + +define_type!(U16, u16, read_u16, write_u16); +define_type!(U32, u32, read_u32, write_u32); +define_type!(U64, u64, read_u64, write_u64); +define_type!(U128, u128, read_u128, write_u128); +define_type!(I16, i16, read_i16, write_i16); +define_type!(I32, i32, read_i32, write_i32); +define_type!(I64, i64, read_i64, write_i64); +define_type!(I128, i128, read_i128, write_i128); diff --git a/heed-types/src/lib.rs b/heed-types/src/lib.rs index d1ee0c8e..68596753 100644 --- a/heed-types/src/lib.rs +++ b/heed-types/src/lib.rs @@ -18,22 +18,12 @@ //! | [`UnalignedSlice`] | `&[T]` | `&[T]` | will _never_ allocate because alignement is always valid | //! | [`UnalignedType`] | `&T` | `&T` | will _never_ allocate because alignement is always valid | //! -//! Note that **all** those types above must implement [`AsBytes`] and [`FromBytes`].
-//! The `UnalignedSlice/Type` types also need to implement the [`Unaligned`] trait. -//! -//! If you don't want to/cannot deal with `AsBytes`, `Frombytes` or `Unaligned` requirements -//! we recommend you to use the `SerdeBincode` or `SerdeJson` types and deal with the `Serialize`/`Deserialize` traits. -//! -//! [`AsBytes`]: zerocopy::AsBytes -//! [`FromBytes`]: zerocopy::FromBytes -//! [`Unaligned`]: zerocopy::Unaligned -//! //! [`Serialize`]: serde::Serialize //! [`Deserialize`]: serde::Deserialize -//! mod cow_slice; mod cow_type; +mod integer; mod owned_slice; mod owned_type; mod str; @@ -47,8 +37,11 @@ mod serde_bincode; #[cfg(feature = "serde-json")] mod serde_json; +use heed_traits::BoxedError; + pub use self::cow_slice::CowSlice; pub use self::cow_type::CowType; +pub use self::integer::*; pub use self::owned_slice::OwnedSlice; pub use self::owned_type::OwnedType; pub use self::str::Str; @@ -71,8 +64,8 @@ pub struct DecodeIgnore; impl heed_traits::BytesDecode<'_> for DecodeIgnore { type DItem = (); - fn bytes_decode(_bytes: &[u8]) -> Option { - Some(()) + fn bytes_decode(_bytes: &[u8]) -> Result { + Ok(()) } } @@ -80,7 +73,3 @@ impl heed_traits::BytesDecode<'_> for DecodeIgnore { pub use self::serde_bincode::SerdeBincode; #[cfg(feature = "serde-json")] pub use self::serde_json::SerdeJson; - -fn aligned_to(bytes: &[u8], align: usize) -> bool { - (bytes as *const _ as *const () as usize) % align == 0 -} diff --git a/heed-types/src/owned_slice.rs b/heed-types/src/owned_slice.rs index c1914260..6b442793 100644 --- a/heed-types/src/owned_slice.rs +++ b/heed-types/src/owned_slice.rs @@ -1,7 +1,7 @@ use std::borrow::Cow; -use heed_traits::{BytesDecode, BytesEncode}; -use zerocopy::{AsBytes, FromBytes}; +use bytemuck::{try_cast_slice, AnyBitPattern, NoUninit}; +use heed_traits::{BoxedError, BytesDecode, BytesEncode}; use crate::CowSlice; @@ -20,24 +20,18 @@ use crate::CowSlice; /// [`CowType`]: crate::CowType pub struct OwnedSlice(std::marker::PhantomData); -impl<'a, T: 'a> BytesEncode<'a> for OwnedSlice -where - T: AsBytes, -{ +impl<'a, T: NoUninit> BytesEncode<'a> for OwnedSlice { type EItem = [T]; - fn bytes_encode(item: &'a Self::EItem) -> Option> { - Some(Cow::Borrowed(<[T] as AsBytes>::as_bytes(item))) + fn bytes_encode(item: &'a Self::EItem) -> Result, BoxedError> { + try_cast_slice(item).map(Cow::Borrowed).map_err(Into::into) } } -impl<'a, T: 'a> BytesDecode<'a> for OwnedSlice -where - T: FromBytes + Copy, -{ +impl<'a, T: AnyBitPattern + NoUninit> BytesDecode<'a> for OwnedSlice { type DItem = Vec; - fn bytes_decode(bytes: &[u8]) -> Option { + fn bytes_decode(bytes: &[u8]) -> Result { CowSlice::::bytes_decode(bytes).map(Cow::into_owned) } } diff --git a/heed-types/src/owned_type.rs b/heed-types/src/owned_type.rs index 349190f2..aa890bae 100644 --- a/heed-types/src/owned_type.rs +++ b/heed-types/src/owned_type.rs @@ -1,7 +1,7 @@ use std::borrow::Cow; -use heed_traits::{BytesDecode, BytesEncode}; -use zerocopy::{AsBytes, FromBytes}; +use bytemuck::{bytes_of, AnyBitPattern, NoUninit}; +use heed_traits::{BoxedError, BytesDecode, BytesEncode}; use crate::CowType; @@ -26,24 +26,18 @@ use crate::CowType; /// [`CowSlice`]: crate::CowSlice pub struct OwnedType(std::marker::PhantomData); -impl<'a, T: 'a> BytesEncode<'a> for OwnedType -where - T: AsBytes, -{ +impl<'a, T: NoUninit> BytesEncode<'a> for OwnedType { type EItem = T; - fn bytes_encode(item: &'a Self::EItem) -> Option> { - Some(Cow::Borrowed(::as_bytes(item))) + fn bytes_encode(item: &'a Self::EItem) -> Result, BoxedError> { + Ok(Cow::Borrowed(bytes_of(item))) } } -impl<'a, T: 'a> BytesDecode<'a> for OwnedType -where - T: FromBytes + Copy, -{ +impl<'a, T: AnyBitPattern + NoUninit> BytesDecode<'a> for OwnedType { type DItem = T; - fn bytes_decode(bytes: &[u8]) -> Option { + fn bytes_decode(bytes: &[u8]) -> Result { CowType::::bytes_decode(bytes).map(Cow::into_owned) } } diff --git a/heed-types/src/serde_bincode.rs b/heed-types/src/serde_bincode.rs index 464aceb4..37fefe01 100644 --- a/heed-types/src/serde_bincode.rs +++ b/heed-types/src/serde_bincode.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; -use heed_traits::{BytesDecode, BytesEncode}; +use heed_traits::{BoxedError, BytesDecode, BytesEncode}; use serde::{Deserialize, Serialize}; /// Describes a type that is [`Serialize`]/[`Deserialize`] and uses `bincode` to do so. @@ -14,8 +14,8 @@ where { type EItem = T; - fn bytes_encode(item: &'a Self::EItem) -> Option> { - bincode::serialize(item).map(Cow::Owned).ok() + fn bytes_encode(item: &'a Self::EItem) -> Result, BoxedError> { + bincode::serialize(item).map(Cow::Owned).map_err(Into::into) } } @@ -25,8 +25,8 @@ where { type DItem = T; - fn bytes_decode(bytes: &'a [u8]) -> Option { - bincode::deserialize(bytes).ok() + fn bytes_decode(bytes: &'a [u8]) -> Result { + bincode::deserialize(bytes).map_err(Into::into) } } diff --git a/heed-types/src/serde_json.rs b/heed-types/src/serde_json.rs index 2327e259..f1f1c3be 100644 --- a/heed-types/src/serde_json.rs +++ b/heed-types/src/serde_json.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; -use heed_traits::{BytesDecode, BytesEncode}; +use heed_traits::{BoxedError, BytesDecode, BytesEncode}; use serde::{Deserialize, Serialize}; /// Describes a type that is [`Serialize`]/[`Deserialize`] and uses `serde_json` to do so. @@ -14,8 +14,8 @@ where { type EItem = T; - fn bytes_encode(item: &Self::EItem) -> Option> { - serde_json::to_vec(item).map(Cow::Owned).ok() + fn bytes_encode(item: &Self::EItem) -> Result, BoxedError> { + serde_json::to_vec(item).map(Cow::Owned).map_err(Into::into) } } @@ -25,8 +25,8 @@ where { type DItem = T; - fn bytes_decode(bytes: &'a [u8]) -> Option { - serde_json::from_slice(bytes).ok() + fn bytes_decode(bytes: &'a [u8]) -> Result { + serde_json::from_slice(bytes).map_err(Into::into) } } diff --git a/heed-types/src/str.rs b/heed-types/src/str.rs index 5b110775..4d00ef2e 100644 --- a/heed-types/src/str.rs +++ b/heed-types/src/str.rs @@ -1,24 +1,22 @@ use std::borrow::Cow; -use heed_traits::{BytesDecode, BytesEncode}; +use heed_traits::{BoxedError, BytesDecode, BytesEncode}; -use crate::UnalignedSlice; - -/// Describes an [`str`]. +/// Describes an [`prim@str`]. pub struct Str; impl BytesEncode<'_> for Str { type EItem = str; - fn bytes_encode(item: &Self::EItem) -> Option> { - UnalignedSlice::::bytes_encode(item.as_bytes()) + fn bytes_encode(item: &Self::EItem) -> Result, BoxedError> { + Ok(Cow::Borrowed(item.as_bytes())) } } impl<'a> BytesDecode<'a> for Str { type DItem = &'a str; - fn bytes_decode(bytes: &'a [u8]) -> Option { - std::str::from_utf8(bytes).ok() + fn bytes_decode(bytes: &'a [u8]) -> Result { + std::str::from_utf8(bytes).map_err(Into::into) } } diff --git a/heed-types/src/unaligned_slice.rs b/heed-types/src/unaligned_slice.rs index b5b1b11a..7d13a1df 100644 --- a/heed-types/src/unaligned_slice.rs +++ b/heed-types/src/unaligned_slice.rs @@ -1,7 +1,7 @@ use std::borrow::Cow; -use heed_traits::{BytesDecode, BytesEncode}; -use zerocopy::{AsBytes, FromBytes, LayoutVerified, Unaligned}; +use bytemuck::{try_cast_slice, AnyBitPattern, NoUninit}; +use heed_traits::{BoxedError, BytesDecode, BytesEncode}; /// Describes a type that is totally borrowed and doesn't /// depends on any [memory alignment]. @@ -13,25 +13,19 @@ use zerocopy::{AsBytes, FromBytes, LayoutVerified, Unaligned}; /// [`CowType`]: crate::CowType pub struct UnalignedSlice(std::marker::PhantomData); -impl<'a, T: 'a> BytesEncode<'a> for UnalignedSlice -where - T: AsBytes + Unaligned, -{ +impl<'a, T: NoUninit> BytesEncode<'a> for UnalignedSlice { type EItem = [T]; - fn bytes_encode(item: &'a Self::EItem) -> Option> { - Some(Cow::Borrowed(<[T] as AsBytes>::as_bytes(item))) + fn bytes_encode(item: &'a Self::EItem) -> Result, BoxedError> { + try_cast_slice(item).map(Cow::Borrowed).map_err(Into::into) } } -impl<'a, T: 'a> BytesDecode<'a> for UnalignedSlice -where - T: FromBytes + Unaligned, -{ +impl<'a, T: AnyBitPattern> BytesDecode<'a> for UnalignedSlice { type DItem = &'a [T]; - fn bytes_decode(bytes: &'a [u8]) -> Option { - LayoutVerified::<_, [T]>::new_slice_unaligned(bytes).map(LayoutVerified::into_slice) + fn bytes_decode(bytes: &'a [u8]) -> Result { + try_cast_slice(bytes).map_err(Into::into) } } diff --git a/heed-types/src/unaligned_type.rs b/heed-types/src/unaligned_type.rs index a87fdb08..886aeceb 100644 --- a/heed-types/src/unaligned_type.rs +++ b/heed-types/src/unaligned_type.rs @@ -1,7 +1,7 @@ use std::borrow::Cow; -use heed_traits::{BytesDecode, BytesEncode}; -use zerocopy::{AsBytes, FromBytes, LayoutVerified, Unaligned}; +use bytemuck::{bytes_of, try_from_bytes, AnyBitPattern, NoUninit}; +use heed_traits::{BoxedError, BytesDecode, BytesEncode}; /// Describes a slice that is totally borrowed and doesn't /// depends on any [memory alignment]. @@ -19,25 +19,19 @@ use zerocopy::{AsBytes, FromBytes, LayoutVerified, Unaligned}; /// [`CowSlice`]: crate::CowSlice pub struct UnalignedType(std::marker::PhantomData); -impl<'a, T: 'a> BytesEncode<'a> for UnalignedType -where - T: AsBytes + Unaligned, -{ +impl<'a, T: NoUninit> BytesEncode<'a> for UnalignedType { type EItem = T; - fn bytes_encode(item: &'a Self::EItem) -> Option> { - Some(Cow::Borrowed(::as_bytes(item))) + fn bytes_encode(item: &'a Self::EItem) -> Result, BoxedError> { + Ok(Cow::Borrowed(bytes_of(item))) } } -impl<'a, T: 'a> BytesDecode<'a> for UnalignedType -where - T: FromBytes + Unaligned, -{ +impl<'a, T: AnyBitPattern> BytesDecode<'a> for UnalignedType { type DItem = &'a T; - fn bytes_decode(bytes: &'a [u8]) -> Option { - LayoutVerified::<_, T>::new_unaligned(bytes).map(LayoutVerified::into_ref) + fn bytes_decode(bytes: &'a [u8]) -> Result { + try_from_bytes(bytes).map_err(Into::into) } } diff --git a/heed-types/src/unit.rs b/heed-types/src/unit.rs index f53152ae..fee8bd56 100644 --- a/heed-types/src/unit.rs +++ b/heed-types/src/unit.rs @@ -1,6 +1,7 @@ use std::borrow::Cow; -use heed_traits::{BytesDecode, BytesEncode}; +use bytemuck::PodCastError; +use heed_traits::{BoxedError, BytesDecode, BytesEncode}; /// Describes the `()` type. pub struct Unit; @@ -8,19 +9,19 @@ pub struct Unit; impl BytesEncode<'_> for Unit { type EItem = (); - fn bytes_encode(_item: &Self::EItem) -> Option> { - Some(Cow::Borrowed(&[])) + fn bytes_encode(_item: &Self::EItem) -> Result, BoxedError> { + Ok(Cow::Borrowed(&[])) } } impl BytesDecode<'_> for Unit { type DItem = (); - fn bytes_decode(bytes: &[u8]) -> Option { + fn bytes_decode(bytes: &[u8]) -> Result { if bytes.is_empty() { - Some(()) + Ok(()) } else { - None + Err(PodCastError::SizeMismatch.into()) } } } diff --git a/heed/Cargo.toml b/heed/Cargo.toml index 19aabae2..45167b6b 100644 --- a/heed/Cargo.toml +++ b/heed/Cargo.toml @@ -1,33 +1,34 @@ [package] name = "heed" -version = "0.12.1" +version = "0.20.0-alpha.0" authors = ["Kerollmops "] -description = "A fully typed LMDB/MDBX wrapper with minimum overhead" +description = "A fully typed LMDB wrapper with minimum overhead" license = "MIT" repository = "https://github.com/Kerollmops/heed" keywords = ["lmdb", "database", "storage", "typed"] categories = ["database", "data-structures"] readme = "../README.md" -edition = "2018" +edition = "2021" [dependencies] -byteorder = { version = "1.3.4", default-features = false } -heed-traits = { version = "0.7.0", path = "../heed-traits" } -heed-types = { version = "0.7.2", path = "../heed-types" } -libc = "0.2.80" -lmdb-rkv-sys = { git = "https://github.com/meilisearch/lmdb-rs" } -once_cell = "1.5.2" -page_size = "0.4.2" -serde = { version = "1.0.118", features = ["derive"], optional = true } -synchronoise = "1.0.0" -zerocopy = "0.3.0" +bytemuck = "1.12.3" +byteorder = { version = "1.4.3", default-features = false } +heed-traits = { version = "0.20.0-alpha.0", path = "../heed-traits" } +heed-types = { version = "0.20.0-alpha.0", path = "../heed-types" } +libc = "0.2.139" +lmdb-master-sys = { version = "0.1.0", path = "../lmdb-master-sys" } +once_cell = "1.16.0" +page_size = "0.5.0" +serde = { version = "1.0.151", features = ["derive"], optional = true } +synchronoise = "1.0.1" [dev-dependencies] -serde = { version = "1.0.118", features = ["derive"] } +serde = { version = "1.0.151", features = ["derive"] } +bytemuck = { version = "1.12.3", features = ["derive"] } tempfile = "3.3.0" [target.'cfg(windows)'.dependencies] -url = "2.2.0" +url = "2.3.1" [features] # The `serde` feature makes some types serializable, @@ -48,3 +49,12 @@ preserve_order = ["heed-types/preserve_order"] arbitrary_precision = ["heed-types/arbitrary_precision"] raw_value = ["heed-types/raw_value"] unbounded_depth = ["heed-types/unbounded_depth"] + +# Whether to tell LMDB to use POSIX semaphores during compilation +# (instead of the default, which are System V semaphores). +# POSIX semaphores are required for Apple's App Sandbox on iOS & macOS, +# and are possibly faster and more appropriate for single-process use. +# There are tradeoffs for both POSIX and SysV semaphores; which you +# should look into before enabling this feature. Also, see here: +# +posix-sem = ["lmdb-master-sys/posix-sem"] diff --git a/heed/examples/all-types-poly.rs b/heed/examples/all-types-poly.rs index f55aa947..61111c6c 100644 --- a/heed/examples/all-types-poly.rs +++ b/heed/examples/all-types-poly.rs @@ -2,9 +2,9 @@ use std::error::Error; use std::fs; use std::path::Path; +use bytemuck::{Pod, Zeroable}; use heed::byteorder::BE; use heed::types::*; -use heed::zerocopy::{AsBytes, FromBytes, Unaligned, I64}; use heed::EnvOpenOptions; use serde::{Deserialize, Serialize}; @@ -22,21 +22,21 @@ fn main() -> Result<(), Box> { // // like here we specify that the key will be an array of two i32 // and the data will be an str - let db = env.create_poly_database(Some("kikou"))?; - let mut wtxn = env.write_txn()?; - db.put::<_, OwnedType<[i32; 2]>, Str>(&mut wtxn, &[2, 3], "what's up?")?; - let ret = db.get::<_, OwnedType<[i32; 2]>, Str>(&wtxn, &[2, 3])?; + let db = env.create_poly_database(&mut wtxn, Some("kikou"))?; + + db.put::, Str>(&mut wtxn, &[2, 3], "what's up?")?; + let ret = db.get::, Str>(&wtxn, &[2, 3])?; println!("{:?}", ret); wtxn.commit()?; // here the key will be an str and the data will be a slice of u8 - let db = env.create_poly_database(Some("kiki"))?; - let mut wtxn = env.write_txn()?; - db.put::<_, Str, ByteSlice>(&mut wtxn, "hello", &[2, 3][..])?; - let ret = db.get::<_, Str, ByteSlice>(&wtxn, "hello")?; + let db = env.create_poly_database(&mut wtxn, Some("kiki"))?; + + db.put::(&mut wtxn, "hello", &[2, 3][..])?; + let ret = db.get::(&wtxn, "hello")?; println!("{:?}", ret); wtxn.commit()?; @@ -47,61 +47,59 @@ fn main() -> Result<(), Box> { string: &'a str, } - let db = env.create_poly_database(Some("serde"))?; - let mut wtxn = env.write_txn()?; + let db = env.create_poly_database(&mut wtxn, Some("serde"))?; let hello = Hello { string: "hi" }; - db.put::<_, Str, SerdeBincode>(&mut wtxn, "hello", &hello)?; + db.put::>(&mut wtxn, "hello", &hello)?; - let ret = db.get::<_, Str, SerdeBincode>(&wtxn, "hello")?; + let ret = db.get::>(&wtxn, "hello")?; println!("serde-bincode:\t{:?}", ret); let hello = Hello { string: "hi" }; - db.put::<_, Str, SerdeJson>(&mut wtxn, "hello", &hello)?; + db.put::>(&mut wtxn, "hello", &hello)?; - let ret = db.get::<_, Str, SerdeJson>(&wtxn, "hello")?; + let ret = db.get::>(&wtxn, "hello")?; println!("serde-json:\t{:?}", ret); wtxn.commit()?; - // it is prefered to use zerocopy when possible - #[derive(Debug, PartialEq, Eq, AsBytes, FromBytes, Unaligned)] + #[derive(Debug, PartialEq, Eq, Clone, Copy, Pod, Zeroable)] #[repr(C)] struct ZeroBytes { bytes: [u8; 12], } - let db = env.create_poly_database(Some("zerocopy-struct"))?; - let mut wtxn = env.write_txn()?; + let db = env.create_poly_database(&mut wtxn, Some("nocopy-struct"))?; let zerobytes = ZeroBytes { bytes: [24; 12] }; - db.put::<_, Str, UnalignedType>(&mut wtxn, "zero", &zerobytes)?; + db.put::>(&mut wtxn, "zero", &zerobytes)?; - let ret = db.get::<_, Str, UnalignedType>(&wtxn, "zero")?; + let ret = db.get::>(&wtxn, "zero")?; println!("{:?}", ret); wtxn.commit()?; // you can ignore the data - let db = env.create_poly_database(Some("ignored-data"))?; - let mut wtxn = env.write_txn()?; - db.put::<_, Str, Unit>(&mut wtxn, "hello", &())?; - let ret = db.get::<_, Str, Unit>(&wtxn, "hello")?; + let db = env.create_poly_database(&mut wtxn, Some("ignored-data"))?; + + db.put::(&mut wtxn, "hello", &())?; + let ret = db.get::(&wtxn, "hello")?; println!("{:?}", ret); - let ret = db.get::<_, Str, Unit>(&wtxn, "non-existant")?; + let ret = db.get::(&wtxn, "non-existant")?; println!("{:?}", ret); wtxn.commit()?; - // database opening and types are tested in a way + // database opening and types are tested in a safe way // // we try to open a database twice with the same types - let _db = env.create_poly_database(Some("ignored-data"))?; + let mut wtxn = env.write_txn()?; + let _db = env.create_poly_database(&mut wtxn, Some("ignored-data"))?; // and here we try to open it with other types // asserting that it correctly returns an error @@ -109,36 +107,34 @@ fn main() -> Result<(), Box> { // NOTE that those types are not saved upon runs and // therefore types cannot be checked upon different runs, // the first database opening fix the types for this run. - let result = env.create_database::, Unit>(Some("ignored-data")); + let result = env.create_database::(&mut wtxn, Some("ignored-data")); assert!(result.is_err()); // you can iterate over keys in order type BEI64 = I64; - let db = env.create_poly_database(Some("big-endian-iter"))?; + let db = env.create_poly_database(&mut wtxn, Some("big-endian-iter"))?; - let mut wtxn = env.write_txn()?; - db.put::<_, OwnedType, Unit>(&mut wtxn, &BEI64::new(0), &())?; - db.put::<_, OwnedType, Unit>(&mut wtxn, &BEI64::new(68), &())?; - db.put::<_, OwnedType, Unit>(&mut wtxn, &BEI64::new(35), &())?; - db.put::<_, OwnedType, Unit>(&mut wtxn, &BEI64::new(42), &())?; + db.put::(&mut wtxn, &0, &())?; + db.put::(&mut wtxn, &68, &())?; + db.put::(&mut wtxn, &35, &())?; + db.put::(&mut wtxn, &42, &())?; - let rets: Result, _> = db.iter::<_, OwnedType, Unit>(&wtxn)?.collect(); + let rets: Result, _> = db.iter::(&wtxn)?.collect(); println!("{:?}", rets); // or iterate over ranges too!!! - let range = BEI64::new(35)..=BEI64::new(42); - let rets: Result, _> = - db.range::<_, OwnedType, Unit, _>(&wtxn, &range)?.collect(); + let range = 35..=42; + let rets: Result, _> = db.range::(&wtxn, &range)?.collect(); println!("{:?}", rets); // delete a range of key - let range = BEI64::new(35)..=BEI64::new(42); - let deleted: usize = db.delete_range::<_, OwnedType, _>(&mut wtxn, &range)?; + let range = 35..=42; + let deleted: usize = db.delete_range::(&mut wtxn, &range)?; - let rets: Result, _> = db.iter::<_, OwnedType, Unit>(&wtxn)?.collect(); + let rets: Result, _> = db.iter::(&wtxn)?.collect(); println!("deleted: {:?}, {:?}", deleted, rets); wtxn.commit()?; diff --git a/heed/examples/all-types.rs b/heed/examples/all-types.rs index c9e43951..e7a1c709 100644 --- a/heed/examples/all-types.rs +++ b/heed/examples/all-types.rs @@ -2,9 +2,9 @@ use std::error::Error; use std::fs; use std::path::Path; +use heed::bytemuck::{Pod, Zeroable}; use heed::byteorder::BE; use heed::types::*; -use heed::zerocopy::{AsBytes, FromBytes, Unaligned, I64}; use heed::{Database, EnvOpenOptions}; use serde::{Deserialize, Serialize}; @@ -22,9 +22,9 @@ fn main() -> Result<(), Box> { // // like here we specify that the key will be an array of two i32 // and the data will be an str - let db: Database, Str> = env.create_database(Some("kikou"))?; - let mut wtxn = env.write_txn()?; + let db: Database, Str> = env.create_database(&mut wtxn, Some("kikou"))?; + let _ret = db.put(&mut wtxn, &[2, 3], "what's up?")?; let ret: Option<&str> = db.get(&wtxn, &[2, 3])?; @@ -32,9 +32,9 @@ fn main() -> Result<(), Box> { wtxn.commit()?; // here the key will be an str and the data will be a slice of u8 - let db: Database = env.create_database(Some("kiki"))?; - let mut wtxn = env.write_txn()?; + let db: Database = env.create_database(&mut wtxn, Some("kiki"))?; + let _ret = db.put(&mut wtxn, "hello", &[2, 3][..])?; let ret: Option<&[u8]> = db.get(&wtxn, "hello")?; @@ -47,9 +47,9 @@ fn main() -> Result<(), Box> { string: &'a str, } - let db: Database> = env.create_database(Some("serde-bincode"))?; - let mut wtxn = env.write_txn()?; + let db: Database> = + env.create_database(&mut wtxn, Some("serde-bincode"))?; let hello = Hello { string: "hi" }; db.put(&mut wtxn, "hello", &hello)?; @@ -59,9 +59,8 @@ fn main() -> Result<(), Box> { wtxn.commit()?; - let db: Database> = env.create_database(Some("serde-json"))?; - let mut wtxn = env.write_txn()?; + let db: Database> = env.create_database(&mut wtxn, Some("serde-json"))?; let hello = Hello { string: "hi" }; db.put(&mut wtxn, "hello", &hello)?; @@ -71,17 +70,16 @@ fn main() -> Result<(), Box> { wtxn.commit()?; - // it is prefered to use zerocopy when possible - #[derive(Debug, PartialEq, Eq, AsBytes, FromBytes, Unaligned)] + // it is prefered to use bytemuck when possible + #[derive(Debug, Clone, Copy, PartialEq, Eq, Zeroable, Pod)] #[repr(C)] struct ZeroBytes { bytes: [u8; 12], } - let db: Database> = - env.create_database(Some("zerocopy-struct"))?; - let mut wtxn = env.write_txn()?; + let db: Database> = + env.create_database(&mut wtxn, Some("simple-struct"))?; let zerobytes = ZeroBytes { bytes: [24; 12] }; db.put(&mut wtxn, "zero", &zerobytes)?; @@ -92,9 +90,9 @@ fn main() -> Result<(), Box> { wtxn.commit()?; // you can ignore the data - let db: Database = env.create_database(Some("ignored-data"))?; - let mut wtxn = env.write_txn()?; + let db: Database = env.create_database(&mut wtxn, Some("ignored-data"))?; + let _ret = db.put(&mut wtxn, "hello", &())?; let ret: Option<()> = db.get(&wtxn, "hello")?; @@ -105,10 +103,11 @@ fn main() -> Result<(), Box> { println!("{:?}", ret); wtxn.commit()?; - // database opening and types are tested in a way + // database opening and types are tested in a safe way // // we try to open a database twice with the same types - let _db: Database = env.create_database(Some("ignored-data"))?; + let mut wtxn = env.write_txn()?; + let _db: Database = env.create_database(&mut wtxn, Some("ignored-data"))?; // and here we try to open it with other types // asserting that it correctly returns an error @@ -116,35 +115,34 @@ fn main() -> Result<(), Box> { // NOTE that those types are not saved upon runs and // therefore types cannot be checked upon different runs, // the first database opening fix the types for this run. - let result = env.create_database::>(Some("ignored-data")); + let result = env.create_database::>(&mut wtxn, Some("ignored-data")); assert!(result.is_err()); // you can iterate over keys in order type BEI64 = I64; - let db: Database, Unit> = env.create_database(Some("big-endian-iter"))?; + let db: Database = env.create_database(&mut wtxn, Some("big-endian-iter"))?; - let mut wtxn = env.write_txn()?; - let _ret = db.put(&mut wtxn, &BEI64::new(0), &())?; - let _ret = db.put(&mut wtxn, &BEI64::new(68), &())?; - let _ret = db.put(&mut wtxn, &BEI64::new(35), &())?; - let _ret = db.put(&mut wtxn, &BEI64::new(42), &())?; + let _ret = db.put(&mut wtxn, &0, &())?; + let _ret = db.put(&mut wtxn, &68, &())?; + let _ret = db.put(&mut wtxn, &35, &())?; + let _ret = db.put(&mut wtxn, &42, &())?; - let rets: Result, _> = db.iter(&wtxn)?.collect(); + let rets: Result, _> = db.iter(&wtxn)?.collect(); println!("{:?}", rets); // or iterate over ranges too!!! - let range = BEI64::new(35)..=BEI64::new(42); - let rets: Result, _> = db.range(&wtxn, &range)?.collect(); + let range = 35..=42; + let rets: Result, _> = db.range(&wtxn, &range)?.collect(); println!("{:?}", rets); // delete a range of key - let range = BEI64::new(35)..=BEI64::new(42); + let range = 35..=42; let deleted: usize = db.delete_range(&mut wtxn, &range)?; - let rets: Result, _> = db.iter(&wtxn)?.collect(); + let rets: Result, _> = db.iter(&wtxn)?.collect(); println!("deleted: {:?}, {:?}", deleted, rets); wtxn.commit()?; diff --git a/heed/examples/clear-database.rs b/heed/examples/clear-database.rs index e87aed38..bc3cea75 100644 --- a/heed/examples/clear-database.rs +++ b/heed/examples/clear-database.rs @@ -18,8 +18,8 @@ fn main() -> Result<(), Box> { .max_dbs(3) .open(env_path)?; - let db: Database = env.create_database(Some("first"))?; let mut wtxn = env.write_txn()?; + let db: Database = env.create_database(&mut wtxn, Some("first"))?; // We fill the db database with entries. db.put(&mut wtxn, "I am here", "to test things")?; diff --git a/heed/examples/cursor-append.rs b/heed/examples/cursor-append.rs index 6f487c41..596761f3 100644 --- a/heed/examples/cursor-append.rs +++ b/heed/examples/cursor-append.rs @@ -18,10 +18,9 @@ fn main() -> Result<(), Box> { .max_dbs(3) .open(env_path)?; - let first: Database = env.create_database(Some("first"))?; - let second: Database = env.create_database(Some("second"))?; - let mut wtxn = env.write_txn()?; + let first: Database = env.create_database(&mut wtxn, Some("first"))?; + let second: Database = env.create_database(&mut wtxn, Some("second"))?; // We fill the first database with entries. first.put(&mut wtxn, "I am here", "to test things")?; diff --git a/heed/examples/multi-env.rs b/heed/examples/multi-env.rs index 49895d63..541fce36 100644 --- a/heed/examples/multi-env.rs +++ b/heed/examples/multi-env.rs @@ -21,18 +21,19 @@ fn main() -> Result<(), Box> { .max_dbs(3000) .open(env2_path)?; - let db1: Database = env1.create_database(Some("hello"))?; - let db2: Database, OwnedType> = env2.create_database(Some("hello"))?; + let mut wtxn1 = env1.write_txn()?; + let mut wtxn2 = env2.write_txn()?; + let db1: Database = env1.create_database(&mut wtxn1, Some("hello"))?; + let db2: Database, OwnedType> = + env2.create_database(&mut wtxn2, Some("hello"))?; // clear db - let mut wtxn = env1.write_txn()?; - db1.clear(&mut wtxn)?; - wtxn.commit()?; + db1.clear(&mut wtxn1)?; + wtxn1.commit()?; // clear db - let mut wtxn = env2.write_txn()?; - db2.clear(&mut wtxn)?; - wtxn.commit()?; + db2.clear(&mut wtxn2)?; + wtxn2.commit()?; // ----- diff --git a/heed/examples/nested.rs b/heed/examples/nested.rs index fea6f654..cb44ec51 100644 --- a/heed/examples/nested.rs +++ b/heed/examples/nested.rs @@ -16,17 +16,16 @@ fn main() -> Result<(), Box> { .open(path)?; // here the key will be an str and the data will be a slice of u8 - let db: Database = env.create_database(None)?; + let mut wtxn = env.write_txn()?; + let db: Database = env.create_database(&mut wtxn, None)?; // clear db - let mut wtxn = env.write_txn()?; db.clear(&mut wtxn)?; wtxn.commit()?; // ----- let mut wtxn = env.write_txn()?; - let mut nwtxn = env.nested_write_txn(&mut wtxn)?; db.put(&mut nwtxn, "what", &[4, 5][..])?; @@ -34,7 +33,7 @@ fn main() -> Result<(), Box> { println!("nested(1) \"what\": {:?}", ret); println!("nested(1) abort"); - nwtxn.abort()?; + nwtxn.abort(); let ret = db.get(&wtxn, "what")?; println!("parent \"what\": {:?}", ret); diff --git a/heed/src/cursor.rs b/heed/src/cursor.rs index 787f982d..8a4238b1 100644 --- a/heed/src/cursor.rs +++ b/heed/src/cursor.rs @@ -11,7 +11,7 @@ pub struct RoCursor<'txn> { } impl<'txn> RoCursor<'txn> { - pub(crate) fn new(txn: &'txn RoTxn, dbi: ffi::MDB_dbi) -> Result> { + pub(crate) fn new(txn: &'txn RoTxn, dbi: ffi::MDB_dbi) -> Result> { let mut cursor: *mut ffi::MDB_cursor = ptr::null_mut(); unsafe { mdb_result(ffi::mdb_cursor_open(txn.txn, dbi, &mut cursor))? } @@ -184,7 +184,7 @@ pub struct RwCursor<'txn> { } impl<'txn> RwCursor<'txn> { - pub(crate) fn new(txn: &'txn RwTxn, dbi: ffi::MDB_dbi) -> Result> { + pub(crate) fn new(txn: &'txn RwTxn, dbi: ffi::MDB_dbi) -> Result> { Ok(RwCursor { cursor: RoCursor::new(txn, dbi)? }) } @@ -254,6 +254,51 @@ impl<'txn> RwCursor<'txn> { } } + /// Write a new value to the current entry. + /// + /// The given key **must** be equal to the one this cursor is pointing otherwise the database + /// can be put into an inconsistent state. + /// + /// Returns `true` if the entry was successfully written. + /// + /// > This is intended to be used when the new data is the same size as the old. + /// > Otherwise it will simply perform a delete of the old record followed by an insert. + /// + /// # Safety + /// + /// Please read the safety notes of the `[put_current]` method. + pub unsafe fn put_current_reserved( + &mut self, + key: &[u8], + data_size: usize, + mut write_func: F, + ) -> Result + where + F: FnMut(&mut ReservedSpace) -> io::Result<()>, + { + let mut key_val = crate::into_val(&key); + let mut reserved = ffi::reserve_size_val(data_size); + let flags = ffi::MDB_RESERVE; + + let result = + mdb_result(ffi::mdb_cursor_put(self.cursor.cursor, &mut key_val, &mut reserved, flags)); + + let found = match result { + Ok(()) => true, + Err(e) if e.not_found() => false, + Err(e) => return Err(e.into()), + }; + + let mut reserved = ReservedSpace::from_val(reserved); + (write_func)(&mut reserved)?; + + if reserved.remaining() == 0 { + Ok(found) + } else { + Err(io::Error::from(io::ErrorKind::UnexpectedEof).into()) + } + } + /// Append the given key/value pair to the end of the database. /// /// If a key is inserted that is less than any previous key a `KeyExist` error diff --git a/heed/src/db/polymorph.rs b/heed/src/db/polymorph.rs index 684855a0..cc918bc8 100644 --- a/heed/src/db/polymorph.rs +++ b/heed/src/db/polymorph.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; use std::ops::{Bound, RangeBounds}; -use std::{mem, ptr}; +use std::{fmt, mem, ptr}; use crate::mdb::error::mdb_result; use crate::mdb::ffi; @@ -21,30 +21,30 @@ use crate::*; /// # use heed::EnvOpenOptions; /// use heed::PolyDatabase; /// use heed::types::*; -/// use heed::{zerocopy::I64, byteorder::BigEndian}; +/// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { -/// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; +/// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) -/// # .open(Path::new("target").join("zerocopy.mdb"))?; +/// # .open(dir.path())?; /// type BEI64 = I64; /// -/// let db: PolyDatabase = env.create_poly_database(Some("big-endian-iter"))?; -/// /// let mut wtxn = env.write_txn()?; +/// let db: PolyDatabase = env.create_poly_database(&mut wtxn, Some("big-endian-iter"))?; +/// /// # db.clear(&mut wtxn)?; -/// db.put::<_, OwnedType, Unit>(&mut wtxn, &BEI64::new(0), &())?; -/// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI64::new(35), "thirty five")?; -/// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI64::new(42), "forty two")?; -/// db.put::<_, OwnedType, Unit>(&mut wtxn, &BEI64::new(68), &())?; +/// db.put::(&mut wtxn, &0, &())?; +/// db.put::(&mut wtxn, &35, "thirty five")?; +/// db.put::(&mut wtxn, &42, "forty two")?; +/// db.put::(&mut wtxn, &68, &())?; /// /// // you can iterate over database entries in order -/// let range = BEI64::new(35)..=BEI64::new(42); -/// let mut range = db.range::<_, OwnedType, Str, _>(&wtxn, &range)?; -/// assert_eq!(range.next().transpose()?, Some((BEI64::new(35), "thirty five"))); -/// assert_eq!(range.next().transpose()?, Some((BEI64::new(42), "forty two"))); +/// let range = 35..=42; +/// let mut range = db.range::(&wtxn, &range)?; +/// assert_eq!(range.next().transpose()?, Some((35, "thirty five"))); +/// assert_eq!(range.next().transpose()?, Some((42, "forty two"))); /// assert_eq!(range.next().transpose()?, None); /// /// drop(range); @@ -64,36 +64,36 @@ use crate::*; /// # use heed::EnvOpenOptions; /// use heed::PolyDatabase; /// use heed::types::*; -/// use heed::{zerocopy::I64, byteorder::BigEndian}; +/// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { -/// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; +/// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) -/// # .open(Path::new("target").join("zerocopy.mdb"))?; +/// # .open(dir.path())?; /// type BEI64 = I64; /// -/// let db: PolyDatabase = env.create_poly_database(Some("big-endian-iter"))?; -/// /// let mut wtxn = env.write_txn()?; +/// let db: PolyDatabase = env.create_poly_database(&mut wtxn, Some("big-endian-iter"))?; +/// /// # db.clear(&mut wtxn)?; -/// db.put::<_, OwnedType, Unit>(&mut wtxn, &BEI64::new(0), &())?; -/// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI64::new(35), "thirty five")?; -/// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI64::new(42), "forty two")?; -/// db.put::<_, OwnedType, Unit>(&mut wtxn, &BEI64::new(68), &())?; +/// db.put::(&mut wtxn, &0, &())?; +/// db.put::(&mut wtxn, &35, "thirty five")?; +/// db.put::(&mut wtxn, &42, "forty two")?; +/// db.put::(&mut wtxn, &68, &())?; /// /// // even delete a range of keys -/// let range = BEI64::new(35)..=BEI64::new(42); -/// let deleted = db.delete_range::<_, OwnedType, _>(&mut wtxn, &range)?; +/// let range = 35..=42; +/// let deleted = db.delete_range::(&mut wtxn, &range)?; /// assert_eq!(deleted, 2); /// -/// let rets: Result<_, _> = db.iter::<_, OwnedType, Unit>(&wtxn)?.collect(); -/// let rets: Vec<(BEI64, _)> = rets?; +/// let rets: Result<_, _> = db.iter::(&wtxn)?.collect(); +/// let rets: Vec<(i64, _)> = rets?; /// /// let expected = vec![ -/// (BEI64::new(0), ()), -/// (BEI64::new(68), ()), +/// (0, ()), +/// (68, ()), /// ]; /// /// assert_eq!(deleted, 2); @@ -123,41 +123,44 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; - /// let db = env.create_poly_database(Some("get-poly-i32"))?; + /// # .open(dir.path())?; + /// type BEI32 = I32; /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("get-poly-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, Str, OwnedType>(&mut wtxn, "i-am-forty-two", &42)?; - /// db.put::<_, Str, OwnedType>(&mut wtxn, "i-am-twenty-seven", &27)?; + /// db.put::(&mut wtxn, "i-am-forty-two", &42)?; + /// db.put::(&mut wtxn, "i-am-twenty-seven", &27)?; /// - /// let ret = db.get::<_, Str, OwnedType>(&wtxn, "i-am-forty-two")?; + /// let ret = db.get::(&wtxn, "i-am-forty-two")?; /// assert_eq!(ret, Some(42)); /// - /// let ret = db.get::<_, Str, OwnedType>(&wtxn, "i-am-twenty-one")?; + /// let ret = db.get::(&wtxn, "i-am-twenty-one")?; /// assert_eq!(ret, None); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn get<'a, 'txn, T, KC, DC>( + pub fn get<'a, 'txn, KC, DC>( &self, - txn: &'txn RoTxn, + txn: &'txn RoTxn, key: &'a KC::EItem, ) -> Result> where KC: BytesEncode<'a>, DC: BytesDecode<'txn>, { - assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); + assert_eq_env_db_txn!(self, txn); - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; let mut key_val = unsafe { crate::into_val(&key_bytes) }; let mut data_val = mem::MaybeUninit::uninit(); @@ -169,7 +172,7 @@ impl PolyDatabase { match result { Ok(()) => { let data = unsafe { crate::from_val(data_val.assume_init()) }; - let data = DC::bytes_decode(data).ok_or(Error::Decoding)?; + let data = DC::bytes_decode(data).map_err(Error::Decoding)?; Ok(Some(data)) } Err(e) if e.not_found() => Ok(None), @@ -190,55 +193,55 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::U32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEU32 = U32; /// - /// let db = env.create_poly_database(Some("get-lt-u32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("get-lt-u32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, OwnedType, Unit>(&mut wtxn, &BEU32::new(27), &())?; - /// db.put::<_, OwnedType, Unit>(&mut wtxn, &BEU32::new(42), &())?; - /// db.put::<_, OwnedType, Unit>(&mut wtxn, &BEU32::new(43), &())?; + /// db.put::(&mut wtxn, &27, &())?; + /// db.put::(&mut wtxn, &42, &())?; + /// db.put::(&mut wtxn, &43, &())?; /// - /// let ret = db.get_lower_than::<_, OwnedType, Unit>(&wtxn, &BEU32::new(4404))?; - /// assert_eq!(ret, Some((BEU32::new(43), ()))); + /// let ret = db.get_lower_than::(&wtxn, &4404)?; + /// assert_eq!(ret, Some((43, ()))); /// - /// let ret = db.get_lower_than::<_, OwnedType, Unit>(&wtxn, &BEU32::new(43))?; - /// assert_eq!(ret, Some((BEU32::new(42), ()))); + /// let ret = db.get_lower_than::(&wtxn, &43)?; + /// assert_eq!(ret, Some((42, ()))); /// - /// let ret = db.get_lower_than::<_, OwnedType, Unit>(&wtxn, &BEU32::new(27))?; + /// let ret = db.get_lower_than::(&wtxn, &27)?; /// assert_eq!(ret, None); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn get_lower_than<'a, 'txn, T, KC, DC>( + pub fn get_lower_than<'a, 'txn, KC, DC>( &self, - txn: &'txn RoTxn, + txn: &'txn RoTxn, key: &'a KC::EItem, ) -> Result> where KC: BytesEncode<'a> + BytesDecode<'txn>, DC: BytesDecode<'txn>, { - assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); + assert_eq_env_db_txn!(self, txn); let mut cursor = RoCursor::new(txn, self.dbi)?; - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; cursor.move_on_key_greater_than_or_equal_to(&key_bytes)?; match cursor.move_on_prev() { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Ok(Some((key, data))), - (_, _) => Err(Error::Decoding), + (Ok(key), Ok(data)) => Ok(Some((key, data))), + (Err(e), _) | (_, Err(e)) => Err(Error::Decoding(e)), }, Ok(None) => Ok(None), Err(e) => Err(e), @@ -258,49 +261,49 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::U32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEU32 = U32; /// - /// let db = env.create_poly_database(Some("get-lte-u32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("get-lte-u32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, OwnedType, Unit>(&mut wtxn, &BEU32::new(27), &())?; - /// db.put::<_, OwnedType, Unit>(&mut wtxn, &BEU32::new(42), &())?; - /// db.put::<_, OwnedType, Unit>(&mut wtxn, &BEU32::new(43), &())?; + /// db.put::(&mut wtxn, &27, &())?; + /// db.put::(&mut wtxn, &42, &())?; + /// db.put::(&mut wtxn, &43, &())?; /// - /// let ret = db.get_lower_than_or_equal_to::<_, OwnedType, Unit>(&wtxn, &BEU32::new(4404))?; - /// assert_eq!(ret, Some((BEU32::new(43), ()))); + /// let ret = db.get_lower_than_or_equal_to::(&wtxn, &4404)?; + /// assert_eq!(ret, Some((43, ()))); /// - /// let ret = db.get_lower_than_or_equal_to::<_, OwnedType, Unit>(&wtxn, &BEU32::new(43))?; - /// assert_eq!(ret, Some((BEU32::new(43), ()))); + /// let ret = db.get_lower_than_or_equal_to::(&wtxn, &43)?; + /// assert_eq!(ret, Some((43, ()))); /// - /// let ret = db.get_lower_than_or_equal_to::<_, OwnedType, Unit>(&wtxn, &BEU32::new(26))?; + /// let ret = db.get_lower_than_or_equal_to::(&wtxn, &26)?; /// assert_eq!(ret, None); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn get_lower_than_or_equal_to<'a, 'txn, T, KC, DC>( + pub fn get_lower_than_or_equal_to<'a, 'txn, KC, DC>( &self, - txn: &'txn RoTxn, + txn: &'txn RoTxn, key: &'a KC::EItem, ) -> Result> where KC: BytesEncode<'a> + BytesDecode<'txn>, DC: BytesDecode<'txn>, { - assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); + assert_eq_env_db_txn!(self, txn); let mut cursor = RoCursor::new(txn, self.dbi)?; - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; let result = match cursor.move_on_key_greater_than_or_equal_to(&key_bytes) { Ok(Some((key, data))) if key == &key_bytes[..] => Ok(Some((key, data))), Ok(_) => cursor.move_on_prev(), @@ -309,8 +312,8 @@ impl PolyDatabase { match result { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Ok(Some((key, data))), - (_, _) => Err(Error::Decoding), + (Ok(key), Ok(data)) => Ok(Some((key, data))), + (Err(e), _) | (_, Err(e)) => Err(Error::Decoding(e)), }, Ok(None) => Ok(None), Err(e) => Err(e), @@ -330,49 +333,49 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::U32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEU32 = U32; /// - /// let db = env.create_poly_database(Some("get-lt-u32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("get-lt-u32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, OwnedType, Unit>(&mut wtxn, &BEU32::new(27), &())?; - /// db.put::<_, OwnedType, Unit>(&mut wtxn, &BEU32::new(42), &())?; - /// db.put::<_, OwnedType, Unit>(&mut wtxn, &BEU32::new(43), &())?; + /// db.put::(&mut wtxn, &27, &())?; + /// db.put::(&mut wtxn, &42, &())?; + /// db.put::(&mut wtxn, &43, &())?; /// - /// let ret = db.get_greater_than::<_, OwnedType, Unit>(&wtxn, &BEU32::new(0))?; - /// assert_eq!(ret, Some((BEU32::new(27), ()))); + /// let ret = db.get_greater_than::(&wtxn, &0)?; + /// assert_eq!(ret, Some((27, ()))); /// - /// let ret = db.get_greater_than::<_, OwnedType, Unit>(&wtxn, &BEU32::new(42))?; - /// assert_eq!(ret, Some((BEU32::new(43), ()))); + /// let ret = db.get_greater_than::(&wtxn, &42)?; + /// assert_eq!(ret, Some((43, ()))); /// - /// let ret = db.get_greater_than::<_, OwnedType, Unit>(&wtxn, &BEU32::new(43))?; + /// let ret = db.get_greater_than::(&wtxn, &43)?; /// assert_eq!(ret, None); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn get_greater_than<'a, 'txn, T, KC, DC>( + pub fn get_greater_than<'a, 'txn, KC, DC>( &self, - txn: &'txn RoTxn, + txn: &'txn RoTxn, key: &'a KC::EItem, ) -> Result> where KC: BytesEncode<'a> + BytesDecode<'txn>, DC: BytesDecode<'txn>, { - assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); + assert_eq_env_db_txn!(self, txn); let mut cursor = RoCursor::new(txn, self.dbi)?; - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; let entry = match cursor.move_on_key_greater_than_or_equal_to(&key_bytes)? { Some((key, data)) if key > &key_bytes[..] => Some((key, data)), Some((_key, _data)) => cursor.move_on_next()?, @@ -381,8 +384,8 @@ impl PolyDatabase { match entry { Some((key, data)) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Ok(Some((key, data))), - (_, _) => Err(Error::Decoding), + (Ok(key), Ok(data)) => Ok(Some((key, data))), + (Err(e), _) | (_, Err(e)) => Err(Error::Decoding(e)), }, None => Ok(None), } @@ -401,53 +404,53 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::U32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEU32 = U32; /// - /// let db = env.create_poly_database(Some("get-lt-u32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("get-lt-u32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, OwnedType, Unit>(&mut wtxn, &BEU32::new(27), &())?; - /// db.put::<_, OwnedType, Unit>(&mut wtxn, &BEU32::new(42), &())?; - /// db.put::<_, OwnedType, Unit>(&mut wtxn, &BEU32::new(43), &())?; + /// db.put::(&mut wtxn, &27, &())?; + /// db.put::(&mut wtxn, &42, &())?; + /// db.put::(&mut wtxn, &43, &())?; /// - /// let ret = db.get_greater_than_or_equal_to::<_, OwnedType, Unit>(&wtxn, &BEU32::new(0))?; - /// assert_eq!(ret, Some((BEU32::new(27), ()))); + /// let ret = db.get_greater_than_or_equal_to::(&wtxn, &0)?; + /// assert_eq!(ret, Some((27, ()))); /// - /// let ret = db.get_greater_than_or_equal_to::<_, OwnedType, Unit>(&wtxn, &BEU32::new(42))?; - /// assert_eq!(ret, Some((BEU32::new(42), ()))); + /// let ret = db.get_greater_than_or_equal_to::(&wtxn, &42)?; + /// assert_eq!(ret, Some((42, ()))); /// - /// let ret = db.get_greater_than_or_equal_to::<_, OwnedType, Unit>(&wtxn, &BEU32::new(44))?; + /// let ret = db.get_greater_than_or_equal_to::(&wtxn, &44)?; /// assert_eq!(ret, None); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn get_greater_than_or_equal_to<'a, 'txn, T, KC, DC>( + pub fn get_greater_than_or_equal_to<'a, 'txn, KC, DC>( &self, - txn: &'txn RoTxn, + txn: &'txn RoTxn, key: &'a KC::EItem, ) -> Result> where KC: BytesEncode<'a> + BytesDecode<'txn>, DC: BytesDecode<'txn>, { - assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); + assert_eq_env_db_txn!(self, txn); let mut cursor = RoCursor::new(txn, self.dbi)?; - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; match cursor.move_on_key_greater_than_or_equal_to(&key_bytes) { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Ok(Some((key, data))), - (_, _) => Err(Error::Decoding), + (Ok(key), Ok(data)) => Ok(Some((key, data))), + (Err(e), _) | (_, Err(e)) => Err(Error::Decoding(e)), }, Ok(None) => Ok(None), Err(e) => Err(e), @@ -466,44 +469,41 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db = env.create_poly_database(Some("first-poly-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("first-poly-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; + /// db.put::(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put::(&mut wtxn, &27, "i-am-twenty-seven")?; /// - /// let ret = db.first::<_, OwnedType, Str>(&wtxn)?; - /// assert_eq!(ret, Some((BEI32::new(27), "i-am-twenty-seven"))); + /// let ret = db.first::(&wtxn)?; + /// assert_eq!(ret, Some((27, "i-am-twenty-seven"))); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn first<'txn, T, KC, DC>( - &self, - txn: &'txn RoTxn, - ) -> Result> + pub fn first<'txn, KC, DC>(&self, txn: &'txn RoTxn) -> Result> where KC: BytesDecode<'txn>, DC: BytesDecode<'txn>, { - assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); + assert_eq_env_db_txn!(self, txn); let mut cursor = RoCursor::new(txn, self.dbi)?; match cursor.move_on_first() { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Ok(Some((key, data))), - (_, _) => Err(Error::Decoding), + (Ok(key), Ok(data)) => Ok(Some((key, data))), + (Err(e), _) | (_, Err(e)) => Err(Error::Decoding(e)), }, Ok(None) => Ok(None), Err(e) => Err(e), @@ -522,44 +522,41 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db = env.create_poly_database(Some("last-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("last-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; + /// db.put::(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put::(&mut wtxn, &27, "i-am-twenty-seven")?; /// - /// let ret = db.last::<_, OwnedType, Str>(&wtxn)?; - /// assert_eq!(ret, Some((BEI32::new(42), "i-am-forty-two"))); + /// let ret = db.last::(&wtxn)?; + /// assert_eq!(ret, Some((42, "i-am-forty-two"))); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn last<'txn, T, KC, DC>( - &self, - txn: &'txn RoTxn, - ) -> Result> + pub fn last<'txn, KC, DC>(&self, txn: &'txn RoTxn) -> Result> where KC: BytesDecode<'txn>, DC: BytesDecode<'txn>, { - assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); + assert_eq_env_db_txn!(self, txn); let mut cursor = RoCursor::new(txn, self.dbi)?; match cursor.move_on_last() { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Ok(Some((key, data))), - (_, _) => Err(Error::Decoding), + (Ok(key), Ok(data)) => Ok(Some((key, data))), + (Err(e), _) | (_, Err(e)) => Err(Error::Decoding(e)), }, Ok(None) => Ok(None), Err(e) => Err(e), @@ -574,52 +571,50 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db = env.create_poly_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; + /// db.put::(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put::(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put::(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put::(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; /// /// let ret = db.len(&wtxn)?; /// assert_eq!(ret, 4); /// - /// db.delete::<_, OwnedType>(&mut wtxn, &BEI32::new(27))?; + /// db.delete::(&mut wtxn, &27)?; /// /// let ret = db.len(&wtxn)?; /// assert_eq!(ret, 3); /// /// wtxn.commit()?; + /// /// # Ok(()) } /// ``` - pub fn len<'txn, T>(&self, txn: &'txn RoTxn) -> Result { - assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); + pub fn len<'txn>(&self, txn: &'txn RoTxn) -> Result { + assert_eq_env_db_txn!(self, txn); - let mut cursor = RoCursor::new(txn, self.dbi)?; - let mut count = 0; - - match cursor.move_on_first()? { - Some(_) => count += 1, - None => return Ok(0), - } + let mut db_stat = mem::MaybeUninit::uninit(); + let result = unsafe { mdb_result(ffi::mdb_stat(txn.txn, self.dbi, db_stat.as_mut_ptr())) }; - while let Some(_) = cursor.move_on_next()? { - count += 1; + match result { + Ok(()) => { + let stats = unsafe { db_stat.assume_init() }; + Ok(stats.ms_entries as u64) + } + Err(e) => Err(e.into()), } - - Ok(count) } /// Returns `true` if and only if this database is empty. @@ -630,24 +625,24 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db = env.create_poly_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; + /// db.put::(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put::(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put::(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put::(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; /// /// let ret = db.is_empty(&wtxn)?; /// assert_eq!(ret, false); @@ -660,8 +655,8 @@ impl PolyDatabase { /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn is_empty<'txn, T>(&self, txn: &'txn RoTxn) -> Result { - assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); + pub fn is_empty<'txn>(&self, txn: &'txn RoTxn) -> Result { + assert_eq_env_db_txn!(self, txn); let mut cursor = RoCursor::new(txn, self.dbi)?; match cursor.move_on_first()? { @@ -678,36 +673,36 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db = env.create_poly_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// - /// let mut iter = db.iter::<_, OwnedType, Str>(&wtxn)?; - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(13), "i-am-thirteen"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two"))); + /// db.put::(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put::(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put::(&mut wtxn, &13, "i-am-thirteen")?; + /// + /// let mut iter = db.iter::(&wtxn)?; + /// assert_eq!(iter.next().transpose()?, Some((13, "i-am-thirteen"))); + /// assert_eq!(iter.next().transpose()?, Some((27, "i-am-twenty-seven"))); + /// assert_eq!(iter.next().transpose()?, Some((42, "i-am-forty-two"))); /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn iter<'txn, T, KC, DC>(&self, txn: &'txn RoTxn) -> Result> { - assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); + pub fn iter<'txn, KC, DC>(&self, txn: &'txn RoTxn) -> Result> { + assert_eq_env_db_txn!(self, txn); RoCursor::new(txn, self.dbi).map(|cursor| RoIter::new(cursor)) } @@ -720,52 +715,49 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db = env.create_poly_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; + /// db.put::(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put::(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put::(&mut wtxn, &13, "i-am-thirteen")?; /// - /// let mut iter = db.iter_mut::<_, OwnedType, Str>(&mut wtxn)?; - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(13), "i-am-thirteen"))); + /// let mut iter = db.iter_mut::(&mut wtxn)?; + /// assert_eq!(iter.next().transpose()?, Some((13, "i-am-thirteen"))); /// let ret = unsafe { iter.del_current()? }; /// assert!(ret); /// - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two"))); - /// let ret = unsafe { iter.put_current(&BEI32::new(42), "i-am-the-new-forty-two")? }; + /// assert_eq!(iter.next().transpose()?, Some((27, "i-am-twenty-seven"))); + /// assert_eq!(iter.next().transpose()?, Some((42, "i-am-forty-two"))); + /// let ret = unsafe { iter.put_current(&42, "i-am-the-new-forty-two")? }; /// assert!(ret); /// /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// - /// let ret = db.get::<_, OwnedType, Str>(&wtxn, &BEI32::new(13))?; + /// let ret = db.get::(&wtxn, &13)?; /// assert_eq!(ret, None); /// - /// let ret = db.get::<_, OwnedType, Str>(&wtxn, &BEI32::new(42))?; + /// let ret = db.get::(&wtxn, &42)?; /// assert_eq!(ret, Some("i-am-the-new-forty-two")); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn iter_mut<'txn, T, KC, DC>( - &self, - txn: &'txn mut RwTxn, - ) -> Result> { - assert_eq!(self.env_ident, txn.txn.env.env_mut_ptr() as usize); + pub fn iter_mut<'txn, KC, DC>(&self, txn: &'txn mut RwTxn) -> Result> { + assert_eq_env_db_txn!(self, txn); RwCursor::new(txn, self.dbi).map(|cursor| RwIter::new(cursor)) } @@ -778,39 +770,36 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db = env.create_poly_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// - /// let mut iter = db.rev_iter::<_, OwnedType, Str>(&wtxn)?; - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(13), "i-am-thirteen"))); + /// db.put::(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put::(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put::(&mut wtxn, &13, "i-am-thirteen")?; + /// + /// let mut iter = db.rev_iter::(&wtxn)?; + /// assert_eq!(iter.next().transpose()?, Some((42, "i-am-forty-two"))); + /// assert_eq!(iter.next().transpose()?, Some((27, "i-am-twenty-seven"))); + /// assert_eq!(iter.next().transpose()?, Some((13, "i-am-thirteen"))); /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn rev_iter<'txn, T, KC, DC>( - &self, - txn: &'txn RoTxn, - ) -> Result> { - assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); + pub fn rev_iter<'txn, KC, DC>(&self, txn: &'txn RoTxn) -> Result> { + assert_eq_env_db_txn!(self, txn); RoCursor::new(txn, self.dbi).map(|cursor| RoRevIter::new(cursor)) } @@ -824,52 +813,52 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db = env.create_poly_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; + /// db.put::(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put::(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put::(&mut wtxn, &13, "i-am-thirteen")?; /// - /// let mut iter = db.rev_iter_mut::<_, OwnedType, Str>(&mut wtxn)?; - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two"))); + /// let mut iter = db.rev_iter_mut::(&mut wtxn)?; + /// assert_eq!(iter.next().transpose()?, Some((42, "i-am-forty-two"))); /// let ret = unsafe { iter.del_current()? }; /// assert!(ret); /// - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(13), "i-am-thirteen"))); - /// let ret = unsafe { iter.put_current(&BEI32::new(13), "i-am-the-new-thirteen")? }; + /// assert_eq!(iter.next().transpose()?, Some((27, "i-am-twenty-seven"))); + /// assert_eq!(iter.next().transpose()?, Some((13, "i-am-thirteen"))); + /// let ret = unsafe { iter.put_current(&13, "i-am-the-new-thirteen")? }; /// assert!(ret); /// /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// - /// let ret = db.get::<_, OwnedType, Str>(&wtxn, &BEI32::new(42))?; + /// let ret = db.get::(&wtxn, &42)?; /// assert_eq!(ret, None); /// - /// let ret = db.get::<_, OwnedType, Str>(&wtxn, &BEI32::new(13))?; + /// let ret = db.get::(&wtxn, &13)?; /// assert_eq!(ret, Some("i-am-the-new-thirteen")); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn rev_iter_mut<'txn, T, KC, DC>( + pub fn rev_iter_mut<'txn, KC, DC>( &self, - txn: &'txn mut RwTxn, + txn: &'txn mut RwTxn, ) -> Result> { - assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); + assert_eq_env_db_txn!(self, txn); RwCursor::new(txn, self.dbi).map(|cursor| RwRevIter::new(cursor)) } @@ -884,53 +873,53 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db = env.create_poly_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; - /// - /// let range = BEI32::new(27)..=BEI32::new(42); - /// let mut iter = db.range::<_, OwnedType, Str, _>(&wtxn, &range)?; - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two"))); + /// db.put::(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put::(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put::(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put::(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; + /// + /// let range = 27..=42; + /// let mut iter = db.range::(&wtxn, &range)?; + /// assert_eq!(iter.next().transpose()?, Some((27, "i-am-twenty-seven"))); + /// assert_eq!(iter.next().transpose()?, Some((42, "i-am-forty-two"))); /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn range<'a, 'txn, T, KC, DC, R>( + pub fn range<'a, 'txn, KC, DC, R>( &self, - txn: &'txn RoTxn, + txn: &'txn RoTxn, range: &'a R, ) -> Result> where KC: BytesEncode<'a>, R: RangeBounds, { - assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); + assert_eq_env_db_txn!(self, txn); let start_bound = match range.start_bound() { Bound::Included(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Included(bytes.into_owned()) } Bound::Excluded(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Excluded(bytes.into_owned()) } Bound::Unbounded => Bound::Unbounded, @@ -938,11 +927,11 @@ impl PolyDatabase { let end_bound = match range.end_bound() { Bound::Included(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Included(bytes.into_owned()) } Bound::Excluded(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Excluded(bytes.into_owned()) } Bound::Unbounded => Bound::Unbounded, @@ -962,66 +951,66 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db = env.create_poly_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; - /// - /// let range = BEI32::new(27)..=BEI32::new(42); - /// let mut range = db.range_mut::<_, OwnedType, Str, _>(&mut wtxn, &range)?; - /// assert_eq!(range.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven"))); + /// db.put::(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put::(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put::(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put::(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; + /// + /// let range = 27..=42; + /// let mut range = db.range_mut::(&mut wtxn, &range)?; + /// assert_eq!(range.next().transpose()?, Some((27, "i-am-twenty-seven"))); /// let ret = unsafe { range.del_current()? }; /// assert!(ret); - /// assert_eq!(range.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two"))); - /// let ret = unsafe { range.put_current(&BEI32::new(42), "i-am-the-new-forty-two")? }; + /// assert_eq!(range.next().transpose()?, Some((42, "i-am-forty-two"))); + /// let ret = unsafe { range.put_current(&42, "i-am-the-new-forty-two")? }; /// assert!(ret); /// /// assert_eq!(range.next().transpose()?, None); /// drop(range); /// /// - /// let mut iter = db.iter::<_, OwnedType, Str>(&wtxn)?; - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(13), "i-am-thirteen"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(42), "i-am-the-new-forty-two"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(521), "i-am-five-hundred-and-twenty-one"))); + /// let mut iter = db.iter::(&wtxn)?; + /// assert_eq!(iter.next().transpose()?, Some((13, "i-am-thirteen"))); + /// assert_eq!(iter.next().transpose()?, Some((42, "i-am-the-new-forty-two"))); + /// assert_eq!(iter.next().transpose()?, Some((521, "i-am-five-hundred-and-twenty-one"))); /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn range_mut<'a, 'txn, T, KC, DC, R>( + pub fn range_mut<'a, 'txn, KC, DC, R>( &self, - txn: &'txn mut RwTxn, + txn: &'txn mut RwTxn, range: &'a R, ) -> Result> where KC: BytesEncode<'a>, R: RangeBounds, { - assert_eq!(self.env_ident, txn.txn.env.env_mut_ptr() as usize); + assert_eq_env_db_txn!(self, txn); let start_bound = match range.start_bound() { Bound::Included(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Included(bytes.into_owned()) } Bound::Excluded(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Excluded(bytes.into_owned()) } Bound::Unbounded => Bound::Unbounded, @@ -1029,11 +1018,11 @@ impl PolyDatabase { let end_bound = match range.end_bound() { Bound::Included(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Included(bytes.into_owned()) } Bound::Excluded(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Excluded(bytes.into_owned()) } Bound::Unbounded => Bound::Unbounded, @@ -1053,53 +1042,53 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db = env.create_poly_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; - /// - /// let range = BEI32::new(27)..=BEI32::new(43); - /// let mut iter = db.rev_range::<_, OwnedType, Str, _>(&wtxn, &range)?; - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven"))); + /// db.put::(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put::(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put::(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put::(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; + /// + /// let range = 27..=43; + /// let mut iter = db.rev_range::(&wtxn, &range)?; + /// assert_eq!(iter.next().transpose()?, Some((42, "i-am-forty-two"))); + /// assert_eq!(iter.next().transpose()?, Some((27, "i-am-twenty-seven"))); /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn rev_range<'a, 'txn, T, KC, DC, R>( + pub fn rev_range<'a, 'txn, KC, DC, R>( &self, - txn: &'txn RoTxn, + txn: &'txn RoTxn, range: &'a R, ) -> Result> where KC: BytesEncode<'a>, R: RangeBounds, { - assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); + assert_eq_env_db_txn!(self, txn); let start_bound = match range.start_bound() { Bound::Included(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Included(bytes.into_owned()) } Bound::Excluded(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Excluded(bytes.into_owned()) } Bound::Unbounded => Bound::Unbounded, @@ -1107,11 +1096,11 @@ impl PolyDatabase { let end_bound = match range.end_bound() { Bound::Included(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Included(bytes.into_owned()) } Bound::Excluded(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Excluded(bytes.into_owned()) } Bound::Unbounded => Bound::Unbounded, @@ -1131,66 +1120,66 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db = env.create_poly_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; - /// - /// let range = BEI32::new(27)..=BEI32::new(42); - /// let mut range = db.rev_range_mut::<_, OwnedType, Str, _>(&mut wtxn, &range)?; - /// assert_eq!(range.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two"))); + /// db.put::(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put::(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put::(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put::(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; + /// + /// let range = 27..=42; + /// let mut range = db.rev_range_mut::(&mut wtxn, &range)?; + /// assert_eq!(range.next().transpose()?, Some((42, "i-am-forty-two"))); /// let ret = unsafe { range.del_current()? }; /// assert!(ret); - /// assert_eq!(range.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven"))); - /// let ret = unsafe { range.put_current(&BEI32::new(27), "i-am-the-new-twenty-seven")? }; + /// assert_eq!(range.next().transpose()?, Some((27, "i-am-twenty-seven"))); + /// let ret = unsafe { range.put_current(&27, "i-am-the-new-twenty-seven")? }; /// assert!(ret); /// /// assert_eq!(range.next().transpose()?, None); /// drop(range); /// /// - /// let mut iter = db.iter::<_, OwnedType, Str>(&wtxn)?; - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(13), "i-am-thirteen"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(27), "i-am-the-new-twenty-seven"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(521), "i-am-five-hundred-and-twenty-one"))); + /// let mut iter = db.iter::(&wtxn)?; + /// assert_eq!(iter.next().transpose()?, Some((13, "i-am-thirteen"))); + /// assert_eq!(iter.next().transpose()?, Some((27, "i-am-the-new-twenty-seven"))); + /// assert_eq!(iter.next().transpose()?, Some((521, "i-am-five-hundred-and-twenty-one"))); /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn rev_range_mut<'a, 'txn, T, KC, DC, R>( + pub fn rev_range_mut<'a, 'txn, KC, DC, R>( &self, - txn: &'txn mut RwTxn, + txn: &'txn mut RwTxn, range: &'a R, ) -> Result> where KC: BytesEncode<'a>, R: RangeBounds, { - assert_eq!(self.env_ident, txn.txn.env.env_mut_ptr() as usize); + assert_eq_env_db_txn!(self, txn); let start_bound = match range.start_bound() { Bound::Included(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Included(bytes.into_owned()) } Bound::Excluded(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Excluded(bytes.into_owned()) } Bound::Unbounded => Bound::Unbounded, @@ -1198,11 +1187,11 @@ impl PolyDatabase { let end_bound = match range.end_bound() { Bound::Included(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Included(bytes.into_owned()) } Bound::Excluded(bound) => { - let bytes = KC::bytes_encode(bound).ok_or(Error::Encoding)?; + let bytes = KC::bytes_encode(bound).map_err(Error::Encoding)?; Bound::Excluded(bytes.into_owned()) } Bound::Unbounded => Bound::Unbounded, @@ -1222,47 +1211,47 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db = env.create_poly_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, Str, OwnedType>(&mut wtxn, "i-am-twenty-eight", &BEI32::new(28))?; - /// db.put::<_, Str, OwnedType>(&mut wtxn, "i-am-twenty-seven", &BEI32::new(27))?; - /// db.put::<_, Str, OwnedType>(&mut wtxn, "i-am-twenty-nine", &BEI32::new(29))?; - /// db.put::<_, Str, OwnedType>(&mut wtxn, "i-am-forty-one", &BEI32::new(41))?; - /// db.put::<_, Str, OwnedType>(&mut wtxn, "i-am-forty-two", &BEI32::new(42))?; - /// - /// let mut iter = db.prefix_iter::<_, Str, OwnedType>(&mut wtxn, "i-am-twenty")?; - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", BEI32::new(28)))); - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", BEI32::new(29)))); - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", BEI32::new(27)))); + /// db.put::(&mut wtxn, "i-am-twenty-eight", &28)?; + /// db.put::(&mut wtxn, "i-am-twenty-seven", &27)?; + /// db.put::(&mut wtxn, "i-am-twenty-nine", &29)?; + /// db.put::(&mut wtxn, "i-am-forty-one", &41)?; + /// db.put::(&mut wtxn, "i-am-forty-two", &42)?; + /// + /// let mut iter = db.prefix_iter::(&mut wtxn, "i-am-twenty")?; + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", 28))); + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", 29))); + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", 27))); /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn prefix_iter<'a, 'txn, T, KC, DC>( + pub fn prefix_iter<'a, 'txn, KC, DC>( &self, - txn: &'txn RoTxn, + txn: &'txn RoTxn, prefix: &'a KC::EItem, ) -> Result> where KC: BytesEncode<'a>, { - assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); + assert_eq_env_db_txn!(self, txn); - let prefix_bytes = KC::bytes_encode(prefix).ok_or(Error::Encoding)?; + let prefix_bytes = KC::bytes_encode(prefix).map_err(Error::Encoding)?; let prefix_bytes = prefix_bytes.into_owned(); RoCursor::new(txn, self.dbi).map(|cursor| RoPrefix::new(cursor, prefix_bytes)) } @@ -1278,60 +1267,60 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db = env.create_poly_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, Str, OwnedType>(&mut wtxn, "i-am-twenty-eight", &BEI32::new(28))?; - /// db.put::<_, Str, OwnedType>(&mut wtxn, "i-am-twenty-seven", &BEI32::new(27))?; - /// db.put::<_, Str, OwnedType>(&mut wtxn, "i-am-twenty-nine", &BEI32::new(29))?; - /// db.put::<_, Str, OwnedType>(&mut wtxn, "i-am-forty-one", &BEI32::new(41))?; - /// db.put::<_, Str, OwnedType>(&mut wtxn, "i-am-forty-two", &BEI32::new(42))?; - /// - /// let mut iter = db.prefix_iter_mut::<_, Str, OwnedType>(&mut wtxn, "i-am-twenty")?; - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", BEI32::new(28)))); + /// db.put::(&mut wtxn, "i-am-twenty-eight", &28)?; + /// db.put::(&mut wtxn, "i-am-twenty-seven", &27)?; + /// db.put::(&mut wtxn, "i-am-twenty-nine", &29)?; + /// db.put::(&mut wtxn, "i-am-forty-one", &41)?; + /// db.put::(&mut wtxn, "i-am-forty-two", &42)?; + /// + /// let mut iter = db.prefix_iter_mut::(&mut wtxn, "i-am-twenty")?; + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", 28))); /// let ret = unsafe { iter.del_current()? }; /// assert!(ret); /// - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", BEI32::new(29)))); - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", BEI32::new(27)))); - /// let ret = unsafe { iter.put_current("i-am-twenty-seven", &BEI32::new(27000))? }; + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", 29))); + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", 27))); + /// let ret = unsafe { iter.put_current("i-am-twenty-seven", &27000)? }; /// assert!(ret); /// /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// - /// let ret = db.get::<_, Str, OwnedType>(&wtxn, "i-am-twenty-eight")?; + /// let ret = db.get::(&wtxn, "i-am-twenty-eight")?; /// assert_eq!(ret, None); /// - /// let ret = db.get::<_, Str, OwnedType>(&wtxn, "i-am-twenty-seven")?; - /// assert_eq!(ret, Some(BEI32::new(27000))); + /// let ret = db.get::(&wtxn, "i-am-twenty-seven")?; + /// assert_eq!(ret, Some(27000)); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn prefix_iter_mut<'a, 'txn, T, KC, DC>( + pub fn prefix_iter_mut<'a, 'txn, KC, DC>( &self, - txn: &'txn mut RwTxn, + txn: &'txn mut RwTxn, prefix: &'a KC::EItem, ) -> Result> where KC: BytesEncode<'a>, { - assert_eq!(self.env_ident, txn.txn.env.env_mut_ptr() as usize); + assert_eq_env_db_txn!(self, txn); - let prefix_bytes = KC::bytes_encode(prefix).ok_or(Error::Encoding)?; + let prefix_bytes = KC::bytes_encode(prefix).map_err(Error::Encoding)?; let prefix_bytes = prefix_bytes.into_owned(); RwCursor::new(txn, self.dbi).map(|cursor| RwPrefix::new(cursor, prefix_bytes)) } @@ -1347,47 +1336,47 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db = env.create_poly_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, Str, OwnedType>(&mut wtxn, "i-am-twenty-eight", &BEI32::new(28))?; - /// db.put::<_, Str, OwnedType>(&mut wtxn, "i-am-twenty-seven", &BEI32::new(27))?; - /// db.put::<_, Str, OwnedType>(&mut wtxn, "i-am-twenty-nine", &BEI32::new(29))?; - /// db.put::<_, Str, OwnedType>(&mut wtxn, "i-am-forty-one", &BEI32::new(41))?; - /// db.put::<_, Str, OwnedType>(&mut wtxn, "i-am-forty-two", &BEI32::new(42))?; - /// - /// let mut iter = db.rev_prefix_iter::<_, Str, OwnedType>(&mut wtxn, "i-am-twenty")?; - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", BEI32::new(27)))); - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", BEI32::new(29)))); - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", BEI32::new(28)))); + /// db.put::(&mut wtxn, "i-am-twenty-eight", &28)?; + /// db.put::(&mut wtxn, "i-am-twenty-seven", &27)?; + /// db.put::(&mut wtxn, "i-am-twenty-nine", &29)?; + /// db.put::(&mut wtxn, "i-am-forty-one", &41)?; + /// db.put::(&mut wtxn, "i-am-forty-two", &42)?; + /// + /// let mut iter = db.rev_prefix_iter::(&mut wtxn, "i-am-twenty")?; + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", 27))); + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", 29))); + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", 28))); /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn rev_prefix_iter<'a, 'txn, T, KC, DC>( + pub fn rev_prefix_iter<'a, 'txn, KC, DC>( &self, - txn: &'txn RoTxn, + txn: &'txn RoTxn, prefix: &'a KC::EItem, ) -> Result> where KC: BytesEncode<'a>, { - assert_eq!(self.env_ident, txn.env.env_mut_ptr() as usize); + assert_eq_env_db_txn!(self, txn); - let prefix_bytes = KC::bytes_encode(prefix).ok_or(Error::Encoding)?; + let prefix_bytes = KC::bytes_encode(prefix).map_err(Error::Encoding)?; let prefix_bytes = prefix_bytes.into_owned(); RoCursor::new(txn, self.dbi).map(|cursor| RoRevPrefix::new(cursor, prefix_bytes)) } @@ -1403,65 +1392,65 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db = env.create_poly_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, Str, OwnedType>(&mut wtxn, "i-am-twenty-eight", &BEI32::new(28))?; - /// db.put::<_, Str, OwnedType>(&mut wtxn, "i-am-twenty-seven", &BEI32::new(27))?; - /// db.put::<_, Str, OwnedType>(&mut wtxn, "i-am-twenty-nine", &BEI32::new(29))?; - /// db.put::<_, Str, OwnedType>(&mut wtxn, "i-am-forty-one", &BEI32::new(41))?; - /// db.put::<_, Str, OwnedType>(&mut wtxn, "i-am-forty-two", &BEI32::new(42))?; - /// - /// let mut iter = db.rev_prefix_iter_mut::<_, Str, OwnedType>(&mut wtxn, "i-am-twenty")?; - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", BEI32::new(27)))); + /// db.put::(&mut wtxn, "i-am-twenty-eight", &28)?; + /// db.put::(&mut wtxn, "i-am-twenty-seven", &27)?; + /// db.put::(&mut wtxn, "i-am-twenty-nine", &29)?; + /// db.put::(&mut wtxn, "i-am-forty-one", &41)?; + /// db.put::(&mut wtxn, "i-am-forty-two", &42)?; + /// + /// let mut iter = db.rev_prefix_iter_mut::(&mut wtxn, "i-am-twenty")?; + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", 27))); /// let ret = unsafe { iter.del_current()? }; /// assert!(ret); /// - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", BEI32::new(29)))); - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", BEI32::new(28)))); - /// let ret = unsafe { iter.put_current("i-am-twenty-eight", &BEI32::new(28000))? }; + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", 29))); + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", 28))); + /// let ret = unsafe { iter.put_current("i-am-twenty-eight", &28000)? }; /// assert!(ret); /// /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// - /// let ret = db.get::<_, Str, OwnedType>(&wtxn, "i-am-twenty-seven")?; + /// let ret = db.get::(&wtxn, "i-am-twenty-seven")?; /// assert_eq!(ret, None); /// - /// let ret = db.get::<_, Str, OwnedType>(&wtxn, "i-am-twenty-eight")?; - /// assert_eq!(ret, Some(BEI32::new(28000))); + /// let ret = db.get::(&wtxn, "i-am-twenty-eight")?; + /// assert_eq!(ret, Some(28000)); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn rev_prefix_iter_mut<'a, 'txn, T, KC, DC>( + pub fn rev_prefix_iter_mut<'a, 'txn, KC, DC>( &self, - txn: &'txn mut RwTxn, + txn: &'txn mut RwTxn, prefix: &'a KC::EItem, ) -> Result> where KC: BytesEncode<'a>, { - assert_eq!(self.env_ident, txn.txn.env.env_mut_ptr() as usize); + assert_eq_env_db_txn!(self, txn); - let prefix_bytes = KC::bytes_encode(prefix).ok_or(Error::Encoding)?; + let prefix_bytes = KC::bytes_encode(prefix).map_err(Error::Encoding)?; let prefix_bytes = prefix_bytes.into_owned(); RwCursor::new(txn, self.dbi).map(|cursor| RwRevPrefix::new(cursor, prefix_bytes)) } - /// Insert a key-value pairs in this database. + /// Insert a key-value pair in this database. /// /// ``` /// # use std::fs; @@ -1469,34 +1458,34 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db = env.create_poly_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; + /// db.put::(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put::(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put::(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put::(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; /// - /// let ret = db.get::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(27))?; + /// let ret = db.get::(&mut wtxn, &27)?; /// assert_eq!(ret, Some("i-am-twenty-seven")); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn put<'a, T, KC, DC>( + pub fn put<'a, KC, DC>( &self, - txn: &mut RwTxn, + txn: &mut RwTxn, key: &'a KC::EItem, data: &'a DC::EItem, ) -> Result<()> @@ -1504,10 +1493,10 @@ impl PolyDatabase { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - assert_eq!(self.env_ident, txn.txn.env.env_mut_ptr() as usize); + assert_eq_env_db_txn!(self, txn); - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + 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)?; let mut key_val = unsafe { crate::into_val(&key_bytes) }; let mut data_val = unsafe { crate::into_val(&data_bytes) }; @@ -1520,6 +1509,71 @@ impl PolyDatabase { Ok(()) } + /// Insert a key-value pair where the value can directly be written to disk. + /// + /// ``` + /// # use std::fs; + /// # use std::path::Path; + /// # use heed::EnvOpenOptions; + /// use std::io::Write; + /// use heed::Database; + /// use heed::types::*; + /// use heed::byteorder::BigEndian; + /// + /// # fn main() -> Result<(), Box> { + /// # let dir = tempfile::tempdir()?; + /// # let env = EnvOpenOptions::new() + /// # .map_size(10 * 1024 * 1024) // 10MB + /// # .max_dbs(3000) + /// # .open(dir.path())?; + /// type BEI32 = I32; + /// + /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("iter-i32"))?; + /// + /// # db.clear(&mut wtxn)?; + /// let value = "I am a long long long value"; + /// db.put_reserved::(&mut wtxn, &42, value.len(), |reserved| { + /// reserved.write_all(value.as_bytes()) + /// })?; + /// + /// let ret = db.get::(&mut wtxn, &42)?; + /// assert_eq!(ret, Some(value)); + /// + /// wtxn.commit()?; + /// # Ok(()) } + /// ``` + pub fn put_reserved<'a, KC, F>( + &self, + txn: &mut RwTxn, + key: &'a KC::EItem, + data_size: usize, + mut write_func: F, + ) -> Result<()> + where + KC: BytesEncode<'a>, + F: FnMut(&mut ReservedSpace) -> io::Result<()>, + { + 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) }; + let mut reserved = ffi::reserve_size_val(data_size); + let flags = ffi::MDB_RESERVE; + + unsafe { + mdb_result(ffi::mdb_put(txn.txn.txn, self.dbi, &mut key_val, &mut reserved, flags))? + } + + let mut reserved = unsafe { ReservedSpace::from_val(reserved) }; + (write_func)(&mut reserved)?; + if reserved.remaining() == 0 { + Ok(()) + } else { + Err(io::Error::from(io::ErrorKind::UnexpectedEof).into()) + } + } + /// Append the given key/data pair to the end of the database. /// /// This option allows fast bulk loading when keys are already known to be in the correct order. @@ -1531,34 +1585,34 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db = env.create_poly_database(Some("append-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("append-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; + /// db.put::(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put::(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put::(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put::(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; /// - /// let ret = db.get::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(27))?; + /// let ret = db.get::(&mut wtxn, &27)?; /// assert_eq!(ret, Some("i-am-twenty-seven")); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn append<'a, T, KC, DC>( + pub fn append<'a, KC, DC>( &self, - txn: &mut RwTxn, + txn: &mut RwTxn, key: &'a KC::EItem, data: &'a DC::EItem, ) -> Result<()> @@ -1566,10 +1620,10 @@ impl PolyDatabase { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - assert_eq!(self.env_ident, txn.txn.env.env_mut_ptr() as usize); + assert_eq_env_db_txn!(self, txn); - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + 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)?; let mut key_val = unsafe { crate::into_val(&key_bytes) }; let mut data_val = unsafe { crate::into_val(&data_bytes) }; @@ -1592,44 +1646,44 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db = env.create_poly_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; + /// db.put::(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put::(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put::(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put::(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; /// - /// let ret = db.delete::<_, OwnedType>(&mut wtxn, &BEI32::new(27))?; + /// let ret = db.delete::(&mut wtxn, &27)?; /// assert_eq!(ret, true); /// - /// let ret = db.get::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(27))?; + /// let ret = db.get::(&mut wtxn, &27)?; /// assert_eq!(ret, None); /// - /// let ret = db.delete::<_, OwnedType>(&mut wtxn, &BEI32::new(467))?; + /// let ret = db.delete::(&mut wtxn, &467)?; /// assert_eq!(ret, false); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn delete<'a, T, KC>(&self, txn: &mut RwTxn, key: &'a KC::EItem) -> Result + pub fn delete<'a, KC>(&self, txn: &mut RwTxn, key: &'a KC::EItem) -> Result where KC: BytesEncode<'a>, { - assert_eq!(self.env_ident, txn.txn.env.env_mut_ptr() as usize); + assert_eq_env_db_txn!(self, txn); - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; let mut key_val = unsafe { crate::into_val(&key_bytes) }; let result = unsafe { @@ -1658,51 +1712,47 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db = env.create_poly_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; + /// db.put::(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put::(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put::(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put::(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; /// - /// let range = BEI32::new(27)..=BEI32::new(42); - /// let ret = db.delete_range::<_, OwnedType, _>(&mut wtxn, &range)?; + /// let range = 27..=42; + /// let ret = db.delete_range::(&mut wtxn, &range)?; /// assert_eq!(ret, 2); /// - /// let mut iter = db.iter::<_, OwnedType, Str>(&wtxn)?; - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(13), "i-am-thirteen"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(521), "i-am-five-hundred-and-twenty-one"))); + /// let mut iter = db.iter::(&wtxn)?; + /// assert_eq!(iter.next().transpose()?, Some((13, "i-am-thirteen"))); + /// assert_eq!(iter.next().transpose()?, Some((521, "i-am-five-hundred-and-twenty-one"))); /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn delete_range<'a, 'txn, T, KC, R>( - &self, - txn: &'txn mut RwTxn, - range: &'a R, - ) -> Result + pub fn delete_range<'a, 'txn, KC, R>(&self, txn: &'txn mut RwTxn, range: &'a R) -> Result where KC: BytesEncode<'a> + BytesDecode<'txn>, R: RangeBounds, { - assert_eq!(self.env_ident, txn.txn.env.env_mut_ptr() as usize); + assert_eq_env_db_txn!(self, txn); let mut count = 0; - let mut iter = self.range_mut::(txn, range)?; + let mut iter = self.range_mut::(txn, range)?; while iter.next().is_some() { // safety: We do not keep any reference from the database while using `del_current`. @@ -1728,24 +1778,24 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db = env.create_poly_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put::<_, OwnedType, Str>(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; + /// db.put::(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put::(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put::(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put::(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; /// /// db.clear(&mut wtxn)?; /// @@ -1755,8 +1805,8 @@ impl PolyDatabase { /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn clear(&self, txn: &mut RwTxn) -> Result<()> { - assert_eq!(self.env_ident, txn.txn.env.env_mut_ptr() as usize); + pub fn clear(&self, txn: &mut RwTxn) -> Result<()> { + assert_eq_env_db_txn!(self, txn); unsafe { mdb_result(ffi::mdb_drop(txn.txn.txn, self.dbi, 0)).map_err(Into::into) } } @@ -1777,26 +1827,26 @@ impl PolyDatabase { /// # use heed::EnvOpenOptions; /// use heed::{Database, PolyDatabase}; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db = env.create_poly_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_poly_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; /// // We remap the types for ease of use. - /// let db = db.as_uniform::, Str>(); - /// db.put(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; + /// let db = db.as_uniform::(); + /// db.put(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; /// /// wtxn.commit()?; /// # Ok(()) } @@ -1805,3 +1855,9 @@ impl PolyDatabase { Database::new(self.env_ident, self.dbi) } } + +impl fmt::Debug for PolyDatabase { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("PolyDatabase").finish() + } +} diff --git a/heed/src/db/uniform.rs b/heed/src/db/uniform.rs index 13a44512..6cbd2548 100644 --- a/heed/src/db/uniform.rs +++ b/heed/src/db/uniform.rs @@ -1,5 +1,5 @@ -use std::marker; use std::ops::RangeBounds; +use std::{any, fmt, marker}; use crate::mdb::ffi; use crate::*; @@ -18,34 +18,34 @@ use crate::*; /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; -/// use heed::{zerocopy::I64, byteorder::BigEndian}; +/// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { -/// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; +/// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) -/// # .open(Path::new("target").join("zerocopy.mdb"))?; +/// # .open(dir.path())?; /// type BEI64 = I64; /// -/// let db: Database, Unit> = env.create_database(Some("big-endian-iter"))?; -/// /// let mut wtxn = env.write_txn()?; +/// let db: Database = env.create_database(&mut wtxn, Some("big-endian-iter"))?; +/// /// # db.clear(&mut wtxn)?; -/// db.put(&mut wtxn, &BEI64::new(68), &())?; -/// db.put(&mut wtxn, &BEI64::new(35), &())?; -/// db.put(&mut wtxn, &BEI64::new(0), &())?; -/// db.put(&mut wtxn, &BEI64::new(42), &())?; +/// db.put(&mut wtxn, &68, &())?; +/// db.put(&mut wtxn, &35, &())?; +/// db.put(&mut wtxn, &0, &())?; +/// db.put(&mut wtxn, &42, &())?; /// /// // you can iterate over database entries in order /// let rets: Result<_, _> = db.iter(&wtxn)?.collect(); -/// let rets: Vec<(BEI64, _)> = rets?; +/// let rets: Vec<(i64, _)> = rets?; /// /// let expected = vec![ -/// (BEI64::new(0), ()), -/// (BEI64::new(35), ()), -/// (BEI64::new(42), ()), -/// (BEI64::new(68), ()), +/// (0, ()), +/// (35, ()), +/// (42, ()), +/// (68, ()), /// ]; /// /// assert_eq!(rets, expected); @@ -65,47 +65,47 @@ use crate::*; /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; -/// use heed::{zerocopy::I64, byteorder::BigEndian}; +/// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { -/// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; +/// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) -/// # .open(Path::new("target").join("zerocopy.mdb"))?; +/// # .open(dir.path())?; /// type BEI64 = I64; /// -/// let db: Database, Unit> = env.create_database(Some("big-endian-iter"))?; -/// /// let mut wtxn = env.write_txn()?; +/// let db: Database = env.create_database(&mut wtxn, Some("big-endian-iter"))?; +/// /// # db.clear(&mut wtxn)?; -/// db.put(&mut wtxn, &BEI64::new(0), &())?; -/// db.put(&mut wtxn, &BEI64::new(68), &())?; -/// db.put(&mut wtxn, &BEI64::new(35), &())?; -/// db.put(&mut wtxn, &BEI64::new(42), &())?; +/// db.put(&mut wtxn, &0, &())?; +/// db.put(&mut wtxn, &68, &())?; +/// db.put(&mut wtxn, &35, &())?; +/// db.put(&mut wtxn, &42, &())?; /// /// // you can iterate over ranges too!!! -/// let range = BEI64::new(35)..=BEI64::new(42); +/// let range = 35..=42; /// let rets: Result<_, _> = db.range(&wtxn, &range)?.collect(); -/// let rets: Vec<(BEI64, _)> = rets?; +/// let rets: Vec<(i64, _)> = rets?; /// /// let expected = vec![ -/// (BEI64::new(35), ()), -/// (BEI64::new(42), ()), +/// (35, ()), +/// (42, ()), /// ]; /// /// assert_eq!(rets, expected); /// /// // even delete a range of keys -/// let range = BEI64::new(35)..=BEI64::new(42); +/// let range = 35..=42; /// let deleted: usize = db.delete_range(&mut wtxn, &range)?; /// /// let rets: Result<_, _> = db.iter(&wtxn)?.collect(); -/// let rets: Vec<(BEI64, _)> = rets?; +/// let rets: Vec<(i64, _)> = rets?; /// /// let expected = vec![ -/// (BEI64::new(0), ()), -/// (BEI64::new(68), ()), +/// (0, ()), +/// (68, ()), /// ]; /// /// assert_eq!(deleted, 2); @@ -134,16 +134,19 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; - /// let db: Database> = env.create_database(Some("get-i32"))?; + /// # .open(dir.path())?; + /// type BEI32= U32; /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("get-i32"))?; + /// /// # db.clear(&mut wtxn)?; /// db.put(&mut wtxn, "i-am-forty-two", &42)?; /// db.put(&mut wtxn, "i-am-twenty-seven", &27)?; @@ -157,16 +160,12 @@ impl Database { /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn get<'a, 'txn, T>( - &self, - txn: &'txn RoTxn, - key: &'a KC::EItem, - ) -> Result> + pub fn get<'a, 'txn>(&self, txn: &'txn RoTxn, key: &'a KC::EItem) -> Result> where KC: BytesEncode<'a>, DC: BytesDecode<'txn>, { - self.dyndb.get::(txn, key) + self.dyndb.get::(txn, key) } /// Retrieves the key/value pair lower than the given one in this database. @@ -182,46 +181,46 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::U32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEU32 = U32; /// - /// let db = env.create_database::, Unit>(Some("get-lt-u32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_database::(&mut wtxn, Some("get-lt-u32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, &BEU32::new(27), &())?; - /// db.put(&mut wtxn, &BEU32::new(42), &())?; - /// db.put(&mut wtxn, &BEU32::new(43), &())?; + /// db.put(&mut wtxn, &27, &())?; + /// db.put(&mut wtxn, &42, &())?; + /// db.put(&mut wtxn, &43, &())?; /// - /// let ret = db.get_lower_than(&wtxn, &BEU32::new(4404))?; - /// assert_eq!(ret, Some((BEU32::new(43), ()))); + /// let ret = db.get_lower_than(&wtxn, &4404)?; + /// assert_eq!(ret, Some((43, ()))); /// - /// let ret = db.get_lower_than(&wtxn, &BEU32::new(43))?; - /// assert_eq!(ret, Some((BEU32::new(42), ()))); + /// let ret = db.get_lower_than(&wtxn, &43)?; + /// assert_eq!(ret, Some((42, ()))); /// - /// let ret = db.get_lower_than(&wtxn, &BEU32::new(27))?; + /// let ret = db.get_lower_than(&wtxn, &27)?; /// assert_eq!(ret, None); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn get_lower_than<'a, 'txn, T>( + pub fn get_lower_than<'a, 'txn>( &self, - txn: &'txn RoTxn, + txn: &'txn RoTxn, key: &'a KC::EItem, ) -> Result> where KC: BytesEncode<'a> + BytesDecode<'txn>, DC: BytesDecode<'txn>, { - self.dyndb.get_lower_than::(txn, key) + self.dyndb.get_lower_than::(txn, key) } /// Retrieves the key/value pair lower than or equal to the given one in this database. @@ -237,46 +236,46 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::U32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEU32 = U32; /// - /// let db = env.create_database::, Unit>(Some("get-lt-u32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_database::(&mut wtxn, Some("get-lt-u32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, &BEU32::new(27), &())?; - /// db.put(&mut wtxn, &BEU32::new(42), &())?; - /// db.put(&mut wtxn, &BEU32::new(43), &())?; + /// db.put(&mut wtxn, &27, &())?; + /// db.put(&mut wtxn, &42, &())?; + /// db.put(&mut wtxn, &43, &())?; /// - /// let ret = db.get_lower_than_or_equal_to(&wtxn, &BEU32::new(4404))?; - /// assert_eq!(ret, Some((BEU32::new(43), ()))); + /// let ret = db.get_lower_than_or_equal_to(&wtxn, &4404)?; + /// assert_eq!(ret, Some((43, ()))); /// - /// let ret = db.get_lower_than_or_equal_to(&wtxn, &BEU32::new(43))?; - /// assert_eq!(ret, Some((BEU32::new(43), ()))); + /// let ret = db.get_lower_than_or_equal_to(&wtxn, &43)?; + /// assert_eq!(ret, Some((43, ()))); /// - /// let ret = db.get_lower_than_or_equal_to(&wtxn, &BEU32::new(26))?; + /// let ret = db.get_lower_than_or_equal_to(&wtxn, &26)?; /// assert_eq!(ret, None); /// /// 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, + txn: &'txn RoTxn, key: &'a KC::EItem, ) -> Result> where KC: BytesEncode<'a> + BytesDecode<'txn>, DC: BytesDecode<'txn>, { - self.dyndb.get_lower_than_or_equal_to::(txn, key) + self.dyndb.get_lower_than_or_equal_to::(txn, key) } /// Retrieves the key/value pair greater than the given one in this database. @@ -292,46 +291,46 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::U32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEU32 = U32; /// - /// let db = env.create_database::, Unit>(Some("get-lt-u32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_database::(&mut wtxn, Some("get-lt-u32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, &BEU32::new(27), &())?; - /// db.put(&mut wtxn, &BEU32::new(42), &())?; - /// db.put(&mut wtxn, &BEU32::new(43), &())?; + /// db.put(&mut wtxn, &27, &())?; + /// db.put(&mut wtxn, &42, &())?; + /// db.put(&mut wtxn, &43, &())?; /// - /// let ret = db.get_greater_than(&wtxn, &BEU32::new(0))?; - /// assert_eq!(ret, Some((BEU32::new(27), ()))); + /// let ret = db.get_greater_than(&wtxn, &0)?; + /// assert_eq!(ret, Some((27, ()))); /// - /// let ret = db.get_greater_than(&wtxn, &BEU32::new(42))?; - /// assert_eq!(ret, Some((BEU32::new(43), ()))); + /// let ret = db.get_greater_than(&wtxn, &42)?; + /// assert_eq!(ret, Some((43, ()))); /// - /// let ret = db.get_greater_than(&wtxn, &BEU32::new(43))?; + /// let ret = db.get_greater_than(&wtxn, &43)?; /// assert_eq!(ret, None); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn get_greater_than<'a, 'txn, T>( + pub fn get_greater_than<'a, 'txn>( &self, - txn: &'txn RoTxn, + txn: &'txn RoTxn, key: &'a KC::EItem, ) -> Result> where KC: BytesEncode<'a> + BytesDecode<'txn>, DC: BytesDecode<'txn>, { - self.dyndb.get_greater_than::(txn, key) + self.dyndb.get_greater_than::(txn, key) } /// Retrieves the key/value pair greater than or equal to the given one in this database. @@ -347,46 +346,46 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::U32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEU32 = U32; /// - /// let db = env.create_database::, Unit>(Some("get-lt-u32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db = env.create_database::(&mut wtxn, Some("get-lt-u32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, &BEU32::new(27), &())?; - /// db.put(&mut wtxn, &BEU32::new(42), &())?; - /// db.put(&mut wtxn, &BEU32::new(43), &())?; + /// db.put(&mut wtxn, &27, &())?; + /// db.put(&mut wtxn, &42, &())?; + /// db.put(&mut wtxn, &43, &())?; /// - /// let ret = db.get_greater_than_or_equal_to(&wtxn, &BEU32::new(0))?; - /// assert_eq!(ret, Some((BEU32::new(27), ()))); + /// let ret = db.get_greater_than_or_equal_to(&wtxn, &0)?; + /// assert_eq!(ret, Some((27, ()))); /// - /// let ret = db.get_greater_than_or_equal_to(&wtxn, &BEU32::new(42))?; - /// assert_eq!(ret, Some((BEU32::new(42), ()))); + /// let ret = db.get_greater_than_or_equal_to(&wtxn, &42)?; + /// assert_eq!(ret, Some((42, ()))); /// - /// let ret = db.get_greater_than_or_equal_to(&wtxn, &BEU32::new(44))?; + /// let ret = db.get_greater_than_or_equal_to(&wtxn, &44)?; /// assert_eq!(ret, None); /// /// 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, + txn: &'txn RoTxn, key: &'a KC::EItem, ) -> Result> where KC: BytesEncode<'a> + BytesDecode<'txn>, DC: BytesDecode<'txn>, { - self.dyndb.get_greater_than_or_equal_to::(txn, key) + self.dyndb.get_greater_than_or_equal_to::(txn, key) } /// Retrieves the first key/value pair of this database. @@ -401,35 +400,35 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database, Str> = env.create_database(Some("first-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("first-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; + /// db.put(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put(&mut wtxn, &27, "i-am-twenty-seven")?; /// /// let ret = db.first(&wtxn)?; - /// assert_eq!(ret, Some((BEI32::new(27), "i-am-twenty-seven"))); + /// assert_eq!(ret, Some((27, "i-am-twenty-seven"))); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn first<'txn, T>(&self, txn: &'txn RoTxn) -> Result> + pub fn first<'txn>(&self, txn: &'txn RoTxn) -> Result> where KC: BytesDecode<'txn>, DC: BytesDecode<'txn>, { - self.dyndb.first::(txn) + self.dyndb.first::(txn) } /// Retrieves the last key/value pair of this database. @@ -444,35 +443,35 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database, Str> = env.create_database(Some("last-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("last-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; + /// db.put(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put(&mut wtxn, &27, "i-am-twenty-seven")?; /// /// let ret = db.last(&wtxn)?; - /// assert_eq!(ret, Some((BEI32::new(42), "i-am-forty-two"))); + /// assert_eq!(ret, Some((42, "i-am-forty-two"))); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn last<'txn, T>(&self, txn: &'txn RoTxn) -> Result> + pub fn last<'txn>(&self, txn: &'txn RoTxn) -> Result> where KC: BytesDecode<'txn>, DC: BytesDecode<'txn>, { - self.dyndb.last::(txn) + self.dyndb.last::(txn) } /// Returns the number of elements in this database. @@ -483,29 +482,29 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database, Str> = env.create_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; + /// db.put(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; /// /// let ret = db.len(&wtxn)?; /// assert_eq!(ret, 4); /// - /// db.delete(&mut wtxn, &BEI32::new(27))?; + /// db.delete(&mut wtxn, &27)?; /// /// let ret = db.len(&wtxn)?; /// assert_eq!(ret, 3); @@ -513,7 +512,7 @@ impl Database { /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn len<'txn, T>(&self, txn: &'txn RoTxn) -> Result { + pub fn len<'txn>(&self, txn: &'txn RoTxn) -> Result { self.dyndb.len(txn) } @@ -525,24 +524,24 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database, Str> = env.create_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; + /// db.put(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; /// /// let ret = db.is_empty(&wtxn)?; /// assert_eq!(ret, false); @@ -555,7 +554,7 @@ impl Database { /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn is_empty<'txn, T>(&self, txn: &'txn RoTxn) -> Result { + pub fn is_empty<'txn>(&self, txn: &'txn RoTxn) -> Result { self.dyndb.is_empty(txn) } @@ -567,36 +566,36 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database, Str> = env.create_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; + /// db.put(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put(&mut wtxn, &13, "i-am-thirteen")?; /// /// let mut iter = db.iter(&wtxn)?; - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(13), "i-am-thirteen"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two"))); + /// assert_eq!(iter.next().transpose()?, Some((13, "i-am-thirteen"))); + /// assert_eq!(iter.next().transpose()?, Some((27, "i-am-twenty-seven"))); + /// assert_eq!(iter.next().transpose()?, Some((42, "i-am-forty-two"))); /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn iter<'txn, T>(&self, txn: &'txn RoTxn) -> Result> { - self.dyndb.iter::(txn) + pub fn iter<'txn>(&self, txn: &'txn RoTxn) -> Result> { + self.dyndb.iter::(txn) } /// Return a mutable lexicographically ordered iterator of all key-value pairs in this database. @@ -607,49 +606,49 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database, Str> = env.create_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; + /// db.put(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put(&mut wtxn, &13, "i-am-thirteen")?; /// /// let mut iter = db.iter_mut(&mut wtxn)?; - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(13), "i-am-thirteen"))); + /// assert_eq!(iter.next().transpose()?, Some((13, "i-am-thirteen"))); /// let ret = unsafe { iter.del_current()? }; /// assert!(ret); /// - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two"))); - /// let ret = unsafe { iter.put_current(&BEI32::new(42), "i-am-the-new-forty-two")? }; + /// assert_eq!(iter.next().transpose()?, Some((27, "i-am-twenty-seven"))); + /// assert_eq!(iter.next().transpose()?, Some((42, "i-am-forty-two"))); + /// let ret = unsafe { iter.put_current(&42, "i-am-the-new-forty-two")? }; /// assert!(ret); /// /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// - /// let ret = db.get(&wtxn, &BEI32::new(13))?; + /// let ret = db.get(&wtxn, &13)?; /// assert_eq!(ret, None); /// - /// let ret = db.get(&wtxn, &BEI32::new(42))?; + /// let ret = db.get(&wtxn, &42)?; /// assert_eq!(ret, Some("i-am-the-new-forty-two")); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn iter_mut<'txn, T>(&self, txn: &'txn mut RwTxn) -> Result> { - self.dyndb.iter_mut::(txn) + pub fn iter_mut<'txn>(&self, txn: &'txn mut RwTxn) -> Result> { + self.dyndb.iter_mut::(txn) } /// Return a reversed lexicographically ordered iterator of all key-value pairs in this database. @@ -660,36 +659,36 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database, Str> = env.create_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; + /// db.put(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put(&mut wtxn, &13, "i-am-thirteen")?; /// /// let mut iter = db.rev_iter(&wtxn)?; - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(13), "i-am-thirteen"))); + /// assert_eq!(iter.next().transpose()?, Some((42, "i-am-forty-two"))); + /// assert_eq!(iter.next().transpose()?, Some((27, "i-am-twenty-seven"))); + /// assert_eq!(iter.next().transpose()?, Some((13, "i-am-thirteen"))); /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn rev_iter<'txn, T>(&self, txn: &'txn RoTxn) -> Result> { - self.dyndb.rev_iter::(txn) + pub fn rev_iter<'txn>(&self, txn: &'txn RoTxn) -> Result> { + self.dyndb.rev_iter::(txn) } /// Return a mutable reversed lexicographically ordered iterator of all key-value\ @@ -701,52 +700,49 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database, Str> = env.create_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; + /// db.put(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put(&mut wtxn, &13, "i-am-thirteen")?; /// /// let mut iter = db.rev_iter_mut(&mut wtxn)?; - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two"))); + /// assert_eq!(iter.next().transpose()?, Some((42, "i-am-forty-two"))); /// let ret = unsafe { iter.del_current()? }; /// assert!(ret); /// - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(13), "i-am-thirteen"))); - /// let ret = unsafe { iter.put_current(&BEI32::new(13), "i-am-the-new-thirteen")? }; + /// assert_eq!(iter.next().transpose()?, Some((27, "i-am-twenty-seven"))); + /// assert_eq!(iter.next().transpose()?, Some((13, "i-am-thirteen"))); + /// let ret = unsafe { iter.put_current(&13, "i-am-the-new-thirteen")? }; /// assert!(ret); /// /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// - /// let ret = db.get(&wtxn, &BEI32::new(42))?; + /// let ret = db.get(&wtxn, &42)?; /// assert_eq!(ret, None); /// - /// let ret = db.get(&wtxn, &BEI32::new(13))?; + /// let ret = db.get(&wtxn, &13)?; /// assert_eq!(ret, Some("i-am-the-new-thirteen")); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn rev_iter_mut<'txn, T>( - &self, - txn: &'txn mut RwTxn, - ) -> Result> { - self.dyndb.rev_iter_mut::(txn) + pub fn rev_iter_mut<'txn>(&self, txn: &'txn mut RwTxn) -> Result> { + self.dyndb.rev_iter_mut::(txn) } /// Return a lexicographically ordered iterator of a range of key-value pairs in this database. @@ -759,45 +755,45 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database, Str> = env.create_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; + /// db.put(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; /// - /// let range = BEI32::new(27)..=BEI32::new(42); + /// let range = 27..=42; /// let mut iter = db.range(&wtxn, &range)?; - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two"))); + /// assert_eq!(iter.next().transpose()?, Some((27, "i-am-twenty-seven"))); + /// assert_eq!(iter.next().transpose()?, Some((42, "i-am-forty-two"))); /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn range<'a, 'txn, T, R>( + pub fn range<'a, 'txn, R>( &self, - txn: &'txn RoTxn, + txn: &'txn RoTxn, range: &'a R, ) -> Result> where KC: BytesEncode<'a>, R: RangeBounds, { - self.dyndb.range::(txn, range) + self.dyndb.range::(txn, range) } /// Return a mutable lexicographically ordered iterator of a range of @@ -811,32 +807,32 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database, Str> = env.create_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; + /// db.put(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; /// - /// let range = BEI32::new(27)..=BEI32::new(42); + /// let range = 27..=42; /// let mut range = db.range_mut(&mut wtxn, &range)?; - /// assert_eq!(range.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven"))); + /// assert_eq!(range.next().transpose()?, Some((27, "i-am-twenty-seven"))); /// let ret = unsafe { range.del_current()? }; /// assert!(ret); - /// assert_eq!(range.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two"))); - /// let ret = unsafe { range.put_current(&BEI32::new(42), "i-am-the-new-forty-two")? }; + /// assert_eq!(range.next().transpose()?, Some((42, "i-am-forty-two"))); + /// let ret = unsafe { range.put_current(&42, "i-am-the-new-forty-two")? }; /// assert!(ret); /// /// assert_eq!(range.next().transpose()?, None); @@ -844,25 +840,25 @@ impl Database { /// /// /// let mut iter = db.iter(&wtxn)?; - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(13), "i-am-thirteen"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(42), "i-am-the-new-forty-two"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(521), "i-am-five-hundred-and-twenty-one"))); + /// assert_eq!(iter.next().transpose()?, Some((13, "i-am-thirteen"))); + /// assert_eq!(iter.next().transpose()?, Some((42, "i-am-the-new-forty-two"))); + /// assert_eq!(iter.next().transpose()?, Some((521, "i-am-five-hundred-and-twenty-one"))); /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn range_mut<'a, 'txn, T, R>( + pub fn range_mut<'a, 'txn, R>( &self, - txn: &'txn mut RwTxn, + txn: &'txn mut RwTxn, range: &'a R, ) -> Result> where KC: BytesEncode<'a>, R: RangeBounds, { - self.dyndb.range_mut::(txn, range) + self.dyndb.range_mut::(txn, range) } /// Return a reversed lexicographically ordered iterator of a range of key-value @@ -876,45 +872,45 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database, Str> = env.create_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; + /// db.put(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; /// - /// let range = BEI32::new(27)..=BEI32::new(43); + /// let range = 27..=43; /// let mut iter = db.rev_range(&wtxn, &range)?; - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven"))); + /// assert_eq!(iter.next().transpose()?, Some((42, "i-am-forty-two"))); + /// assert_eq!(iter.next().transpose()?, Some((27, "i-am-twenty-seven"))); /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn rev_range<'a, 'txn, T, R>( + pub fn rev_range<'a, 'txn, R>( &self, - txn: &'txn RoTxn, + txn: &'txn RoTxn, range: &'a R, ) -> Result> where KC: BytesEncode<'a>, R: RangeBounds, { - self.dyndb.rev_range::(txn, range) + self.dyndb.rev_range::(txn, range) } /// Return a mutable reversed lexicographically ordered iterator of a range of @@ -928,32 +924,32 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database, Str> = env.create_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; + /// db.put(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; /// - /// let range = BEI32::new(27)..=BEI32::new(42); + /// let range = 27..=42; /// let mut range = db.rev_range_mut(&mut wtxn, &range)?; - /// assert_eq!(range.next().transpose()?, Some((BEI32::new(42), "i-am-forty-two"))); + /// assert_eq!(range.next().transpose()?, Some((42, "i-am-forty-two"))); /// let ret = unsafe { range.del_current()? }; /// assert!(ret); - /// assert_eq!(range.next().transpose()?, Some((BEI32::new(27), "i-am-twenty-seven"))); - /// let ret = unsafe { range.put_current(&BEI32::new(27), "i-am-the-new-twenty-seven")? }; + /// assert_eq!(range.next().transpose()?, Some((27, "i-am-twenty-seven"))); + /// let ret = unsafe { range.put_current(&27, "i-am-the-new-twenty-seven")? }; /// assert!(ret); /// /// assert_eq!(range.next().transpose()?, None); @@ -961,25 +957,25 @@ impl Database { /// /// /// let mut iter = db.iter(&wtxn)?; - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(13), "i-am-thirteen"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(27), "i-am-the-new-twenty-seven"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(521), "i-am-five-hundred-and-twenty-one"))); + /// assert_eq!(iter.next().transpose()?, Some((13, "i-am-thirteen"))); + /// assert_eq!(iter.next().transpose()?, Some((27, "i-am-the-new-twenty-seven"))); + /// assert_eq!(iter.next().transpose()?, Some((521, "i-am-five-hundred-and-twenty-one"))); /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn rev_range_mut<'a, 'txn, T, R>( + pub fn rev_range_mut<'a, 'txn, R>( &self, - txn: &'txn mut RwTxn, + txn: &'txn mut RwTxn, range: &'a R, ) -> Result> where KC: BytesEncode<'a>, R: RangeBounds, { - self.dyndb.rev_range_mut::(txn, range) + self.dyndb.rev_range_mut::(txn, range) } /// Return a lexicographically ordered iterator of all key-value pairs @@ -993,45 +989,45 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database> = env.create_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, "i-am-twenty-eight", &BEI32::new(28))?; - /// db.put(&mut wtxn, "i-am-twenty-seven", &BEI32::new(27))?; - /// db.put(&mut wtxn, "i-am-twenty-nine", &BEI32::new(29))?; - /// db.put(&mut wtxn, "i-am-forty-one", &BEI32::new(41))?; - /// db.put(&mut wtxn, "i-am-forty-two", &BEI32::new(42))?; + /// db.put(&mut wtxn, "i-am-twenty-eight", &28)?; + /// db.put(&mut wtxn, "i-am-twenty-seven", &27)?; + /// db.put(&mut wtxn, "i-am-twenty-nine", &29)?; + /// db.put(&mut wtxn, "i-am-forty-one", &41)?; + /// db.put(&mut wtxn, "i-am-forty-two", &42)?; /// /// let mut iter = db.prefix_iter(&mut wtxn, "i-am-twenty")?; - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", BEI32::new(28)))); - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", BEI32::new(29)))); - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", BEI32::new(27)))); + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", 28))); + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", 29))); + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", 27))); /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn prefix_iter<'a, 'txn, T>( + pub fn prefix_iter<'a, 'txn>( &self, - txn: &'txn RoTxn, + txn: &'txn RoTxn, prefix: &'a KC::EItem, ) -> Result> where KC: BytesEncode<'a>, { - self.dyndb.prefix_iter::(txn, prefix) + self.dyndb.prefix_iter::(txn, prefix) } /// Return a mutable lexicographically ordered iterator of all key-value pairs @@ -1045,34 +1041,34 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database> = env.create_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, "i-am-twenty-eight", &BEI32::new(28))?; - /// db.put(&mut wtxn, "i-am-twenty-seven", &BEI32::new(27))?; - /// db.put(&mut wtxn, "i-am-twenty-nine", &BEI32::new(29))?; - /// db.put(&mut wtxn, "i-am-forty-one", &BEI32::new(41))?; - /// db.put(&mut wtxn, "i-am-forty-two", &BEI32::new(42))?; + /// db.put(&mut wtxn, "i-am-twenty-eight", &28)?; + /// db.put(&mut wtxn, "i-am-twenty-seven", &27)?; + /// db.put(&mut wtxn, "i-am-twenty-nine", &29)?; + /// db.put(&mut wtxn, "i-am-forty-one", &41)?; + /// db.put(&mut wtxn, "i-am-forty-two", &42)?; /// /// let mut iter = db.prefix_iter_mut(&mut wtxn, "i-am-twenty")?; - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", BEI32::new(28)))); + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", 28))); /// let ret = unsafe { iter.del_current()? }; /// assert!(ret); /// - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", BEI32::new(29)))); - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", BEI32::new(27)))); - /// let ret = unsafe { iter.put_current("i-am-twenty-seven", &BEI32::new(27000))? }; + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", 29))); + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", 27))); + /// let ret = unsafe { iter.put_current("i-am-twenty-seven", &27000)? }; /// assert!(ret); /// /// assert_eq!(iter.next().transpose()?, None); @@ -1083,20 +1079,20 @@ impl Database { /// assert_eq!(ret, None); /// /// let ret = db.get(&wtxn, "i-am-twenty-seven")?; - /// assert_eq!(ret, Some(BEI32::new(27000))); + /// assert_eq!(ret, Some(27000)); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn prefix_iter_mut<'a, 'txn, T>( + pub fn prefix_iter_mut<'a, 'txn>( &self, - txn: &'txn mut RwTxn, + txn: &'txn mut RwTxn, prefix: &'a KC::EItem, ) -> Result> where KC: BytesEncode<'a>, { - self.dyndb.prefix_iter_mut::(txn, prefix) + self.dyndb.prefix_iter_mut::(txn, prefix) } /// Return a reversed lexicographically ordered iterator of all key-value pairs @@ -1110,45 +1106,45 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database> = env.create_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, "i-am-twenty-eight", &BEI32::new(28))?; - /// db.put(&mut wtxn, "i-am-twenty-seven", &BEI32::new(27))?; - /// db.put(&mut wtxn, "i-am-twenty-nine", &BEI32::new(29))?; - /// db.put(&mut wtxn, "i-am-forty-one", &BEI32::new(41))?; - /// db.put(&mut wtxn, "i-am-forty-two", &BEI32::new(42))?; + /// db.put(&mut wtxn, "i-am-twenty-eight", &28)?; + /// db.put(&mut wtxn, "i-am-twenty-seven", &27)?; + /// db.put(&mut wtxn, "i-am-twenty-nine", &29)?; + /// db.put(&mut wtxn, "i-am-forty-one", &41)?; + /// db.put(&mut wtxn, "i-am-forty-two", &42)?; /// /// let mut iter = db.rev_prefix_iter(&mut wtxn, "i-am-twenty")?; - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", BEI32::new(27)))); - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", BEI32::new(29)))); - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", BEI32::new(28)))); + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", 27))); + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", 29))); + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", 28))); /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn rev_prefix_iter<'a, 'txn, T>( + pub fn rev_prefix_iter<'a, 'txn>( &self, - txn: &'txn RoTxn, + txn: &'txn RoTxn, prefix: &'a KC::EItem, ) -> Result> where KC: BytesEncode<'a>, { - self.dyndb.rev_prefix_iter::(txn, prefix) + self.dyndb.rev_prefix_iter::(txn, prefix) } /// Return a mutable reversed lexicographically ordered iterator of all key-value pairs @@ -1162,34 +1158,34 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database> = env.create_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, "i-am-twenty-eight", &BEI32::new(28))?; - /// db.put(&mut wtxn, "i-am-twenty-seven", &BEI32::new(27))?; - /// db.put(&mut wtxn, "i-am-twenty-nine", &BEI32::new(29))?; - /// db.put(&mut wtxn, "i-am-forty-one", &BEI32::new(41))?; - /// db.put(&mut wtxn, "i-am-forty-two", &BEI32::new(42))?; + /// db.put(&mut wtxn, "i-am-twenty-eight", &28)?; + /// db.put(&mut wtxn, "i-am-twenty-seven", &27)?; + /// db.put(&mut wtxn, "i-am-twenty-nine", &29)?; + /// db.put(&mut wtxn, "i-am-forty-one", &41)?; + /// db.put(&mut wtxn, "i-am-forty-two", &42)?; /// /// let mut iter = db.rev_prefix_iter_mut(&mut wtxn, "i-am-twenty")?; - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", BEI32::new(27)))); + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-seven", 27))); /// let ret = unsafe { iter.del_current()? }; /// assert!(ret); /// - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", BEI32::new(29)))); - /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", BEI32::new(28)))); - /// let ret = unsafe { iter.put_current("i-am-twenty-eight", &BEI32::new(28000))? }; + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-nine", 29))); + /// assert_eq!(iter.next().transpose()?, Some(("i-am-twenty-eight", 28))); + /// let ret = unsafe { iter.put_current("i-am-twenty-eight", &28000)? }; /// assert!(ret); /// /// assert_eq!(iter.next().transpose()?, None); @@ -1200,20 +1196,20 @@ impl Database { /// assert_eq!(ret, None); /// /// let ret = db.get(&wtxn, "i-am-twenty-eight")?; - /// assert_eq!(ret, Some(BEI32::new(28000))); + /// assert_eq!(ret, Some(28000)); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn rev_prefix_iter_mut<'a, 'txn, T>( + pub fn rev_prefix_iter_mut<'a, 'txn>( &self, - txn: &'txn mut RwTxn, + txn: &'txn mut RwTxn, prefix: &'a KC::EItem, ) -> Result> where KC: BytesEncode<'a>, { - self.dyndb.rev_prefix_iter_mut::(txn, prefix) + self.dyndb.rev_prefix_iter_mut::(txn, prefix) } /// Insert a key-value pairs in this database. @@ -1224,42 +1220,85 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database, Str> = env.create_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; + /// db.put(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; /// - /// let ret = db.get(&mut wtxn, &BEI32::new(27))?; + /// let ret = db.get(&mut wtxn, &27)?; /// assert_eq!(ret, Some("i-am-twenty-seven")); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn put<'a, T>( + pub fn put<'a>(&self, txn: &mut RwTxn, key: &'a KC::EItem, data: &'a DC::EItem) -> Result<()> + where + KC: BytesEncode<'a>, + DC: BytesEncode<'a>, + { + self.dyndb.put::(txn, key, data) + } + + /// Insert a key-value pair where the value can directly be written to disk. + /// + /// ``` + /// # use std::fs; + /// # use std::path::Path; + /// # use heed::EnvOpenOptions; + /// use std::io::Write; + /// use heed::Database; + /// use heed::types::*; + /// use heed::byteorder::BigEndian; + /// + /// # fn main() -> Result<(), Box> { + /// # let dir = tempfile::tempdir()?; + /// # let env = EnvOpenOptions::new() + /// # .map_size(10 * 1024 * 1024) // 10MB + /// # .max_dbs(3000) + /// # .open(dir.path())?; + /// type BEI32 = I32; + /// + /// let mut wtxn = env.write_txn()?; + /// let db = env.create_database::(&mut wtxn, Some("number-string"))?; + /// + /// # db.clear(&mut wtxn)?; + /// let value = "I am a long long long value"; + /// db.put_reserved(&mut wtxn, &42, value.len(), |reserved| { + /// reserved.write_all(value.as_bytes()) + /// })?; + /// + /// let ret = db.get(&mut wtxn, &42)?; + /// assert_eq!(ret, Some(value)); + /// + /// wtxn.commit()?; + /// # Ok(()) } + /// ``` + pub fn put_reserved<'a, F>( &self, - txn: &mut RwTxn, + txn: &mut RwTxn, key: &'a KC::EItem, - data: &'a DC::EItem, + data_size: usize, + write_func: F, ) -> Result<()> where KC: BytesEncode<'a>, - DC: BytesEncode<'a>, + F: FnMut(&mut ReservedSpace) -> io::Result<()>, { - self.dyndb.put::(txn, key, data) + self.dyndb.put_reserved::(txn, key, data_size, write_func) } /// Append the given key/data pair to the end of the database. @@ -1273,42 +1312,37 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database, Str> = env.create_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; + /// db.put(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; /// - /// let ret = db.get(&mut wtxn, &BEI32::new(27))?; + /// let ret = db.get(&mut wtxn, &27)?; /// assert_eq!(ret, Some("i-am-twenty-seven")); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn append<'a, T>( - &self, - txn: &mut RwTxn, - key: &'a KC::EItem, - data: &'a DC::EItem, - ) -> Result<()> + pub fn append<'a>(&self, txn: &mut RwTxn, key: &'a KC::EItem, data: &'a DC::EItem) -> Result<()> where KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - self.dyndb.append::(txn, key, data) + self.dyndb.append::(txn, key, data) } /// Deletes a key-value pairs in this database. @@ -1321,42 +1355,42 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database, Str> = env.create_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; + /// db.put(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; /// - /// let ret = db.delete(&mut wtxn, &BEI32::new(27))?; + /// let ret = db.delete(&mut wtxn, &27)?; /// assert_eq!(ret, true); /// - /// let ret = db.get(&mut wtxn, &BEI32::new(27))?; + /// let ret = db.get(&mut wtxn, &27)?; /// assert_eq!(ret, None); /// - /// let ret = db.delete(&mut wtxn, &BEI32::new(467))?; + /// let ret = db.delete(&mut wtxn, &467)?; /// assert_eq!(ret, false); /// /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn delete<'a, T>(&self, txn: &mut RwTxn, key: &'a KC::EItem) -> Result + pub fn delete<'a>(&self, txn: &mut RwTxn, key: &'a KC::EItem) -> Result where KC: BytesEncode<'a>, { - self.dyndb.delete::(txn, key) + self.dyndb.delete::(txn, key) } /// Deletes a range of key-value pairs in this database. @@ -1374,49 +1408,45 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database, Str> = env.create_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; + /// db.put(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; /// - /// let range = BEI32::new(27)..=BEI32::new(42); + /// let range = 27..=42; /// let ret = db.delete_range(&mut wtxn, &range)?; /// assert_eq!(ret, 2); /// /// /// let mut iter = db.iter(&wtxn)?; - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(13), "i-am-thirteen"))); - /// assert_eq!(iter.next().transpose()?, Some((BEI32::new(521), "i-am-five-hundred-and-twenty-one"))); + /// assert_eq!(iter.next().transpose()?, Some((13, "i-am-thirteen"))); + /// assert_eq!(iter.next().transpose()?, Some((521, "i-am-five-hundred-and-twenty-one"))); /// assert_eq!(iter.next().transpose()?, None); /// /// drop(iter); /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn delete_range<'a, 'txn, T, R>( - &self, - txn: &'txn mut RwTxn, - range: &'a R, - ) -> Result + pub fn delete_range<'a, 'txn, R>(&self, txn: &'txn mut RwTxn, range: &'a R) -> Result where KC: BytesEncode<'a> + BytesDecode<'txn>, R: RangeBounds, { - self.dyndb.delete_range::(txn, range) + self.dyndb.delete_range::(txn, range) } /// Deletes all key/value pairs in this database. @@ -1432,24 +1462,24 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database, Str> = env.create_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; + /// db.put(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; /// /// db.clear(&mut wtxn)?; /// @@ -1459,7 +1489,7 @@ impl Database { /// wtxn.commit()?; /// # Ok(()) } /// ``` - pub fn clear(&self, txn: &mut RwTxn) -> Result<()> { + pub fn clear(&self, txn: &mut RwTxn) -> Result<()> { self.dyndb.clear(txn) } @@ -1479,26 +1509,26 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::{Database, PolyDatabase}; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database = env.create_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; /// // We remap the types for ease of use. - /// let db = db.remap_types::, Str>(); - /// db.put(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; + /// let db = db.remap_types::(); + /// db.put(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; /// /// wtxn.commit()?; /// # Ok(()) } @@ -1543,27 +1573,27 @@ impl Database { /// # use heed::EnvOpenOptions; /// use heed::Database; /// use heed::types::*; - /// use heed::{zerocopy::I32, byteorder::BigEndian}; + /// use heed::byteorder::BigEndian; /// /// # fn main() -> Result<(), Box> { - /// # fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// # let dir = tempfile::tempdir()?; /// # let env = EnvOpenOptions::new() /// # .map_size(10 * 1024 * 1024) // 10MB /// # .max_dbs(3000) - /// # .open(Path::new("target").join("zerocopy.mdb"))?; + /// # .open(dir.path())?; /// type BEI32 = I32; /// - /// let db: Database, Str> = env.create_database(Some("iter-i32"))?; - /// /// let mut wtxn = env.write_txn()?; + /// let db: Database = env.create_database(&mut wtxn, Some("iter-i32"))?; + /// /// # db.clear(&mut wtxn)?; - /// db.put(&mut wtxn, &BEI32::new(42), "i-am-forty-two")?; - /// db.put(&mut wtxn, &BEI32::new(27), "i-am-twenty-seven")?; - /// db.put(&mut wtxn, &BEI32::new(13), "i-am-thirteen")?; - /// db.put(&mut wtxn, &BEI32::new(521), "i-am-five-hundred-and-twenty-one")?; + /// db.put(&mut wtxn, &42, "i-am-forty-two")?; + /// db.put(&mut wtxn, &27, "i-am-twenty-seven")?; + /// db.put(&mut wtxn, &13, "i-am-thirteen")?; + /// db.put(&mut wtxn, &521, "i-am-five-hundred-and-twenty-one")?; /// /// // Check if a key exists and skip potentially expensive deserializing - /// let ret = db.as_polymorph().get::<_, OwnedType, DecodeIgnore>(&wtxn, &BEI32::new(42))?; + /// let ret = db.as_polymorph().get::(&wtxn, &42)?; /// assert!(ret.is_some()); /// /// wtxn.commit()?; @@ -1581,3 +1611,12 @@ impl Clone for Database { } impl Copy for Database {} + +impl fmt::Debug for Database { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("Database") + .field("key_codec", &any::type_name::()) + .field("data_codec", &any::type_name::()) + .finish() + } +} diff --git a/heed/src/env.rs b/heed/src/env.rs index 878229ac..91aacb1c 100644 --- a/heed/src/env.rs +++ b/heed/src/env.rs @@ -1,23 +1,30 @@ use std::any::TypeId; use std::collections::hash_map::{Entry, HashMap}; -use std::ffi::CString; -#[cfg(windows)] -use std::ffi::OsStr; -use std::fs::File; +use std::ffi::{c_void, CString}; +use std::fs::{File, Metadata}; #[cfg(unix)] -use std::os::unix::ffi::OsStrExt; +use std::os::unix::{ + ffi::OsStrExt, + io::{AsRawFd, BorrowedFd, RawFd}, +}; use std::path::{Path, PathBuf}; use std::sync::{Arc, RwLock}; use std::time::Duration; -use std::{io, ptr, sync}; +#[cfg(windows)] +use std::{ + ffi::OsStr, + os::windows::io::{AsRawHandle, BorrowedHandle, RawHandle}, +}; +use std::{fmt, io, mem, ptr, sync}; use once_cell::sync::Lazy; use synchronoise::event::SignalEvent; -use crate::flags::Flags; use crate::mdb::error::mdb_result; use crate::mdb::ffi; -use crate::{Database, Error, PolyDatabase, Result, 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 @@ -61,18 +68,33 @@ impl OsStrExtLmdb for OsStr { } } +#[cfg(unix)] +fn get_file_fd(file: &File) -> RawFd { + file.as_raw_fd() +} + #[cfg(windows)] -fn get_file_fd(file: &File) -> std::os::windows::io::RawHandle { - use std::os::windows::io::AsRawHandle; +fn get_file_fd(file: &File) -> RawHandle { file.as_raw_handle() } #[cfg(unix)] -fn get_file_fd(file: &File) -> std::os::unix::io::RawFd { - use std::os::unix::io::AsRawFd; - file.as_raw_fd() +/// Get metadata from a file descriptor. +unsafe fn metadata_from_fd(raw_fd: RawFd) -> io::Result { + let fd = BorrowedFd::borrow_raw(raw_fd); + let owned = fd.try_clone_to_owned()?; + File::from(owned).metadata() } +#[cfg(windows)] +/// Get metadata from a file descriptor. +unsafe fn metadata_from_fd(raw_fd: RawHandle) -> io::Result { + let fd = BorrowedHandle::borrow_raw(raw_fd); + let owned = fd.try_clone_to_owned()?; + File::from(owned).metadata() +} + +/// Options and flags which can be used to configure how an environment is opened. #[derive(Clone, Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct EnvOpenOptions { @@ -83,47 +105,51 @@ pub struct EnvOpenOptions { } impl EnvOpenOptions { + /// Creates a blank new set of options ready for configuration. pub fn new() -> EnvOpenOptions { EnvOpenOptions { map_size: None, max_readers: None, max_dbs: None, flags: 0 } } + /// Set the size of the memory map to use for this environment. pub fn map_size(&mut self, size: usize) -> &mut Self { self.map_size = Some(size); self } + /// Set the maximum number of threads/reader slots for the environment. pub fn max_readers(&mut self, readers: u32) -> &mut Self { self.max_readers = Some(readers); self } + /// Set the maximum number of named databases for the environment. pub fn max_dbs(&mut self, dbs: u32) -> &mut Self { self.max_dbs = Some(dbs); self } - /// Set one or more LMDB flags (see http://www.lmdb.tech/doc/group__mdb__env.html). + /// Set one or [more LMDB flags](http://www.lmdb.tech/doc/group__mdb__env.html). /// ``` /// use std::fs; /// use std::path::Path; - /// use heed::{EnvOpenOptions, Database}; + /// use heed::{EnvOpenOptions, Database, Flags}; /// use heed::types::*; - /// use heed::flags::Flags; /// /// # fn main() -> Result<(), Box> { - /// fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; + /// fs::create_dir_all(Path::new("target").join("database.mdb"))?; /// let mut env_builder = EnvOpenOptions::new(); /// unsafe { /// env_builder.flag(Flags::MdbNoTls); /// env_builder.flag(Flags::MdbNoMetaSync); /// } - /// let env = env_builder.open(Path::new("target").join("zerocopy.mdb"))?; + /// let dir = tempfile::tempdir().unwrap(); + /// let env = env_builder.open(dir.path())?; /// /// // we will open the default unamed database - /// let db: Database> = env.create_database(None)?; + /// let mut wtxn = env.write_txn()?; + /// let db: Database> = env.create_database(&mut wtxn, None)?; /// /// // opening a write transaction - /// let mut wtxn = env.write_txn()?; /// db.put(&mut wtxn, "seven", &7)?; /// db.put(&mut wtxn, "zero", &0)?; /// db.put(&mut wtxn, "five", &5)?; @@ -149,6 +175,7 @@ impl EnvOpenOptions { self } + /// Open an environment that will be located at the specified path. pub fn open>(&self, path: P) -> Result { let path = canonicalize_path(path.as_ref())?; @@ -156,10 +183,13 @@ impl EnvOpenOptions { match lock.entry(path) { Entry::Occupied(entry) => { - if &entry.get().options != self { - return Err(Error::BadOpenOptions); + let env = entry.get().env.clone().ok_or(Error::DatabaseClosing)?; + let options = entry.get().options.clone(); + if &options == self { + return Ok(env); + } else { + return Err(Error::BadOpenOptions { env, options }); } - entry.get().env.clone().ok_or(Error::DatabaseClosing) } Entry::Vacant(entry) => { let path = entry.key(); @@ -238,9 +268,17 @@ pub fn env_closing_event>(path: P) -> Option { lock.get(path.as_ref()).map(|e| EnvClosingEvent(e.signal_event.clone())) } +/// An environment handle constructed by using [`EnvOpenOptions`]. #[derive(Clone)] pub struct Env(Arc); +impl fmt::Debug for Env { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let EnvInner { env: _, dbi_open_mutex: _, path } = self.0.as_ref(); + f.debug_struct("Env").field("path", &path.display()).finish_non_exhaustive() + } +} + struct EnvInner { env: *mut ffi::MDB_env, dbi_open_mutex: sync::Mutex>>, @@ -261,16 +299,23 @@ 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(); } } } } +/// Whether to perform compaction while copying an environment. #[derive(Debug, Copy, Clone)] pub enum CompactionOption { + /// Omit free pages and sequentially renumber all pages in output. + /// + /// This option consumes more CPU and runs more slowly than the default. + /// Currently it fails if the environment has suffered a page leak. Enabled, + + /// Copy everything without taking any special action about free pages. Disabled, } @@ -279,109 +324,214 @@ impl Env { self.0.env } - pub fn open_database(&self, name: Option<&str>) -> Result>> - where - KC: 'static, - DC: 'static, - { - let types = (TypeId::of::(), TypeId::of::()); - Ok(self - .raw_open_database(name, Some(types))? - .map(|db| Database::new(self.env_mut_ptr() as _, db))) + /// The size of the data file on disk. + /// + /// # Example + /// + /// ``` + /// use heed::EnvOpenOptions; + /// + /// # fn main() -> Result<(), Box> { + /// let dir = tempfile::tempdir()?; + /// let size_in_bytes = 1024 * 1024; + /// let env = EnvOpenOptions::new().map_size(size_in_bytes).open(dir.path())?; + /// + /// let actual_size = env.real_disk_size()? as usize; + /// assert!(actual_size < size_in_bytes); + /// # Ok(()) } + /// ``` + pub fn real_disk_size(&self) -> Result { + let mut fd = std::mem::MaybeUninit::uninit(); + unsafe { mdb_result(ffi::mdb_env_get_fd(self.env_mut_ptr(), fd.as_mut_ptr()))? }; + let fd = unsafe { fd.assume_init() }; + let metadata = unsafe { metadata_from_fd(fd)? }; + Ok(metadata.len()) } - pub fn open_poly_database(&self, name: Option<&str>) -> Result> { - Ok(self - .raw_open_database(name, None)? - .map(|db| PolyDatabase::new(self.env_mut_ptr() as _, db))) + /// Check if a flag was specified when opening this environment. + pub fn contains_flag(&self, flag: Flags) -> Result { + let flags = self.raw_flags()?; + let set = flags & (flag as u32); + Ok(set != 0) } - fn raw_open_database( - &self, - name: Option<&str>, - types: Option<(TypeId, TypeId)>, - ) -> Result> { - let rtxn = self.read_txn()?; + /// Return the raw flags the environment was opened with. + pub fn raw_flags(&self) -> Result { + let mut flags = std::mem::MaybeUninit::uninit(); + unsafe { mdb_result(ffi::mdb_env_get_flags(self.env_mut_ptr(), flags.as_mut_ptr()))? }; + let flags = unsafe { flags.assume_init() }; - let mut dbi = 0; - let name = name.map(|n| CString::new(n).unwrap()); - let name_ptr = match name { - Some(ref name) => name.as_bytes_with_nul().as_ptr() as *const _, - None => ptr::null(), + Ok(flags) + } + + /// Returns some basic informations about this environment. + pub fn info(&self) -> EnvInfo { + let mut raw_info = mem::MaybeUninit::uninit(); + unsafe { ffi::mdb_env_info(self.0.env, raw_info.as_mut_ptr()) }; + let raw_info = unsafe { raw_info.assume_init() }; + + EnvInfo { + map_addr: raw_info.me_mapaddr, + map_size: raw_info.me_mapsize, + last_page_number: raw_info.me_last_pgno, + last_txn_id: raw_info.me_last_txnid, + maximum_number_of_readers: raw_info.me_maxreaders, + number_of_readers: raw_info.me_numreaders, + } + } + + /// Returns the size used by all the databases in the environment without the free pages. + pub fn non_free_pages_size(&self) -> Result { + let compute_size = |stat: ffi::MDB_stat| { + (stat.ms_leaf_pages + stat.ms_branch_pages + stat.ms_overflow_pages) as u64 + * stat.ms_psize as u64 }; - let mut lock = self.0.dbi_open_mutex.lock().unwrap(); + let mut size = 0; - let result = unsafe { mdb_result(ffi::mdb_dbi_open(rtxn.txn, name_ptr, 0, &mut dbi)) }; + let mut stat = std::mem::MaybeUninit::uninit(); + unsafe { mdb_result(ffi::mdb_env_stat(self.env_mut_ptr(), stat.as_mut_ptr()))? }; + let stat = unsafe { stat.assume_init() }; + size += compute_size(stat); - drop(name); + let rtxn = self.read_txn()?; + let dbi = self.raw_open_dbi(rtxn.txn, None, 0)?; - match result { - Ok(()) => { - rtxn.commit()?; + // we don’t want anyone to open an environment while we’re computing the stats + // thus we take a lock on the dbi + let dbi_open = self.0.dbi_open_mutex.lock().unwrap(); - let old_types = lock.entry(dbi).or_insert(types); + // We’re going to iterate on the unnamed database + let mut cursor = RoCursor::new(&rtxn, dbi)?; - if *old_types == types { - Ok(Some(dbi)) - } else { - Err(Error::InvalidDatabaseTyping) + while let Some((key, _value)) = cursor.move_on_next()? { + if key.contains(&0) { + continue; + } + + let key = String::from_utf8(key.to_vec()).unwrap(); + if let Ok(dbi) = self.raw_open_dbi(rtxn.txn, Some(&key), 0) { + let mut stat = std::mem::MaybeUninit::uninit(); + unsafe { mdb_result(ffi::mdb_stat(rtxn.txn, dbi, stat.as_mut_ptr()))? }; + let stat = unsafe { stat.assume_init() }; + size += compute_size(stat); + + // if the db wasn’t already opened + if !dbi_open.contains_key(&dbi) { + unsafe { ffi::mdb_dbi_close(self.env_mut_ptr(), dbi) } } } - Err(e) if e.not_found() => Ok(None), - Err(e) => Err(e.into()), } + + Ok(size) } - pub fn create_database(&self, name: Option<&str>) -> Result> + /// Opens a typed database that already exists in this environment. + /// + /// If the database was previously opened in this program run, types will be checked. + /// + /// ## Important Information + /// + /// LMDB have an important restriction on the unnamed database when named ones are opened, + /// the names of the named databases are stored as keys in the unnamed one and are immutable, + /// these keys can only be read and not written. + pub fn open_database( + &self, + rtxn: &RoTxn, + name: Option<&str>, + ) -> Result>> where KC: 'static, DC: 'static, { - let mut parent_wtxn = self.write_txn()?; - let db = self.create_database_with_txn(name, &mut parent_wtxn)?; - parent_wtxn.commit()?; - Ok(db) + 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))), + Err(Error::Mdb(e)) if e.not_found() => Ok(None), + Err(e) => Err(e), + } + } + + /// Opens an untyped database that already exists in this environment. + /// + /// If the database was previously opened as a typed one, an error will be returned. + /// + /// ## Important Information + /// + /// LMDB have an important restriction on the unnamed database when named ones are opened, + /// the names of the named databases are stored as keys in the unnamed one and are immutable, + /// these keys can only be read and not written. + pub fn open_poly_database( + &self, + 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), + Err(e) => Err(e), + } } - pub fn create_database_with_txn( + /// Creates a typed database that can already exist in this environment. + /// + /// If the database was previously opened in this program run, types will be checked. + /// + /// ## Important Information + /// + /// LMDB have an important restriction on the unnamed database when named ones are opened, + /// the names of the named databases are stored as keys in the unnamed one and are immutable, + /// these keys can only be read and not written. + pub fn create_database( &self, + wtxn: &mut RwTxn, name: Option<&str>, - parent_wtxn: &mut RwTxn, ) -> Result> where KC: 'static, DC: 'static, { - let types = (TypeId::of::(), TypeId::of::()); - self.raw_create_database(name, Some(types), parent_wtxn) - .map(|db| Database::new(self.env_mut_ptr() as _, db)) - } + assert_eq_env_txn!(self, wtxn); - pub fn create_poly_database(&self, name: Option<&str>) -> Result { - let mut parent_wtxn = self.write_txn()?; - let db = self.create_poly_database_with_txn(name, &mut parent_wtxn)?; - parent_wtxn.commit()?; - Ok(db) + 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)), + Err(e) => Err(e), + } } - pub fn create_poly_database_with_txn( + /// Creates a typed database that can already exist in this environment. + /// + /// If the database was previously opened as a typed one, an error will be returned. + /// + /// ## Important Information + /// + /// LMDB have an important restriction on the unnamed database when named ones are opened, + /// the names of the named databases are stored as keys in the unnamed one and are immutable, + /// these keys can only be read and not written. + pub fn create_poly_database( &self, + wtxn: &mut RwTxn, name: Option<&str>, - parent_wtxn: &mut RwTxn, ) -> Result { - self.raw_create_database(name, None, parent_wtxn) - .map(|db| PolyDatabase::new(self.env_mut_ptr() as _, db)) + 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), + } } - fn raw_create_database( + fn raw_open_dbi( &self, + raw_txn: *mut ffi::MDB_txn, name: Option<&str>, - types: Option<(TypeId, TypeId)>, - parent_wtxn: &mut RwTxn, - ) -> Result { - let wtxn = self.nested_write_txn(parent_wtxn)?; - + flags: u32, + ) -> std::result::Result { let mut dbi = 0; let name = name.map(|n| CString::new(n).unwrap()); let name_ptr = match name { @@ -389,20 +539,26 @@ impl Env { None => ptr::null(), }; - let mut lock = self.0.dbi_open_mutex.lock().unwrap(); - - let result = unsafe { - mdb_result(ffi::mdb_dbi_open(wtxn.txn.txn, name_ptr, ffi::MDB_CREATE, &mut dbi)) - }; + // safety: The name cstring is cloned by LMDB, we can drop it after. + // If a read-only is used with the MDB_CREATE flag, LMDB will throw an error. + unsafe { mdb_result(ffi::mdb_dbi_open(raw_txn, name_ptr, flags, &mut dbi))? }; - drop(name); + Ok(dbi) + } - match result { - Ok(()) => { - wtxn.commit()?; + fn raw_init_database( + &self, + raw_txn: *mut ffi::MDB_txn, + name: Option<&str>, + types: Option<(TypeId, TypeId)>, + create: bool, + ) -> Result { + let mut lock = self.0.dbi_open_mutex.lock().unwrap(); + let flags = if create { ffi::MDB_CREATE } else { 0 }; + match self.raw_open_dbi(raw_txn, name, flags) { + Ok(dbi) => { let old_types = lock.entry(dbi).or_insert(types); - if *old_types == types { Ok(dbi) } else { @@ -413,37 +569,36 @@ impl Env { } } + /// Create a transaction with read and write access for use with the environment. pub fn write_txn(&self) -> Result { RwTxn::new(self) } - pub fn typed_write_txn(&self) -> Result> { - RwTxn::::new(self) - } - - pub fn nested_write_txn<'e, 'p: 'e, T>( - &'e self, - parent: &'p mut RwTxn, - ) -> Result> { + /// Create a nested transaction with read and write access for use with the environment. + /// + /// The new transaction will be a nested transaction, with the transaction indicated by parent + /// as its parent. Transactions may be nested to any level. + /// + /// A parent transaction and its cursors may not issue any other operations than _commit_ and + /// _abort_ while it has active child transactions. + pub fn nested_write_txn<'p>(&'p self, parent: &'p mut RwTxn) -> Result> { RwTxn::nested(self, parent) } + /// Create a transaction with read-only access for use with the environment. pub fn read_txn(&self) -> Result { RoTxn::new(self) } - pub fn typed_read_txn(&self) -> Result> { - RoTxn::new(self) - } - - // TODO rename into `copy_to_file` for more clarity - pub fn copy_to_path>(&self, path: P, option: CompactionOption) -> Result { + /// Copy an LMDB environment to the specified path, with options. + /// + /// This function may be used to make a backup of an existing environment. + /// No lockfile is created, since it gets recreated at need. + pub fn copy_to_file>(&self, path: P, option: CompactionOption) -> Result { let file = File::options().create_new(true).write(true).open(&path)?; let fd = get_file_fd(&file); - unsafe { - self.copy_to_fd(fd, option)?; - } + unsafe { self.copy_to_fd(fd, option)? }; // We reopen the file to make sure the cursor is at the start, // even a seek to start doesn't work properly. @@ -452,21 +607,23 @@ impl Env { Ok(file) } + /// Copy an LMDB environment to the specified file descriptor, with compaction option. + /// + /// This function may be used to make a backup of an existing environment. + /// No lockfile is created, since it gets recreated at need. pub unsafe fn copy_to_fd( &self, fd: ffi::mdb_filehandle_t, option: CompactionOption, ) -> Result<()> { let flags = if let CompactionOption::Enabled = option { ffi::MDB_CP_COMPACT } else { 0 }; - - mdb_result(ffi::mdb_env_copy2fd(self.0.env, fd, flags))?; - + mdb_result(ffi::mdb_env_copyfd2(self.0.env, fd, flags))?; Ok(()) } + /// Flush the data buffers to disk. pub fn force_sync(&self) -> Result<()> { unsafe { mdb_result(ffi::mdb_env_sync(self.0.env, 1))? } - Ok(()) } @@ -482,9 +639,7 @@ impl Env { /// when all references are dropped, the last one will eventually close the environment. pub fn prepare_for_closing(self) -> EnvClosingEvent { let mut lock = OPENED_ENV.write().unwrap(); - let env = lock.get_mut(&self.0.path); - - match env { + match lock.get_mut(self.path()) { None => panic!("cannot find the env that we are trying to close"), Some(EnvEntry { env, signal_event, .. }) => { // We remove the env from the global list and replace it with a None. @@ -500,8 +655,38 @@ impl Env { } } } + + /// Check for stale entries in the reader lock table and clear them. + /// + /// Returns the number of stale readers cleared. + pub fn clear_stale_readers(&self) -> Result { + let mut dead: i32 = 0; + unsafe { mdb_result(ffi::mdb_reader_check(self.0.env, &mut dead))? } + // safety: The reader_check function asks for an i32, initialize it to zero + // and never decrements it. It is safe to use either an u32 or u64 (usize). + Ok(dead as usize) + } +} + +/// Contains information about the environment. +#[derive(Debug, Clone, Copy)] +pub struct EnvInfo { + /// Address of map, if fixed. + pub map_addr: *mut c_void, + /// Size of the data memory map. + pub map_size: usize, + /// ID of the last used page. + pub last_page_number: usize, + /// ID of the last committed transaction. + pub last_txn_id: usize, + /// Maximum number of reader slots in the environment. + pub maximum_number_of_readers: u32, + /// Maximum number of reader slots used in the environment. + pub number_of_readers: u32, } +/// A structure that can be used to wait for the closing event, +/// multiple threads can wait on this event. #[derive(Clone)] pub struct EnvClosingEvent(Arc); @@ -524,24 +709,27 @@ impl EnvClosingEvent { } } +impl fmt::Debug for EnvClosingEvent { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("EnvClosingEvent").finish() + } +} + #[cfg(test)] mod tests { - use std::thread; use std::time::Duration; - - use tempfile::tempdir; + use std::{fs, thread}; use crate::types::*; - use crate::{env_closing_event, EnvOpenOptions}; + use crate::{env_closing_event, EnvOpenOptions, Error}; #[test] fn close_env() { - let dir = tempdir().unwrap(); - let path = dir.path(); + let dir = tempfile::tempdir().unwrap(); let env = EnvOpenOptions::new() .map_size(10 * 1024 * 1024) // 10MB .max_dbs(30) - .open(&path) + .open(&dir.path()) .unwrap(); // Force a thread to keep the env for 1 second. @@ -551,14 +739,15 @@ mod tests { thread::sleep(Duration::from_secs(1)); }); - let db = env.create_database::(None).unwrap(); + let mut wtxn = env.write_txn().unwrap(); + let db = env.create_database::(&mut wtxn, None).unwrap(); + wtxn.commit().unwrap(); // Create an ordered list of keys... let mut wtxn = env.write_txn().unwrap(); db.put(&mut wtxn, "hello", "hello").unwrap(); db.put(&mut wtxn, "world", "world").unwrap(); - // Lets check that we can prefix_iter on that sequence with the key "255". let mut iter = db.iter(&wtxn).unwrap(); assert_eq!(iter.next().transpose().unwrap(), Some(("hello", "hello"))); assert_eq!(iter.next().transpose().unwrap(), Some(("world", "world"))); @@ -574,22 +763,96 @@ mod tests { eprintln!("env closed successfully"); // Make sure we don't have a reference to the env - assert!(env_closing_event(&path).is_none()); + assert!(env_closing_event(&dir.path()).is_none()); } #[test] fn reopen_env_with_different_options_is_err() { - let dir = tempdir().unwrap(); - let path = dir.path(); + let dir = tempfile::tempdir().unwrap(); + let _env = EnvOpenOptions::new() + .map_size(10 * 1024 * 1024) // 10MB + .open(&dir.path()) + .unwrap(); + + let result = EnvOpenOptions::new() + .map_size(12 * 1024 * 1024) // 12MB + .open(&dir.path()); + + assert!(matches!(result, Err(Error::BadOpenOptions { .. }))); + } + + #[test] + fn open_env_with_named_path() { + let dir = tempfile::tempdir().unwrap(); + fs::create_dir_all(dir.path().join("babar.mdb")).unwrap(); let _env = EnvOpenOptions::new() .map_size(10 * 1024 * 1024) // 10MB - .open(&path) + .open(dir.path().join("babar.mdb")) + .unwrap(); + + let _env = EnvOpenOptions::new() + .map_size(10 * 1024 * 1024) // 10MB + .open(dir.path().join("babar.mdb")) + .unwrap(); + } + + #[test] + #[cfg(not(windows))] + fn open_database_with_writemap_flag() { + let dir = tempfile::tempdir().unwrap(); + let mut envbuilder = EnvOpenOptions::new(); + envbuilder.map_size(10 * 1024 * 1024); // 10MB + envbuilder.max_dbs(10); + unsafe { envbuilder.flag(crate::Flags::MdbWriteMap) }; + let env = envbuilder.open(&dir.path()).unwrap(); + + let mut wtxn = env.write_txn().unwrap(); + let _db = env.create_database::(&mut wtxn, Some("my-super-db")).unwrap(); + wtxn.commit().unwrap(); + } + + #[test] + fn create_database_without_commit() { + let dir = tempfile::tempdir().unwrap(); + let env = EnvOpenOptions::new() + .map_size(10 * 1024 * 1024) // 10MB + .max_dbs(10) + .open(&dir.path()) + .unwrap(); + + let mut wtxn = env.write_txn().unwrap(); + let _db = env.create_database::(&mut wtxn, Some("my-super-db")).unwrap(); + wtxn.abort(); + + let rtxn = env.read_txn().unwrap(); + let option = env.open_database::(&rtxn, Some("my-super-db")).unwrap(); + assert!(option.is_none()); + } + + #[test] + fn open_already_existing_database() { + let dir = tempfile::tempdir().unwrap(); + let env = EnvOpenOptions::new() + .map_size(10 * 1024 * 1024) // 10MB + .max_dbs(10) + .open(&dir.path()) .unwrap(); + // we first create a database + let mut wtxn = env.write_txn().unwrap(); + let _db = env.create_database::(&mut wtxn, Some("my-super-db")).unwrap(); + wtxn.commit().unwrap(); + + // Close the environement and reopen it, databases must not be loaded in memory. + env.prepare_for_closing().wait(); let env = EnvOpenOptions::new() - .map_size(12 * 1024 * 1024) // 10MB - .open(&path); + .map_size(10 * 1024 * 1024) // 10MB + .max_dbs(10) + .open(&dir.path()) + .unwrap(); - assert!(env.is_err()); + let rtxn = env.read_txn().unwrap(); + let option = env.open_database::(&rtxn, Some("my-super-db")).unwrap(); + assert!(option.is_some()); } } diff --git a/heed/src/iter/iter.rs b/heed/src/iter/iter.rs index c50db7fa..1e2814ca 100644 --- a/heed/src/iter/iter.rs +++ b/heed/src/iter/iter.rs @@ -3,6 +3,7 @@ use std::marker; use crate::*; +/// A read-only iterator structure. pub struct RoIter<'txn, KC, DC> { cursor: RoCursor<'txn>, move_on_first: bool, @@ -56,8 +57,8 @@ where match result { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), }, Ok(None) => None, Err(e) => Some(Err(e)), @@ -79,8 +80,8 @@ where match result { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), }, Ok(None) => None, Err(e) => Some(Err(e)), @@ -88,6 +89,7 @@ where } } +/// A read-write iterator structure. pub struct RwIter<'txn, KC, DC> { cursor: RwCursor<'txn>, move_on_first: bool, @@ -148,11 +150,38 @@ impl<'txn, KC, DC> RwIter<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + 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)?; self.cursor.put_current(&key_bytes, &data_bytes) } + /// Write a new value to the current entry. + /// + /// The given key **must** be equal to the one this cursor is pointing otherwise the database + /// can be put into an inconsistent state. + /// + /// Returns `true` if the entry was successfully written. + /// + /// > This is intended to be used when the new data is the same size as the old. + /// > Otherwise it will simply perform a delete of the old record followed by an insert. + /// + /// # Safety + /// + /// Please read the safety notes of the [`RwIter::put_current`] method. + pub unsafe fn put_current_reserved<'a, F>( + &mut self, + key: &'a KC::EItem, + data_size: usize, + write_func: F, + ) -> Result + where + KC: BytesEncode<'a>, + F: FnMut(&mut ReservedSpace) -> io::Result<()>, + { + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; + self.cursor.put_current_reserved(&key_bytes, data_size, write_func) + } + /// Append the given key/value pair to the end of the database. /// /// If a key is inserted that is less than any previous key a `KeyExist` error @@ -176,8 +205,8 @@ impl<'txn, KC, DC> RwIter<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + 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)?; self.cursor.append(&key_bytes, &data_bytes) } @@ -223,8 +252,8 @@ where match result { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), }, Ok(None) => None, Err(e) => Some(Err(e)), @@ -246,8 +275,8 @@ where match result { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), }, Ok(None) => None, Err(e) => Some(Err(e)), @@ -255,6 +284,7 @@ where } } +/// A reverse read-only iterator structure. pub struct RoRevIter<'txn, KC, DC> { cursor: RoCursor<'txn>, move_on_last: bool, @@ -308,8 +338,8 @@ where match result { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), }, Ok(None) => None, Err(e) => Some(Err(e)), @@ -331,8 +361,8 @@ where match result { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), }, Ok(None) => None, Err(e) => Some(Err(e)), @@ -340,6 +370,7 @@ where } } +/// A reverse read-write iterator structure. pub struct RwRevIter<'txn, KC, DC> { cursor: RwCursor<'txn>, move_on_last: bool, @@ -400,11 +431,38 @@ impl<'txn, KC, DC> RwRevIter<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + 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)?; self.cursor.put_current(&key_bytes, &data_bytes) } + /// Write a new value to the current entry. + /// + /// The given key **must** be equal to the one this cursor is pointing otherwise the database + /// can be put into an inconsistent state. + /// + /// Returns `true` if the entry was successfully written. + /// + /// > This is intended to be used when the new data is the same size as the old. + /// > Otherwise it will simply perform a delete of the old record followed by an insert. + /// + /// # Safety + /// + /// Please read the safety notes of the [`RwRevIter::put_current`] method. + pub unsafe fn put_current_reserved<'a, F>( + &mut self, + key: &'a KC::EItem, + data_size: usize, + write_func: F, + ) -> Result + where + KC: BytesEncode<'a>, + F: FnMut(&mut ReservedSpace) -> io::Result<()>, + { + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; + self.cursor.put_current_reserved(&key_bytes, data_size, write_func) + } + /// Append the given key/value pair to the end of the database. /// /// If a key is inserted that is less than any previous key a `KeyExist` error @@ -414,8 +472,8 @@ impl<'txn, KC, DC> RwRevIter<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + 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)?; self.cursor.append(&key_bytes, &data_bytes) } @@ -461,8 +519,8 @@ where match result { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), }, Ok(None) => None, Err(e) => Some(Err(e)), @@ -484,8 +542,8 @@ where match result { Ok(Some((key, data))) => match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), }, Ok(None) => None, Err(e) => Some(Err(e)), diff --git a/heed/src/iter/mod.rs b/heed/src/iter/mod.rs index ca6bfac5..4b90fff0 100644 --- a/heed/src/iter/mod.rs +++ b/heed/src/iter/mod.rs @@ -27,19 +27,19 @@ fn retreat_key(bytes: &mut Vec) { mod tests { #[test] fn prefix_iter_with_byte_255() { - use std::fs; - use std::path::Path; - use crate::types::*; use crate::EnvOpenOptions; - fs::create_dir_all(Path::new("target").join("prefix_iter_with_byte_255.mdb")).unwrap(); + let dir = tempfile::tempdir().unwrap(); let env = EnvOpenOptions::new() .map_size(10 * 1024 * 1024) // 10MB .max_dbs(3000) - .open(Path::new("target").join("prefix_iter_with_byte_255.mdb")) + .open(dir.path()) .unwrap(); - let db = env.create_database::(None).unwrap(); + + let mut wtxn = env.write_txn().unwrap(); + let db = env.create_database::(&mut wtxn, None).unwrap(); + wtxn.commit().unwrap(); // Create an ordered list of keys... let mut wtxn = env.write_txn().unwrap(); @@ -61,184 +61,184 @@ mod tests { assert_eq!(iter.next().transpose().unwrap(), None); drop(iter); - wtxn.abort().unwrap(); + wtxn.abort(); } #[test] fn iter_last() { - use std::fs; - use std::path::Path; - use crate::byteorder::BigEndian; use crate::types::*; - use crate::zerocopy::I32; use crate::EnvOpenOptions; - fs::create_dir_all(Path::new("target").join("iter_last.mdb")).unwrap(); + let dir = tempfile::tempdir().unwrap(); let env = EnvOpenOptions::new() .map_size(10 * 1024 * 1024) // 10MB .max_dbs(3000) - .open(Path::new("target").join("iter_last.mdb")) + .open(dir.path()) .unwrap(); - let db = env.create_database::, Unit>(None).unwrap(); + + let mut wtxn = env.write_txn().unwrap(); + let db = env.create_database::(&mut wtxn, None).unwrap(); + wtxn.commit().unwrap(); + type BEI32 = I32; // Create an ordered list of keys... let mut wtxn = env.write_txn().unwrap(); - db.put(&mut wtxn, &BEI32::new(1), &()).unwrap(); - db.put(&mut wtxn, &BEI32::new(2), &()).unwrap(); - db.put(&mut wtxn, &BEI32::new(3), &()).unwrap(); - db.put(&mut wtxn, &BEI32::new(4), &()).unwrap(); + db.put(&mut wtxn, &1, &()).unwrap(); + db.put(&mut wtxn, &2, &()).unwrap(); + db.put(&mut wtxn, &3, &()).unwrap(); + db.put(&mut wtxn, &4, &()).unwrap(); // Lets check that we properly get the last entry. let iter = db.iter(&wtxn).unwrap(); - assert_eq!(iter.last().transpose().unwrap(), Some((BEI32::new(4), ()))); + assert_eq!(iter.last().transpose().unwrap(), Some((4, ()))); let mut iter = db.iter(&wtxn).unwrap(); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(1), ()))); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(2), ()))); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(3), ()))); - assert_eq!(iter.last().transpose().unwrap(), Some((BEI32::new(4), ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((1, ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((2, ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((3, ()))); + assert_eq!(iter.last().transpose().unwrap(), Some((4, ()))); let mut iter = db.iter(&wtxn).unwrap(); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(1), ()))); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(2), ()))); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(3), ()))); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(4), ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((1, ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((2, ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((3, ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((4, ()))); assert_eq!(iter.last().transpose().unwrap(), None); let mut iter = db.iter(&wtxn).unwrap(); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(1), ()))); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(2), ()))); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(3), ()))); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(4), ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((1, ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((2, ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((3, ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((4, ()))); assert_eq!(iter.next().transpose().unwrap(), None); assert_eq!(iter.last().transpose().unwrap(), None); - wtxn.abort().unwrap(); + wtxn.abort(); // Create an ordered list of keys... let mut wtxn = env.write_txn().unwrap(); - db.put(&mut wtxn, &BEI32::new(1), &()).unwrap(); + db.put(&mut wtxn, &1, &()).unwrap(); // Lets check that we properly get the last entry. let iter = db.iter(&wtxn).unwrap(); - assert_eq!(iter.last().transpose().unwrap(), Some((BEI32::new(1), ()))); + assert_eq!(iter.last().transpose().unwrap(), Some((1, ()))); let mut iter = db.iter(&wtxn).unwrap(); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(1), ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((1, ()))); assert_eq!(iter.last().transpose().unwrap(), None); - wtxn.abort().unwrap(); + wtxn.abort(); } #[test] fn range_iter_last() { - use std::fs; - use std::path::Path; - use crate::byteorder::BigEndian; use crate::types::*; - use crate::zerocopy::I32; use crate::EnvOpenOptions; - fs::create_dir_all(Path::new("target").join("iter_last.mdb")).unwrap(); + let dir = tempfile::tempdir().unwrap(); let env = EnvOpenOptions::new() .map_size(10 * 1024 * 1024) // 10MB .max_dbs(3000) - .open(Path::new("target").join("iter_last.mdb")) + .open(dir.path()) .unwrap(); - let db = env.create_database::, Unit>(None).unwrap(); + + let mut wtxn = env.write_txn().unwrap(); + let db = env.create_database::(&mut wtxn, None).unwrap(); + wtxn.commit().unwrap(); + type BEI32 = I32; // Create an ordered list of keys... let mut wtxn = env.write_txn().unwrap(); - db.put(&mut wtxn, &BEI32::new(1), &()).unwrap(); - db.put(&mut wtxn, &BEI32::new(2), &()).unwrap(); - db.put(&mut wtxn, &BEI32::new(3), &()).unwrap(); - db.put(&mut wtxn, &BEI32::new(4), &()).unwrap(); + db.put(&mut wtxn, &1, &()).unwrap(); + db.put(&mut wtxn, &2, &()).unwrap(); + db.put(&mut wtxn, &3, &()).unwrap(); + db.put(&mut wtxn, &4, &()).unwrap(); // Lets check that we properly get the last entry. let iter = db.range(&wtxn, &(..)).unwrap(); - assert_eq!(iter.last().transpose().unwrap(), Some((BEI32::new(4), ()))); + assert_eq!(iter.last().transpose().unwrap(), Some((4, ()))); let mut iter = db.range(&wtxn, &(..)).unwrap(); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(1), ()))); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(2), ()))); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(3), ()))); - assert_eq!(iter.last().transpose().unwrap(), Some((BEI32::new(4), ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((1, ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((2, ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((3, ()))); + assert_eq!(iter.last().transpose().unwrap(), Some((4, ()))); let mut iter = db.range(&wtxn, &(..)).unwrap(); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(1), ()))); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(2), ()))); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(3), ()))); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(4), ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((1, ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((2, ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((3, ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((4, ()))); assert_eq!(iter.last().transpose().unwrap(), None); let mut iter = db.range(&wtxn, &(..)).unwrap(); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(1), ()))); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(2), ()))); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(3), ()))); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(4), ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((1, ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((2, ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((3, ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((4, ()))); assert_eq!(iter.next().transpose().unwrap(), None); assert_eq!(iter.last().transpose().unwrap(), None); - let range = BEI32::new(2)..=BEI32::new(4); + let range = 2..=4; let mut iter = db.range(&wtxn, &range).unwrap(); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(2), ()))); - assert_eq!(iter.last().transpose().unwrap(), Some((BEI32::new(4), ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((2, ()))); + assert_eq!(iter.last().transpose().unwrap(), Some((4, ()))); - let range = BEI32::new(2)..BEI32::new(4); + let range = 2..4; let mut iter = db.range(&wtxn, &range).unwrap(); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(2), ()))); - assert_eq!(iter.last().transpose().unwrap(), Some((BEI32::new(3), ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((2, ()))); + assert_eq!(iter.last().transpose().unwrap(), Some((3, ()))); - let range = BEI32::new(2)..BEI32::new(4); + let range = 2..4; let mut iter = db.range(&wtxn, &range).unwrap(); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(2), ()))); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(3), ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((2, ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((3, ()))); assert_eq!(iter.last().transpose().unwrap(), None); - let range = BEI32::new(2)..BEI32::new(2); + let range = 2..2; let iter = db.range(&wtxn, &range).unwrap(); assert_eq!(iter.last().transpose().unwrap(), None); - let range = BEI32::new(2)..=BEI32::new(1); + let range = 2..=1; let iter = db.range(&wtxn, &range).unwrap(); assert_eq!(iter.last().transpose().unwrap(), None); - wtxn.abort().unwrap(); + wtxn.abort(); // Create an ordered list of keys... let mut wtxn = env.write_txn().unwrap(); - db.put(&mut wtxn, &BEI32::new(1), &()).unwrap(); + db.put(&mut wtxn, &1, &()).unwrap(); // Lets check that we properly get the last entry. let iter = db.range(&wtxn, &(..)).unwrap(); - assert_eq!(iter.last().transpose().unwrap(), Some((BEI32::new(1), ()))); + assert_eq!(iter.last().transpose().unwrap(), Some((1, ()))); let mut iter = db.range(&wtxn, &(..)).unwrap(); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(1), ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((1, ()))); assert_eq!(iter.last().transpose().unwrap(), None); - wtxn.abort().unwrap(); + wtxn.abort(); } #[test] fn prefix_iter_last() { - use std::fs; - use std::path::Path; - use crate::types::*; use crate::EnvOpenOptions; - fs::create_dir_all(Path::new("target").join("prefix_iter_last.mdb")).unwrap(); + let dir = tempfile::tempdir().unwrap(); let env = EnvOpenOptions::new() .map_size(10 * 1024 * 1024) // 10MB .max_dbs(3000) - .open(Path::new("target").join("prefix_iter_last.mdb")) + .open(dir.path()) .unwrap(); - let db = env.create_database::(None).unwrap(); + + let mut wtxn = env.write_txn().unwrap(); + let db = env.create_database::(&mut wtxn, None).unwrap(); + wtxn.commit().unwrap(); // Create an ordered list of keys... let mut wtxn = env.write_txn().unwrap(); @@ -296,24 +296,24 @@ mod tests { ); assert_eq!(iter.last().transpose().unwrap(), None); - wtxn.abort().unwrap(); + wtxn.abort(); } #[test] fn rev_prefix_iter_last() { - use std::fs; - use std::path::Path; - use crate::types::*; use crate::EnvOpenOptions; - fs::create_dir_all(Path::new("target").join("prefix_iter_last.mdb")).unwrap(); + let dir = tempfile::tempdir().unwrap(); let env = EnvOpenOptions::new() .map_size(10 * 1024 * 1024) // 10MB .max_dbs(3000) - .open(Path::new("target").join("prefix_iter_last.mdb")) + .open(dir.path()) .unwrap(); - let db = env.create_database::(None).unwrap(); + + let mut wtxn = env.write_txn().unwrap(); + let db = env.create_database::(&mut wtxn, None).unwrap(); + wtxn.commit().unwrap(); // Create an ordered list of keys... let mut wtxn = env.write_txn().unwrap(); @@ -371,58 +371,58 @@ mod tests { ); assert_eq!(iter.last().transpose().unwrap(), None); - wtxn.abort().unwrap(); + wtxn.abort(); } #[test] fn rev_range_iter_last() { - use std::fs; - use std::path::Path; - use crate::byteorder::BigEndian; use crate::types::*; - use crate::zerocopy::I32; use crate::EnvOpenOptions; - fs::create_dir_all(Path::new("target").join("range_iter_last.mdb")).unwrap(); + let dir = tempfile::tempdir().unwrap(); let env = EnvOpenOptions::new() .map_size(10 * 1024 * 1024) // 10MB .max_dbs(3000) - .open(Path::new("target").join("range_iter_last.mdb")) + .open(dir.path()) .unwrap(); - let db = env.create_database::, Unit>(None).unwrap(); + + let mut wtxn = env.write_txn().unwrap(); + let db = env.create_database::(&mut wtxn, None).unwrap(); + wtxn.commit().unwrap(); + type BEI32 = I32; // Create an ordered list of keys... let mut wtxn = env.write_txn().unwrap(); - db.put(&mut wtxn, &BEI32::new(1), &()).unwrap(); - db.put(&mut wtxn, &BEI32::new(2), &()).unwrap(); - db.put(&mut wtxn, &BEI32::new(3), &()).unwrap(); - db.put(&mut wtxn, &BEI32::new(4), &()).unwrap(); + db.put(&mut wtxn, &1, &()).unwrap(); + db.put(&mut wtxn, &2, &()).unwrap(); + db.put(&mut wtxn, &3, &()).unwrap(); + db.put(&mut wtxn, &4, &()).unwrap(); // Lets check that we properly get the last entry. - let iter = db.rev_range(&wtxn, &(BEI32::new(1)..=BEI32::new(3))).unwrap(); - assert_eq!(iter.last().transpose().unwrap(), Some((BEI32::new(1), ()))); - - let mut iter = db.rev_range(&wtxn, &(BEI32::new(0)..BEI32::new(4))).unwrap(); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(3), ()))); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(2), ()))); - assert_eq!(iter.last().transpose().unwrap(), Some((BEI32::new(1), ()))); - - let mut iter = db.rev_range(&wtxn, &(BEI32::new(0)..=BEI32::new(5))).unwrap(); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(4), ()))); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(3), ()))); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(2), ()))); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(1), ()))); + let iter = db.rev_range(&wtxn, &(1..=3)).unwrap(); + assert_eq!(iter.last().transpose().unwrap(), Some((1, ()))); + + let mut iter = db.rev_range(&wtxn, &(0..4)).unwrap(); + assert_eq!(iter.next().transpose().unwrap(), Some((3, ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((2, ()))); + assert_eq!(iter.last().transpose().unwrap(), Some((1, ()))); + + let mut iter = db.rev_range(&wtxn, &(0..=5)).unwrap(); + assert_eq!(iter.next().transpose().unwrap(), Some((4, ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((3, ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((2, ()))); + assert_eq!(iter.next().transpose().unwrap(), Some((1, ()))); assert_eq!(iter.last().transpose().unwrap(), None); - let iter = db.rev_range(&wtxn, &(BEI32::new(0)..=BEI32::new(5))).unwrap(); - assert_eq!(iter.last().transpose().unwrap(), Some((BEI32::new(1), ()))); + let iter = db.rev_range(&wtxn, &(0..=5)).unwrap(); + assert_eq!(iter.last().transpose().unwrap(), Some((1, ()))); - let mut iter = db.rev_range(&wtxn, &(BEI32::new(4)..=BEI32::new(4))).unwrap(); - assert_eq!(iter.next().transpose().unwrap(), Some((BEI32::new(4), ()))); + let mut iter = db.rev_range(&wtxn, &(4..=4)).unwrap(); + assert_eq!(iter.next().transpose().unwrap(), Some((4, ()))); assert_eq!(iter.last().transpose().unwrap(), None); - wtxn.abort().unwrap(); + wtxn.abort(); } } diff --git a/heed/src/iter/prefix.rs b/heed/src/iter/prefix.rs index f8063ce1..7d36922a 100644 --- a/heed/src/iter/prefix.rs +++ b/heed/src/iter/prefix.rs @@ -15,6 +15,7 @@ fn move_on_prefix_end<'txn>( result } +/// A read-only prefix iterator structure. pub struct RoPrefix<'txn, KC, DC> { cursor: RoCursor<'txn>, prefix: Vec, @@ -72,8 +73,8 @@ where Ok(Some((key, data))) => { if key.starts_with(&self.prefix) { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -101,8 +102,8 @@ where Ok(Some((key, data))) => { if key.starts_with(&self.prefix) { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -114,6 +115,7 @@ where } } +/// A read-write prefix iterator structure. pub struct RwPrefix<'txn, KC, DC> { cursor: RwCursor<'txn>, prefix: Vec, @@ -175,11 +177,38 @@ impl<'txn, KC, DC> RwPrefix<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + 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)?; self.cursor.put_current(&key_bytes, &data_bytes) } + /// Write a new value to the current entry. + /// + /// The given key **must** be equal to the one this cursor is pointing otherwise the database + /// can be put into an inconsistent state. + /// + /// Returns `true` if the entry was successfully written. + /// + /// > This is intended to be used when the new data is the same size as the old. + /// > Otherwise it will simply perform a delete of the old record followed by an insert. + /// + /// # Safety + /// + /// Please read the safety notes of the [`RwPrefix::put_current`] method. + pub unsafe fn put_current_reserved<'a, F>( + &mut self, + key: &'a KC::EItem, + data_size: usize, + write_func: F, + ) -> Result + where + KC: BytesEncode<'a>, + F: FnMut(&mut ReservedSpace) -> io::Result<()>, + { + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; + self.cursor.put_current_reserved(&key_bytes, data_size, write_func) + } + /// Append the given key/value pair to the end of the database. /// /// If a key is inserted that is less than any previous key a `KeyExist` error @@ -203,8 +232,8 @@ impl<'txn, KC, DC> RwPrefix<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + 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)?; self.cursor.append(&key_bytes, &data_bytes) } @@ -253,8 +282,8 @@ where Ok(Some((key, data))) => { if key.starts_with(&self.prefix) { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -282,8 +311,8 @@ where Ok(Some((key, data))) => { if key.starts_with(&self.prefix) { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -295,6 +324,7 @@ where } } +/// A reverse read-only prefix iterator structure. pub struct RoRevPrefix<'txn, KC, DC> { cursor: RoCursor<'txn>, prefix: Vec, @@ -352,8 +382,8 @@ where Ok(Some((key, data))) => { if key.starts_with(&self.prefix) { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -383,8 +413,8 @@ where Ok(Some((key, data))) => { if key.starts_with(&self.prefix) { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -396,6 +426,7 @@ where } } +/// A reverse read-write prefix iterator structure. pub struct RwRevPrefix<'txn, KC, DC> { cursor: RwCursor<'txn>, prefix: Vec, @@ -457,11 +488,38 @@ impl<'txn, KC, DC> RwRevPrefix<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + 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)?; self.cursor.put_current(&key_bytes, &data_bytes) } + /// Write a new value to the current entry. + /// + /// The given key **must** be equal to the one this cursor is pointing otherwise the database + /// can be put into an inconsistent state. + /// + /// Returns `true` if the entry was successfully written. + /// + /// > This is intended to be used when the new data is the same size as the old. + /// > Otherwise it will simply perform a delete of the old record followed by an insert. + /// + /// # Safety + /// + /// Please read the safety notes of the [`RwRevPrefix::put_current`] method. + pub unsafe fn put_current_reserved<'a, F>( + &mut self, + key: &'a KC::EItem, + data_size: usize, + write_func: F, + ) -> Result + where + KC: BytesEncode<'a>, + F: FnMut(&mut ReservedSpace) -> io::Result<()>, + { + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; + self.cursor.put_current_reserved(&key_bytes, data_size, write_func) + } + /// Append the given key/value pair to the end of the database. /// /// If a key is inserted that is less than any previous key a `KeyExist` error @@ -485,8 +543,8 @@ impl<'txn, KC, DC> RwRevPrefix<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + 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)?; self.cursor.append(&key_bytes, &data_bytes) } @@ -535,8 +593,8 @@ where Ok(Some((key, data))) => { if key.starts_with(&self.prefix) { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -566,8 +624,8 @@ where Ok(Some((key, data))) => { if key.starts_with(&self.prefix) { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None diff --git a/heed/src/iter/range.rs b/heed/src/iter/range.rs index ff3c1a9c..8d55783f 100644 --- a/heed/src/iter/range.rs +++ b/heed/src/iter/range.rs @@ -38,6 +38,7 @@ fn move_on_range_start<'txn>( } } +/// A read-only range iterator structure. pub struct RoRange<'txn, KC, DC> { cursor: RoCursor<'txn>, move_on_start: bool, @@ -113,8 +114,8 @@ where if must_be_returned { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -148,8 +149,8 @@ where if must_be_returned { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -161,6 +162,7 @@ where } } +/// A read-write range iterator structure. pub struct RwRange<'txn, KC, DC> { cursor: RwCursor<'txn>, move_on_start: bool, @@ -233,11 +235,38 @@ impl<'txn, KC, DC> RwRange<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + 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)?; self.cursor.put_current(&key_bytes, &data_bytes) } + /// Write a new value to the current entry. + /// + /// The given key **must** be equal to the one this cursor is pointing otherwise the database + /// can be put into an inconsistent state. + /// + /// Returns `true` if the entry was successfully written. + /// + /// > This is intended to be used when the new data is the same size as the old. + /// > Otherwise it will simply perform a delete of the old record followed by an insert. + /// + /// # Safety + /// + /// Please read the safety notes of the [`RwRange::put_current`] method. + pub unsafe fn put_current_reserved<'a, F>( + &mut self, + key: &'a KC::EItem, + data_size: usize, + write_func: F, + ) -> Result + where + KC: BytesEncode<'a>, + F: FnMut(&mut ReservedSpace) -> io::Result<()>, + { + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; + self.cursor.put_current_reserved(&key_bytes, data_size, write_func) + } + /// Append the given key/value pair to the end of the database. /// /// If a key is inserted that is less than any previous key a `KeyExist` error @@ -261,8 +290,8 @@ impl<'txn, KC, DC> RwRange<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + 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)?; self.cursor.append(&key_bytes, &data_bytes) } @@ -318,8 +347,8 @@ where if must_be_returned { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -353,8 +382,8 @@ where if must_be_returned { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -366,6 +395,7 @@ where } } +/// A reverse read-only range iterator structure. pub struct RoRevRange<'txn, KC, DC> { cursor: RoCursor<'txn>, move_on_end: bool, @@ -441,8 +471,8 @@ where if must_be_returned { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -478,8 +508,8 @@ where if must_be_returned { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -491,6 +521,7 @@ where } } +/// A reverse read-write range iterator structure. pub struct RwRevRange<'txn, KC, DC> { cursor: RwCursor<'txn>, move_on_end: bool, @@ -563,11 +594,38 @@ impl<'txn, KC, DC> RwRevRange<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + 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)?; self.cursor.put_current(&key_bytes, &data_bytes) } + /// Write a new value to the current entry. + /// + /// The given key **must** be equal to the one this cursor is pointing otherwise the database + /// can be put into an inconsistent state. + /// + /// Returns `true` if the entry was successfully written. + /// + /// > This is intended to be used when the new data is the same size as the old. + /// > Otherwise it will simply perform a delete of the old record followed by an insert. + /// + /// # Safety + /// + /// Please read the safety notes of the [`RwRevRange::put_current`] method. + pub unsafe fn put_current_reserved<'a, F>( + &mut self, + key: &'a KC::EItem, + data_size: usize, + write_func: F, + ) -> Result + where + KC: BytesEncode<'a>, + F: FnMut(&mut ReservedSpace) -> io::Result<()>, + { + let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).map_err(Error::Encoding)?; + self.cursor.put_current_reserved(&key_bytes, data_size, write_func) + } + /// Append the given key/value pair to the end of the database. /// /// If a key is inserted that is less than any previous key a `KeyExist` error @@ -591,8 +649,8 @@ impl<'txn, KC, DC> RwRevRange<'txn, KC, DC> { KC: BytesEncode<'a>, DC: BytesEncode<'a>, { - let key_bytes: Cow<[u8]> = KC::bytes_encode(&key).ok_or(Error::Encoding)?; - let data_bytes: Cow<[u8]> = DC::bytes_encode(&data).ok_or(Error::Encoding)?; + 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)?; self.cursor.append(&key_bytes, &data_bytes) } @@ -648,8 +706,8 @@ where if must_be_returned { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None @@ -685,8 +743,8 @@ where if must_be_returned { match (KC::bytes_decode(key), DC::bytes_decode(data)) { - (Some(key), Some(data)) => Some(Ok((key, data))), - (_, _) => Some(Err(Error::Decoding)), + (Ok(key), Ok(data)) => Some(Ok((key, data))), + (Err(e), _) | (_, Err(e)) => Some(Err(Error::Decoding(e))), } } else { None diff --git a/heed/src/lazy_decode.rs b/heed/src/lazy_decode.rs index b3b4b8e0..3ec3b64e 100644 --- a/heed/src/lazy_decode.rs +++ b/heed/src/lazy_decode.rs @@ -1,6 +1,6 @@ use std::marker; -use crate::{Error, Result}; +use heed_traits::BoxedError; /// Lazily decode the data bytes, it can be used to avoid CPU intensive decoding /// before making sure we really need to decode it (e.g. based on the key). @@ -10,8 +10,8 @@ pub struct LazyDecode(marker::PhantomData); impl<'a, C: 'static> heed_traits::BytesDecode<'a> for LazyDecode { type DItem = Lazy<'a, C>; - fn bytes_decode(bytes: &'a [u8]) -> Option { - Some(Lazy { data: bytes, _phantom: marker::PhantomData }) + fn bytes_decode(bytes: &'a [u8]) -> Result { + Ok(Lazy { data: bytes, _phantom: marker::PhantomData }) } } @@ -23,7 +23,7 @@ pub struct Lazy<'a, C> { } impl<'a, C: heed_traits::BytesDecode<'a>> Lazy<'a, C> { - pub fn decode(&self) -> Result { - C::bytes_decode(self.data).ok_or(Error::Decoding) + pub fn decode(&self) -> Result { + C::bytes_decode(self.data) } } diff --git a/heed/src/lib.rs b/heed/src/lib.rs index 3d23eb3c..2609b641 100644 --- a/heed/src/lib.rs +++ b/heed/src/lib.rs @@ -1,18 +1,18 @@ //! Crate `heed` is a high-level wrapper of [LMDB], high-level doesn't mean heavy (think about Rust). //! //! It provides you a way to store types in LMDB without any limit and with a minimal overhead as possible, -//! relying on the [zerocopy] library to avoid copying bytes when that's unnecessary and the serde library +//! relying on the [bytemuck] library to avoid copying bytes when that's unnecessary and the serde library //! when this is unavoidable. //! //! The Lightning Memory-Mapped Database (LMDB) directly maps files parts into main memory, combined -//! with the zerocopy library allows us to safely zero-copy parse and serialize Rust types into LMDB. +//! with the bytemuck library allows us to safely zero-copy parse and serialize Rust types into LMDB. //! //! [LMDB]: https://en.wikipedia.org/wiki/Lightning_Memory-Mapped_Database //! //! # Examples //! -//! Discern let you open a database, that will support some typed key/data -//! and ensures, at compile time, that you'll write those types and not others. +//! Open a database, that will support some typed key/data and ensures, at compile time, +//! that you'll write those types and not others. //! //! ``` //! use std::fs; @@ -21,14 +21,14 @@ //! use heed::types::*; //! //! # fn main() -> Result<(), Box> { -//! fs::create_dir_all(Path::new("target").join("zerocopy.mdb"))?; -//! let env = EnvOpenOptions::new().open(Path::new("target").join("zerocopy.mdb"))?; +//! let dir = tempfile::tempdir()?; +//! let env = EnvOpenOptions::new().open(dir.path())?; //! //! // we will open the default unamed database -//! let db: Database> = env.create_database(None)?; +//! let mut wtxn = env.write_txn()?; +//! let db: Database> = env.create_database(&mut wtxn, None)?; //! //! // opening a write transaction -//! let mut wtxn = env.write_txn()?; //! db.put(&mut wtxn, "seven", &7)?; //! db.put(&mut wtxn, "zero", &0)?; //! db.put(&mut wtxn, "five", &5)?; @@ -53,12 +53,13 @@ mod env; mod iter; mod lazy_decode; mod mdb; +mod reserved_space; mod txn; use std::{error, fmt, io, result}; use heed_traits as traits; -pub use {byteorder, heed_types as types, zerocopy}; +pub use {bytemuck, byteorder, heed_types as types}; use self::cursor::{RoCursor, RwCursor}; pub use self::db::{Database, PolyDatabase}; @@ -70,8 +71,9 @@ pub use self::iter::{ pub use self::lazy_decode::{Lazy, LazyDecode}; pub use self::mdb::error::Error as MdbError; use self::mdb::ffi::{from_val, into_val}; -pub use self::mdb::flags; -pub use self::traits::{BytesDecode, BytesEncode}; +pub use self::mdb::flags::Flags; +pub use self::reserved_space::ReservedSpace; +pub use self::traits::{BoxedError, BytesDecode, BytesEncode}; pub use self::txn::{RoTxn, RwTxn}; /// An error that encapsulates all possible errors in this crate. @@ -79,11 +81,16 @@ pub use self::txn::{RoTxn, RwTxn}; pub enum Error { Io(io::Error), Mdb(MdbError), - Encoding, - Decoding, + Encoding(BoxedError), + Decoding(BoxedError), InvalidDatabaseTyping, DatabaseClosing, - BadOpenOptions, + BadOpenOptions { + /// The options that were used to originaly open this env. + options: EnvOpenOptions, + /// The env opened with the original options. + env: Env, + }, } impl fmt::Display for Error { @@ -91,15 +98,15 @@ impl fmt::Display for Error { match self { Error::Io(error) => write!(f, "{}", error), Error::Mdb(error) => write!(f, "{}", error), - Error::Encoding => f.write_str("error while encoding"), - Error::Decoding => f.write_str("error while decoding"), + Error::Encoding(error) => write!(f, "error while encoding: {}", error), + Error::Decoding(error) => write!(f, "error while decoding: {}", error), Error::InvalidDatabaseTyping => { f.write_str("database was previously opened with different types") } Error::DatabaseClosing => { f.write_str("database is in a closing phase, you can't open it at the same time") } - Error::BadOpenOptions => { + Error::BadOpenOptions { .. } => { f.write_str("an environment is already opened with different options") } } @@ -123,4 +130,38 @@ impl From for Error { } } +/// Either a success or an [`Error`]. pub type Result = result::Result; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn error_is_send_sync() { + fn give_me_send_sync(_: T) {} + + let error = Error::Encoding(Box::from("There is an issue, you know?")); + give_me_send_sync(error); + } +} + +macro_rules! assert_eq_env_db_txn { + ($database:ident, $txn:ident) => { + assert!( + $database.env_ident == $txn.env_mut_ptr() as usize, + "The database environment doesn't match the transaction's environment" + ); + }; +} + +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}; diff --git a/heed/src/mdb/lmdb_error.rs b/heed/src/mdb/lmdb_error.rs index 9f652447..11674b2f 100644 --- a/heed/src/mdb/lmdb_error.rs +++ b/heed/src/mdb/lmdb_error.rs @@ -4,7 +4,7 @@ use std::os::raw::c_char; use std::{fmt, str}; use libc::c_int; -use lmdb_sys as ffi; +use lmdb_master_sys as ffi; /// An LMDB error kind. #[derive(Debug, Copy, Clone, Eq, PartialEq)] @@ -53,6 +53,8 @@ pub enum Error { BadValSize, /// The specified DBI was changed unexpectedly. BadDbi, + /// Unexpected problem - transaction should abort. + Problem, /// Other error. Other(c_int), } @@ -85,6 +87,7 @@ impl Error { ffi::MDB_BAD_TXN => Error::BadTxn, ffi::MDB_BAD_VALSIZE => Error::BadValSize, ffi::MDB_BAD_DBI => Error::BadDbi, + ffi::MDB_PROBLEM => Error::Problem, other => Error::Other(other), } } @@ -113,6 +116,7 @@ impl Error { Error::BadTxn => ffi::MDB_BAD_TXN, Error::BadValSize => ffi::MDB_BAD_VALSIZE, Error::BadDbi => ffi::MDB_BAD_DBI, + Error::Problem => ffi::MDB_PROBLEM, Error::Other(err_code) => err_code, } } diff --git a/heed/src/mdb/lmdb_ffi.rs b/heed/src/mdb/lmdb_ffi.rs index 8d88d07c..9f093db7 100644 --- a/heed/src/mdb/lmdb_ffi.rs +++ b/heed/src/mdb/lmdb_ffi.rs @@ -1,12 +1,15 @@ +use std::ptr; + pub use ffi::{ mdb_cursor_close, mdb_cursor_del, mdb_cursor_get, mdb_cursor_open, mdb_cursor_put, - mdb_dbi_open, mdb_del, mdb_drop, mdb_env_close, mdb_env_copyfd2 as mdb_env_copy2fd, - mdb_env_create, mdb_env_open, mdb_env_set_mapsize, mdb_env_set_maxdbs, mdb_env_set_maxreaders, - mdb_env_sync, mdb_filehandle_t, mdb_get, mdb_put, mdb_txn_abort, mdb_txn_begin, mdb_txn_commit, - MDB_cursor, MDB_dbi, MDB_env, MDB_txn, MDB_APPEND, MDB_CP_COMPACT, MDB_CREATE, MDB_CURRENT, - MDB_RDONLY, + mdb_dbi_close, mdb_dbi_open, mdb_del, mdb_drop, mdb_env_close, mdb_env_copyfd2, mdb_env_create, + mdb_env_get_fd, mdb_env_get_flags, mdb_env_info, mdb_env_open, mdb_env_set_mapsize, + mdb_env_set_maxdbs, mdb_env_set_maxreaders, mdb_env_stat, mdb_env_sync, mdb_filehandle_t, + mdb_get, mdb_put, mdb_reader_check, mdb_stat, mdb_txn_abort, mdb_txn_begin, mdb_txn_commit, + MDB_cursor, MDB_dbi, MDB_env, MDB_envinfo, MDB_stat, MDB_txn, MDB_val, MDB_APPEND, + MDB_CP_COMPACT, MDB_CREATE, MDB_CURRENT, MDB_RDONLY, MDB_RESERVE, }; -use lmdb_sys as ffi; +use lmdb_master_sys as ffi; pub mod cursor_op { use super::ffi::{self, MDB_cursor_op}; @@ -19,6 +22,10 @@ pub mod cursor_op { pub const MDB_GET_CURRENT: MDB_cursor_op = ffi::MDB_GET_CURRENT; } +pub fn reserve_size_val(size: usize) -> ffi::MDB_val { + ffi::MDB_val { mv_size: size, mv_data: ptr::null_mut() } +} + pub unsafe fn into_val(value: &[u8]) -> ffi::MDB_val { ffi::MDB_val { mv_data: value.as_ptr() as *mut libc::c_void, mv_size: value.len() } } diff --git a/heed/src/mdb/lmdb_flags.rs b/heed/src/mdb/lmdb_flags.rs index dee6fcc1..f11ec935 100644 --- a/heed/src/mdb/lmdb_flags.rs +++ b/heed/src/mdb/lmdb_flags.rs @@ -1,17 +1,18 @@ -// LMDB flags (see http://www.lmdb.tech/doc/group__mdb__env.html for more details). +use lmdb_master_sys as ffi; + +/// LMDB flags (see for more details). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[repr(u32)] pub enum Flags { - MdbFixedmap = lmdb_sys::MDB_FIXEDMAP, - MdbNoSubDir = lmdb_sys::MDB_NOSUBDIR, - MdbNoSync = lmdb_sys::MDB_NOSYNC, - MdbRdOnly = lmdb_sys::MDB_RDONLY, - MdbNoMetaSync = lmdb_sys::MDB_NOMETASYNC, - MdbWriteMap = lmdb_sys::MDB_WRITEMAP, - MdbMapAsync = lmdb_sys::MDB_MAPASYNC, - MdbNoTls = lmdb_sys::MDB_NOTLS, - MdbNoLock = lmdb_sys::MDB_NOLOCK, - MdbNoRdAhead = lmdb_sys::MDB_NORDAHEAD, - MdbNoMemInit = lmdb_sys::MDB_NOMEMINIT, - /// Always free single pages instead of keeping them in a list, for future reuse. - MdbAlwaysFreePages = lmdb_sys::MDB_ALWAYSFREEPAGES, + MdbFixedmap = ffi::MDB_FIXEDMAP, + MdbNoSubDir = ffi::MDB_NOSUBDIR, + MdbNoSync = ffi::MDB_NOSYNC, + MdbRdOnly = ffi::MDB_RDONLY, + MdbNoMetaSync = ffi::MDB_NOMETASYNC, + MdbWriteMap = ffi::MDB_WRITEMAP, + MdbMapAsync = ffi::MDB_MAPASYNC, + MdbNoTls = ffi::MDB_NOTLS, + MdbNoLock = ffi::MDB_NOLOCK, + MdbNoRdAhead = ffi::MDB_NORDAHEAD, + MdbNoMemInit = ffi::MDB_NOMEMINIT, } diff --git a/heed/src/mdb/mdbx_error.rs b/heed/src/mdb/mdbx_error.rs deleted file mode 100644 index e69de29b..00000000 diff --git a/heed/src/mdb/mdbx_ffi.rs b/heed/src/mdb/mdbx_ffi.rs deleted file mode 100644 index e69de29b..00000000 diff --git a/heed/src/reserved_space.rs b/heed/src/reserved_space.rs new file mode 100644 index 00000000..83ad7237 --- /dev/null +++ b/heed/src/reserved_space.rs @@ -0,0 +1,51 @@ +use std::{fmt, io}; + +use crate::mdb::ffi; + +/// A structure that is used to improve the write speed in LMDB. +/// +/// You must write the exact amount of bytes, no less, no more. +pub struct ReservedSpace { + size: usize, + start_ptr: *mut u8, + written: usize, +} + +impl ReservedSpace { + pub(crate) unsafe fn from_val(val: ffi::MDB_val) -> ReservedSpace { + ReservedSpace { size: val.mv_size, start_ptr: val.mv_data as *mut u8, written: 0 } + } + + /// The total number of bytes that this memory buffer is. + pub fn size(&self) -> usize { + self.size + } + + /// The remaining number of bytes that this memory buffer has. + pub fn remaining(&self) -> usize { + self.size - self.written + } +} + +impl io::Write for ReservedSpace { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.remaining() >= buf.len() { + let dest = unsafe { self.start_ptr.add(self.written) }; + unsafe { buf.as_ptr().copy_to_nonoverlapping(dest, buf.len()) }; + self.written += buf.len(); + Ok(buf.len()) + } else { + Err(io::Error::from(io::ErrorKind::WriteZero)) + } + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} + +impl fmt::Debug for ReservedSpace { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("ReservedSpace").finish() + } +} diff --git a/heed/src/txn.rs b/heed/src/txn.rs index b6f9c202..1c4364d2 100644 --- a/heed/src/txn.rs +++ b/heed/src/txn.rs @@ -1,18 +1,18 @@ use std::ops::Deref; -use std::{marker, ptr}; +use std::ptr; use crate::mdb::error::mdb_result; use crate::mdb::ffi; use crate::{Env, Result}; -pub struct RoTxn<'e, T = ()> { +/// A read-only transaction. +pub struct RoTxn<'e> { pub(crate) txn: *mut ffi::MDB_txn, - pub(crate) env: &'e Env, - _phantom: marker::PhantomData, + env: &'e Env, } -impl<'e, T> RoTxn<'e, T> { - pub(crate) fn new(env: &'e Env) -> Result> { +impl<'e> RoTxn<'e> { + pub(crate) fn new(env: &'e Env) -> Result> { let mut txn: *mut ffi::MDB_txn = ptr::null_mut(); unsafe { @@ -24,82 +24,72 @@ impl<'e, T> RoTxn<'e, T> { ))? }; - Ok(RoTxn { txn, env, _phantom: marker::PhantomData }) + Ok(RoTxn { txn, env }) } - pub fn commit(mut self) -> Result<()> { - let result = unsafe { mdb_result(ffi::mdb_txn_commit(self.txn)) }; - self.txn = ptr::null_mut(); - result.map_err(Into::into) - } - - pub fn abort(mut self) -> Result<()> { - let result = abort_txn(self.txn); - self.txn = ptr::null_mut(); - result + pub(crate) fn env_mut_ptr(&self) -> *mut ffi::MDB_env { + self.env.env_mut_ptr() } } -impl Drop for RoTxn<'_, T> { +impl Drop for RoTxn<'_> { fn drop(&mut self) { if !self.txn.is_null() { - let _ = abort_txn(self.txn); + abort_txn(self.txn); } } } #[cfg(feature = "sync-read-txn")] -unsafe impl Sync for RoTxn<'_, T> {} +unsafe impl Sync for RoTxn<'_> {} -fn abort_txn(txn: *mut ffi::MDB_txn) -> Result<()> { +fn abort_txn(txn: *mut ffi::MDB_txn) { // Asserts that the transaction hasn't been already committed. assert!(!txn.is_null()); - Ok(unsafe { ffi::mdb_txn_abort(txn) }) + unsafe { ffi::mdb_txn_abort(txn) } } -pub struct RwTxn<'e, 'p, T = ()> { - pub(crate) txn: RoTxn<'e, T>, - _parent: marker::PhantomData<&'p mut ()>, +/// A read-write transaction. +pub struct RwTxn<'p> { + pub(crate) txn: RoTxn<'p>, } -impl<'e, T> RwTxn<'e, 'e, T> { - pub(crate) fn new(env: &'e Env) -> Result> { +impl<'p> RwTxn<'p> { + pub(crate) fn new(env: &'p Env) -> Result> { let mut txn: *mut ffi::MDB_txn = ptr::null_mut(); unsafe { mdb_result(ffi::mdb_txn_begin(env.env_mut_ptr(), ptr::null_mut(), 0, &mut txn))? }; - Ok(RwTxn { - txn: RoTxn { txn, env, _phantom: marker::PhantomData }, - _parent: marker::PhantomData, - }) + Ok(RwTxn { txn: RoTxn { txn, env } }) } - pub(crate) fn nested<'p: 'e>( - env: &'e Env, - parent: &'p mut RwTxn, - ) -> Result> { + pub(crate) fn nested(env: &'p Env, parent: &'p mut RwTxn) -> Result> { let mut txn: *mut ffi::MDB_txn = ptr::null_mut(); let parent_ptr: *mut ffi::MDB_txn = parent.txn.txn; unsafe { mdb_result(ffi::mdb_txn_begin(env.env_mut_ptr(), parent_ptr, 0, &mut txn))? }; - Ok(RwTxn { - txn: RoTxn { txn, env, _phantom: marker::PhantomData }, - _parent: marker::PhantomData, - }) + Ok(RwTxn { txn: RoTxn { txn, env } }) } - pub fn commit(self) -> Result<()> { - self.txn.commit() + pub(crate) fn env_mut_ptr(&self) -> *mut ffi::MDB_env { + self.txn.env.env_mut_ptr() + } + + pub fn commit(mut self) -> Result<()> { + let result = unsafe { mdb_result(ffi::mdb_txn_commit(self.txn.txn)) }; + self.txn.txn = ptr::null_mut(); + result.map_err(Into::into) } - pub fn abort(self) -> Result<()> { - self.txn.abort() + pub fn abort(mut self) { + abort_txn(self.txn.txn); + self.txn.txn = ptr::null_mut(); } } -impl<'e, 'p, T> Deref for RwTxn<'e, 'p, T> { - type Target = RoTxn<'e, T>; +impl<'p> Deref for RwTxn<'p> { + type Target = RoTxn<'p>; fn deref(&self) -> &Self::Target { &self.txn diff --git a/lmdb-master-sys/.rustfmt.toml b/lmdb-master-sys/.rustfmt.toml new file mode 100644 index 00000000..fc441bba --- /dev/null +++ b/lmdb-master-sys/.rustfmt.toml @@ -0,0 +1,3 @@ +ignore = [ + "src/bindings.rs" +] \ No newline at end of file diff --git a/lmdb-master-sys/Cargo.toml b/lmdb-master-sys/Cargo.toml new file mode 100644 index 00000000..afda7a9e --- /dev/null +++ b/lmdb-master-sys/Cargo.toml @@ -0,0 +1,39 @@ +[package] +name = "lmdb-master-sys" +# NB: When modifying, also modify html_root_url in lib.rs +version = "0.1.0" +authors = [ + "Kerollmops ", + "Dan Burkert ", + "Victor Porof ", +] +license = "Apache-2.0" +description = "Rust bindings for liblmdb on the mdb.master branch." +documentation = "https://docs.rs/lmdb-master-sys" +repository = "https://github.com/meilisearch/heed/tree/main/lmdb-master-sys" +readme = "README.md" +keywords = ["LMDB", "database", "storage-engine", "bindings", "library"] +categories = ["database", "external-ffi-bindings"] +edition = "2021" + +# NB: Use "--features bindgen" to generate bindings. +build = "build.rs" + +[lib] +name = "lmdb_master_sys" + +[dependencies] +libc = "0.2.139" + +[build-dependencies] +bindgen = { version = "0.63.0", default-features = false, optional = true, features = ["runtime"] } +cc = "1.0.78" +doxygen-rs = "0.2.2" +pkg-config = "0.3.26" + +[features] +default = [] +asan = [] +fuzzer = [] +fuzzer-no-link = [] +posix-sem = [] diff --git a/lmdb-master-sys/README.md b/lmdb-master-sys/README.md new file mode 100644 index 00000000..7612705c --- /dev/null +++ b/lmdb-master-sys/README.md @@ -0,0 +1,3 @@ +# lmdb-master-sys + +Rust bindings for liblmdb on the mdb.master branch. diff --git a/lmdb-master-sys/bindgen.rs b/lmdb-master-sys/bindgen.rs new file mode 100644 index 00000000..2f91c765 --- /dev/null +++ b/lmdb-master-sys/bindgen.rs @@ -0,0 +1,77 @@ +use bindgen::callbacks::IntKind; +use bindgen::callbacks::ParseCallbacks; +use std::env; +use std::path::PathBuf; + +#[derive(Debug)] +struct Callbacks; + +impl ParseCallbacks for Callbacks { + fn process_comment(&self, comment: &str) -> Option { + Some(doxygen_rs::transform(comment)) + } + + fn int_macro(&self, name: &str, _value: i64) -> Option { + match name { + "MDB_SUCCESS" + | "MDB_KEYEXIST" + | "MDB_NOTFOUND" + | "MDB_PAGE_NOTFOUND" + | "MDB_CORRUPTED" + | "MDB_PANIC" + | "MDB_VERSION_MISMATCH" + | "MDB_INVALID" + | "MDB_MAP_FULL" + | "MDB_DBS_FULL" + | "MDB_READERS_FULL" + | "MDB_TLS_FULL" + | "MDB_TXN_FULL" + | "MDB_CURSOR_FULL" + | "MDB_PAGE_FULL" + | "MDB_MAP_RESIZED" + | "MDB_INCOMPATIBLE" + | "MDB_BAD_RSLOT" + | "MDB_BAD_TXN" + | "MDB_BAD_VALSIZE" + | "MDB_BAD_DBI" + | "MDB_LAST_ERRCODE" => Some(IntKind::Int), + "MDB_SIZE_MAX" => Some(IntKind::U64), + "MDB_PROBLEM" | "MDB_BAD_CHECKSUM" | "MDB_CRYPTO_FAIL" | "MDB_ENV_ENCRYPTION" => { + Some(IntKind::Int) + } + _ => Some(IntKind::UInt), + } + } +} + +pub fn generate() { + let mut lmdb = PathBuf::from(&env::var("CARGO_MANIFEST_DIR").unwrap()); + lmdb.push("lmdb"); + lmdb.push("libraries"); + lmdb.push("liblmdb"); + + let mut out_path = PathBuf::from(&env::var("CARGO_MANIFEST_DIR").unwrap()); + out_path.push("src"); + + let bindings = bindgen::Builder::default() + .header(lmdb.join("lmdb.h").to_string_lossy()) + .allowlist_var("^(MDB|mdb)_.*") + .allowlist_type("^(MDB|mdb)_.*") + .allowlist_function("^(MDB|mdb)_.*") + .size_t_is_usize(true) + .ctypes_prefix("::libc") + .blocklist_item("mode_t") + .blocklist_item("mdb_mode_t") + .blocklist_item("mdb_filehandle_t") + .blocklist_item("^__.*") + .parse_callbacks(Box::new(Callbacks {})) + .layout_tests(false) + .prepend_enum_name(false) + .rustfmt_bindings(true) + .generate() + .expect("Unable to generate bindings"); + + bindings + .write_to_file(out_path.join("bindings.rs")) + .expect("Couldn't write bindings!"); +} diff --git a/lmdb-master-sys/build.rs b/lmdb-master-sys/build.rs new file mode 100644 index 00000000..2d57321e --- /dev/null +++ b/lmdb-master-sys/build.rs @@ -0,0 +1,59 @@ +extern crate cc; +extern crate pkg_config; + +#[cfg(feature = "bindgen")] +extern crate bindgen; + +#[cfg(feature = "bindgen")] +#[path = "bindgen.rs"] +mod generate; + +use std::env; +use std::path::PathBuf; + +macro_rules! warn { + ($message:expr) => { + println!("cargo:warning={}", $message); + }; +} + +fn main() { + #[cfg(feature = "bindgen")] + generate::generate(); + + let mut lmdb = PathBuf::from(&env::var("CARGO_MANIFEST_DIR").unwrap()); + lmdb.push("lmdb"); + lmdb.push("libraries"); + lmdb.push("liblmdb"); + + if cfg!(feature = "with-fuzzer") && cfg!(feature = "with-fuzzer-no-link") { + warn!("Features `with-fuzzer` and `with-fuzzer-no-link` are mutually exclusive."); + warn!("Building with `-fsanitize=fuzzer`."); + } + + let mut builder = cc::Build::new(); + + builder + .file(lmdb.join("mdb.c")) + .file(lmdb.join("midl.c")) + // https://github.com/mozilla/lmdb/blob/b7df2cac50fb41e8bd16aab4cc5fd167be9e032a/libraries/liblmdb/Makefile#L23 + .flag_if_supported("-Wno-unused-parameter") + .flag_if_supported("-Wbad-function-cast") + .flag_if_supported("-Wuninitialized"); + + if cfg!(feature = "posix-sem") { + builder.define("MDB_USE_POSIX_SEM", None); + } + + if cfg!(feature = "asan") { + builder.flag("-fsanitize=address"); + } + + if cfg!(feature = "fuzzer") { + builder.flag("-fsanitize=fuzzer"); + } else if cfg!(feature = "fuzzer-no-link") { + builder.flag("-fsanitize=fuzzer-no-link"); + } + + builder.compile("liblmdb.a") +} diff --git a/lmdb-master-sys/lmdb b/lmdb-master-sys/lmdb new file mode 160000 index 00000000..3947014a --- /dev/null +++ b/lmdb-master-sys/lmdb @@ -0,0 +1 @@ +Subproject commit 3947014aed7ffe39a79991fa7fb5b234da47ad1a diff --git a/lmdb-master-sys/src/bindings.rs b/lmdb-master-sys/src/bindings.rs new file mode 100644 index 00000000..53c24224 --- /dev/null +++ b/lmdb-master-sys/src/bindings.rs @@ -0,0 +1,488 @@ +/* automatically generated by rust-bindgen 0.63.0 */ + +pub const MDB_FMT_Z: &[u8; 2usize] = b"z\0"; +pub const MDB_SIZE_MAX: u64 = 18446744073709551615; +pub const MDB_VERSION_MAJOR: ::libc::c_uint = 0; +pub const MDB_VERSION_MINOR: ::libc::c_uint = 9; +pub const MDB_VERSION_PATCH: ::libc::c_uint = 70; +pub const MDB_VERSION_DATE: &[u8; 18usize] = b"December 19, 2015\0"; +pub const MDB_FIXEDMAP: ::libc::c_uint = 1; +pub const MDB_NOSUBDIR: ::libc::c_uint = 16384; +pub const MDB_NOSYNC: ::libc::c_uint = 65536; +pub const MDB_RDONLY: ::libc::c_uint = 131072; +pub const MDB_NOMETASYNC: ::libc::c_uint = 262144; +pub const MDB_WRITEMAP: ::libc::c_uint = 524288; +pub const MDB_MAPASYNC: ::libc::c_uint = 1048576; +pub const MDB_NOTLS: ::libc::c_uint = 2097152; +pub const MDB_NOLOCK: ::libc::c_uint = 4194304; +pub const MDB_NORDAHEAD: ::libc::c_uint = 8388608; +pub const MDB_NOMEMINIT: ::libc::c_uint = 16777216; +pub const MDB_PREVSNAPSHOT: ::libc::c_uint = 33554432; +pub const MDB_REVERSEKEY: ::libc::c_uint = 2; +pub const MDB_DUPSORT: ::libc::c_uint = 4; +pub const MDB_INTEGERKEY: ::libc::c_uint = 8; +pub const MDB_DUPFIXED: ::libc::c_uint = 16; +pub const MDB_INTEGERDUP: ::libc::c_uint = 32; +pub const MDB_REVERSEDUP: ::libc::c_uint = 64; +pub const MDB_CREATE: ::libc::c_uint = 262144; +pub const MDB_NOOVERWRITE: ::libc::c_uint = 16; +pub const MDB_NODUPDATA: ::libc::c_uint = 32; +pub const MDB_CURRENT: ::libc::c_uint = 64; +pub const MDB_RESERVE: ::libc::c_uint = 65536; +pub const MDB_APPEND: ::libc::c_uint = 131072; +pub const MDB_APPENDDUP: ::libc::c_uint = 262144; +pub const MDB_MULTIPLE: ::libc::c_uint = 524288; +pub const MDB_CP_COMPACT: ::libc::c_uint = 1; +pub const MDB_SUCCESS: ::libc::c_int = 0; +pub const MDB_KEYEXIST: ::libc::c_int = -30799; +pub const MDB_NOTFOUND: ::libc::c_int = -30798; +pub const MDB_PAGE_NOTFOUND: ::libc::c_int = -30797; +pub const MDB_CORRUPTED: ::libc::c_int = -30796; +pub const MDB_PANIC: ::libc::c_int = -30795; +pub const MDB_VERSION_MISMATCH: ::libc::c_int = -30794; +pub const MDB_INVALID: ::libc::c_int = -30793; +pub const MDB_MAP_FULL: ::libc::c_int = -30792; +pub const MDB_DBS_FULL: ::libc::c_int = -30791; +pub const MDB_READERS_FULL: ::libc::c_int = -30790; +pub const MDB_TLS_FULL: ::libc::c_int = -30789; +pub const MDB_TXN_FULL: ::libc::c_int = -30788; +pub const MDB_CURSOR_FULL: ::libc::c_int = -30787; +pub const MDB_PAGE_FULL: ::libc::c_int = -30786; +pub const MDB_MAP_RESIZED: ::libc::c_int = -30785; +pub const MDB_INCOMPATIBLE: ::libc::c_int = -30784; +pub const MDB_BAD_RSLOT: ::libc::c_int = -30783; +pub const MDB_BAD_TXN: ::libc::c_int = -30782; +pub const MDB_BAD_VALSIZE: ::libc::c_int = -30781; +pub const MDB_BAD_DBI: ::libc::c_int = -30780; +pub const MDB_PROBLEM: ::libc::c_int = -30779; +pub const MDB_LAST_ERRCODE: ::libc::c_int = -30779; +#[doc = "Unsigned type used for mapsize, entry counts and page/transaction IDs.\nIt is normally size_t, hence the name. Defining MDB_VL32 makes it\nuint64_t, but do not try this unless you know what you are doing.\n\n"] +pub type mdb_size_t = usize; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct MDB_env { + _unused: [u8; 0], +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct MDB_txn { + _unused: [u8; 0], +} +#[doc = "A handle for an individual database in the DB environment.\n\n"] +pub type MDB_dbi = ::libc::c_uint; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct MDB_cursor { + _unused: [u8; 0], +} +#[doc = "Generic structure used for passing keys and data in and out\n\nof the database.\nValues returned from the database are valid only until a subsequent\nupdate operation, or the end of the transaction. Do not modify or\nfree them, they commonly point into the database itself.\nKey sizes must be between 1 and #mdb_env_get_maxkeysize() inclusive.\nThe same applies to data sizes in databases with the #MDB_DUPSORT flag.\nOther data items can in theory be from 0 to 0xffffffff bytes long.\n\n"] +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct MDB_val { + #[doc = "size of the data item\n\n"] + pub mv_size: usize, + #[doc = "address of the data item\n\n"] + pub mv_data: *mut ::libc::c_void, +} +#[doc = "A callback function used to compare two keys in a database\n\n"] +pub type MDB_cmp_func = ::std::option::Option< + unsafe extern "C" fn(a: *const MDB_val, b: *const MDB_val) -> ::libc::c_int, +>; +#[doc = "A callback function used to relocate a position-dependent data item\n\nin a fixed-address database.\nThe \\b newptr gives the item's desired address in\nthe memory map, and \\b oldptr gives its previous address. The item's actual\ndata resides at the address in \\b item. This callback is expected to walk\nthrough the fields of the record in \\b item and modify any\nvalues based at the \\b oldptr address to be relative to the \\b newptr address.\n\n# Arguments\n\n* `item` - The item that is to be relocated. [Direction: Out]\n* `oldptr` - The previous address. [Direction: In]\n* `newptr` - The new address to relocate to. [Direction: In]\n* `relctx` - An application-provided context, set by #mdb_set_relctx(). [Direction: In]\n\n# To Do\n\n* This feature is currently unimplemented.\n"] +pub type MDB_rel_func = ::std::option::Option< + unsafe extern "C" fn( + item: *mut MDB_val, + oldptr: *mut ::libc::c_void, + newptr: *mut ::libc::c_void, + relctx: *mut ::libc::c_void, + ), +>; +#[doc = "Position at first key/data item\n\n"] +pub const MDB_FIRST: MDB_cursor_op = 0; +#[doc = "Position at first data item of current key.\nOnly for #MDB_DUPSORT\n\n"] +pub const MDB_FIRST_DUP: MDB_cursor_op = 1; +#[doc = "Position at key/data pair. Only for #MDB_DUPSORT\n\n"] +pub const MDB_GET_BOTH: MDB_cursor_op = 2; +#[doc = "position at key, nearest data. Only for #MDB_DUPSORT\n\n"] +pub const MDB_GET_BOTH_RANGE: MDB_cursor_op = 3; +#[doc = "Return key/data at current cursor position\n\n"] +pub const MDB_GET_CURRENT: MDB_cursor_op = 4; +#[doc = "Return up to a page of duplicate data items\nfrom current cursor position. Move cursor to prepare\nfor #MDB_NEXT_MULTIPLE. Only for #MDB_DUPFIXED\n\n"] +pub const MDB_GET_MULTIPLE: MDB_cursor_op = 5; +#[doc = "Position at last key/data item\n\n"] +pub const MDB_LAST: MDB_cursor_op = 6; +#[doc = "Position at last data item of current key.\nOnly for #MDB_DUPSORT\n\n"] +pub const MDB_LAST_DUP: MDB_cursor_op = 7; +#[doc = "Position at next data item\n\n"] +pub const MDB_NEXT: MDB_cursor_op = 8; +#[doc = "Position at next data item of current key.\nOnly for #MDB_DUPSORT\n\n"] +pub const MDB_NEXT_DUP: MDB_cursor_op = 9; +#[doc = "Return up to a page of duplicate data items\nfrom next cursor position. Move cursor to prepare\nfor #MDB_NEXT_MULTIPLE. Only for #MDB_DUPFIXED\n\n"] +pub const MDB_NEXT_MULTIPLE: MDB_cursor_op = 10; +#[doc = "Position at first data item of next key\n\n"] +pub const MDB_NEXT_NODUP: MDB_cursor_op = 11; +#[doc = "Position at previous data item\n\n"] +pub const MDB_PREV: MDB_cursor_op = 12; +#[doc = "Position at previous data item of current key.\nOnly for #MDB_DUPSORT\n\n"] +pub const MDB_PREV_DUP: MDB_cursor_op = 13; +#[doc = "Position at last data item of previous key\n\n"] +pub const MDB_PREV_NODUP: MDB_cursor_op = 14; +#[doc = "Position at specified key\n\n"] +pub const MDB_SET: MDB_cursor_op = 15; +#[doc = "Position at specified key, return key + data\n\n"] +pub const MDB_SET_KEY: MDB_cursor_op = 16; +#[doc = "Position at first key greater than or equal to specified key.\n\n"] +pub const MDB_SET_RANGE: MDB_cursor_op = 17; +#[doc = "Position at previous page and return up to\na page of duplicate data items. Only for #MDB_DUPFIXED\n\n"] +pub const MDB_PREV_MULTIPLE: MDB_cursor_op = 18; +#[doc = "Cursor Get operations.\n\nThis is the set of all operations for retrieving data\nusing a cursor.\n\n"] +pub type MDB_cursor_op = ::libc::c_uint; +#[doc = "Statistics for a database in the environment\n\n"] +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct MDB_stat { + #[doc = "Size of a database page.\nThis is currently the same for all databases.\n\n"] + pub ms_psize: ::libc::c_uint, + #[doc = "Depth (height) of the B-tree\n\n"] + pub ms_depth: ::libc::c_uint, + #[doc = "Number of internal (non-leaf) pages\n\n"] + pub ms_branch_pages: mdb_size_t, + #[doc = "Number of leaf pages\n\n"] + pub ms_leaf_pages: mdb_size_t, + #[doc = "Number of overflow pages\n\n"] + pub ms_overflow_pages: mdb_size_t, + #[doc = "Number of data items\n\n"] + pub ms_entries: mdb_size_t, +} +#[doc = "Information about the environment\n\n"] +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct MDB_envinfo { + #[doc = "Address of map, if fixed\n\n"] + pub me_mapaddr: *mut ::libc::c_void, + #[doc = "Size of the data memory map\n\n"] + pub me_mapsize: mdb_size_t, + #[doc = "ID of the last used page\n\n"] + pub me_last_pgno: mdb_size_t, + #[doc = "ID of the last committed transaction\n\n"] + pub me_last_txnid: mdb_size_t, + #[doc = "max reader slots in the environment\n\n"] + pub me_maxreaders: ::libc::c_uint, + #[doc = "max reader slots used in the environment\n\n"] + pub me_numreaders: ::libc::c_uint, +} +extern "C" { + #[doc = "Return the LMDB library version information.\n\n# Arguments\n\n* `major` - if non-NULL, the library major version number is copied here [Direction: In, Out]\n* `minor` - if non-NULL, the library minor version number is copied here [Direction: In, Out]\n* `patch` - if non-NULL, the library patch version number is copied here [Direction: In, Out]\n\n# Return values\n* \"version string\" The library version as a string\n\n"] + pub fn mdb_version( + major: *mut ::libc::c_int, + minor: *mut ::libc::c_int, + patch: *mut ::libc::c_int, + ) -> *mut ::libc::c_char; +} +extern "C" { + #[doc = "Return a string describing a given error code.\n\nThis function is a superset of the ANSI C X3.159-1989 (ANSI C) strerror(3)\nfunction. If the error code is greater than or equal to 0, then the string\nreturned by the system function strerror(3) is returned. If the error code\nis less than 0, an error string corresponding to the LMDB library error is\nreturned. See [`errors`] for a list of LMDB-specific error codes.\n\n# Arguments\n\n* `err` - The error code [Direction: In]\n\n# Return values\n* \"error message\" The description of the error\n\n"] + pub fn mdb_strerror(err: ::libc::c_int) -> *mut ::libc::c_char; +} +extern "C" { + #[doc = "Create an LMDB environment handle.\n\nThis function allocates memory for a #MDB_env structure. To release\nthe allocated memory and discard the handle, call #mdb_env_close().\nBefore the handle may be used, it must be opened using #mdb_env_open().\nVarious other options may also need to be set before opening the handle,\ne.g. #mdb_env_set_mapsize(), #mdb_env_set_maxreaders(), #mdb_env_set_maxdbs(),\ndepending on usage requirements.\n\nReturns:\n\n* A non-zero error value on failure and 0 on success.\n\n# Arguments\n\n* `env` - The address where the new handle will be stored [Direction: In, Out]\n\n"] + pub fn mdb_env_create(env: *mut *mut MDB_env) -> ::libc::c_int; +} +extern "C" { + #[doc = "Open an environment handle.\n\nIf this function fails, #mdb_env_close() must be called to discard the #MDB_env handle.\ndirectory must already exist and be writable.\nmust be set to 0 or by bitwise OR'ing together one or more of the\nvalues described here.\nFlags set by mdb_env_set_flags() are also used.\n
    \n
  • #MDB_FIXEDMAP\nuse a fixed address for the mmap region. This flag must be specified\nwhen creating the environment, and is stored persistently in the environment.\nIf successful, the memory map will always reside at the same virtual address\nand pointers used to reference data items in the database will be constant\nacross multiple invocations. This option may not always work, depending on\nhow the operating system has allocated memory to shared libraries and other uses.\nThe feature is highly experimental.\n
  • #MDB_NOSUBDIR\nBy default, LMDB creates its environment in a directory whose\npathname is given in \\b path, and creates its data and lock files\nunder that directory. With this option, \\b path is used as-is for\nthe database main data file. The database lock file is the \\b path\nwith \"-lock\" appended.\n
  • #MDB_RDONLY\nOpen the environment in read-only mode. No write operations will be\nallowed. LMDB will still modify the lock file - except on read-only\nfilesystems, where LMDB does not use locks.\n
  • #MDB_WRITEMAP\nUse a writeable memory map unless MDB_RDONLY is set. This uses\nfewer mallocs but loses protection from application bugs\nlike wild pointer writes and other bad updates into the database.\nThis may be slightly faster for DBs that fit entirely in RAM, but\nis slower for DBs larger than RAM.\nIncompatible with nested transactions.\nDo not mix processes with and without MDB_WRITEMAP on the same\nenvironment. This can defeat durability (#mdb_env_sync etc).\n
  • #MDB_NOMETASYNC\nFlush system buffers to disk only once per transaction, omit the\nmetadata flush. Defer that until the system flushes files to disk,\nor next non-MDB_RDONLY commit or #mdb_env_sync(). This optimization\nmaintains database integrity, but a system crash may undo the last\ncommitted transaction. I.e. it preserves the ACI (atomicity,\nconsistency, isolation) but not D (durability) database property.\nThis flag may be changed at any time using #mdb_env_set_flags().\n
  • #MDB_NOSYNC\nDon't flush system buffers to disk when committing a transaction.\nThis optimization means a system crash can corrupt the database or\nlose the last transactions if buffers are not yet flushed to disk.\nThe risk is governed by how often the system flushes dirty buffers\nto disk and how often #mdb_env_sync() is called. However, if the\nfilesystem preserves write order and the #MDB_WRITEMAP flag is not\nused, transactions exhibit ACI (atomicity, consistency, isolation)\nproperties and only lose D (durability). I.e. database integrity\nis maintained, but a system crash may undo the final transactions.\nNote that (#MDB_NOSYNC | #MDB_WRITEMAP) leaves the system with no\nhint for when to write transactions to disk, unless #mdb_env_sync()\nis called. (#MDB_MAPASYNC | #MDB_WRITEMAP) may be preferable.\nThis flag may be changed at any time using #mdb_env_set_flags().\n
  • #MDB_MAPASYNC\nWhen using #MDB_WRITEMAP, use asynchronous flushes to disk.\nAs with #MDB_NOSYNC, a system crash can then corrupt the\ndatabase or lose the last transactions. Calling #mdb_env_sync()\nensures on-disk database integrity until next commit.\nThis flag may be changed at any time using #mdb_env_set_flags().\n
  • #MDB_NOTLS\nDon't use Thread-Local Storage. Tie reader locktable slots to\n#MDB_txn objects instead of to threads. I.e. #mdb_txn_reset() keeps\nthe slot reserved for the #MDB_txn object. A thread may use parallel\nread-only transactions. A read-only transaction may span threads if\nthe user synchronizes its use. Applications that multiplex many\nuser threads over individual OS threads need this option. Such an\napplication must also serialize the write transactions in an OS\nthread, since LMDB's write locking is unaware of the user threads.\n
  • #MDB_NOLOCK\nDon't do any locking. If concurrent access is anticipated, the\ncaller must manage all concurrency itself. For proper operation\nthe caller must enforce single-writer semantics, and must ensure\nthat no readers are using old transactions while a writer is\nactive. The simplest approach is to use an exclusive lock so that\nno readers may be active at all when a writer begins.\n
  • #MDB_NORDAHEAD\nTurn off readahead. Most operating systems perform readahead on\nread requests by default. This option turns it off if the OS\nsupports it. Turning it off may help random read performance\nwhen the DB is larger than RAM and system RAM is full.\nThe option is not implemented on Windows.\n
  • #MDB_NOMEMINIT\nDon't initialize malloc'd memory before writing to unused spaces\nin the data file. By default, memory for pages written to the data\nfile is obtained using malloc. While these pages may be reused in\nsubsequent transactions, freshly malloc'd pages will be initialized\nto zeroes before use. This avoids persisting leftover data from other\ncode (that used the heap and subsequently freed the memory) into the\ndata file. Note that many other system libraries may allocate\nand free memory from the heap for arbitrary uses. E.g., stdio may\nuse the heap for file I/O buffers. This initialization step has a\nmodest performance cost so some applications may want to disable\nit using this flag. This option can be a problem for applications\nwhich handle sensitive data like passwords, and it makes memory\ncheckers like Valgrind noisy. This flag is not needed with #MDB_WRITEMAP,\nwhich writes directly to the mmap instead of using malloc for pages. The\ninitialization is also skipped if #MDB_RESERVE is used; the\ncaller is expected to overwrite all of the memory that was\nreserved in that case.\nThis flag may be changed at any time using #mdb_env_set_flags().\n
  • #MDB_PREVSNAPSHOT\nOpen the environment with the previous snapshot rather than the latest\none. This loses the latest transaction, but may help work around some\ntypes of corruption. If opened with write access, this must be the\nonly process using the environment. This flag is automatically reset\nafter a write transaction is successfully committed.\n
\nThis parameter is ignored on Windows.\nerrors are:\n
    \n
  • #MDB_VERSION_MISMATCH - the version of the LMDB library doesn't match the\nversion that created the database environment.\n
  • #MDB_INVALID - the environment file headers are corrupted.\n
  • ENOENT - the directory specified by the path parameter doesn't exist.\n
  • EACCES - the user didn't have permission to access the environment files.\n
  • EAGAIN - the environment was locked by another process.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create() [Direction: In]\n* `path` - The directory in which the database files reside. This [Direction: In]\n* `flags` - Special options for this environment. This parameter [Direction: In]\n* `mode` - The UNIX permissions to set on created files and semaphores. [Direction: In]\n\n"] + pub fn mdb_env_open( + env: *mut MDB_env, + path: *const ::libc::c_char, + flags: ::libc::c_uint, + mode: mdb_mode_t, + ) -> ::libc::c_int; +} +extern "C" { + #[doc = "Copy an LMDB environment to the specified path.\n\nThis function may be used to make a backup of an existing environment.\nNo lockfile is created, since it gets recreated at need.\nparallel with write transactions, because it employs a read-only\ntransaction. See long-lived transactions under [`caveats_sec`]\nmust have already been opened successfully.\ndirectory must already exist and be writable but must otherwise be\nempty.\n\nReturns:\n\n* A non-zero error value on failure and 0 on success.\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create(). It [Direction: In]\n* `path` - The directory in which the copy will reside. This [Direction: In]\n\n# Notes\n\n* This call can trigger significant file size growth if run in\n\n"] + pub fn mdb_env_copy(env: *mut MDB_env, path: *const ::libc::c_char) -> ::libc::c_int; +} +extern "C" { + #[doc = "Copy an LMDB environment to the specified file descriptor.\n\nThis function may be used to make a backup of an existing environment.\nNo lockfile is created, since it gets recreated at need.\nparallel with write transactions, because it employs a read-only\ntransaction. See long-lived transactions under [`caveats_sec`]\nmust have already been opened successfully.\nhave already been opened for Write access.\n\nReturns:\n\n* A non-zero error value on failure and 0 on success.\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create(). It [Direction: In]\n* `fd` - The filedescriptor to write the copy to. It must [Direction: In]\n\n# Notes\n\n* This call can trigger significant file size growth if run in\n\n"] + pub fn mdb_env_copyfd(env: *mut MDB_env, fd: mdb_filehandle_t) -> ::libc::c_int; +} +extern "C" { + #[doc = "Copy an LMDB environment to the specified path, with options.\n\nThis function may be used to make a backup of an existing environment.\nNo lockfile is created, since it gets recreated at need.\nparallel with write transactions, because it employs a read-only\ntransaction. See long-lived transactions under [`caveats_sec`]\nmust have already been opened successfully.\ndirectory must already exist and be writable but must otherwise be\nempty.\nmust be set to 0 or by bitwise OR'ing together one or more of the\nvalues described here.\n
    \n
  • #MDB_CP_COMPACT - Perform compaction while copying: omit free\npages and sequentially renumber all pages in output. This option\nconsumes more CPU and runs more slowly than the default.\nCurrently it fails if the environment has suffered a page leak.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success.\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create(). It [Direction: In]\n* `path` - The directory in which the copy will reside. This [Direction: In]\n* `flags` - Special options for this operation. This parameter [Direction: In]\n\n# Notes\n\n* This call can trigger significant file size growth if run in\n\n"] + pub fn mdb_env_copy2( + env: *mut MDB_env, + path: *const ::libc::c_char, + flags: ::libc::c_uint, + ) -> ::libc::c_int; +} +extern "C" { + #[doc = "Copy an LMDB environment to the specified file descriptor,\n\nwith options.\nThis function may be used to make a backup of an existing environment.\nNo lockfile is created, since it gets recreated at need. See\n#mdb_env_copy2() for further details.\nparallel with write transactions, because it employs a read-only\ntransaction. See long-lived transactions under [`caveats_sec`]\nmust have already been opened successfully.\nhave already been opened for Write access.\nSee #mdb_env_copy2() for options.\n\nReturns:\n\n* A non-zero error value on failure and 0 on success.\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create(). It [Direction: In]\n* `fd` - The filedescriptor to write the copy to. It must [Direction: In]\n* `flags` - Special options for this operation. [Direction: In]\n\n# Notes\n\n* This call can trigger significant file size growth if run in\n\n"] + pub fn mdb_env_copyfd2( + env: *mut MDB_env, + fd: mdb_filehandle_t, + flags: ::libc::c_uint, + ) -> ::libc::c_int; +} +extern "C" { + #[doc = "Return statistics about the LMDB environment.\n\nwhere the statistics will be copied\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create() [Direction: In]\n* `stat` - The address of an #MDB_stat structure [Direction: In, Out]\n\n"] + pub fn mdb_env_stat(env: *mut MDB_env, stat: *mut MDB_stat) -> ::libc::c_int; +} +extern "C" { + #[doc = "Return information about the LMDB environment.\n\nwhere the information will be copied\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create() [Direction: In]\n* `stat` - The address of an #MDB_envinfo structure [Direction: In, Out]\n\n"] + pub fn mdb_env_info(env: *mut MDB_env, stat: *mut MDB_envinfo) -> ::libc::c_int; +} +extern "C" { + #[doc = "Flush the data buffers to disk.\n\nData is always written to disk when #mdb_txn_commit() is called,\nbut the operating system may keep it buffered. LMDB always flushes\nthe OS buffers upon commit as well, unless the environment was\nopened with #MDB_NOSYNC or in part #MDB_NOMETASYNC. This call is\nnot valid if the environment was opened with #MDB_RDONLY.\nif the environment has the #MDB_NOSYNC flag set the flushes\nwill be omitted, and with #MDB_MAPASYNC they will be asynchronous.\nerrors are:\n
    \n
  • EACCES - the environment is read-only.\n
  • EINVAL - an invalid parameter was specified.\n
  • EIO - an error occurred during synchronization.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create() [Direction: In]\n* `force` - If non-zero, force a synchronous flush. Otherwise [Direction: In]\n\n"] + pub fn mdb_env_sync(env: *mut MDB_env, force: ::libc::c_int) -> ::libc::c_int; +} +extern "C" { + #[doc = "Close the environment and release the memory map.\n\nOnly a single thread may call this function. All transactions, databases,\nand cursors must already be closed before calling this function. Attempts to\nuse any such handles after calling this function will cause a SIGSEGV.\nThe environment handle will be freed and must not be used again after this call.\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create() [Direction: In]\n\n"] + pub fn mdb_env_close(env: *mut MDB_env); +} +extern "C" { + #[doc = "Set environment flags.\n\nThis may be used to set some flags in addition to those from\n#mdb_env_open(), or to unset these flags. If several threads\nchange the flags at the same time, the result is undefined.\nerrors are:\n
    \n
  • EINVAL - an invalid parameter was specified.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create() [Direction: In]\n* `flags` - The flags to change, bitwise OR'ed together [Direction: In]\n* `onoff` - A non-zero value sets the flags, zero clears them. [Direction: In]\n\n"] + pub fn mdb_env_set_flags( + env: *mut MDB_env, + flags: ::libc::c_uint, + onoff: ::libc::c_int, + ) -> ::libc::c_int; +} +extern "C" { + #[doc = "Get environment flags.\n\nerrors are:\n
    \n
  • EINVAL - an invalid parameter was specified.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create() [Direction: In]\n* `flags` - The address of an integer to store the flags [Direction: In, Out]\n\n"] + pub fn mdb_env_get_flags(env: *mut MDB_env, flags: *mut ::libc::c_uint) -> ::libc::c_int; +} +extern "C" { + #[doc = "Return the path that was used in #mdb_env_open().\n\nis the actual string in the environment, not a copy. It should not be\naltered in any way.\nerrors are:\n
    \n
  • EINVAL - an invalid parameter was specified.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create() [Direction: In]\n* `path` - Address of a string pointer to contain the path. This [Direction: In, Out]\n\n"] + pub fn mdb_env_get_path(env: *mut MDB_env, path: *mut *const ::libc::c_char) -> ::libc::c_int; +} +extern "C" { + #[doc = "Return the filedescriptor for the given environment.\n\nThis function may be called after fork(), so the descriptor can be\nclosed before exec*(). Other LMDB file descriptors have FD_CLOEXEC.\n(Until LMDB 0.9.18, only the lockfile had that.)\nerrors are:\n
    \n
  • EINVAL - an invalid parameter was specified.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create() [Direction: In]\n* `fd` - Address of a mdb_filehandle_t to contain the descriptor. [Direction: In, Out]\n\n"] + pub fn mdb_env_get_fd(env: *mut MDB_env, fd: *mut mdb_filehandle_t) -> ::libc::c_int; +} +extern "C" { + #[doc = "Set the size of the memory map to use for this environment.\n\nThe size should be a multiple of the OS page size. The default is\n10485760 bytes. The size of the memory map is also the maximum size\nof the database. The value should be chosen as large as possible,\nto accommodate future growth of the database.\nThis function should be called after #mdb_env_create() and before #mdb_env_open().\nIt may be called at later times if no transactions are active in\nthis process. Note that the library does not check for this condition,\nthe caller must ensure it explicitly.\nThe new size takes effect immediately for the current process but\nwill not be persisted to any others until a write transaction has been\ncommitted by the current process. Also, only mapsize increases are\npersisted into the environment.\nIf the mapsize is increased by another process, and data has grown\nbeyond the range of the current mapsize, #mdb_txn_begin() will\nreturn #MDB_MAP_RESIZED. This function may be called with a size\nof zero to adopt the new size.\nAny attempt to set a size smaller than the space already consumed\nby the environment will be silently changed to the current size of the used space.\nerrors are:\n
    \n
  • EINVAL - an invalid parameter was specified, or the environment has\nan active write transaction.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create() [Direction: In]\n* `size` - The size in bytes [Direction: In]\n\n"] + pub fn mdb_env_set_mapsize(env: *mut MDB_env, size: mdb_size_t) -> ::libc::c_int; +} +extern "C" { + #[doc = "Set the maximum number of threads/reader slots for the environment.\n\nThis defines the number of slots in the lock table that is used to track readers in the\nthe environment. The default is 126.\nStarting a read-only transaction normally ties a lock table slot to the\ncurrent thread until the environment closes or the thread exits. If\nMDB_NOTLS is in use, #mdb_txn_begin() instead ties the slot to the\nMDB_txn object until it or the #MDB_env object is destroyed.\nThis function may only be called after #mdb_env_create() and before #mdb_env_open().\nerrors are:\n
    \n
  • EINVAL - an invalid parameter was specified, or the environment is already open.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create() [Direction: In]\n* `readers` - The maximum number of reader lock table slots [Direction: In]\n\n"] + pub fn mdb_env_set_maxreaders(env: *mut MDB_env, readers: ::libc::c_uint) -> ::libc::c_int; +} +extern "C" { + #[doc = "Get the maximum number of threads/reader slots for the environment.\n\nerrors are:\n
    \n
  • EINVAL - an invalid parameter was specified.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create() [Direction: In]\n* `readers` - Address of an integer to store the number of readers [Direction: In, Out]\n\n"] + pub fn mdb_env_get_maxreaders(env: *mut MDB_env, readers: *mut ::libc::c_uint) + -> ::libc::c_int; +} +extern "C" { + #[doc = "Set the maximum number of named databases for the environment.\n\nThis function is only needed if multiple databases will be used in the\nenvironment. Simpler applications that use the environment as a single\nunnamed database can ignore this option.\nThis function may only be called after #mdb_env_create() and before #mdb_env_open().\nCurrently a moderate number of slots are cheap but a huge number gets\nexpensive: 7-120 words per transaction, and every #mdb_dbi_open()\ndoes a linear search of the opened slots.\nerrors are:\n
    \n
  • EINVAL - an invalid parameter was specified, or the environment is already open.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create() [Direction: In]\n* `dbs` - The maximum number of databases [Direction: In]\n\n"] + pub fn mdb_env_set_maxdbs(env: *mut MDB_env, dbs: MDB_dbi) -> ::libc::c_int; +} +extern "C" { + #[doc = "Get the maximum size of keys and #MDB_DUPSORT data we can write.\n\nDepends on the compile-time constant #MDB_MAXKEYSIZE. Default 511.\nSee [`MDB_val`]\n\nReturns:\n\n* The maximum size of a key we can write\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create() [Direction: In]\n\n"] + pub fn mdb_env_get_maxkeysize(env: *mut MDB_env) -> ::libc::c_int; +} +extern "C" { + #[doc = "Set application information associated with the #MDB_env.\n\nReturns:\n\n* A non-zero error value on failure and 0 on success.\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create() [Direction: In]\n* `ctx` - An arbitrary pointer for whatever the application needs. [Direction: In]\n\n"] + pub fn mdb_env_set_userctx(env: *mut MDB_env, ctx: *mut ::libc::c_void) -> ::libc::c_int; +} +extern "C" { + #[doc = "Get the application information associated with the #MDB_env.\n\nReturns:\n\n* The pointer set by #mdb_env_set_userctx().\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create() [Direction: In]\n\n"] + pub fn mdb_env_get_userctx(env: *mut MDB_env) -> *mut ::libc::c_void; +} +#[doc = "A callback function for most LMDB assert() failures,\n\ncalled before printing the message and aborting.\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create(). [Direction: In]\n* `msg` - The assertion message, not including newline. [Direction: In]\n\n"] +pub type MDB_assert_func = + ::std::option::Option; +extern "C" { + #[doc = "Set or reset the assert() callback of the environment.\nDisabled if liblmdb is built with NDEBUG.\n\nReturns:\n\n* A non-zero error value on failure and 0 on success.\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create(). [Direction: In]\n* `func` - An #MDB_assert_func function, or 0. [Direction: In]\n\n# Notes\n\n* This hack should become obsolete as lmdb's error handling matures.\n\n"] + pub fn mdb_env_set_assert(env: *mut MDB_env, func: MDB_assert_func) -> ::libc::c_int; +} +extern "C" { + #[doc = "Create a transaction for use with the environment.\n\nThe transaction handle may be discarded using #mdb_txn_abort() or #mdb_txn_commit().\nthread, and a thread may only have a single transaction at a time.\nIf #MDB_NOTLS is in use, this does not apply to read-only transactions.\nwill be a nested transaction, with the transaction indicated by \\b parent\nas its parent. Transactions may be nested to any level. A parent\ntransaction and its cursors may not issue any other operations than\nmdb_txn_commit and mdb_txn_abort while it has active child transactions.\nmust be set to 0 or by bitwise OR'ing together one or more of the\nvalues described here.\n
    \n
  • #MDB_RDONLY\nThis transaction will not perform any write operations.\n
  • #MDB_NOSYNC\nDon't flush system buffers to disk when committing this transaction.\n
  • #MDB_NOMETASYNC\nFlush system buffers but omit metadata flush when committing this transaction.\n
\nerrors are:\n
    \n
  • #MDB_PANIC - a fatal error occurred earlier and the environment\nmust be shut down.\n
  • #MDB_MAP_RESIZED - another process wrote data beyond this MDB_env's\nmapsize and this environment's map must be resized as well.\nSee #mdb_env_set_mapsize().\n
  • #MDB_READERS_FULL - a read-only transaction was requested and\nthe reader lock table is full. See #mdb_env_set_maxreaders().\n
  • ENOMEM - out of memory.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create() [Direction: In]\n* `parent` - If this parameter is non-NULL, the new transaction [Direction: In]\n* `flags` - Special options for this transaction. This parameter [Direction: In]\n* `txn` - Address where the new #MDB_txn handle will be stored [Direction: In, Out]\n\n# Notes\n\n* A transaction and its cursors must only be used by a single\n* Cursors may not span transactions.\n\n"] + pub fn mdb_txn_begin( + env: *mut MDB_env, + parent: *mut MDB_txn, + flags: ::libc::c_uint, + txn: *mut *mut MDB_txn, + ) -> ::libc::c_int; +} +extern "C" { + #[doc = "Returns the transaction's #MDB_env\n\n# Arguments\n\n* `txn` - A transaction handle returned by #mdb_txn_begin() [Direction: In]\n\n"] + pub fn mdb_txn_env(txn: *mut MDB_txn) -> *mut MDB_env; +} +extern "C" { + #[doc = "Return the transaction's ID.\n\nThis returns the identifier associated with this transaction. For a\nread-only transaction, this corresponds to the snapshot being read;\nconcurrent readers will frequently have the same transaction ID.\n\nReturns:\n\n* A transaction ID, valid if input is an active transaction.\n\n# Arguments\n\n* `txn` - A transaction handle returned by #mdb_txn_begin() [Direction: In]\n\n"] + pub fn mdb_txn_id(txn: *mut MDB_txn) -> mdb_size_t; +} +extern "C" { + #[doc = "Commit all the operations of a transaction into the database.\n\nThe transaction handle is freed. It and its cursors must not be used\nagain after this call, except with #mdb_cursor_renew().\nOnly write-transactions free cursors.\nerrors are:\n
    \n
  • EINVAL - an invalid parameter was specified.\n
  • ENOSPC - no more disk space.\n
  • EIO - a low-level I/O error occurred while writing.\n
  • ENOMEM - out of memory.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `txn` - A transaction handle returned by #mdb_txn_begin() [Direction: In]\n\n# Notes\n\n* Earlier documentation incorrectly said all cursors would be freed.\n\n"] + pub fn mdb_txn_commit(txn: *mut MDB_txn) -> ::libc::c_int; +} +extern "C" { + #[doc = "Abandon all the operations of the transaction instead of saving them.\n\nThe transaction handle is freed. It and its cursors must not be used\nagain after this call, except with #mdb_cursor_renew().\nOnly write-transactions free cursors.\n\n# Arguments\n\n* `txn` - A transaction handle returned by #mdb_txn_begin() [Direction: In]\n\n# Notes\n\n* Earlier documentation incorrectly said all cursors would be freed.\n\n"] + pub fn mdb_txn_abort(txn: *mut MDB_txn); +} +extern "C" { + #[doc = "Reset a read-only transaction.\n\nAbort the transaction like #mdb_txn_abort(), but keep the transaction\nhandle. #mdb_txn_renew() may reuse the handle. This saves allocation\noverhead if the process will start a new read-only transaction soon,\nand also locking overhead if #MDB_NOTLS is in use. The reader table\nlock is released, but the table slot stays tied to its thread or\n#MDB_txn. Use mdb_txn_abort() to discard a reset handle, and to free\nits lock table slot if MDB_NOTLS is in use.\nCursors opened within the transaction must not be used\nagain after this call, except with #mdb_cursor_renew().\nReader locks generally don't interfere with writers, but they keep old\nversions of database pages allocated. Thus they prevent the old pages\nfrom being reused when writers commit new data, and so under heavy load\nthe database size may grow much more rapidly than otherwise.\n\n# Arguments\n\n* `txn` - A transaction handle returned by #mdb_txn_begin() [Direction: In]\n\n"] + pub fn mdb_txn_reset(txn: *mut MDB_txn); +} +extern "C" { + #[doc = "Renew a read-only transaction.\n\nThis acquires a new reader lock for a transaction handle that had been\nreleased by #mdb_txn_reset(). It must be called before a reset transaction\nmay be used again.\nerrors are:\n
    \n
  • #MDB_PANIC - a fatal error occurred earlier and the environment\nmust be shut down.\n
  • EINVAL - an invalid parameter was specified.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `txn` - A transaction handle returned by #mdb_txn_begin() [Direction: In]\n\n"] + pub fn mdb_txn_renew(txn: *mut MDB_txn) -> ::libc::c_int; +} +extern "C" { + #[doc = "Open a database in the environment.\n\nA database handle denotes the name and parameters of a database,\nindependently of whether such a database exists.\nThe database handle may be discarded by calling #mdb_dbi_close().\nThe old database handle is returned if the database was already open.\nThe handle may only be closed once.\nThe database handle will be private to the current transaction until\nthe transaction is successfully committed. If the transaction is\naborted the handle will be closed automatically.\nAfter a successful commit the handle will reside in the shared\nenvironment, and may be used by other transactions.\nThis function must not be called from multiple concurrent\ntransactions in the same process. A transaction that uses\nthis function must finish (either commit or abort) before\nany other transaction in the process may use this function.\nTo use named databases (with name != NULL), #mdb_env_set_maxdbs()\nmust be called before opening the environment. Database names are\nkeys in the unnamed database, and may be read but not written.\ndatabase is needed in the environment, this value may be NULL.\nmust be set to 0 or by bitwise OR'ing together one or more of the\nvalues described here.\n
    \n
  • #MDB_REVERSEKEY\nKeys are strings to be compared in reverse order, from the end\nof the strings to the beginning. By default, Keys are treated as strings and\ncompared from beginning to end.\n
  • #MDB_DUPSORT\nDuplicate keys may be used in the database. (Or, from another perspective,\nkeys may have multiple data items, stored in sorted order.) By default\nkeys must be unique and may have only a single data item.\n
  • #MDB_INTEGERKEY\nKeys are binary integers in native byte order, either unsigned int\nor #mdb_size_t, and will be sorted as such.\n(lmdb expects 32-bit int <= size_t <= 32/64-bit mdb_size_t.)\nThe keys must all be of the same size.\n
  • #MDB_DUPFIXED\nThis flag may only be used in combination with #MDB_DUPSORT. This option\ntells the library that the data items for this database are all the same\nsize, which allows further optimizations in storage and retrieval. When\nall data items are the same size, the #MDB_GET_MULTIPLE, #MDB_NEXT_MULTIPLE\nand #MDB_PREV_MULTIPLE cursor operations may be used to retrieve multiple\nitems at once.\n
  • #MDB_INTEGERDUP\nThis option specifies that duplicate data items are binary integers,\nsimilar to #MDB_INTEGERKEY keys.\n
  • #MDB_REVERSEDUP\nThis option specifies that duplicate data items should be compared as\nstrings in reverse order.\n
  • #MDB_CREATE\nCreate the named database if it doesn't exist. This option is not\nallowed in a read-only transaction or a read-only environment.\n
\nerrors are:\n
    \n
  • #MDB_NOTFOUND - the specified database doesn't exist in the environment\nand #MDB_CREATE was not specified.\n
  • #MDB_DBS_FULL - too many databases have been opened. See #mdb_env_set_maxdbs().\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `txn` - A transaction handle returned by #mdb_txn_begin() [Direction: In]\n* `name` - The name of the database to open. If only a single [Direction: In]\n* `flags` - Special options for this database. This parameter [Direction: In]\n* `dbi` - Address where the new #MDB_dbi handle will be stored [Direction: In, Out]\n\n"] + pub fn mdb_dbi_open( + txn: *mut MDB_txn, + name: *const ::libc::c_char, + flags: ::libc::c_uint, + dbi: *mut MDB_dbi, + ) -> ::libc::c_int; +} +extern "C" { + #[doc = "Retrieve statistics for a database.\n\nwhere the statistics will be copied\nerrors are:\n
    \n
  • EINVAL - an invalid parameter was specified.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `txn` - A transaction handle returned by #mdb_txn_begin() [Direction: In]\n* `dbi` - A database handle returned by #mdb_dbi_open() [Direction: In]\n* `stat` - The address of an #MDB_stat structure [Direction: In, Out]\n\n"] + pub fn mdb_stat(txn: *mut MDB_txn, dbi: MDB_dbi, stat: *mut MDB_stat) -> ::libc::c_int; +} +extern "C" { + #[doc = "Retrieve the DB flags for a database handle.\n\nReturns:\n\n* A non-zero error value on failure and 0 on success.\n\n# Arguments\n\n* `txn` - A transaction handle returned by #mdb_txn_begin() [Direction: In]\n* `dbi` - A database handle returned by #mdb_dbi_open() [Direction: In]\n* `flags` - Address where the flags will be returned. [Direction: In, Out]\n\n"] + pub fn mdb_dbi_flags( + txn: *mut MDB_txn, + dbi: MDB_dbi, + flags: *mut ::libc::c_uint, + ) -> ::libc::c_int; +} +extern "C" { + #[doc = "Close a database handle. Normally unnecessary. Use with care:\n\nThis call is not mutex protected. Handles should only be closed by\na single thread, and only if no other threads are going to reference\nthe database handle or one of its cursors any further. Do not close\na handle if an existing transaction has modified its database.\nDoing so can cause misbehavior from database corruption to errors\nlike MDB_BAD_VALSIZE (since the DB name is gone).\nClosing a database handle is not necessary, but lets #mdb_dbi_open()\nreuse the handle value. Usually it's better to set a bigger\n#mdb_env_set_maxdbs(), unless that value would be large.\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create() [Direction: In]\n* `dbi` - A database handle returned by #mdb_dbi_open() [Direction: In]\n\n"] + pub fn mdb_dbi_close(env: *mut MDB_env, dbi: MDB_dbi); +} +extern "C" { + #[doc = "Empty or delete+close a database.\n\nSee #mdb_dbi_close() for restrictions about closing the DB handle.\nenvironment and close the DB handle.\n\nReturns:\n\n* A non-zero error value on failure and 0 on success.\n\n# Arguments\n\n* `txn` - A transaction handle returned by #mdb_txn_begin() [Direction: In]\n* `dbi` - A database handle returned by #mdb_dbi_open() [Direction: In]\n* `del` - 0 to empty the DB, 1 to delete it from the [Direction: In]\n\n"] + pub fn mdb_drop(txn: *mut MDB_txn, dbi: MDB_dbi, del: ::libc::c_int) -> ::libc::c_int; +} +extern "C" { + #[doc = "Set a custom key comparison function for a database.\n\nThe comparison function is called whenever it is necessary to compare a\nkey specified by the application with a key currently stored in the database.\nIf no comparison function is specified, and no special key flags were specified\nwith #mdb_dbi_open(), the keys are compared lexically, with shorter keys collating\nbefore longer keys.\notherwise data corruption may occur. The same comparison function must be used by every\nprogram accessing the database, every time the database is used.\nerrors are:\n
    \n
  • EINVAL - an invalid parameter was specified.\n
\n\n**Warning!**\n\n* This function must be called before any data access functions are used,\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `txn` - A transaction handle returned by #mdb_txn_begin() [Direction: In]\n* `dbi` - A database handle returned by #mdb_dbi_open() [Direction: In]\n* `cmp` - A #MDB_cmp_func function [Direction: In]\n\n"] + pub fn mdb_set_compare(txn: *mut MDB_txn, dbi: MDB_dbi, cmp: MDB_cmp_func) -> ::libc::c_int; +} +extern "C" { + #[doc = "Set a custom data comparison function for a #MDB_DUPSORT database.\n\nThis comparison function is called whenever it is necessary to compare a data\nitem specified by the application with a data item currently stored in the database.\nThis function only takes effect if the database was opened with the #MDB_DUPSORT\nflag.\nIf no comparison function is specified, and no special key flags were specified\nwith #mdb_dbi_open(), the data items are compared lexically, with shorter items collating\nbefore longer items.\notherwise data corruption may occur. The same comparison function must be used by every\nprogram accessing the database, every time the database is used.\nerrors are:\n
    \n
  • EINVAL - an invalid parameter was specified.\n
\n\n**Warning!**\n\n* This function must be called before any data access functions are used,\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `txn` - A transaction handle returned by #mdb_txn_begin() [Direction: In]\n* `dbi` - A database handle returned by #mdb_dbi_open() [Direction: In]\n* `cmp` - A #MDB_cmp_func function [Direction: In]\n\n"] + pub fn mdb_set_dupsort(txn: *mut MDB_txn, dbi: MDB_dbi, cmp: MDB_cmp_func) -> ::libc::c_int; +} +extern "C" { + #[doc = "Set a relocation function for a #MDB_FIXEDMAP database.\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `txn` - A transaction handle returned by #mdb_txn_begin() [Direction: In]\n* `dbi` - A database handle returned by #mdb_dbi_open() [Direction: In]\n* `rel` - A #MDB_rel_func function [Direction: In]\n\n# To Do\n\n* The relocation function is called whenever it is necessary to move the data\nof an item to a different position in the database (e.g. through tree\nbalancing operations, shifts as a result of adds or deletes, etc.). It is\nintended to allow address/position-dependent data items to be stored in\na database in an environment opened with the #MDB_FIXEDMAP option.\nCurrently the relocation feature is unimplemented and setting\nthis function has no effect.\nerrors are:\n
    \n
  • EINVAL - an invalid parameter was specified.\n
\n"] + pub fn mdb_set_relfunc(txn: *mut MDB_txn, dbi: MDB_dbi, rel: MDB_rel_func) -> ::libc::c_int; +} +extern "C" { + #[doc = "Set a context pointer for a #MDB_FIXEDMAP database's relocation function.\n\nSee #mdb_set_relfunc and #MDB_rel_func for more details.\nIt will be passed to the callback function set by #mdb_set_relfunc\nas its \\b relctx parameter whenever the callback is invoked.\nerrors are:\n
    \n
  • EINVAL - an invalid parameter was specified.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `txn` - A transaction handle returned by #mdb_txn_begin() [Direction: In]\n* `dbi` - A database handle returned by #mdb_dbi_open() [Direction: In]\n* `ctx` - An arbitrary pointer for whatever the application needs. [Direction: In]\n\n"] + pub fn mdb_set_relctx( + txn: *mut MDB_txn, + dbi: MDB_dbi, + ctx: *mut ::libc::c_void, + ) -> ::libc::c_int; +} +extern "C" { + #[doc = "Get items from a database.\n\nThis function retrieves key/data pairs from the database. The address\nand length of the data associated with the specified \\b key are returned\nin the structure to which \\b data refers.\nIf the database supports duplicate keys (#MDB_DUPSORT) then the\nfirst data item for the key will be returned. Retrieval of other\nitems requires the use of #mdb_cursor_get().\ndatabase. The caller need not dispose of the memory, and may not\nmodify it in any way. For values returned in a read-only transaction\nany modification attempts will cause a SIGSEGV.\nsubsequent update operation, or the end of the transaction.\nerrors are:\n
    \n
  • #MDB_NOTFOUND - the key was not in the database.\n
  • EINVAL - an invalid parameter was specified.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `txn` - A transaction handle returned by #mdb_txn_begin() [Direction: In]\n* `dbi` - A database handle returned by #mdb_dbi_open() [Direction: In]\n* `key` - The key to search for in the database [Direction: In]\n* `data` - The data corresponding to the key [Direction: In, Out]\n\n# Notes\n\n* The memory pointed to by the returned values is owned by the\n* Values returned from the database are valid only until a\n\n"] + pub fn mdb_get( + txn: *mut MDB_txn, + dbi: MDB_dbi, + key: *mut MDB_val, + data: *mut MDB_val, + ) -> ::libc::c_int; +} +extern "C" { + #[doc = "Store items into a database.\n\nThis function stores key/data pairs in the database. The default behavior\nis to enter the new key/data pair, replacing any previously existing key\nif duplicates are disallowed, or adding a duplicate data item if\nduplicates are allowed (#MDB_DUPSORT).\nmust be set to 0 or by bitwise OR'ing together one or more of the\nvalues described here.\n
    \n
  • #MDB_NODUPDATA - enter the new key/data pair only if it does not\nalready appear in the database. This flag may only be specified\nif the database was opened with #MDB_DUPSORT. The function will\nreturn #MDB_KEYEXIST if the key/data pair already appears in the\ndatabase.\n
  • #MDB_NOOVERWRITE - enter the new key/data pair only if the key\ndoes not already appear in the database. The function will return\n#MDB_KEYEXIST if the key already appears in the database, even if\nthe database supports duplicates (#MDB_DUPSORT). The \\b data\nparameter will be set to point to the existing item.\n
  • #MDB_RESERVE - reserve space for data of the given size, but\ndon't copy the given data. Instead, return a pointer to the\nreserved space, which the caller can fill in later - before\nthe next update operation or the transaction ends. This saves\nan extra memcpy if the data is being generated later.\nLMDB does nothing else with this memory, the caller is expected\nto modify all of the space requested. This flag must not be\nspecified if the database was opened with #MDB_DUPSORT.\n
  • #MDB_APPEND - append the given key/data pair to the end of the\ndatabase. This option allows fast bulk loading when keys are\nalready known to be in the correct order. Loading unsorted keys\nwith this flag will cause a #MDB_KEYEXIST error.\n
  • #MDB_APPENDDUP - as above, but for sorted dup data.\n
\nerrors are:\n
    \n
  • #MDB_MAP_FULL - the database is full, see #mdb_env_set_mapsize().\n
  • #MDB_TXN_FULL - the transaction has too many dirty pages.\n
  • EACCES - an attempt was made to write in a read-only transaction.\n
  • EINVAL - an invalid parameter was specified.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `txn` - A transaction handle returned by #mdb_txn_begin() [Direction: In]\n* `dbi` - A database handle returned by #mdb_dbi_open() [Direction: In]\n* `key` - The key to store in the database [Direction: In]\n* `data` - The data to store [Direction: Out]\n* `flags` - Special options for this operation. This parameter [Direction: In]\n\n"] + pub fn mdb_put( + txn: *mut MDB_txn, + dbi: MDB_dbi, + key: *mut MDB_val, + data: *mut MDB_val, + flags: ::libc::c_uint, + ) -> ::libc::c_int; +} +extern "C" { + #[doc = "Delete items from a database.\n\nThis function removes key/data pairs from the database.\nIf the database does not support sorted duplicate data items\n(#MDB_DUPSORT) the data parameter is ignored.\nIf the database supports sorted duplicates and the data parameter\nis NULL, all of the duplicate data items for the key will be\ndeleted. Otherwise, if the data parameter is non-NULL\nonly the matching data item will be deleted.\nThis function will return #MDB_NOTFOUND if the specified key/data\npair is not in the database.\nerrors are:\n
    \n
  • EACCES - an attempt was made to write in a read-only transaction.\n
  • EINVAL - an invalid parameter was specified.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `txn` - A transaction handle returned by #mdb_txn_begin() [Direction: In]\n* `dbi` - A database handle returned by #mdb_dbi_open() [Direction: In]\n* `key` - The key to delete from the database [Direction: In]\n* `data` - The data to delete [Direction: In]\n\n"] + pub fn mdb_del( + txn: *mut MDB_txn, + dbi: MDB_dbi, + key: *mut MDB_val, + data: *mut MDB_val, + ) -> ::libc::c_int; +} +extern "C" { + #[doc = "Create a cursor handle.\n\nA cursor is associated with a specific transaction and database.\nA cursor cannot be used when its database handle is closed. Nor\nwhen its transaction has ended, except with #mdb_cursor_renew().\nIt can be discarded with #mdb_cursor_close().\nA cursor in a write-transaction can be closed before its transaction\nends, and will otherwise be closed when its transaction ends.\nA cursor in a read-only transaction must be closed explicitly, before\nor after its transaction ends. It can be reused with\n#mdb_cursor_renew() before finally closing it.\nwere closed when the transaction committed or aborted.\nerrors are:\n
    \n
  • EINVAL - an invalid parameter was specified.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `txn` - A transaction handle returned by #mdb_txn_begin() [Direction: In]\n* `dbi` - A database handle returned by #mdb_dbi_open() [Direction: In]\n* `cursor` - Address where the new #MDB_cursor handle will be stored [Direction: In, Out]\n\n# Notes\n\n* Earlier documentation said that cursors in every transaction\n\n"] + pub fn mdb_cursor_open( + txn: *mut MDB_txn, + dbi: MDB_dbi, + cursor: *mut *mut MDB_cursor, + ) -> ::libc::c_int; +} +extern "C" { + #[doc = "Close a cursor handle.\n\nThe cursor handle will be freed and must not be used again after this call.\nIts transaction must still be live if it is a write-transaction.\n\n# Arguments\n\n* `cursor` - A cursor handle returned by #mdb_cursor_open() [Direction: In]\n\n"] + pub fn mdb_cursor_close(cursor: *mut MDB_cursor); +} +extern "C" { + #[doc = "Renew a cursor handle.\n\nA cursor is associated with a specific transaction and database.\nCursors that are only used in read-only\ntransactions may be re-used, to avoid unnecessary malloc/free overhead.\nThe cursor may be associated with a new read-only transaction, and\nreferencing the same database handle as it was created with.\nThis may be done whether the previous transaction is live or dead.\nerrors are:\n
    \n
  • EINVAL - an invalid parameter was specified.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `txn` - A transaction handle returned by #mdb_txn_begin() [Direction: In]\n* `cursor` - A cursor handle returned by #mdb_cursor_open() [Direction: In]\n\n"] + pub fn mdb_cursor_renew(txn: *mut MDB_txn, cursor: *mut MDB_cursor) -> ::libc::c_int; +} +extern "C" { + #[doc = "Return the cursor's transaction handle.\n\n# Arguments\n\n* `cursor` - A cursor handle returned by #mdb_cursor_open() [Direction: In]\n\n"] + pub fn mdb_cursor_txn(cursor: *mut MDB_cursor) -> *mut MDB_txn; +} +extern "C" { + #[doc = "Return the cursor's database handle.\n\n# Arguments\n\n* `cursor` - A cursor handle returned by #mdb_cursor_open() [Direction: In]\n\n"] + pub fn mdb_cursor_dbi(cursor: *mut MDB_cursor) -> MDB_dbi; +} +extern "C" { + #[doc = "Retrieve by cursor.\n\nThis function retrieves key/data pairs from the database. The address and length\nof the key are returned in the object to which \\b key refers (except for the\ncase of the #MDB_SET option, in which the \\b key object is unchanged), and\nthe address and length of the data are returned in the object to which \\b data\nrefers.\nSee #mdb_get() for restrictions on using the output values.\nerrors are:\n
    \n
  • #MDB_NOTFOUND - no matching key found.\n
  • EINVAL - an invalid parameter was specified.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `cursor` - A cursor handle returned by #mdb_cursor_open() [Direction: In]\n* `key` - The key for a retrieved item [Direction: Out]\n* `data` - The data of a retrieved item [Direction: Out]\n* `op` - A cursor operation #MDB_cursor_op [Direction: In]\n\n"] + pub fn mdb_cursor_get( + cursor: *mut MDB_cursor, + key: *mut MDB_val, + data: *mut MDB_val, + op: MDB_cursor_op, + ) -> ::libc::c_int; +} +extern "C" { + #[doc = "Store by cursor.\n\nThis function stores key/data pairs into the database.\nThe cursor is positioned at the new item, or on failure usually near it.\nstate of the cursor unchanged.\nmust be set to 0 or one of the values described here.\n
    \n
  • #MDB_CURRENT - replace the item at the current cursor position.\nThe \\b key parameter must still be provided, and must match it.\nIf using sorted duplicates (#MDB_DUPSORT) the data item must still\nsort into the same place. This is intended to be used when the\nnew data is the same size as the old. Otherwise it will simply\nperform a delete of the old record followed by an insert.\n
  • #MDB_NODUPDATA - enter the new key/data pair only if it does not\nalready appear in the database. This flag may only be specified\nif the database was opened with #MDB_DUPSORT. The function will\nreturn #MDB_KEYEXIST if the key/data pair already appears in the\ndatabase.\n
  • #MDB_NOOVERWRITE - enter the new key/data pair only if the key\ndoes not already appear in the database. The function will return\n#MDB_KEYEXIST if the key already appears in the database, even if\nthe database supports duplicates (#MDB_DUPSORT).\n
  • #MDB_RESERVE - reserve space for data of the given size, but\ndon't copy the given data. Instead, return a pointer to the\nreserved space, which the caller can fill in later - before\nthe next update operation or the transaction ends. This saves\nan extra memcpy if the data is being generated later. This flag\nmust not be specified if the database was opened with #MDB_DUPSORT.\n
  • #MDB_APPEND - append the given key/data pair to the end of the\ndatabase. No key comparisons are performed. This option allows\nfast bulk loading when keys are already known to be in the\ncorrect order. Loading unsorted keys with this flag will cause\na #MDB_KEYEXIST error.\n
  • #MDB_APPENDDUP - as above, but for sorted dup data.\n
  • #MDB_MULTIPLE - store multiple contiguous data elements in a\nsingle request. This flag may only be specified if the database\nwas opened with #MDB_DUPFIXED. The \\b data argument must be an\narray of two MDB_vals. The mv_size of the first MDB_val must be\nthe size of a single data element. The mv_data of the first MDB_val\nmust point to the beginning of the array of contiguous data elements.\nThe mv_size of the second MDB_val must be the count of the number\nof data elements to store. On return this field will be set to\nthe count of the number of elements actually written. The mv_data\nof the second MDB_val is unused.\n
\nerrors are:\n
    \n
  • #MDB_MAP_FULL - the database is full, see #mdb_env_set_mapsize().\n
  • #MDB_TXN_FULL - the transaction has too many dirty pages.\n
  • EACCES - an attempt was made to write in a read-only transaction.\n
  • EINVAL - an invalid parameter was specified.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `cursor` - A cursor handle returned by #mdb_cursor_open() [Direction: In]\n* `key` - The key operated on. [Direction: In]\n* `data` - The data operated on. [Direction: In]\n* `flags` - Options for this operation. This parameter [Direction: In]\n\n# Notes\n\n* Earlier documentation incorrectly said errors would leave the\n\n"] + pub fn mdb_cursor_put( + cursor: *mut MDB_cursor, + key: *mut MDB_val, + data: *mut MDB_val, + flags: ::libc::c_uint, + ) -> ::libc::c_int; +} +extern "C" { + #[doc = "Delete current key/data pair\n\nThis function deletes the key/data pair to which the cursor refers.\nThis does not invalidate the cursor, so operations such as MDB_NEXT\ncan still be used on it.\nBoth MDB_NEXT and MDB_GET_CURRENT will return the same record after\nthis operation.\nmust be set to 0 or one of the values described here.\n
    \n
  • #MDB_NODUPDATA - delete all of the data items for the current key.\nThis flag may only be specified if the database was opened with #MDB_DUPSORT.\n
\nerrors are:\n
    \n
  • EACCES - an attempt was made to write in a read-only transaction.\n
  • EINVAL - an invalid parameter was specified.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `cursor` - A cursor handle returned by #mdb_cursor_open() [Direction: In]\n* `flags` - Options for this operation. This parameter [Direction: In]\n\n"] + pub fn mdb_cursor_del(cursor: *mut MDB_cursor, flags: ::libc::c_uint) -> ::libc::c_int; +} +extern "C" { + #[doc = "Return count of duplicates for current key.\n\nThis call is only valid on databases that support sorted duplicate\ndata items #MDB_DUPSORT.\nerrors are:\n
    \n
  • EINVAL - cursor is not initialized, or an invalid parameter was specified.\n
\n\nReturns:\n\n* A non-zero error value on failure and 0 on success. Some possible\n\n# Arguments\n\n* `cursor` - A cursor handle returned by #mdb_cursor_open() [Direction: In]\n* `countp` - Address where the count will be stored [Direction: In, Out]\n\n"] + pub fn mdb_cursor_count(cursor: *mut MDB_cursor, countp: *mut mdb_size_t) -> ::libc::c_int; +} +extern "C" { + #[doc = "Compare two data items according to a particular database.\n\nThis returns a comparison as if the two data items were keys in the\nspecified database.\n\nReturns:\n\n* < 0 if a < b, 0 if a == b, > 0 if a > b\n\n# Arguments\n\n* `txn` - A transaction handle returned by #mdb_txn_begin() [Direction: In]\n* `dbi` - A database handle returned by #mdb_dbi_open() [Direction: In]\n* `a` - The first item to compare [Direction: In]\n* `b` - The second item to compare [Direction: In]\n\n"] + pub fn mdb_cmp( + txn: *mut MDB_txn, + dbi: MDB_dbi, + a: *const MDB_val, + b: *const MDB_val, + ) -> ::libc::c_int; +} +extern "C" { + #[doc = "Compare two data items according to a particular database.\n\nThis returns a comparison as if the two items were data items of\nthe specified database. The database must have the #MDB_DUPSORT flag.\n\nReturns:\n\n* < 0 if a < b, 0 if a == b, > 0 if a > b\n\n# Arguments\n\n* `txn` - A transaction handle returned by #mdb_txn_begin() [Direction: In]\n* `dbi` - A database handle returned by #mdb_dbi_open() [Direction: In]\n* `a` - The first item to compare [Direction: In]\n* `b` - The second item to compare [Direction: In]\n\n"] + pub fn mdb_dcmp( + txn: *mut MDB_txn, + dbi: MDB_dbi, + a: *const MDB_val, + b: *const MDB_val, + ) -> ::libc::c_int; +} +#[doc = "A callback function used to print a message from the library.\n\nReturns:\n\n* < 0 on failure, >= 0 on success.\n\n# Arguments\n\n* `msg` - The string to be printed. [Direction: In]\n* `ctx` - An arbitrary context pointer for the callback. [Direction: In]\n\n"] +pub type MDB_msg_func = ::std::option::Option< + unsafe extern "C" fn(msg: *const ::libc::c_char, ctx: *mut ::libc::c_void) -> ::libc::c_int, +>; +extern "C" { + #[doc = "Dump the entries in the reader lock table.\n\nReturns:\n\n* < 0 on failure, >= 0 on success.\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create() [Direction: In]\n* `func` - A #MDB_msg_func function [Direction: In]\n* `ctx` - Anything the message function needs [Direction: In]\n\n"] + pub fn mdb_reader_list( + env: *mut MDB_env, + func: MDB_msg_func, + ctx: *mut ::libc::c_void, + ) -> ::libc::c_int; +} +extern "C" { + #[doc = "Check for stale entries in the reader lock table.\n\nReturns:\n\n* 0 on success, non-zero on failure.\n\n# Arguments\n\n* `env` - An environment handle returned by #mdb_env_create() [Direction: In]\n* `dead` - Number of stale slots that were cleared [Direction: In, Out]\n\n"] + pub fn mdb_reader_check(env: *mut MDB_env, dead: *mut ::libc::c_int) -> ::libc::c_int; +} diff --git a/lmdb-master-sys/src/lib.rs b/lmdb-master-sys/src/lib.rs new file mode 100644 index 00000000..947b880b --- /dev/null +++ b/lmdb-master-sys/src/lib.rs @@ -0,0 +1,23 @@ +#![deny(warnings)] +#![allow(rustdoc::broken_intra_doc_links)] +#![allow(non_camel_case_types)] +#![allow(clippy::all)] +#![doc(html_root_url = "https://docs.rs/lmdb-master-sys/0.1.0")] + +extern crate libc; + +#[cfg(unix)] +#[allow(non_camel_case_types)] +pub type mdb_mode_t = ::libc::mode_t; +#[cfg(windows)] +#[allow(non_camel_case_types)] +pub type mdb_mode_t = ::libc::c_int; + +#[cfg(unix)] +#[allow(non_camel_case_types)] +pub type mdb_filehandle_t = ::libc::c_int; +#[cfg(windows)] +#[allow(non_camel_case_types)] +pub type mdb_filehandle_t = *mut ::libc::c_void; + +include!("bindings.rs"); diff --git a/lmdb-master-sys/tests/fixtures/testdb-32/data.mdb b/lmdb-master-sys/tests/fixtures/testdb-32/data.mdb new file mode 100644 index 00000000..81ba3895 Binary files /dev/null and b/lmdb-master-sys/tests/fixtures/testdb-32/data.mdb differ diff --git a/lmdb-master-sys/tests/fixtures/testdb-32/lock.mdb b/lmdb-master-sys/tests/fixtures/testdb-32/lock.mdb new file mode 100644 index 00000000..f271a673 Binary files /dev/null and b/lmdb-master-sys/tests/fixtures/testdb-32/lock.mdb differ diff --git a/lmdb-master-sys/tests/lmdb.rs b/lmdb-master-sys/tests/lmdb.rs new file mode 100644 index 00000000..7279ce07 --- /dev/null +++ b/lmdb-master-sys/tests/lmdb.rs @@ -0,0 +1,26 @@ +use std::env; +use std::path::PathBuf; +use std::process::Command; + +#[test] +fn test_lmdb() { + let mut lmdb = PathBuf::from(&env::var("CARGO_MANIFEST_DIR").unwrap()); + lmdb.push("lmdb"); + lmdb.push("libraries"); + lmdb.push("liblmdb"); + + let make_cmd = Command::new("make") + .current_dir(&lmdb) + .status() + .expect("failed to execute process"); + + assert_eq!(make_cmd.success(), true); + + let make_test_cmd = Command::new("make") + .arg("test") + .current_dir(&lmdb) + .status() + .expect("failed to execute process"); + + assert_eq!(make_test_cmd.success(), true); +} diff --git a/lmdb-master-sys/tests/simple.rs b/lmdb-master-sys/tests/simple.rs new file mode 100644 index 00000000..e82fb670 --- /dev/null +++ b/lmdb-master-sys/tests/simple.rs @@ -0,0 +1,94 @@ +use lmdb_master_sys::*; + +use std::ffi::c_void; +use std::fs::{self, File}; +use std::ptr; + +// https://github.com/victorporof/lmdb/blob/mdb.master/libraries/liblmdb/moz-test.c + +macro_rules! E { + ($expr:expr) => {{ + match $expr { + lmdb_master_sys::MDB_SUCCESS => (), + err_code => assert!(false, "Failed with code {}", err_code), + } + }}; +} + +macro_rules! str { + ($expr:expr) => { + std::ffi::CString::new($expr).unwrap().as_ptr() + }; +} + +#[test] +#[cfg(target_pointer_width = "32")] +fn test_simple_32() { + test_simple("./tests/fixtures/testdb-32") +} + +#[test] +#[cfg(target_pointer_width = "64")] +fn test_simple_64() { + test_simple("./tests/fixtures/testdb") +} + +#[cfg(windows)] +fn get_file_fd(file: &File) -> std::os::windows::io::RawHandle { + use std::os::windows::io::AsRawHandle; + file.as_raw_handle() +} + +#[cfg(unix)] +fn get_file_fd(file: &File) -> std::os::unix::io::RawFd { + use std::os::unix::io::AsRawFd; + file.as_raw_fd() +} + +fn test_simple(env_path: &str) { + let _ = fs::remove_dir_all(env_path); + fs::create_dir_all(env_path).unwrap(); + + let mut env: *mut MDB_env = ptr::null_mut(); + let mut dbi: MDB_dbi = 0; + let mut key = MDB_val { + mv_size: 0, + mv_data: ptr::null_mut(), + }; + let mut data = MDB_val { + mv_size: 0, + mv_data: ptr::null_mut(), + }; + let mut txn: *mut MDB_txn = ptr::null_mut(); + let sval = str!("foo") as *mut c_void; + let dval = str!("bar") as *mut c_void; + + unsafe { + E!(mdb_env_create(&mut env)); + E!(mdb_env_set_maxdbs(env, 2)); + E!(mdb_env_open(env, str!(env_path), 0, 0664)); + + E!(mdb_txn_begin(env, ptr::null_mut(), 0, &mut txn)); + E!(mdb_dbi_open(txn, str!("subdb"), MDB_CREATE, &mut dbi)); + E!(mdb_txn_commit(txn)); + + key.mv_size = 3; + key.mv_data = sval; + data.mv_size = 3; + data.mv_data = dval; + + E!(mdb_txn_begin(env, ptr::null_mut(), 0, &mut txn)); + E!(mdb_put(txn, dbi, &mut key, &mut data, 0)); + E!(mdb_txn_commit(txn)); + } + + let file = File::create("./tests/fixtures/copytestdb.mdb").unwrap(); + + unsafe { + let fd = get_file_fd(&file); + E!(mdb_env_copyfd(env, fd)); + + mdb_dbi_close(env, dbi); + mdb_env_close(env); + } +}