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 .\" 3. 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.3.2.3 2003/02/23 19:45:52 trhodes Exp $
30 .\" $DragonFly: src/lib/libc/db/man/dbopen.3,v 1.5 2007/07/30 22:11:32 swildner Exp $
32 .Dd September 19, 2005
37 .Nd "database access methods"
44 .Fn dbopen "const char *file" "int flags" "mode_t mode" "DBTYPE type" "const void *openinfo"
49 is the library interface to database files.
50 The supported file formats are btree, hashed and
53 The btree format is a representation of a sorted, balanced tree structure.
54 The hashed format is an extensible, dynamic hashing scheme.
55 The flat-file format is a byte stream file with fixed or variable length
57 The formats and file format specific information are described in detail
58 in their respective manual pages
69 for reading and/or writing.
70 Files never intended to be preserved on disk may be created by setting
81 are as specified to the
83 routine, however, only the
84 .Dv O_CREAT , O_EXCL , O_EXLOCK , O_NONBLOCK ,
85 .Dv O_RDONLY , O_RDWR , O_SHLOCK
89 (Note, opening a database file
92 .\"Three additional options may be specified by
97 .\".Bl -tag -width indent
99 .\"Do the necessary locking in the database to support concurrent access.
100 .\"If concurrent access isn't needed or the database is read-only this
101 .\"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.
108 .\"Support transactions in the database.
113 .\"flags must be set as well.
124 .Dv DB_BTREE , DB_HASH
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
137 and the access method.
142 returns a pointer to a
144 structure on success and
149 structure is defined in the
151 include file, and contains at
152 least the following fields:
156 int (*close)(const DB *db);
157 int (*del)(const DB *db, const DBT *key, u_int flags);
158 int (*fd)(const DB *db);
159 int (*get)(const DB *db, DBT *key, DBT *data, u_int flags);
160 int (*put)(const DB *db, DBT *key, const DBT *data,
162 int (*sync)(const DB *db, u_int flags);
163 int (*seq)(const DB *db, DBT *key, DBT *data, u_int flags);
167 These elements describe a database type and a set of functions performing
169 These functions take a pointer to a structure as returned by
171 and sometimes one or more pointers to key/data structures and a flag value.
172 .Bl -tag -width indent
174 The type of the underlying access method (and file format).
176 A pointer to a routine to flush any cached information to disk, free any
177 allocated resources, and close the underlying file(s).
178 Since key/data pairs may be cached in memory, failing to sync the file
183 function may result in inconsistent or lost information.
185 routines return -1 on error (setting
189 A pointer to a routine to remove key/data pairs from the database.
194 may be set to the following value:
195 .Bl -tag -width indent
197 Delete the record referenced by the cursor.
198 The cursor must have previously been initialized.
202 routines return -1 on error (setting
204 0 on success, and 1 if the specified
208 A pointer to a routine which returns a file descriptor representative
209 of the underlying database.
210 A file descriptor referencing the same file will be returned to all
216 This file descriptor may be safely used as an argument to the
221 The file descriptor is not necessarily associated with any of the
222 underlying files used by the access method.
223 No file descriptor is available for in memory databases.
225 routines return -1 on error (setting
227 and the file descriptor on success.
229 A pointer to a routine which is the interface for keyed retrieval from
231 The address and length of the data associated with the specified
233 are returned in the structure referenced by
236 routines return -1 on error (setting
238 0 on success, and 1 if the
242 A pointer to a routine to store key/data pairs in the database.
247 may be set to one of the following values:
248 .Bl -tag -width indent
250 Replace the key/data pair referenced by the cursor.
251 The cursor must have previously been initialized.
253 Append the data immediately after the data referenced by
255 creating a new key/data pair.
256 The record number of the appended key/data pair is returned in the
259 (Applicable only to the
263 Insert the data immediately before the data referenced by
265 creating a new key/data pair.
266 The record number of the inserted key/data pair is returned in the
269 (Applicable only to the
273 Enter the new key/data pair only if the key does not previously exist.
275 Store the key/data pair, setting or initializing the position of the
276 cursor to reference it.
277 (Applicable only to the
285 is available only for the
290 methods because it implies that the keys have an inherent order
291 which does not change.
296 are available only for the
298 access method because they each imply that the access method is able to
300 This is only true if the keys are ordered and independent, record numbers
303 The default behavior of the
305 routines is to enter the new key/data pair, replacing any previously
309 routines return -1 on error (setting
311 0 on success, and 1 if the
314 was set and the key already exists in the file.
316 A pointer to a routine which is the interface for sequential
317 retrieval from the database.
318 The address and length of the key are returned in the structure
321 and the address and length of the data are returned in the
326 Sequential key/data pair retrieval may begin at any time, and the
329 is not affected by calls to the
336 Modifications to the database during a sequential scan will be reflected
337 in the scan, i.e. records inserted behind the cursor will not be returned
338 while records inserted in front of the cursor will be returned.
344 be set to one of the following values:
345 .Bl -tag -width indent
347 The data associated with the specified key is returned.
348 This differs from the
350 routines in that it sets or initializes the cursor to the location of
354 access method, the returned key is not necessarily an
355 exact match for the specified key.
356 The returned key is the smallest key greater than or equal to the specified
357 key, permitting partial key matches and range searches.)
359 The first key/data pair of the database is returned, and the cursor
360 is set or initialized to reference it.
362 The last key/data pair of the database is returned, and the cursor
363 is set or initialized to reference it.
364 (Applicable only to the
370 Retrieve the key/data pair immediately after the cursor.
371 If the cursor is not yet set, this is the same as the
375 Retrieve the key/data pair immediately before the cursor.
376 If the cursor is not yet set, this is the same as the
379 (Applicable only to the
389 are available only for the
393 access methods because they each imply that the keys have an inherent
394 order which does not change.
397 routines return -1 on error (setting
399 0 on success and 1 if there are no key/data pairs less than or greater
400 than the specified or current key.
403 access method is being used, and if the database file
404 is a character special file and no complete key/data pairs are currently
409 A pointer to a routine to flush any cached information to disk.
410 If the database is in memory only, the
412 routine has no effect and will always succeed.
416 argument may be set to the following value:
417 .Bl -tag -width indent
421 access method is being used, this flag causes
424 routine to apply to the btree file which underlies the
425 recno file, not the recno file itself.
430 manual page for more information.)
434 routines return -1 on error (setting
439 Access to all file types is based on key/data pairs.
440 Both keys and data are represented by the following data structure:
450 structure are defined as follows:
451 .Bl -tag -width "data"
453 A pointer to a byte string.
455 The length of the byte string.
458 Key and data byte strings may reference strings of essentially unlimited
459 length although any two of them must fit into available memory at the same
461 It should be noted that the access methods provide no guarantees about
462 byte string alignment.
466 routine may fail and set
468 for any of the errors specified for the library routines
475 A file is incorrectly formatted.
477 An argument has been specified (hash function, pad byte etc.) that is
478 incompatible with the current file specification or which is not
479 meaningful for the function (for example, use of the cursor without
480 prior initialization) or there is a mismatch between the version
481 number of file and the software.
486 routines may fail and set
488 for any of the errors specified for the library routines
502 routines may fail and set
504 for any of the errors specified for the library routines
513 routines will fail and set
517 for in memory databases.
521 routines may fail and set
523 for any of the errors specified for the library routine
531 .%T "LIBTP: Portable, Modular Transactions for UNIX"
534 .%R "USENIX proceedings"
541 .Dq "data base thang" ,
543 because noone could think of a reasonable name that wasn't already used.
545 The file descriptor interface is a kluge and will be deleted in a
546 future version of the interface.
548 None of the access methods provide any form of concurrent access,
549 locking, or transactions.