Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add missing documentation. #177

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions heed-traits/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//! Crate `heed-traits` contains the traits used to encode and decode database content.

#![warn(missing_docs)]

use std::borrow::Cow;
use std::error::Error as StdError;

Expand All @@ -6,14 +10,18 @@ pub type BoxedError = Box<dyn StdError + Send + Sync + 'static>;

/// A trait that represents an encoding structure.
pub trait BytesEncode<'a> {
/// The type to encode
type EItem: ?Sized + 'a;

/// Encode the given item as bytes
fn bytes_encode(item: &'a Self::EItem) -> Result<Cow<'a, [u8]>, BoxedError>;
}

/// A trait that represents a decoding structure.
pub trait BytesDecode<'a> {
/// The type to decode
type DItem: 'a;

/// Decode the given bytes as DItem
fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError>;
}
6 changes: 6 additions & 0 deletions heed-types/src/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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 {
Expand All @@ -23,6 +24,7 @@ impl BytesDecode<'_> for U8 {
}
}

/// Encodable version of [`i8`]
pub struct I8;

impl BytesEncode<'_> for I8 {
Expand All @@ -43,6 +45,10 @@ impl BytesDecode<'_> for I8 {

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<O>(PhantomData<O>);

impl<O: ByteOrder> BytesEncode<'_> for $name<O> {
Expand Down
2 changes: 2 additions & 0 deletions heed-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
//! [`Serialize`]: serde::Serialize
//! [`Deserialize`]: serde::Deserialize

#![warn(missing_docs)]

mod cow_slice;
mod cow_type;
mod integer;
Expand Down
1 change: 1 addition & 0 deletions heed/src/lazy_decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct Lazy<'a, C> {
}

impl<'a, C: heed_traits::BytesDecode<'a>> Lazy<'a, C> {
/// Decode the given bytes as DItem
pub fn decode(&self) -> Result<C::DItem, BoxedError> {
C::bytes_decode(self.data)
}
Expand Down
8 changes: 8 additions & 0 deletions heed/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
//! assert_eq!(ret, Some(5));
//! # Ok(()) }
//! ```
#![warn(missing_docs)]

mod cursor;
mod db;
Expand Down Expand Up @@ -79,12 +80,19 @@ pub use self::txn::{RoTxn, RwTxn};
/// An error that encapsulates all possible errors in this crate.
#[derive(Debug)]
pub enum Error {
/// I/O error: can come from the std or be a rewrapped [`MdbError`]
Io(io::Error),
/// Lmdb error
Mdb(MdbError),
/// Encoding error
Encoding(BoxedError),
/// Decoding error
Decoding(BoxedError),
/// Incoherent types when opening a database
InvalidDatabaseTyping,
/// Database closing in progress
DatabaseClosing,
/// Attempt to open Env with different options
BadOpenOptions {
/// The options that were used to originally open this env.
options: EnvOpenOptions,
Expand Down
1 change: 1 addition & 0 deletions heed/src/mdb/lmdb_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub enum Error {
}

impl Error {
/// Return true if the given error is [`Error::NotFound`]
pub fn not_found(&self) -> bool {
*self == Error::NotFound
}
Expand Down
11 changes: 11 additions & 0 deletions heed/src/mdb/lmdb_flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@ use lmdb_master_sys as ffi;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum Flag {
/// mmap at a fixed address (experimental)
Fixedmap = ffi::MDB_FIXEDMAP,
/// no environment directory
NoSubDir = ffi::MDB_NOSUBDIR,
/// don't fsync after commit
NoSync = ffi::MDB_NOSYNC,
/// read only
RdOnly = ffi::MDB_RDONLY,
/// don't fsync metapage after commit
NoMetaSync = ffi::MDB_NOMETASYNC,
/// use writable mmap
WriteMap = ffi::MDB_WRITEMAP,
/// use asynchronous msync when MDB_WRITEMAP is used
MapAsync = ffi::MDB_MAPASYNC,
/// tie reader locktable slots to MDB_txn objects instead of to threads
NoTls = ffi::MDB_NOTLS,
/// don't do any locking, caller must manage their own locks
NoLock = ffi::MDB_NOLOCK,
/// don't do readahead (no effect on Windows)
NoRdAhead = ffi::MDB_NORDAHEAD,
/// don't initialize malloc'd memory before writing to datafile
NoMemInit = ffi::MDB_NOMEMINIT,
}
4 changes: 4 additions & 0 deletions heed/src/txn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,16 @@ impl<'p> RwTxn<'p> {
self.txn.env.env_mut_ptr()
}

/// Commit all the operations of a transaction into the database.
/// The transaction is reset.
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)
}

/// Abandon all the operations of the transaction instead of saving them.
/// The transaction is reset.
pub fn abort(mut self) {
abort_txn(self.txn.txn);
self.txn.txn = ptr::null_mut();
Expand Down