heed-types-0.20.1/.cargo_vcs_info.json0000644000000001500000000000100131570ustar { "git": { "sha1": "cfa1bca9f4be1db079bef5ec6d8fa57ef23aba90" }, "path_in_vcs": "heed-types" }heed-types-0.20.1/Cargo.toml0000644000000026730000000000100111710ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" name = "heed-types" version = "0.20.1" authors = ["Kerollmops "] description = "The types used with the fully typed LMDB wrapper, heed" readme = "README.md" license = "MIT" repository = "https://github.com/Kerollmops/heed" [dependencies.bincode] version = "1.3.3" optional = true [dependencies.byteorder] version = "1.5.0" [dependencies.heed-traits] version = "0.20.0" [dependencies.rmp-serde] version = "1.3.0" optional = true [dependencies.serde] version = "1.0.203" optional = true [dependencies.serde_json] version = "1.0.120" optional = true [features] arbitrary_precision = ["serde_json/arbitrary_precision"] default = [ "serde-bincode", "serde-json", ] preserve_order = ["serde_json/preserve_order"] raw_value = ["serde_json/raw_value"] serde-bincode = [ "serde", "bincode", ] serde-json = [ "serde", "serde_json", ] serde-rmp = [ "serde", "rmp-serde", ] unbounded_depth = ["serde_json/unbounded_depth"] heed-types-0.20.1/Cargo.toml.orig000064400000000000000000000016701046102023000146460ustar 00000000000000[package] name = "heed-types" version = "0.20.1" 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 = "2021" [dependencies] bincode = { version = "1.3.3", optional = true } byteorder = "1.5.0" heed-traits = { version = "0.20.0", path = "../heed-traits" } serde = { version = "1.0.203", optional = true } serde_json = { version = "1.0.120", optional = true } rmp-serde = { version = "1.3.0", optional = true } [features] default = ["serde-bincode", "serde-json"] serde-bincode = ["serde", "bincode"] serde-json = ["serde", "serde_json"] serde-rmp = ["serde", "rmp-serde"] # serde_json features preserve_order = ["serde_json/preserve_order"] arbitrary_precision = ["serde_json/arbitrary_precision"] raw_value = ["serde_json/raw_value"] unbounded_depth = ["serde_json/unbounded_depth"] heed-types-0.20.1/README.md000064400000000000000000000045221046102023000132350ustar 00000000000000

heed

