DB->open

API Ref

#include <db.h>

int DB->open(DB *db, DB_TXN *txnid, const char *file, const char *database, DBTYPE type, u_int32_t flags, int mode);

int DB->get_dbname(DB *db, const char **filenamep, const char **dbnamep);

int DB->get_open_flags(DB *db, u_int32_t *flagsp);

int DB->get_transactional(DB *db);


Description: DB->open

The DB->open method opens the database represented by the file and database parameters for both reading and writing.

The currently supported Berkeley DB file formats (or access methods) are Btree, Hash, Queue, and Recno. The Btree format is a representation of a sorted, balanced tree structure. The Hash format is an extensible, dynamic hashing scheme. The Queue format supports fast access to fixed-length records accessed sequentially or by logical record number. The Recno format supports fixed- or variable-length records, accessed sequentially or by logical record number, and optionally backed by a flat text file.

Storage and retrieval for the Berkeley DB access methods are based on key/data pairs; see DBT for more information.

Calling DB->open is a relatively expensive operation, and maintaining a set of open databases will normally be preferable to repeatedly opening and closing the database for each new query.

The DB->open method returns a non-zero error value on failure and 0 on success. If DB->open fails, the DB->close method should be called to discard the DB handle.

Parameters

database
The database parameter is optional, and allows applications to have multiple databases in a single file. Although no database parameter needs to be specified, it is an error to attempt to open a second database in a file that was not initially created using a database name. Further, the database parameter is not supported by the Queue format. Finally, when opening multiple databases in the same physical file, it is important to consider locking and memory cache issues; see Opening multiple databases in a single file for more information.

In-memory databases never intended to be preserved on disk may be created by setting both the file and database parameters to NULL. Note that in-memory databases can only ever be shared by sharing the single database handle that created them, in circumstances where doing so is safe.

file
The file parameter is used as the name of an underlying file that will be used to back the database.

In-memory databases never intended to be preserved on disk may be created by setting both the file and database parameters to NULL. Note that in-memory databases can only ever be shared by sharing the single database handle that created them, in circumstances where doing so is safe.

flags
The flags parameter must be set to 0 or by bitwise inclusively OR'ing together one or more of the following values:

DB_AUTO_COMMIT
Enclose the DB->open call within a transaction. If the call succeeds, the open operation will be recoverable. If the call fails, no database will have been created.

DB_CREATE
Create the database. If the database does not already exist and the DB_CREATE flag is not specified, the DB->open will fail.

DB_DIRTY_READ
Support dirty reads; that is, read operations on the database may request the return of modified but not yet committed data. This flag must be specified on all DB handles used to perform dirty reads or database updates, otherwise requests for dirty reads may not be honored and the read may block.

DB_EXCL
Return an error if the database already exists. The DB_EXCL flag is only meaningful when specified with the DB_CREATE flag.

DB_NOMMAP
Do not map this database into process memory (see the DB_ENV->set_mp_mmapsize method 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 DB handle returned by DB->open to be free-threaded; that is, usable by multiple threads within a single address space.

DB_TRUNCATE
Physically truncate the underlying file, discarding all previous databases it might have held. Underlying filesystem primitives are used to implement this flag. For this reason, it is applicable only to the file and cannot be used to discard databases within a file.

The DB_TRUNCATE flag cannot be lock or transaction-protected, and it is an error to specify it in a locking or transaction-protected environment.

mode
On UNIX systems or in IEEE/ANSI Std 1003.1 (POSIX) environments, all files created by the database open 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)). If mode is 0, the database open will use a default mode of readable and writable by both owner and group. On Windows systems, the mode parameter is ignored. The group ownership of created files is based on the system and directory defaults, and is not further specified by Berkeley DB.

txnid
If the operation is to be transaction-protected, (other than by specifying the DB_AUTO_COMMIT flag), the txnid parameter is a transaction handle returned from DB_ENV->txn_begin; otherwise, NULL. Note that transactionally protected operations on a DB handle requires the DB handle itself be transactionally protected during its open.

type
The type parameter is of type DBTYPE, and must be set to one of DB_BTREE, DB_HASH, DB_QUEUE, DB_RECNO, or DB_UNKNOWN. If type is DB_UNKNOWN, the database must already exist and DB->open will automatically determine its type. The DB->get_type method may be used to determine the underlying type of databases opened using DB_UNKNOWN.

Environment Variables

If the database was opened within a database environment, the environment variable DB_HOME may be used as the path of the database environment home.

DB->open is affected by any database directory specified using the DB_ENV->set_data_dir method, or by setting the "set_data_dir" string in the environment's DB_CONFIG file.

TMPDIR
If the file and dbenv parameters to DB->open are NULL, the environment variable TMPDIR may be used as a directory in which to create temporary backing files

Errors

The DB->open method may fail and return one of the following non-zero errors:

DB_LOCK_DEADLOCK
A transactional database environment operation was selected to resolve a deadlock.

DB_LOCK_NOTGRANTED
A Berkeley DB Concurrent Data Store database environment configured for lock timeouts was unable to grant a lock in the allowed time.

ENOENT
The file or directory does not exist.

DB_OLD_VERSION
The database cannot be opened without being first upgraded.

EEXIST
DB_CREATE and DB_EXCL were specified and the database exists.

EINVAL
If an unknown database type, page size, hash function, pad byte, byte order, or a flag value or parameter that is incompatible with the specified database was specified; the DB_THREAD flag was specified and fast mutexes are not available for this architecture; the DB_THREAD flag was specified to DB->open, but was not specified to the DB_ENV->open call for the environment in which the DB handle was created; a backing flat text file was specified with either the DB_THREAD flag or the provided database environment supports transaction processing; or if an invalid flag value or parameter was specified.

ENOENT
A nonexistent re_source file was specified.

DB_REP_HANDLE_DEAD
The database handle has been invalidated because a replication election unrolled a committed transaction.

Description: DB->get_database

The DB->get_database method returns the current filename and database name.

Parameters

filenamep
The filenamep parameter references memory into which a pointer to the current filename is copied.

dbnamep
The dbnamep parameter references memory into which a pointer to the current database name is copied.

The DB->get_database method may be called at any time during the life of the application.

The DB->get_database method returns a non-zero error value on failure and 0 on success.


Description: DB->get_open_flags

The DB->get_open_flags method returns the current open method flags.

The DB->get_open_flags method may not be called before the DB->open method has been called.

The DB->get_open_flags method returns a non-zero error value on failure and 0 on success.

Parameters

flagsp
The DB->get_open_flags method returns the current open method flags in flagsp.

Description: DB->get_transactional

The DB->get_transactional method returns non-zero if the DB handle has been opened in a transactional mode.

The DB->get_transactional method may be called at any time during the life of the application.


Class

DB

See Also

Databases and Related Methods

APIRef

Copyright (c) 1996-2003 Sleepycat Software, Inc. - All rights reserved.