1 .\" $NetBSD: dbopen.3,v 1.19 2010/12/16 12:08:16 jruoho 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
67 for reading and/or writing.
68 Files never intended to be preserved on disk may be created by setting
76 arguments are as specified to the
78 routine, however, only the
89 (Note, opening a database file
92 .\"Three additional options may be specified by or'ing
98 .\"Do the necessary locking in the database to support concurrent access.
99 .\"If concurrent access isn't needed or the database is read-only this
100 .\"flag should not be set, as it tends to have an associated performance
104 .\"Place the underlying memory pool used by the database in shared
106 .\"Necessary for concurrent access.
109 .\"Support transactions in the database.
114 .\"flags must be set as well.
122 include file) and may be set to
130 argument is a pointer to an access method specific structure described
131 in the access method's manual page.
136 each access method will use defaults appropriate for the system and
141 function returns a pointer to a DB structure on success and
144 The DB structure is defined in the
146 include file, and contains at least the following fields:
147 .Bd -literal -offset indent
150 int (*close)(const DB *db);
151 int (*del)(const DB *db, const DBT *key, u_int flags);
152 int (*fd)(const DB *db);
153 int (*get)(const DB *db, DBT *key, DBT *data, u_int flags);
154 int (*put)(const DB *db, DBT *key, const DBT *data,
156 int (*sync)(const DB *db, u_int flags);
157 int (*seq)(const DB *db, DBT *key, DBT *data, u_int flags);
161 These elements describe a database type and a set of functions
162 performing various actions.
163 These functions take a pointer to a structure as returned by
165 and sometimes one or more pointers to key/data structures and a flag
167 .Bl -tag -width closex -offset indent
169 The type of the underlying access method (and file format).
171 A pointer to a routine to flush any cached information to disk, free
172 any allocated resources, and close the underlying file(s).
173 Since key/data pairs may be cached in memory, failing to sync the file
178 function may result in inconsistent or lost information.
180 routines return \-1 on error (setting
184 A pointer to a routine to remove key/data pairs from the database.
188 may be set to the following value:
189 .Bl -tag -width R_CURSORX
191 Delete the record referenced by the cursor.
192 The cursor must have previously been initialized.
196 routines return \-1 on error (setting
198 0 on success, and 1 if the specified
202 A pointer to a routine which returns a file descriptor representative
203 of the underlying database.
204 A file descriptor referencing the same file will be returned to all
210 This file descriptor may be safely used as an argument to the
215 The file descriptor is not necessarily associated with any of the
216 underlying files used by the access method.
217 No file descriptor is available for in memory databases.
219 routines return \-1 on error (setting
221 and the file descriptor on success.
223 A pointer to a routine which is the interface for keyed retrieval from
225 The address and length of the data associated with the specified
227 are returned in the structure referenced by
230 routines return \-1 on error (setting
232 0 on success, and 1 if the
236 A pointer to a routine to store key/data pairs in the database.
240 may be set to one of the following values:
241 .Bl -tag -width R_NOOVERWRITEX
243 Replace the key/data pair referenced by the cursor.
244 The cursor must have previously been initialized.
246 Append the data immediately after the data referenced by
248 creating a new key/data pair.
249 The record number of the appended key/data pair is returned in the
252 (Applicable only to the
256 Insert the data immediately before the data referenced by
258 creating a new key/data pair.
259 The record number of the inserted key/data pair is returned in the
262 (Applicable only to the
266 Enter the new key/data pair only if the key does not previously
269 Store the key/data pair, setting or initializing the position of the
270 cursor to reference it.
271 (Applicable only to the
279 is available only for the
283 access methods because it implies that the keys have an inherent order
284 which does not change.
289 are available only for the
291 access method because they each imply that the access method is able
293 This is only true if the keys are ordered and independent, record
296 The default behavior of the
298 routines is to enter the new key/data pair, replacing any previously
302 routines return \-1 on error (setting
304 0 on success, and 1 if the
307 was set and the key already exists in the file.
309 A pointer to a routine which is the interface for sequential
310 retrieval from the database.
311 The address and length of the key are returned in the structure
314 and the address and length of the data are returned in the
315 structure referenced by
318 Sequential key/data pair retrieval may begin at any time, and the
321 is not affected by calls to the
328 Modifications to the database during a sequential scan will be
329 reflected in the scan, i.e., records inserted behind the cursor will
330 not be returned while records inserted in front of the cursor will be
335 be set to one of the following values:
336 .Bl -tag -width R_CURSORX
338 The data associated with the specified key is returned.
339 This differs from the
341 routines in that it sets or initializes the cursor to the location of
345 access method, the returned key is not necessarily an exact match for
347 The returned key is the smallest key greater than or equal to the
348 specified key, permitting partial key matches and range searches.)
350 The first key/data pair of the database is returned, and the cursor
351 is set or initialized to reference it.
353 The last key/data pair of the database is returned, and the cursor
354 is set or initialized to reference it.
355 (Applicable only to the
361 Retrieve the key/data pair immediately after the cursor.
362 If the cursor is not yet set, this is the same as the
366 Retrieve the key/data pair immediately before the cursor.
367 If the cursor is not yet set, this is the same as the
370 (Applicable only to the
380 are available only for the
384 access methods because they each imply that the keys have an inherent
385 order which does not change.
388 routines return \-1 on error (setting
390 0 on success and 1 if there are no key/data pairs less than or greater
391 than the specified or current key.
394 access method is being used, and if the database file is a character
395 special file and no complete key/data pairs are currently available,
400 A pointer to a routine to flush any cached information to disk.
401 If the database is in memory only, the
403 routine has no effect and will always succeed.
405 The flag value may be set to the following value:
406 .Bl -tag -width ".Dv R_RECNOSYNC"
410 access method is being used, this flag causes the sync routine to
411 apply to the btree file which underlies the recno file, not the recno
417 manual page for more information.)
421 routines return \-1 on error (setting
426 Access to all file types is based on key/data pairs.
427 Both keys and data are represented by the following data structure:
428 .Bd -literal -offset indent
435 The elements of the DBT structure are defined as follows:
436 .Bl -tag -width datax -offset indent
438 A pointer to a byte string.
440 The length of the byte string.
443 Key and data byte strings may reference strings of essentially
444 unlimited length although any two of them must fit into available
445 memory at the same time.
446 It should be noted that the access methods provide no guarantees about
447 byte string alignment.
451 routine may fail and set
453 for any of the errors specified for the library routines
460 A file is incorrectly formatted.
462 A parameter has been specified (hash function, pad byte, etc.) that is
463 incompatible with the current file specification or which is not
464 meaningful for the function (for example, use of the cursor without
465 prior initialization) or there is a mismatch between the version
466 number of file and the software.
468 The key could not be inserted due to limitations in the DB file format
469 (e.g., a hash database was out of overflow pages).
474 routines may fail and set
476 for any of the errors specified for the library routines
490 routines may fail and set
492 for any of the errors specified for the library routines
501 routines will fail and set
505 for in memory databases.
509 routines may fail and set
511 for any of the errors specified for the library routine
520 .%T LIBTP: Portable, Modular Transactions for UNIX
523 .%I USENIX Association
524 .%B Proceedings of the 1992 Winter USENIX Technical Conference
529 The typedef DBT is a mnemonic for
530 .Dq data base thang ,
531 and was used because no one could think of a reasonable name that
534 The file descriptor interface is a kludge and will be deleted in a
535 future version of the interface.
537 None of the access methods provide any form of concurrent access,
538 locking, or transactions.