Berkeley DB: dbm/ndbm
Description: Berkeley DB: An embedded database programmatic toolkit.
Keywords: embedded,database,programmatic,toolkit,b+tree,btr ee,hash,hashing,transaction,transactions,locking,logging,access method,access me thods,java,C,C++

# dbm/ndbm

`

###

#

#

    #define DB_DBM_HSEARCH    1
    #include <db.h>

    typedef struct {
    	char *dptr;
    	int dsize;
    } datum;

    Dbm Functions
    int
    dbminit(char *file);

    int
    dbmclose();

    datum
    fetch(datum key);

    int
    store(datum key, datum content);

    int
    delete(datum key);

    datum
    firstkey(void);

    datum
    nextkey(datum key);

    Ndbm Functions
    DBM *
    dbm_open(char *file, int flags, int mode);

    void
    dbm_close(DBM *db);

    datum
    dbm_fetch(DBM *db, datum key);

    int
    dbm_store(DBM *db, datum key, datum content, int flags);

    int
    dbm_delete(DBM *db, datum key);

    datum
    dbm_firstkey(DBM *db);

    datum
    dbm_nextkey(DBM *db);

    int
    dbm_error(DBM *db);

    int
    dbm_clearerr(DBM *db);

# Description

The dbm interfaces to the Berkeley DB library are intended to provide high-performance implementations and source code compatibility for applications written to historic interfaces. They are not recommended for any other purpose. The historic dbm database format is not supported, and databases previously built using the real dbm libraries cannot be read by the Berkeley DB functions.

To compile dbm applications, replace the application's #include of the dbm or ndbm include file (e.g., #include <dbm.h> or #include <ndbm.h>) with the following two lines:

    #define DB_DBM_HSEARCH    1
    #include <db.h>

and recompile. If the application attempts to load against a dbm library (e.g., -ldbm), remove the library from the load line.

Key and content arguments are objects described by the datum typedef. A datum specifies a string of dsize bytes pointed to by dptr. Arbitrary binary data, as well as normal text strings, is allowed.

# Dbm Functions

Before a database can be accessed, it must be opened by dbminit. This will open and/or create the database file.db. If created, the database file is created read/write by owner only (as described in chmod(2) and modified by the process' umask value at the time of creation (see umask(2)). The group ownership of created files is based on the system and directory defaults, and is not further specified by Berkeley DB.

A database may be closed, and any held resources released, by calling dbmclose.

Once open, the data stored under a key is accessed by fetch and data is placed under a key by store. A key (and its associated contents) is deleted by delete. A linear pass through all keys in a database may be made, in an (apparently) random order, by use of firstkey and nextkey. Firstkey will return the first key in the database. With any key nextkey will return the next key in the database.

The following code will traverse the data base:

    for (key = firstkey();
        key.dptr != NULL; key = nextkey(key)) {
    	...
    }

# Ndbm Functions

Before a database can be accessed, it must be opened by dbm_open. This will open and/or create the database file file.db depending on the flags parameter (see open(2)). If created, the database file is created with mode mode (as described in chmod(2)) and modified by the process' umask value at the time of creation (see umask(2)). The group ownership of created files is based on the system and directory defaults, and is not further specified by Berkeley DB.

Once open, the data stored under a key is accessed by dbm_fetch and data is placed under a key by dbm_store. The flags field can be either DBM_INSERT or DBM_REPLACE. DBM_INSERT will only insert new entries into the database and will not change an existing entry with the same key. DBM_REPLACE will replace an existing entry if it has the same key. A key (and its associated contents) is deleted by dbm_delete. A linear pass through all keys in a database may be made, in an (apparently) random order, by use of dbm_firstkey and dbm_nextkey. Dbm_firstkey will return the first key in the database. Dbm_nextkey will return the next key in the database. The following code will traverse the data base:

    for (key = dbm_firstkey(db);
        key.dptr != NULL; key = dbm_nextkey(db)) {
    	...
    }

# Compatibility Notes

The historic dbm library created two underlying database files, traditionally named file.dir and file.pag. The Berkeley DB library creates a single database file named file.db. Applications that are aware of the underlying database file names may require additional source code modifications.

The historic dbminit interface required that the underlying .dir and .pag files already exist (empty databases were created by first manually creating zero-length .dir and .pag files). Applications that expect to create databases using this method may require additional source code modifications.

The historic dbm_dirfno and dbm_pagfno macros are supported, but will return identical file descriptors as there is only a single underlying file used by the Berkeley DB hashing access method. Applications using both file descriptors for locking may require additional source code modifications.

If applications using the dbm interface exits without first closing the database, it may lose updates because the Berkeley DB library buffers writes to underlying databases. Such applications will require additional source code modifications to work correctly with the Berkeley DB library.

# Dbm Diagnostics

The dbminit function returns -1 on failure, setting errno, and 0 on success.

The fetch function sets the dptr field of the returned datum to NULL on failure, setting errno, and returns a non-NULL dptr on success.

The store function returns -1 on failure, setting errno, and 0 on success.

The delete function returns -1 on failure, setting errno, and 0 on success.

The firstkey function sets the dptr field of the returned datum to NULL on failure, setting errno, and returns a non-NULL dptr on success.

The nextkey function sets the dptr field of the returned datum to NULL on failure, setting errno, and returns a non-NULL dptr on success.

# Errors

If a fatal error occurs in Berkeley DB, the dbminit function may fail and return DB_RUNRECOVERY, at which point all subsequent database calls will also return DB_RUNRECOVERY.

The fetch function may fail and return errno for any of the errors specified for the following Berkeley DB and C library functions: dbm_fetch, and fprintf(3).

The store function may fail and return errno for any of the errors specified for the following Berkeley DB and C library functions: dbm_store, and fprintf(3).

The delete function may fail and return errno for any of the errors specified for the following Berkeley DB and C library functions: dbm_store, and fprintf(3).

The firstkey function may fail and return errno for any of the errors specified for the following Berkeley DB and C library functions: dbm_firstkey, and fprintf(3).

The nextkey function may fail and return errno for any of the errors specified for the following Berkeley DB and C library functions: dbm_nextkey, and fprintf(3).

# Ndbm Diagnostics

Dbm_error returns non-zero when an error has occurred reading or writing the database.

Dbm_clearerr resets the error condition on the named database.

The dbm_open function returns NULL on failure, setting errno, and 0 on success.

The dbm_fetch function sets the dptr field of the returned datum to NULL on failure, setting errno, and returns a non-NULL dptr on success.

The dbm_store function returns -1 on failure, setting errno, 0 on success, and 1 if DBM_INSERT was set and the specified key already existed in the database.

The dbm_delete function returns -1 on failure, setting errno, and 0 on success.

The dbm_firstkey function sets the dptr field of the returned datum to NULL on failure, setting errno, and returns a non-NULL dptr on success.

The dbm_nextkey function sets the dptr field of the returned datum to NULL on failure, setting errno, and returns a non-NULL dptr on success.

The dbm_error function returns -1 on failure, setting errno, and 0 on success.

The dbm_clearerr function returns -1 on failure, setting errno, and 0 on success.

# Errors

If a fatal error occurs in Berkeley DB, the dbm_open function may fail and return DB_RUNRECOVERY, at which point all subsequent database calls will also return DB_RUNRECOVERY.

The dbm_open function may fail and return errno for any of the errors specified for the following Berkeley DB and C library functions: DB->close, DB->cursor, db_open, memset(3), strcat(3), strcpy(3), and strlen(3).

The dbm_close function may fail and return errno for any of the errors specified for the following Berkeley DB and C library functions: dbc->dbp->close(3).

The dbm_fetch function may fail and return errno for any of the errors specified for the following Berkeley DB and C library functions: dbc->dbp->get(3), and memset(3).

The dbm_store function may fail and return errno for any of the errors specified for the following Berkeley DB and C library functions: dbc->dbp->put(3), and memset(3).

The dbm_delete function may fail and return errno for any of the errors specified for the following Berkeley DB and C library functions: dbc->dbp->del(3), and memset(3).

The dbm_firstkey function may fail and return errno for any of the errors specified for the following Berkeley DB and C library functions: DBcursor->c_get, and memset(3).

The dbm_nextkey function may fail and return errno for any of the errors specified for the following Berkeley DB and C library functions: DBcursor->c_get, and memset(3).

`