DBOPEN(3) BSD Programmer's Manual DBOPEN(3)
dbopen - database access methods
S
\bSY
\bYN
\bNO
\bOP
\bPS
\bSI
\bIS
\bS
#
\b#i
\bin
\bnc
\bcl
\blu
\bud
\bde
\be <
\b<s
\bsy
\bys
\bs/
\b/t
\bty
\byp
\bpe
\bes
\bs.
\b.h
\bh>
\b>
#
\b#i
\bin
\bnc
\bcl
\blu
\bud
\bde
\be <
\b<l
\bli
\bim
\bmi
\bit
\bts
\bs.
\b.h
\bh>
\b>
#
\b#i
\bin
\bnc
\bcl
\blu
\bud
\bde
\be <
\b<d
\bdb
\bb.
\b.h
\bh>
\b>
d
\bdb
\bbo
\bop
\bpe
\ben
\bn(
\b(c
\bco
\bon
\bns
\bst
\bt c
\bch
\bha
\bar
\br *
\b*f
\bfi
\bil
\ble
\be,
\b, i
\bin
\bnt
\bt f
\bfl
\bla
\bag
\bgs
\bs,
\b, i
\bin
\bnt
\bt m
\bmo
\bod
\bde
\be,
\b, D
\bDB
\bBT
\bTY
\bYP
\bPE
\bE t
\bty
\byp
\bpe
\be,
\b,
c
\bco
\bon
\bns
\bst
\bt v
\bvo
\boi
\bid
\bd *
\b*o
\bop
\bpe
\ben
\bni
\bin
\bnf
\bfo
\bo)
\b);
\b;
D
\bDE
\bES
\bSC
\bCR
\bRI
\bIP
\bPT
\bTI
\bIO
\bON
\bN
_
\bD_
\bb_
\bo_
\bp_
\be_
\bn is the library interface to database files. The
supported file formats are btree, hashed and UNIX file
oriented. The btree format is a representation of a
sorted, balanced tree structure. The hashed format is an
extensible, dynamic hashing scheme. The flat-file format
is a byte stream file with fixed or variable length
records. The formats and file format specific information
are described in detail in their respective manual pages
_
\bb_
\bt_
\br_
\be_
\be(3), _
\bh_
\ba_
\bs_
\bh(3) and _
\br_
\be_
\bc_
\bn_
\bo(3).
Dbopen opens _
\bf_
\bi_
\bl_
\be for reading and/or writing. Files never
intended to be preserved on disk may be created by setting
the file parameter to NULL.
The _
\bf_
\bl_
\ba_
\bg_
\bs and _
\bm_
\bo_
\bd_
\be _
\ba_
\br_
\bg_
\bu_
\bm_
\be_
\bn_
\bt_
\bs are as specified to the
_
\bo_
\bp_
\be_
\bn(2) routine, however, only the O_CREAT, O_EXCL,
O_EXLOCK, O_RDONLY, O_RDWR, O_SHLOCK and O_TRUNC flags are
meaningful. (Note, opening a database file O_WRONLY is
The _
\bt_
\by_
\bp_
\be argument is of type DBTYPE (as defined in the
<db.h> include file) and may be set to DB_BTREE, DB_HASH
The _
\bo_
\bp_
\be_
\bn_
\bi_
\bn_
\bf_
\bo argument is a pointer to an access method
specific structure described in the access method's manual
page. If _
\bo_
\bp_
\be_
\bn_
\bi_
\bn_
\bf_
\bo is NULL, each access method will use
defaults appropriate for the system and the access method.
_
\bD_
\bb_
\bo_
\bp_
\be_
\bn returns a pointer to a DB structure on success and
NULL on error. The DB structure is defined in the <db.h>
include file, and contains at least the following fields:
int (*close)(const DB *db);
int (*del)(const DB *db, const DBT *key, u_int flags);
4.4 Berkeley Distribution June 4, 1993 1
DBOPEN(3) BSD Programmer's Manual DBOPEN(3)
int (*get)(const DB *db, DBT *key, DBT *data, u_int flags);
int (*put)(const DB *db, DBT *key, const DBT *data,
int (*sync)(const DB *db, u_int flags);
int (*seq)(const DB *db, DBT *key, DBT *data, u_int flags);
These elements describe a database type and a set of func-
tions performing various actions. These functions take a
pointer to a structure as returned by _
\bd_
\bb_
\bo_
\bp_
\be_
\bn, and some-
times one or more pointers to key/data structures and a
type The type of the underlying access method (and file
close A pointer to a routine to flush any cached informa-
tion to disk, free any allocated resources, and
close the underlying file(s). Since key/data pairs
may be cached in memory, failing to sync the file
with a _
\bc_
\bl_
\bo_
\bs_
\be or _
\bs_
\by_
\bn_
\bc function may result in incon-
sistent or lost information. _
\bC_
\bl_
\bo_
\bs_
\be routines return
-1 on error (setting _
\be_
\br_
\br_
\bn_
\bo) and 0 on success.
del A pointer to a routine to remove key/data pairs
The parameter _
\bf_
\bl_
\ba_
\bg may be set to the following
Delete the record referenced by the cursor.
The cursor must have previously been ini-
_
\bD_
\be_
\bl_
\be_
\bt_
\be routines return -1 on error (setting _
\be_
\br_
\br_
\bn_
\bo),
0 on success, and 1 if the specified _
\bk_
\be_
\by was not in
fd A pointer to a routine which returns a file
descriptor representative of the underlying
database. A file descriptor referencing the same
file will be returned to all processes which call
_
\bd_
\bb_
\bo_
\bp_
\be_
\bn with the same _
\bf_
\bi_
\bl_
\be name. This file descrip-
tor may be safely used as a argument to the
_
\bf_
\bc_
\bn_
\bt_
\bl(2) and _
\bf_
\bl_
\bo_
\bc_
\bk(2) locking functions. The file
descriptor is not necessarily associated with any
of the underlying files used by the access method.
No file descriptor is available for in memory
databases. _
\bF_
\bd routines return -1 on error (setting
_
\be_
\br_
\br_
\bn_
\bo), and the file descriptor on success.
4.4 Berkeley Distribution June 4, 1993 2
DBOPEN(3) BSD Programmer's Manual DBOPEN(3)
get A pointer to a routine which is the interface for
keyed retrieval from the database. The address and
length of the data associated with the specified
_
\bk_
\be_
\by are returned in the structure referenced by
_
\bd_
\ba_
\bt_
\ba. _
\bG_
\be_
\bt routines return -1 on error (setting
_
\be_
\br_
\br_
\bn_
\bo), 0 on success, and 1 if the _
\bk_
\be_
\by was not in
put A pointer to a routine to store key/data pairs in
The parameter _
\bf_
\bl_
\ba_
\bg may be set to one of the follow-
Replace the key/data pair referenced by the
cursor. The cursor must have previously
Append the data immediately after the data
referenced by _
\bk_
\be_
\by, creating a new key/data
pair. The record number of the appended
key/data pair is returned in the _
\bk_
\be_
\by struc-
ture. (Applicable only to the DB_RECNO
Insert the data immediately before the data
referenced by _
\bk_
\be_
\by, creating a new key/data
pair. The record number of the inserted
key/data pair is returned in the _
\bk_
\be_
\by struc-
ture. (Applicable only to the DB_RECNO
Enter the new key/data pair only if the key
does not previously exist.
Store the key/data pair, setting or initial-
izing the position of the cursor to refer-
ence it. (Applicable only to the DB_BTREE
and DB_RECNO access methods.)
R_SETCURSOR is available only for the DB_BTREE and
DB_RECNO access methods because it implies that the
keys have an inherent order which does not change.
R_IAFTER and R_IBEFORE are available only for the
DB_RECNO access method because they each imply that
4.4 Berkeley Distribution June 4, 1993 3
DBOPEN(3) BSD Programmer's Manual DBOPEN(3)
the access method is able to create new keys. This
is only true if the keys are ordered and indepen-
dent, record numbers for example.
The default behavior of the _
\bp_
\bu_
\bt routines is to
enter the new key/data pair, replacing any previ-
_
\bP_
\bu_
\bt routines return -1 on error (setting _
\be_
\br_
\br_
\bn_
\bo), 0
on success, and 1 if the R_NOOVERWRITE _
\bf_
\bl_
\ba_
\bg was set
and the key already exists in the file.
seq A pointer to a routine which is the interface for
sequential retrieval from the database. The
address and length of the key are returned in the
structure referenced by _
\bk_
\be_
\by, and the address and
length of the data are returned in the structure
referenced by _
\bd_
\ba_
\bt_
\ba.
Sequential key/data pair retrieval may begin at any
time, and the position of the ``cursor'' is not
affected by calls to the _
\bd_
\be_
\bl, _
\bg_
\be_
\bt, _
\bp_
\bu_
\bt, or _
\bs_
\by_
\bn_
\bc
routines. Modifications to the database during a
sequential scan will be reflected in the scan, i.e.
records inserted behind the cursor will not be
returned while records inserted in front of the
The flag value m
\bmu
\bus
\bst
\bt be set to one of the following
The data associated with the specified key
is returned. This differs from the _
\bg_
\be_
\bt rou-
tines in that it sets or initializes the
cursor to the location of the key as well.
(Note, for the DB_BTREE access method, the
returned key is not necessarily an exact
match for the specified key. The returned
key is the smallest key greater than or
equal to the specified key, permitting par-
tial key matches and range searches.)
The first key/data pair of the database is
returned, and the cursor is set or initial-
R_LAST The last key/data pair of the database is
returned, and the cursor is set or initial-
ized to reference it. (Applicable only to
4.4 Berkeley Distribution June 4, 1993 4
DBOPEN(3) BSD Programmer's Manual DBOPEN(3)
the DB_BTREE and DB_RECNO access methods.)
R_NEXT Retrieve the key/data pair immediately after
the cursor. If the cursor is not yet set,
this is the same as the R_FIRST flag.
R_PREV Retrieve the key/data pair immediately
before the cursor. If the cursor is not yet
set, this is the same as the R_LAST flag.
(Applicable only to the DB_BTREE and
DB_RECNO access methods.)
R_LAST and R_PREV are available only for the
DB_BTREE and DB_RECNO access methods because they
each imply that the keys have an inherent order
_
\bS_
\be_
\bq routines return -1 on error (setting _
\be_
\br_
\br_
\bn_
\bo), 0
on success and 1 if there are no key/data pairs
less than or greater than the specified or current
key. If the DB_RECNO access method is being used,
and if the database file is a character special
file and no complete key/data pairs are currently
available, the _
\bs_
\be_
\bq routines return 2.
sync A pointer to a routine to flush any cached informa-
tion to disk. If the database is in memory only,
the _
\bs_
\by_
\bn_
\bc routine has no effect and will always suc-
The flag value may be set to the following value:
If the DB_RECNO access method is being used,
this flag causes the sync routine to apply
to the btree file which underlies the recno
file, not the recno file itself. (See the
_
\bb_
\bf_
\bn_
\ba_
\bm_
\be field of the _
\br_
\be_
\bc_
\bn_
\bo(3) manual page for
_
\bS_
\by_
\bn_
\bc routines return -1 on error (setting _
\be_
\br_
\br_
\bn_
\bo)
K
\bKE
\bEY
\bY/
\b/D
\bDA
\bAT
\bTA
\bA P
\bPA
\bAI
\bIR
\bRS
\bS
Access to all file types is based on key/data pairs. Both
keys and data are represented by the following data struc-
4.4 Berkeley Distribution June 4, 1993 5
DBOPEN(3) BSD Programmer's Manual DBOPEN(3)
The elements of the DBT structure are defined as follows:
data A pointer to a byte string.
size The length of the byte string.
Key and data byte strings may reference strings of essen-
tially unlimited length although any two of them must fit
into available memory at the same time. It should be
noted that the access methods provide no guarantees about
The _
\bd_
\bb_
\bo_
\bp_
\be_
\bn routine may fail and set _
\be_
\br_
\br_
\bn_
\bo for any of the
errors specified for the library routines _
\bo_
\bp_
\be_
\bn(2) and _
\bm_
\ba_
\bl_
\b-
_
\bl_
\bo_
\bc(3) or the following:
A file is incorrectly formatted.
A parameter has been specified (hash function, pad
byte etc.) that is incompatible with the current
file specification or which is not meaningful for
the function (for example, use of the cursor with-
out prior initialization) or there is a mismatch
between the version number of file and the soft-
The _
\bc_
\bl_
\bo_
\bs_
\be routines may fail and set _
\be_
\br_
\br_
\bn_
\bo for any of the
errors specified for the library routines _
\bc_
\bl_
\bo_
\bs_
\be(2),
_
\br_
\be_
\ba_
\bd(2), _
\bw_
\br_
\bi_
\bt_
\be(2), _
\bf_
\br_
\be_
\be(3), or _
\bf_
\bs_
\by_
\bn_
\bc(2).
The _
\bd_
\be_
\bl, _
\bg_
\be_
\bt, _
\bp_
\bu_
\bt and _
\bs_
\be_
\bq routines may fail and set _
\be_
\br_
\br_
\bn_
\bo
for any of the errors specified for the library routines
_
\br_
\be_
\ba_
\bd(2), _
\bw_
\br_
\bi_
\bt_
\be(2), _
\bf_
\br_
\be_
\be(3) or _
\bm_
\ba_
\bl_
\bl_
\bo_
\bc(3).
The _
\bf_
\bd routines will fail and set _
\be_
\br_
\br_
\bn_
\bo to ENOENT for in
The _
\bs_
\by_
\bn_
\bc routines may fail and set _
\be_
\br_
\br_
\bn_
\bo for any of the
errors specified for the library routine _
\bf_
\bs_
\by_
\bn_
\bc(2).
S
\bSE
\bEE
\bE A
\bAL
\bLS
\bSO
\bO
_
\bb_
\bt_
\br_
\be_
\be(3), _
\bh_
\ba_
\bs_
\bh(3), _
\bm_
\bp_
\bo_
\bo_
\bl(3), _
\br_
\be_
\bc_
\bn_
\bo(3)
The typedef DBT is a mnemonic for ``data base thang'', and
was used because noone could think of a reasonable name
4.4 Berkeley Distribution June 4, 1993 6
DBOPEN(3) BSD Programmer's Manual DBOPEN(3)
that wasn't already used.
The file descriptor interface is a kluge and will be
deleted in a future version of the interface.
None of the access methods provide any form of concurrent
access, locking, or transactions.
4.4 Berkeley Distribution June 4, 1993 7