1 .\" $NetBSD: dbopen.3,v 1.16 2004/08/31 17:11:33 uwe Exp $
3 .\" Copyright (c) 1990, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" @(#)dbopen.3 8.5 (Berkeley) 1/2/94
38 .Nd database access methods
45 .Fn dbopen "const char *file" "int flags" "mode_t mode" \
46 "DBTYPE type" "const void *openinfo"
49 is the library interface to database files.
50 The supported file formats are btree, hashed, and UNIX file oriented.
51 The btree format is a representation of a sorted, balanced tree
53 The hashed format is an extensible, dynamic hashing scheme.
54 The flat-file format is a byte stream file with fixed or variable
56 The formats and file format specific information are described in
57 detail 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
75 arguments are as specified to the
77 routine, however, only the
88 (Note, opening a database file
91 .\"Three additional options may be specified by or'ing
97 .\"Do the necessary locking in the database to support concurrent access.
98 .\"If concurrent access isn't needed or the database is read-only this
99 .\"flag should not be set, as it tends to have an associated performance
103 .\"Place the underlying memory pool used by the database in shared
105 .\"Necessary for concurrent access.
108 .\"Support transactions in the database.
113 .\"flags must be set as well.
121 include file) and may be set to
129 argument is a pointer to an access method specific structure described
130 in the access method's manual page.
135 each access method will use defaults appropriate for the system and
139 returns a pointer to a DB structure on success and
142 The DB structure is defined in the
144 include file, and contains at least the following fields:
148 int (*close)(const DB *db);
149 int (*del)(const DB *db, const DBT *key, u_int flags);
150 int (*fd)(const DB *db);
151 int (*get)(const DB *db, DBT *key, DBT *data, u_int flags);
152 int (*put)(const DB *db, DBT *key, const DBT *data,
154 int (*sync)(const DB *db, u_int flags);
155 int (*seq)(const DB *db, DBT *key, DBT *data, u_int flags);
159 These elements describe a database type and a set of functions
160 performing various actions.
161 These functions take a pointer to a structure as returned by
163 and sometimes one or more pointers to key/data structures and a flag
165 .Bl -tag -width closex
167 The type of the underlying access method (and file format).
169 A pointer to a routine to flush any cached information to disk, free
170 any allocated resources, and close the underlying file(s).
171 Since key/data pairs may be cached in memory, failing to sync the file
176 function may result in inconsistent or lost information.
178 routines return \-1 on error (setting
182 A pointer to a routine to remove key/data pairs from the database.
186 may be set to the following value:
187 .Bl -tag -width R_CURSORX
189 Delete the record referenced by the cursor.
190 The cursor must have previously been initialized.
194 routines return \-1 on error (setting
196 0 on success, and 1 if the specified
200 A pointer to a routine which returns a file descriptor representative
201 of the underlying database.
202 A file descriptor referencing the same file will be returned to all
208 This file descriptor may be safely used as an argument to the
213 The file descriptor is not necessarily associated with any of the
214 underlying files used by the access method.
215 No file descriptor is available for in memory databases.
217 routines return \-1 on error (setting
219 and the file descriptor on success.
221 A pointer to a routine which is the interface for keyed retrieval from
223 The address and length of the data associated with the specified
225 are returned in the structure referenced by
228 routines return \-1 on error (setting
230 0 on success, and 1 if the
234 A pointer to a routine to store key/data pairs in the database.
238 may be set to one of the following values:
239 .Bl -tag -width R_NOOVERWRITEX
241 Replace the key/data pair referenced by the cursor.
242 The cursor must have previously been initialized.
244 Append the data immediately after the data referenced by
246 creating a new key/data pair.
247 The record number of the appended key/data pair is returned in the
250 (Applicable only to the
254 Insert the data immediately before the data referenced by
256 creating a new key/data pair.
257 The record number of the inserted key/data pair is returned in the
260 (Applicable only to the
264 Enter the new key/data pair only if the key does not previously
267 Store the key/data pair, setting or initializing the position of the
268 cursor to reference it.
269 (Applicable only to the
277 is available only for the
281 access methods because it implies that the keys have an inherent order
282 which does not change.
287 are available only for the
289 access method because they each imply that the access method is able
291 This is only true if the keys are ordered and independent, record
294 The default behavior of the
296 routines is to enter the new key/data pair, replacing any previously
300 routines return \-1 on error (setting
302 0 on success, and 1 if the
305 was set and the key already exists in the file.
307 A pointer to a routine which is the interface for sequential
308 retrieval from the database.
309 The address and length of the key are returned in the structure
312 and the address and length of the data are returned in the
313 structure referenced by
316 Sequential key/data pair retrieval may begin at any time, and the
319 is not affected by calls to the
326 Modifications to the database during a sequential scan will be
327 reflected in the scan, i.e., records inserted behind the cursor will
328 not be returned while records inserted in front of the cursor will be
333 be set to one of the following values:
334 .Bl -tag -width R_CURSORX
336 The data associated with the specified key is returned.
337 This differs from the
339 routines in that it sets or initializes the cursor to the location of
343 access method, the returned key is not necessarily an exact match for
345 The returned key is the smallest key greater than or equal to the
346 specified key, permitting partial key matches and range searches.)
348 The first key/data pair of the database is returned, and the cursor
349 is set or initialized to reference it.
351 The last key/data pair of the database is returned, and the cursor
352 is set or initialized to reference it.
353 (Applicable only to the
359 Retrieve the key/data pair immediately after the cursor.
360 If the cursor is not yet set, this is the same as the
364 Retrieve the key/data pair immediately before the cursor.
365 If the cursor is not yet set, this is the same as the
368 (Applicable only to the
378 are available only for the
382 access methods because they each imply that the keys have an inherent
383 order which does not change.
386 routines return \-1 on error (setting
388 0 on success and 1 if there are no key/data pairs less than or greater
389 than the specified or current key.
392 access method is being used, and if the database file is a character
393 special file and no complete key/data pairs are currently available,
398 A pointer to a routine to flush any cached information to disk.
399 If the database is in memory only, the
401 routine has no effect and will always succeed.
403 The flag value may be set to the following value:
404 .Bl -tag -width ".Dv R_RECNOSYNC"
408 access method is being used, this flag causes the sync routine to
409 apply to the btree file which underlies the recno file, not the recno
415 manual page for more information.)
419 routines return \-1 on error (setting
424 Access to all file types is based on key/data pairs.
425 Both keys and data are represented by the following data structure:
433 The elements of the DBT structure are defined as follows:
434 .Bl -tag -width datax
436 A pointer to a byte string.
438 The length of the byte string.
441 Key and data byte strings may reference strings of essentially
442 unlimited length although any two of them must fit into available
443 memory at the same time.
444 It should be noted that the access methods provide no guarantees about
445 byte string alignment.
449 routine may fail and set
451 for any of the errors specified for the library routines
458 A file is incorrectly formatted.
460 A parameter has been specified (hash function, pad byte, etc.) that is
461 incompatible with the current file specification or which is not
462 meaningful for the function (for example, use of the cursor without
463 prior initialization) or there is a mismatch between the version
464 number of file and the software.
466 The key could not be inserted due to limitations in the DB file format
467 (e.g., a hash database was out of overflow pages).
472 routines may fail and set
474 for any of the errors specified for the library routines
488 routines may fail and set
490 for any of the errors specified for the library routines
499 routines will fail and set
503 for in memory databases.
507 routines may fail and set
509 for any of the errors specified for the library routine
518 .%T "LIBTP: Portable, Modular Transactions for UNIX"
521 .%J USENIX proceedings
525 The typedef DBT is a mnemonic for
526 .Dq data base thang ,
527 and was used because no one could think of a reasonable name that
530 The file descriptor interface is a kludge and will be deleted in a
531 future version of the interface.
533 None of the access methods provide any form of concurrent access,
534 locking, or transactions.