Skip to main content

Crate db185

Crate db185 

Source
Expand description

Read and write Berkeley DB 1.85 btree files - the on-disk format used by 4.4BSD’s dbopen(3) and still shipped today in NetBSD libc and pkgsrc’s libnbcompat. Hash and recno access methods are out of scope.

§Reader

Db opens an existing file, retrieves values by key, and iterates all key/value pairs in sorted order via Iter. Files written in either byte order are accepted.

§Writer

Writer builds a new file from scratch via a sequence of Writer::put / Writer::del operations, flushed on Writer::finish. It does not modify existing files, and writes native-endian only (the reader handles both byte orders).

Each key/value pair must fit in a single page (~4 KiB); larger entries return Error::EntryTooLarge. This is enough to rebuild pkgsrc’s pkgdb.byfile.db, but means this is not a full Berkeley DB 1.85 writer.

§Errors

Fallible operations return Result<T>. Error distinguishes I/O failures from on-disk format problems (bad magic, unsupported version, corrupt pages).

§Example

use db185::Db;

let db = Db::open("/var/db/pkg/pkgdb.byfile.db")?;
if let Some(value) = db.get(b"/opt/pkg/bin/foo\0")? {
    println!("{}", String::from_utf8_lossy(value.as_ref()));
}
for entry in &db {
    let entry = entry?;
    println!(
        "{} -> {}",
        String::from_utf8_lossy(entry.key()),
        String::from_utf8_lossy(entry.value()),
    );
}

Structs§

Db
Open handle on a btree database file.
Entry
A key/value pair yielded by Iter.
Iter
Forward iterator over a Db. Yields Result<Entry>; iteration stops on the first error.
Writer
Builder for a new btree file.

Enums§

Error
Errors returned by this crate.

Type Aliases§

Result
Result alias for fallible operations in this crate.