Skip to main content

Writer

Struct Writer 

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

Builder for a new btree file.

Writer::create_new(path) initialises the in-memory state with an empty root leaf; finish() flushes that plus the meta page to disk. Dropping a Writer without calling finish() leaves the newly-created file empty on disk - nothing is written until finish.

Implementations§

Source§

impl Writer

Source

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

Create a new file at path and stage an empty btree.

Mirrors std::fs::File::create_new: the file is opened with create_new, so an existing file at path causes an I/O error. Nothing is written to disk until Writer::finish is called.

§Errors

Returns Error::Io if the file cannot be created.

Source

pub fn del(&mut self, key: &[u8]) -> Result<bool>

Delete the entry for key from the tree.

Returns true if an entry was removed, false if key wasn’t present. Mirrors __bt_delete / __bt_dleaf from libnbcompat: the leaf is compacted in place and the tree’s nrecs field is left alone (the btree access method ignores it). Page underflow doesn’t trigger merges - the BSD btree just leaves underfull pages alone.

§Errors

Returns an Error if any in-memory page fails its own decoder invariants while descending to find key. That would only happen if the writer’s own state were corrupted, which indicates a bug in this crate, not bad input.

§Panics

Panics if the writer’s internal page map is inconsistent (i.e. a page reached via descent isn’t present in the map). Reaching this branch indicates a bug in this crate.

Source

pub fn put(&mut self, key: &[u8], val: &[u8]) -> Result<bool>

Insert key / val into the tree.

Mirrors dbopen(3)’s R_NOOVERWRITE semantics: returns Ok(true) on a successful insert, or Ok(false) if key is already present (in which case the existing value is left untouched). Matches std::collections::HashSet::insert.

§Errors

Returns an Error if any in-memory page fails its own decoder invariants while descending to find the insertion point. That would only happen if the writer’s own state were corrupted, which indicates a bug in this crate, not bad input.

§Panics

Panics if the writer’s internal page map is inconsistent (i.e. a page reached via descent isn’t present in the map). Reaching this branch indicates a bug in this crate.

Source

pub fn finish(self) -> Result<()>

Flush all buffered pages to disk, consuming the writer.

Pages are written in pgno order through a single buffered writer (no per-page seeks): the in-memory arena is one contiguous psize-aligned buffer indexed by pgno, so finish writes the meta page into slot 0 and then flushes the whole buffer as a single write_all.

No fsync is issued: this matches libnbcompat’s dbopen, where the close path just unmaps and flushes userspace buffers. Callers that need on-disk durability should fsync the containing directory themselves.

§Errors

Returns Error::Io if any of the writes fail.

§Panics

Panics if the arena is not a positive whole multiple of psize bytes. This is a writer-internal invariant established at construction; a failure here indicates a bug in this crate, and asserting rather than silently truncating is the only safe choice at the persistence boundary.

Auto Trait Implementations§

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.