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
impl Writer
Sourcepub fn create_new(path: impl AsRef<Path>) -> Result<Self>
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.
Sourcepub fn del(&mut self, key: &[u8]) -> Result<bool>
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.
Sourcepub fn put(&mut self, key: &[u8], val: &[u8]) -> Result<bool>
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.
Sourcepub fn finish(self) -> Result<()>
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.