(dbm) - Generic DBM interface

Library (dbm)

The library provides the generic interface to access DBM.

Sagittarius currently supports following DBM implementation;

(dbm dumb)

DBM-like library. Inspired by Python's dbm.dumb. This library must be used as the last resort. It has poor performance and memory usage.

The following code shows a typical usage;

(import (dbm))

;; Open the database
(define *db* (dbm-open (dbm-type->class 'dumb) :path "dumb.db"))

;; Put the value to the database
(dbm-put! *db* "key1" "value1")

;; Get the value from the database
(dbm-get *db* "key1")

;; Iterate over the database
(dbm-for-each *db* (lambda (key val) #| do something useful |#))

;; Close the database
(dbm-close *db*)

Opening and closing a dbm database

Class <dbm>

An abstract class for DBM-like database. The class has the following slots. It must be filled by dbm-open.

path

Pathname of the dbm database.

rw-mode

Specifies read/write mode. Can be one of the following keywords:

read

The database will be opened in read-only mode.

write

The database will be opened in read-write mode. If the database file does not exist, dbm-open creates one.

write

The database will be opened in read-write mode. If the database file exists, dbm-open truncates it. The keywords are indication so actual implementation may not behave as it described.

key-convert
value-convert

By default, you can use only strings for both key and values. With this option, however, you can specify how to convert other Scheme values to/from string to be stored in the database. The possible values are the followings: #f

The default value. Keys (values) are not converted. They must be a string. #t

Keys (values) are converted to its string representation, using write/ss, to store in the database and convert back Scheme values, using read/ss, to retrieve from the database. a list of two procedures

Both procedure must take a single argument. The first procedure must receive a Scheme object and returns a string. It is used to convert the keys (values) to store in the database. The second procedure must receive a string and returns a Scheme object. It is used to convert the stored data in the database to a Scheme object.

Metaclass <dbm-meta>

A metaclass of <dbm> and its subclasses.

Opens a dbm database. dbm must be an instance of one of the concrete classes derived from the <dbm>.

A convenient method that creates dbm instance and opens it.

Closes a dbm database dbm. If the database is not closed, then the database file may not be synchronised properly. So it is user's responsibility to close it.

Returns true if the dbm database dbm is closed, otherwise #f.

The returned value may be non boolean value.

Returns DBM class if DBM implementation dbmtype exists, otherwise #f.

The dbmtype must be a symbol that names the type of dbm implementation, and the implementation library name must be (dbm _dbmtype_). For example, to get the foo DBM then the library name must be (dbm foo).

Accessing a dbm database

Once a database is opened, you can use the following methods to access individual key/value pairs.

Put a value with key

Get a value associated with key. If no value exists for _key_and default is specified, it will be returned. If no value exists for key and default is not specified, then an error will be raised.

Return true value if a value exists for key, #f otherwise.

Delete a value associated with key.

Iterating on a dbm database

To walk over the entire database, following methods are provided.

The basic iterator. For each key/value pair, procedure is called as _procedure_ key value r, where r is knil for the first call of procedure, and the return value of the previous call for subsequent calls. Returns the result of the last call of procedure. If no data is in the database, knil is returned.

For each key/value pair in the database dbm, procedure is called. The procedure must accept 2 arguments, a key and a value respectively. The result of procedure is discarded.

For each key/value pair in the database dbm, procedure is called. The procedure must accept 2 arguments, a key and a value respectively. The result of procedure is accumulated to a list which is returned as a result of dbm-map.

Managing dbm database instance

Returns #t if a database of class class specified by _name_exists.

Removes an entire database of class class specified by name.

Copy a database of class specified by from to to.

Moves or renames a database of class specified by _from_to to.