1 .\" Copyright (c) 1990, 1993
2 .\" The Regents of the University of California. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 4. Neither the name of the University nor the names of its contributors
13 .\" may be used to endorse or promote products derived from this software
14 .\" without specific prior written permission.
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 .\" @(#)dbopen.3 8.5 (Berkeley) 1/2/94
29 .\" $FreeBSD: src/lib/libc/db/man/dbopen.3,v 1.13 2007/01/09 00:27:51 imp Exp $
36 .Nd "database access methods"
43 .Fn dbopen "const char *file" "int flags" "int mode" "DBTYPE type" "const void *openinfo"
48 is the library interface to database files.
49 The supported file formats are btree, hashed and UNIX file oriented.
50 The btree format is a representation of a sorted, balanced tree structure.
51 The hashed format is an extensible, dynamic hashing scheme.
52 The flat-file format is a byte stream file with fixed or variable length
54 The formats and file format specific information are described in detail
55 in their respective manual pages
66 for reading and/or writing.
67 Files never intended to be preserved on disk may be created by setting
78 are as specified to the
80 routine, however, only the
81 .Dv O_CREAT , O_EXCL , O_EXLOCK , O_NONBLOCK ,
82 .Dv O_RDONLY , O_RDWR , O_SHLOCK
86 (Note, opening a database file
89 .\"Three additional options may be specified by
94 .\".Bl -tag -width indent
96 .\"Do the necessary locking in the database to support concurrent access.
97 .\"If concurrent access is not needed or the database is read-only this
98 .\"flag should not be set, as it tends to have an associated performance
101 .\"Place the underlying memory pool used by the database in shared
103 .\"Necessary for concurrent access.
105 .\"Support transactions in the database.
110 .\"flags must be set as well.
121 .Dv DB_BTREE , DB_HASH
127 argument is a pointer to an access method specific structure described
128 in the access method's manual page.
133 each access method will use defaults appropriate for the system
134 and the access method.
139 returns a pointer to a
141 structure on success and
146 structure is defined in the
148 include file, and contains at
149 least the following fields:
153 int (*close)(DB *db);
154 int (*del)(const DB *db, const DBT *key, u_int flags);
155 int (*fd)(const DB *db);
156 int (*get)(const DB *db, const DBT *key, DBT *data, u_int flags);
157 int (*put)(const DB *db, DBT *key, const DBT *data,
159 int (*sync)(const DB *db, u_int flags);
160 int (*seq)(const DB *db, DBT *key, DBT *data, u_int flags);
164 These elements describe a database type and a set of functions performing
166 These functions take a pointer to a structure as returned by
168 and sometimes one or more pointers to key/data structures and a flag value.
169 .Bl -tag -width indent
171 The type of the underlying access method (and file format).
173 A pointer to a routine to flush any cached information to disk, free any
174 allocated resources, and close the underlying file(s).
175 Since key/data pairs may be cached in memory, failing to sync the file
180 function may result in inconsistent or lost information.
182 routines return -1 on error (setting
186 A pointer to a routine to remove key/data pairs from the database.
191 may be set to the following value:
192 .Bl -tag -width indent
194 Delete the record referenced by the cursor.
195 The cursor must have previously been initialized.
199 routines return -1 on error (setting
201 0 on success, and 1 if the specified
205 A pointer to a routine which returns a file descriptor representative
206 of the underlying database.
207 A file descriptor referencing the same file will be returned to all
213 This file descriptor may be safely used as an argument to the
218 The file descriptor is not necessarily associated with any of the
219 underlying files used by the access method.
220 No file descriptor is available for in memory databases.
222 routines return -1 on error (setting
224 and the file descriptor on success.
226 A pointer to a routine which is the interface for keyed retrieval from
228 The address and length of the data associated with the specified
230 are returned in the structure referenced by
233 routines return -1 on error (setting
235 0 on success, and 1 if the
239 A pointer to a routine to store key/data pairs in the database.
244 may be set to one of the following values:
245 .Bl -tag -width indent
247 Replace the key/data pair referenced by the cursor.
248 The cursor must have previously been initialized.
250 Append the data immediately after the data referenced by
252 creating a new key/data pair.
253 The record number of the appended key/data pair is returned in the
256 (Applicable only to the
260 Insert the data immediately before the data referenced by
262 creating a new key/data pair.
263 The record number of the inserted key/data pair is returned in the
266 (Applicable only to the
270 Enter the new key/data pair only if the key does not previously exist.
272 Store the key/data pair, setting or initializing the position of the
273 cursor to reference it.
274 (Applicable only to the
282 is available only for the
287 methods because it implies that the keys have an inherent order
288 which does not change.
293 are available only for the
295 access method because they each imply that the access method is able to
297 This is only true if the keys are ordered and independent, record numbers
300 The default behavior of the
302 routines is to enter the new key/data pair, replacing any previously
306 routines return -1 on error (setting
308 0 on success, and 1 if the
311 was set and the key already exists in the file.
313 A pointer to a routine which is the interface for sequential
314 retrieval from the database.
315 The address and length of the key are returned in the structure
318 and the address and length of the data are returned in the
323 Sequential key/data pair retrieval may begin at any time, and the
326 is not affected by calls to the
333 Modifications to the database during a sequential scan will be reflected
334 in the scan, i.e., records inserted behind the cursor will not be returned
335 while records inserted in front of the cursor will be returned.
341 be set to one of the following values:
342 .Bl -tag -width indent
344 The data associated with the specified key is returned.
345 This differs from the
347 routines in that it sets or initializes the cursor to the location of
351 access method, the returned key is not necessarily an
352 exact match for the specified key.
353 The returned key is the smallest key greater than or equal to the specified
354 key, permitting partial key matches and range searches.)
356 The first key/data pair of the database is returned, and the cursor
357 is set or initialized to reference it.
359 The last key/data pair of the database is returned, and the cursor
360 is set or initialized to reference it.
361 (Applicable only to the
367 Retrieve the key/data pair immediately after the cursor.
368 If the cursor is not yet set, this is the same as the
372 Retrieve the key/data pair immediately before the cursor.
373 If the cursor is not yet set, this is the same as the
376 (Applicable only to the
386 are available only for the
390 access methods because they each imply that the keys have an inherent
391 order which does not change.
394 routines return -1 on error (setting
396 0 on success and 1 if there are no key/data pairs less than or greater
397 than the specified or current key.
400 access method is being used, and if the database file
401 is a character special file and no complete key/data pairs are currently
406 A pointer to a routine to flush any cached information to disk.
407 If the database is in memory only, the
409 routine has no effect and will always succeed.
413 argument may be set to the following value:
414 .Bl -tag -width indent
418 access method is being used, this flag causes
421 routine to apply to the btree file which underlies the
422 recno file, not the recno file itself.
427 manual page for more information.)
431 routines return -1 on error (setting
436 Access to all file types is based on key/data pairs.
437 Both keys and data are represented by the following data structure:
447 structure are defined as follows:
448 .Bl -tag -width "data"
450 A pointer to a byte string.
452 The length of the byte string.
455 Key and data byte strings may reference strings of essentially unlimited
456 length although any two of them must fit into available memory at the same
458 It should be noted that the access methods provide no guarantees about
459 byte string alignment.
463 routine may fail and set
465 for any of the errors specified for the library routines
472 A file is incorrectly formatted.
474 An argument has been specified (hash function, pad byte etc.) that is
475 incompatible with the current file specification or which is not
476 meaningful for the function (for example, use of the cursor without
477 prior initialization) or there is a mismatch between the version
478 number of file and the software.
483 routines may fail and set
485 for any of the errors specified for the library routines
499 routines may fail and set
501 for any of the errors specified for the library routines
510 routines will fail and set
514 for in memory databases.
518 routines may fail and set
520 for any of the errors specified for the library routine
528 .%T "LIBTP: Portable, Modular Transactions for UNIX"
531 .%R "USENIX proceedings"
538 .Dq "data base thang" ,
540 because noone could think of a reasonable name that was not already used.
542 The file descriptor interface is a kluge and will be deleted in a
543 future version of the interface.
545 None of the access methods provide any form of concurrent access,
546 locking, or transactions.