[![License](https://img.shields.io/badge/license-MIT-green)](#LICENSE) [![Crates.io](https://img.shields.io/crates/v/heed)](https://crates.io/crates/heed) [![Docs](https://docs.rs/heed/badge.svg)](https://docs.rs/heed) [![dependency status](https://deps.rs/repo/github/meilisearch/heed/status.svg)](https://deps.rs/repo/github/meilisearch/heed) [![Build](https://github.com/meilisearch/heed/actions/workflows/rust.yml/badge.svg)](https://github.com/meilisearch/heed/actions/workflows/rust.yml) A Rust-centric [LMDB](https://en.wikipedia.org/wiki/Lightning_Memory-Mapped_Database) abstraction with minimal overhead. This library enables the storage of various Rust types within LMDB, extending support to include Serde-compatible types. ## Simple Example Usage Here is an example on how to store and read entries into LMDB in a safe and ACID way. For usage examples, see [heed/examples/](heed/examples/). To see more advanced usage techniques go check our [Cookbook](https://docs.rs/heed/latest/heed/cookbook/index.html). ```rust use std::fs; use std::path::Path; use heed::{EnvOpenOptions, Database}; use heed::types::*; fn main() -> Result<(), Box> { let env = unsafe { EnvOpenOptions::new().open("my-first-db")? }; // We open the default unnamed database let mut wtxn = env.write_txn()?; let db: Database> = env.create_database(&mut wtxn, None)?; // We open a write transaction 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()?; // We open a read transaction to check if those values are now available let mut rtxn = env.read_txn()?; let ret = db.get(&rtxn, "zero")?; assert_eq!(ret, Some(0)); let ret = db.get(&rtxn, "five")?; assert_eq!(ret, Some(5)); Ok(()) } ``` ## Building from Source You can use this command to clone the repository: ```bash git clone --recursive https://github.com/meilisearch/heed.git cd heed cargo build ``` However, if you already cloned it and forgot to initialize the submodules, execute the following command: ```bash git submodule update --init ``` heed-types-0.20.1/src/bytes.rs000064400000000000000000000011331046102023000142340ustar 00000000000000use std::borrow::Cow; use heed_traits::{BoxedError, BytesDecode, BytesEncode}; /// Describes a byte slice `[u8]` that is totally borrowed and doesn't depend on /// any [memory alignment]. /// /// [memory alignment]: std::mem::align_of() pub enum Bytes {} impl<'a> BytesEncode<'a> for Bytes { type EItem = [u8]; fn bytes_encode(item: &'a Self::EItem) -> Result, BoxedError> { Ok(Cow::Borrowed(item)) } } impl<'a> BytesDecode<'a> for Bytes { type DItem = &'a [u8]; fn bytes_decode(bytes: &'a [u8]) -> Result { Ok(bytes) } } heed-types-0.20.1/src/decode_ignore.rs000064400000000000000000000006131046102023000156760ustar 00000000000000use heed_traits::BoxedError; /// A convenient struct made to ignore the type when decoding it. /// /// For example, it is appropriate to be used to count keys or to ensure that an /// entry exists. pub enum DecodeIgnore {} impl heed_traits::BytesDecode<'_> for DecodeIgnore { type DItem = (); fn bytes_decode(_bytes: &[u8]) -> Result { Ok(()) } } heed-types-0.20.1/src/integer.rs000064400000000000000000000043111046102023000145440ustar 00000000000000use std::borrow::Cow; use std::marker::PhantomData; use std::mem::size_of; use byteorder::{ByteOrder, ReadBytesExt}; use heed_traits::{BoxedError, BytesDecode, BytesEncode}; /// Encodable version of [`u8`]. 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) } } /// Encodable version of [`i8`]. 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) => { #[doc = "Encodable version of [`"] #[doc = stringify!($native)] #[doc = "`]."] pub struct $name(PhantomData); impl BytesEncode<'_> for $name { type EItem = $native; fn bytes_encode(item: &Self::EItem) -> Result, BoxedError> { let mut buf = vec![0; size_of::()]; O::$write_method(&mut buf, *item); Ok(Cow::from(buf)) } } 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); heed-types-0.20.1/src/lazy_decode.rs000064400000000000000000000021021046102023000153650ustar 00000000000000use std::marker; use heed_traits::BoxedError; /// Lazily decodes the data bytes. /// /// It can be used to avoid CPU-intensive decoding before making sure that it /// actually needs to be decoded (e.g. based on the key). #[derive(Default)] 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]) -> Result { Ok(Lazy { data: bytes, _phantom: marker::PhantomData }) } } /// Owns bytes that can be decoded on demand. #[derive(Copy, Clone)] pub struct Lazy<'a, C> { data: &'a [u8], _phantom: marker::PhantomData, } impl<'a, C: heed_traits::BytesDecode<'a>> Lazy<'a, C> { /// Change the codec type of the given bytes, specifying the new codec. pub fn remap(&self) -> Lazy<'a, NC> { Lazy { data: self.data, _phantom: marker::PhantomData } } /// Decode the given bytes as `DItem`. pub fn decode(&self) -> Result { C::bytes_decode(self.data) } } heed-types-0.20.1/src/lib.rs000064400000000000000000000017341046102023000136630ustar 00000000000000#![doc( html_favicon_url = "https://raw.githubusercontent.com/meilisearch/heed/main/assets/heed-pigeon.ico?raw=true" )] #![doc( html_logo_url = "https://raw.githubusercontent.com/meilisearch/heed/main/assets/heed-pigeon-logo.png?raw=true" )] //! Types that can be used to serialize and deserialize types inside databases. #![warn(missing_docs)] mod bytes; mod decode_ignore; mod integer; mod lazy_decode; mod str; mod unit; #[cfg(feature = "serde-bincode")] mod serde_bincode; #[cfg(feature = "serde-json")] mod serde_json; #[cfg(feature = "serde-rmp")] mod serde_rmp; pub use self::bytes::Bytes; pub use self::decode_ignore::DecodeIgnore; pub use self::integer::*; pub use self::lazy_decode::{Lazy, LazyDecode}; #[cfg(feature = "serde-bincode")] pub use self::serde_bincode::SerdeBincode; #[cfg(feature = "serde-json")] pub use self::serde_json::SerdeJson; #[cfg(feature = "serde-rmp")] pub use self::serde_rmp::SerdeRmp; pub use self::str::Str; pub use self::unit::Unit; heed-types-0.20.1/src/serde_bincode.rs000064400000000000000000000016071046102023000157010ustar 00000000000000use std::borrow::Cow; use heed_traits::{BoxedError, BytesDecode, BytesEncode}; use serde::{Deserialize, Serialize}; /// Describes a type that is [`Serialize`]/[`Deserialize`] and uses `bincode` to do so. /// /// It can borrow bytes from the original slice. pub struct SerdeBincode(std::marker::PhantomData); impl<'a, T: 'a> BytesEncode<'a> for SerdeBincode where T: Serialize, { type EItem = T; fn bytes_encode(item: &'a Self::EItem) -> Result, BoxedError> { bincode::serialize(item).map(Cow::Owned).map_err(Into::into) } } impl<'a, T: 'a> BytesDecode<'a> for SerdeBincode where T: Deserialize<'a>, { type DItem = T; fn bytes_decode(bytes: &'a [u8]) -> Result { bincode::deserialize(bytes).map_err(Into::into) } } unsafe impl Send for SerdeBincode {} unsafe impl Sync for SerdeBincode {} heed-types-0.20.1/src/serde_json.rs000064400000000000000000000015721046102023000152500ustar 00000000000000use std::borrow::Cow; use heed_traits::{BoxedError, BytesDecode, BytesEncode}; use serde::{Deserialize, Serialize}; /// Describes a type that is [`Serialize`]/[`Deserialize`] and uses `serde_json` to do so. /// /// It can borrow bytes from the original slice. pub struct SerdeJson(std::marker::PhantomData); impl<'a, T: 'a> BytesEncode<'a> for SerdeJson where T: Serialize, { type EItem = T; fn bytes_encode(item: &Self::EItem) -> Result, BoxedError> { serde_json::to_vec(item).map(Cow::Owned).map_err(Into::into) } } impl<'a, T: 'a> BytesDecode<'a> for SerdeJson where T: Deserialize<'a>, { type DItem = T; fn bytes_decode(bytes: &'a [u8]) -> Result { serde_json::from_slice(bytes).map_err(Into::into) } } unsafe impl Send for SerdeJson {} unsafe impl Sync for SerdeJson {} heed-types-0.20.1/src/serde_rmp.rs000064400000000000000000000015621046102023000150740ustar 00000000000000use std::borrow::Cow; use heed_traits::{BoxedError, BytesDecode, BytesEncode}; use serde::{Deserialize, Serialize}; /// Describes a type that is [`Serialize`]/[`Deserialize`] and uses `rmp_serde` to do so. /// /// It can borrow bytes from the original slice. pub struct SerdeRmp(std::marker::PhantomData); impl<'a, T: 'a> BytesEncode<'a> for SerdeRmp where T: Serialize, { type EItem = T; fn bytes_encode(item: &Self::EItem) -> Result, BoxedError> { rmp_serde::to_vec(item).map(Cow::Owned).map_err(Into::into) } } impl<'a, T: 'a> BytesDecode<'a> for SerdeRmp where T: Deserialize<'a>, { type DItem = T; fn bytes_decode(bytes: &'a [u8]) -> Result { rmp_serde::from_slice(bytes).map_err(Into::into) } } unsafe impl Send for SerdeRmp {} unsafe impl Sync for SerdeRmp {} heed-types-0.20.1/src/str.rs000064400000000000000000000010051046102023000137140ustar 00000000000000use std::borrow::Cow; use std::str; use heed_traits::{BoxedError, BytesDecode, BytesEncode}; /// Describes a [`prim@str`]. pub enum Str {} impl BytesEncode<'_> for Str { type EItem = str; 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]) -> Result { str::from_utf8(bytes).map_err(Into::into) } } heed-types-0.20.1/src/unit.rs000064400000000000000000000017051046102023000140720ustar 00000000000000use std::borrow::Cow; use std::{error, fmt}; use heed_traits::{BoxedError, BytesDecode, BytesEncode}; /// Describes the unit `()` type. pub enum Unit {} impl BytesEncode<'_> for Unit { type EItem = (); fn bytes_encode(_item: &Self::EItem) -> Result, BoxedError> { Ok(Cow::Borrowed(&[])) } } impl BytesDecode<'_> for Unit { type DItem = (); fn bytes_decode(bytes: &[u8]) -> Result { if bytes.is_empty() { Ok(()) } else { Err(NonEmptyError.into()) } } } /// The slice of bytes is non-empty and therefore is not a unit `()` type. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct NonEmptyError; impl fmt::Display for NonEmptyError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str("the slice of bytes is non-empty and therefore is not a unit `()` type") } } impl error::Error for NonEmptyError {}