Overview
The DB library is a family of classes that provides a modular programming interface to transactions and record-oriented file access. The library includes support for transactions, locking, logging and file page caching, as well as various indexed access methods. Many of the classes (e.g., the file page caching class) are useful independent of the other DB classes, although some classes are explicitly based on other classes (e.g., transactions and logging). For a general description of the DB package, see db_intro(3).
This manual page describes the Db class, which is the center of access activity.
The currently supported file formats are btree, hashed and recno. The btree format is a representation of a sorted, balanced tree structure. The hashed format is an extensible, dynamic hashing scheme. The recno format supports fixed or variable length records (optionally retrieved from a flat text file).
Storage and retrieval for the Db access methods are based on key/data pairs, using the Dbt class. See Dbt(3) for specific information on the structure and capabilities of a Dbt.
The Db::open method opens the database represented by file for both reading and writing. Files never intended to be shared or preserved on disk may be created by setting the file parameter to NULL.
The Db::open method copies a pointer to a Db object into the memory location referenced by dbpp. The methods of this object allow you to perform various database actions, as described below. The Db::open method throws a DbException(3) or returns the value of errno on failure and 0 on success.
Note, while most of the access methods use file as the name of an underlying file on disk, this is not guaranteed. Also, calling Db::open is a reasonably expensive operation. (This is based on a model where the DBMS keeps a set of files open for a long time rather than opening and closing them on each query.)
The type argument is of type DBTYPE (as defined in the <db2/db_cxx.h> include file) and must be set to one of DB_BTREE, DB_HASH, DB_RECNO or DB_UNKNOWN. If type is DB_UNKNOWN, the database must already exist and Db::open will then determine if it is of type DB_BTREE, DB_HASH or DB_RECNO.
The flags and mode arguments specify how files will be opened and/or created when they don't already exist. The flags value is specified by or'ing together one or more of the following values:
- DB_CREATE
-
Create any underlying files, as necessary. If the files do not already exist and the DB_CREATE flag is not specified, the call will fail.
-
- DB_NOMMAP
-
Do not map this file (see DbMpool(3) for further information).
-
- DB_RDONLY
-
Open the database for reading only. Any attempt to write the database using the access methods will fail regardless of the actual permissions of any underlying files.
-
- DB_THREAD
-
Cause the Db handle returned by the Db::open method to be useable by multiple threads within a single address space, i.e., to be ``free-threaded''.
-
- DB_TRUNCATE
-
``Truncate'' the database if it exists, i.e., behave as if the database were just created, discarding any previous contents.
All files created by the access methods are 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 DB.
See DbEnv(3) for a description of the dbenv argument, and DbInfo(3) for a description of the dbinfo argument.
Errors
Methods marked as returning errno will, by default, throw an exception that encapsulates the error information. The default error behavior can be changed, see DbException(3).
The Db::open method may fail and throw a DbException(3) or return errno for any of the errors specified for the following DB and library functions: Db::sync(3), DbLock::get(3), DbLock::put(3), DbLockTab::id(3), DbLockTab::vec(3), DbLog::db_register(3), DbLog::put(3), DbMpool::close(3), DbMpool::db_register(3), DbMpool::open(3), DbMpoolFile::close(3), DbMpoolFile::get(3), DbMpoolFile::open(3), DbMpoolFile::put(3), DbMpoolFile::set(3), DbMpoolFile::sync(3), calloc(3), close(2), fcntl(2), fflush(3), malloc(3), memcpy(3), memmove(3), memset(3), mmap(2), munmap(2), open(2), read(2), realloc(3), sigfillset(3), sigprocmask(2), stat(2), strcpy(3), strdup(3), strerror(3), strlen(3), time(3), and unlink(2).
In addition, the Db::open method may fail and throw a DbException(3) or return errno for the following conditions:
- [EAGAIN]
-
A lock was unavailable.
- [EINVAL]
-
An invalid flag value or parameter was specified (e.g., unknown database type, page size, hash method, recno pad byte, byte order) or a flag value or parameter that is incompatible with the current file specification.
The DB_THREAD flag was specified and spinlocks are not implemented for this architecture.
There is a mismatch between the version number of file and the software.
A re_source file was specified with either the DB_THREAD flag or a non-NULL tx_info field in the DbEnv argument to Db::open.
- [ENOENT]
-
A non-existent re_source file was specified.
- [EPERM]
-
Database corruption was detected. All subsequent database calls (other than Db::close) will return EPERM.
The Db::close method may fail and throw a DbException(3) or return errno for any of the errors specified for the following DB and library functions: Db::sync(3), DbLock::get(3), DbLock::put(3), DbLockTab::vec(3), DbLog::db_register(3), DbLog::put(3), DbMpool::close(3), DbMpoolFile::close(3), DbMpoolFile::get(3), DbMpoolFile::put(3), DbMpoolFile::set(3), DbMpoolFile::sync(3), calloc(3), close(2), fflush(3), malloc(3), memcpy(3), memmove(3), memset(3), munmap(2), realloc(3), and strerror(3).
The Db::cursor method may fail and throw a DbException(3) or return errno for any of the errors specified for the following DB and library functions: calloc(3).
In addition, the Db::cursor method may fail and throw a DbException(3) or return errno for the following conditions:
- [EINVAL]
-
An invalid flag value or parameter was specified.
- [EPERM]
-
Database corruption was detected. All subsequent database calls (other than Db::close) will return EPERM.
The Db::del method may fail and throw a DbException(3) or return errno for any of the errors specified for the following DB and library functions: DbLock::get(3), DbLock::put(3), DbLockTab::id(3), DbLockTab::vec(3), DbLog::put(3), DbMpoolFile::get(3), DbMpoolFile::put(3), DbMpoolFile::set(3), calloc(3), fcntl(2), fflush(3), malloc(3), memcmp(3), memcpy(3), memmove(3), memset(3), realloc(3), and strerror(3).
In addition, the Db::del method may fail and throw a DbException(3) or return errno for the following conditions:
- [EAGAIN]
-
A lock was unavailable.
- [EINVAL]
-
An invalid flag value or parameter was specified.
- [EPERM]
-
Database corruption was detected. All subsequent database calls (other than Db::close) will return EPERM.
In addition, the Db::fd method may fail and throw a DbException(3) or return errno for the following conditions:
- [ENOENT]
-
The Db::fd method was called for an in-memory database, or no underlying file has yet been created.
- [EPERM]
-
Database corruption was detected. All subsequent database calls (other than Db::close) will return EPERM.
The Db::get method may fail and throw a DbException(3) or return errno for any of the errors specified for the following DB and library functions: DbLock::get(3), DbLock::put(3), DbLockTab::id(3), DbLockTab::vec(3), DbLog::put(3), DbMpoolFile::get(3), DbMpoolFile::put(3), DbMpoolFile::set(3), Dbc::get(3), calloc(3), fcntl(2), fflush(3), malloc(3), memcmp(3), memcpy(3), memmove(3), memset(3), realloc(3), and strerror(3).
In addition, the Db::get method may fail and throw a DbException(3) or return errno for the following conditions:
- [EAGAIN]
-
A lock was unavailable.
- [EINVAL]
-
An invalid flag value or parameter was specified.
The DB_THREAD flag was specified to the Db::open(3) method and neither the DB_DBT_MALLOC or DB_DBT_USERMEM flags were set in the Dbt.
A record number of 0 was specified.
- [EPERM]
-
Database corruption was detected. All subsequent database calls (other than Db::close) will return EPERM.
The Db::put method may fail and throw a DbException(3) or return errno for any of the errors specified for the following DB and library functions: DbLock::get(3), DbLock::put(3), DbLockTab::id(3), DbLockTab::vec(3), DbLog::put(3), DbMpoolFile::get(3), DbMpoolFile::put(3), DbMpoolFile::set(3), calloc(3), fcntl(2), fflush(3), malloc(3), memcmp(3), memcpy(3), memmove(3), memset(3), realloc(3), and strerror(3).
In addition, the Db::put method may fail and throw a DbException(3) or return errno for the following conditions:
- [EACCES]
-
An attempt was made to modify a read-only database.
- [EAGAIN]
-
A lock was unavailable.
- [EINVAL]
-
An invalid flag value or parameter was specified.
A record number of 0 was specified.
An attempt was made to add a record to a fixed-length database that was too large to fit.
An attempt was made to do a partial put.
- [EPERM]
-
Database corruption was detected. All subsequent database calls (other than Db::close) will return EPERM.
- [ENOSPC]
-
A btree exceeded the maximum btree depth (255).
The Db::stat method may fail and throw a DbException(3) or return errno for any of the errors specified for the following DB and library functions: DbLock::get(3), DbLock::put(3), DbLockTab::id(3), DbLockTab::vec(3), DbMpoolFile::get(3), DbMpoolFile::put(3), calloc(3), fcntl(2), fflush(3), malloc(3), memcpy(3), and memset(3).
The Db::sync method may fail and throw a DbException(3) or return errno for any of the errors specified for the following DB and library functions: Db::get(3), Db::sync(3), DbLock::get(3), DbLock::put(3), DbLockTab::id(3), DbLockTab::vec(3), DbLog::put(3), DbMpoolFile::get(3), DbMpoolFile::put(3), DbMpoolFile::set(3), DbMpoolFile::sync(3), calloc(3), close(2), fcntl(2), fflush(3), malloc(3), memcpy(3), memmove(3), memset(3), munmap(2), open(2), realloc(3), strerror(3), unlink(2), and write(2).
In addition, the Db::sync method may fail and throw a DbException(3) or return errno for the following conditions:
- [EINVAL]
-
An invalid flag value or parameter was specified.
- [EPERM]
-
Database corruption was detected. All subsequent database calls (other than Db::close) will return EPERM.