Skip to main content

Db

Struct Db 

Source
pub struct Db { /* private fields */ }
Expand description

Open handle on a btree database file.

Wraps a read-only mmap of the file plus the parameters decoded from its meta page (page size, host/foreign byte order).

Because the file is held via mmap, the caller is responsible for ensuring it is not mutated by another process for the lifetime of the Db. External writes can observably change bytes the API hands out and would constitute undefined behaviour at the mmap layer. For the typical pkgsrc use case - a single short-lived reader over pkgdb.byfile.db - this is trivially satisfied.

Implementations§

Source§

impl Db

Source

pub fn open(path: impl AsRef<Path>) -> Result<Self>

Open path for reading.

Maps the file with mmap(2) and validates the meta page. Recno trees are rejected; btree pages themselves are validated lazily on first access.

See the Db type-level documentation for the mmap concurrent-writer invariant the caller must uphold.

§Errors

Returns Error::Io if the file cannot be opened or mapped, or Error::ShortFile if it is too small to contain a meta page. Returns Error::BadMagic, Error::BadVersion, Error::BadPageSize, Error::UnsupportedFlags or Error::UnalignedFileLength when the meta page is recognisable but its contents fall outside the supported subset.

Source

pub fn get(&self, key: &[u8]) -> Result<Option<Cow<'_, [u8]>>>

Look up a key.

Returns Ok(Some(value)) on hit and Ok(None) if the key is absent. Inline values are returned as Cow::Borrowed slices into the mmap; overflow values are materialised into Cow::Owned. Keys are compared as raw byte strings with unsigned byte-by-byte ordering and shorter-wins (matching __bt_defcmp in bt_utils.c, which is equivalent to <[u8] as Ord>::cmp).

§Errors

Returns an Error on I/O failure or on-disk corruption encountered while descending the tree.

Source

pub const fn iter(&self) -> Iter<'_>

Begin a forward iteration over all key/value pairs in sorted order. Equivalent to seq(R_FIRST) followed by repeated seq(R_NEXT) in the historical dbopen(3) API.

Trait Implementations§

Source§

impl Debug for Db

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> IntoIterator for &'a Db

Source§

type Item = Result<Entry<'a>, Error>

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl Freeze for Db

§

impl RefUnwindSafe for Db

§

impl Send for Db

§

impl Sync for Db

§

impl Unpin for Db

§

impl UnsafeUnpin for Db

§

impl UnwindSafe for Db

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.