Berkeley DB: db_open
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++

# db_open

`

###

    #include <db.h>

    int
    db_open(const char *file, DBTYPE type, u_int32_t flags,
        int mode, DB_ENV *dbenv, DB_INFO *dbinfo, DB **dbpp);

# Description

The currently supported Berkeley DB file formats (or access methods) are B+tree, Extended Linear Hashing, and Fixed and Variable-length records. Generally, these are referred to as the btree, hash and recno access methods. 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 Berkeley DB access methods are based on key/data pairs, or DBT structures.

The db_open function 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.

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, 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 if they do not already exist. The flags value is specified by logically 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 memp_open for further information).

DB_RDONLY
  Open the database for reading only. Any attempt to modify items in the database will fail regardless of the actual permissions of any underlying files.

DB_THREAD
  Cause the m4_reg(DB) handle returned by db_open to be useable by multiple threads within a single address space, i.e., to be free-threaded.

DB_TRUNCATE
  Logically 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 Berkeley DB.

When sharing a database environment with other processes, it is necessary to provide the access methods with database environment information. The db_env argument to db_open is intended for this purpose.

Additionally, there is access method specific information that may be specified when calling db_open. The db_info argument to db_open is intended for this purpose.

Before returning, the db_open function copies a pointer to a DB structure, into the memory location referenced by dbpp. This structure describes a database type and includes a set of functions to perform various database actions. Each of these functions takes a pointer to a DB structure, and may take a pointer to one or more DBT structures and a flag value as well. The set of functions are described in the following manual pages: DB->close, DB->cursor, DB->del, DB->fd, DB->get, DB->put and DB->sync.

In addition, the returned DB structure includes the following fields:

DBTYPE type;
  The type of the underlying access method (and file format). Set to one of DB_BTREE, DB_HASH or DB_RECNO. This field may be used to determine the type of the database after a return from db_open with the type argument set to DB_UNKNOWN.

int byteswapped;
  Set to 0 if the underlying database files were created on an architecture of the same byte order as the current one, and 1 if they were not (i.e., big-endian on a little-endian machine or vice-versa). This field may be used to determine if application data needs to be adjusted for this architecture or not.

The db_open function returns the value of errno on failure, and 0 on success.

# Environment Variables

DB_HOME
  If the dbenv argument to db_open was initialized using db_appinit the environment variable DB_HOME may be used as the path of the database home for the interpretation of the dir. Specifically, db_open is affected by the configuration string value of DB_DATA_DIR.

# Errors

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

The db_open function may fail and return errno for any of the errors specified for the following Berkeley DB and C library functions: DB->cursor, DB->sync, DBcursor->c_close(3), DBmemp->pgin(3), DBmemp->pgout(3), __account_page(3), abort(3), close(3), dbenv->db_paniccall(3), dbp->h_hash(3), fcntl(3), fflush(3), fprintf(3), free(3), fstat(3), fsync(3), func(3), getenv(3), getpid(3), getuid(3), isdigit(3), lock_get, lock_id, lock_put, lock_vec, log_compare, log_flush, log_put, log_register, log_unregister, lseek(3), malloc(3), memcmp(3), memcpy(3), memmove(3), memp_close, memp_fclose, memp_fget, memp_fopen, memp_fput, memp_fset, memp_fsync, memp_open, memp_register, memset(3), mmap(3), munmap(3), open(3), pread(3), pstat_getdynamic(3), pwrite(3), read(3), realloc(3), sigfillset(3), sigprocmask(3), stat(3), strerror(3), strlen(3), sysconf(3), time(3), unlink(3), vfprintf(3), vsnprintf(3), and write(3).

In addition, the db_open function may fail and 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 function, 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 DB_ENV argument to db_open.

ENOENT
  A non-existent re_source file was specified.

# See Also

db_appexit, db_appinit, db_version, DB->close, DB->cursor, DB->del, DB->fd, DB->get, DB->join, db_open, DB->put, DB->stat, DB->sync, DBcursor->c_close, DBcursor->c_del, DBcursor->c_get and DBcursor->c_put.`