chickadee » sdbm

Outdated egg!

This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.

If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.

sdbm

sdbm is a clone of the SDBM database library.

Overview

sdbm is a reimplementation of the public-domain SDBM database library, which is itself essentially a clone of NDBM. SDBM provides a simple key-value store with a fixed limit on data length and no ACID semantics to speak of, providing no write locking, no atomicity, no transactions, and little guarantee that your file won't be corrupted in a crash. It also relies on sparse file support, which is not present on filesystems such as HFS+.

Despite these shortcomings, it is a simple implementation without dependencies, written completely in Scheme. And some issues with the original implementation have been remedied: byte order is configurable, and page and directory block size can be adjusted at runtime. Therefore, sdbm might still be useful as a very simple key-value store for non-critical applications.

Installation

Use chicken-install as usual. But some configuration can be done by defining certain features at compile-time.

Byte order
sdbm-little-endian or sdbm-big-endian set the read and write order of bytes in the file. If no byte order is specified, host order is used, as in the original implementation.
Hash function
sdbm-hash-djb selects an alternate hash function by Dan Bernstein. If no hash function is specified, the native SDBM hash function is used.

To define a feature, set it in CSC_OPTIONS before calling chicken-install:

CSC_OPTIONS="-Dsdbm-hash-djb -Dsdbm-big-endian" chicken-install sdbm

will configure sdbm to use the DJB hash and big-endian order.

Basic interface

open-database pathname #!key flags mode page-block-power dir-block-powerprocedure

Opens existing SDBM database pathname or creates an empty database if pathname does not exist. The database resides in two files: pathname.dir (directory file) and pathname.pag (page file). Returns an opaque database object.

Optional keyword arguments are:

flags
flags passed to file-open, default: (+ open/rdwr open/creat)
mode
permissions passed to file-open, default: (+ perm/irwxu perm/irgrp perm/iroth)
page-block-power
bytes in each data page, as a power of 2; default: 12 (4096 bytes)
dir-block-power
bytes in each directory block, as a power of 2; default: 12 (4096 bytes)

The data page size limits the length of a key/value pair, so you may need to increase it to correspond with your maximum pair size. An undersized page can lead to frequent hash bucket splits and a bloated file size with many holes. An oversized page can incur disk performance overhead on read and write, since an entire page is read or written for every operation. Values between 4096 and 16384 bytes seem reasonable.

Note: The SDBM format has no database header, so you must always specify the same page-block-power and dir-block-power for a given database. The reference implementation uses page-block-power of 10 (1024 bytes) and dir-block-power of 12 (4096 bytes).

close-database dbprocedure

Close database associated with db.

fetch db keyprocedure

Fetch key from SDBM database db, returning the associated value or #f if the key did not exist. The returned value is a string. key is normally a string; if not, it is converted into a string.

store! db key val #!optional (replace #t)procedure

Store key, val pair into SDBM database db. val must be a string; key is converted into a string if not already.

If the key exists, and optional argument replace is #t (the default) then the pair will be replaced. If replace is #f instead, an error is returned.

delete! db keyprocedure

Delete key from SDBM database db. If key does not exist, an error is raised.

Enumeration

pair-iterator dbprocedure

Return a new pair iterator object that can be used to iterate over pairs in the SDBM database db. Pass this iterator to next-pair repeatedly.

next-pair iterprocedure

Return the next pair that iter, a pair-iterator, sees in the database. If there are no more pairs, returns #f. Otherwise, it returns a (key . val) pair, where both values are strings.

pair-fold db kons knilprocedure

Perform a fold over all pairs in SDBM database db. knil is the initial value. kons is a procedure of three arguments: (key val kvs). The return value of kons is passed to the next execution of kons in kvs.

Author

Jim Ursetto

Version history

0.1.0
Initial release

License

BSD

Contents »