1.\" Copyright (c) 1990, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 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. 15.\" 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 26.\" SUCH DAMAGE. 27.\" 28.Dd September 10, 2010 29.Dt DBOPEN 3 30.Os 31.Sh NAME 32.Nm dbopen 33.Nd "database access methods" 34.Sh SYNOPSIS 35.In sys/types.h 36.In db.h 37.In fcntl.h 38.In limits.h 39.Ft DB * 40.Fn dbopen "const char *file" "int flags" "int mode" "DBTYPE type" "const void *openinfo" 41.Sh DESCRIPTION 42The 43.Fn dbopen 44function 45is the library interface to database files. 46The supported file formats are btree, hashed and UNIX file oriented. 47The btree format is a representation of a sorted, balanced tree structure. 48The hashed format is an extensible, dynamic hashing scheme. 49The flat-file format is a byte stream file with fixed or variable length 50records. 51The formats and file format specific information are described in detail 52in their respective manual pages 53.Xr btree 3 , 54.Xr hash 3 55and 56.Xr recno 3 . 57.Pp 58The 59.Fn dbopen 60function 61opens 62.Fa file 63for reading and/or writing. 64Files never intended to be preserved on disk may be created by setting 65the 66.Fa file 67argument to 68.Dv NULL . 69.Pp 70The 71.Fa flags 72and 73.Fa mode 74arguments 75are as specified to the 76.Xr open 2 77routine, however, only the 78.Dv O_CREAT , O_EXCL , O_EXLOCK , O_NOFOLLOW , O_NONBLOCK , 79.Dv O_RDONLY , O_RDWR , O_SHLOCK , O_SYNC, O_WRONLY, 80and 81.Dv O_TRUNC 82flags are meaningful. 83.\"Three additional options may be specified by 84.\".Em or Ns 'ing 85.\"them into the 86.\".Fa flags 87.\"argument. 88.\".Bl -tag -width indent 89.\".It Dv DB_LOCK 90.\"Do the necessary locking in the database to support concurrent access. 91.\"If concurrent access is not needed or the database is read-only this 92.\"flag should not be set, as it tends to have an associated performance 93.\"penalty. 94.\".It Dv DB_SHMEM 95.\"Place the underlying memory pool used by the database in shared 96.\"memory. 97.\"Necessary for concurrent access. 98.\".It Dv DB_TXN 99.\"Support transactions in the database. 100.\"The 101.\".Dv DB_LOCK 102.\"and 103.\".Dv DB_SHMEM 104.\"flags must be set as well. 105.\".El 106.Pp 107The 108.Fa type 109argument is of type 110.Ft DBTYPE 111(as defined in the 112.In db.h 113include file) and 114may be set to 115.Dv DB_BTREE , DB_HASH 116or 117.Dv DB_RECNO . 118.Pp 119The 120.Fa openinfo 121argument is a pointer to an access method specific structure described 122in the access method's manual page. 123If 124.Fa openinfo 125is 126.Dv NULL , 127each access method will use defaults appropriate for the system 128and the access method. 129.Pp 130The 131.Fn dbopen 132function 133returns a pointer to a 134.Ft DB 135structure on success and 136.Dv NULL 137on error. 138The 139.Ft DB 140structure is defined in the 141.In db.h 142include file, and contains at 143least the following fields: 144.Bd -literal 145typedef struct { 146 DBTYPE type; 147 int (*close)(DB *db); 148 int (*del)(const DB *db, const DBT *key, u_int flags); 149 int (*fd)(const DB *db); 150 int (*get)(const DB *db, const DBT *key, DBT *data, u_int flags); 151 int (*put)(const DB *db, DBT *key, const DBT *data, 152 u_int flags); 153 int (*sync)(const DB *db, u_int flags); 154 int (*seq)(const DB *db, DBT *key, DBT *data, u_int flags); 155} DB; 156.Ed 157.Pp 158These elements describe a database type and a set of functions performing 159various actions. 160These functions take a pointer to a structure as returned by 161.Fn dbopen , 162and sometimes one or more pointers to key/data structures and a flag value. 163.Bl -tag -width indent 164.It Va type 165The type of the underlying access method (and file format). 166.It Va close 167A pointer to a routine to flush any cached information to disk, free any 168allocated resources, and close the underlying file(s). 169Since key/data pairs may be cached in memory, failing to sync the file 170with a 171.Va close 172or 173.Va sync 174function may result in inconsistent or lost information. 175.Va close 176routines return -1 on error (setting 177.Va errno ) 178and 0 on success. 179.It Va del 180A pointer to a routine to remove key/data pairs from the database. 181.Pp 182The 183.Fa flags 184argument 185may be set to the following value: 186.Bl -tag -width indent 187.It Dv R_CURSOR 188Delete the record referenced by the cursor. 189The cursor must have previously been initialized. 190.El 191.Pp 192.Va delete 193routines return -1 on error (setting 194.Va errno ) , 1950 on success, and 1 if the specified 196.Fa key 197was not in the file. 198.It Va fd 199A pointer to a routine which returns a file descriptor representative 200of the underlying database. 201A file descriptor referencing the same file will be returned to all 202processes which call 203.Fn dbopen 204with the same 205.Fa file 206name. 207This file descriptor may be safely used as an argument to the 208.Xr fcntl 2 209and 210.Xr flock 2 211locking functions. 212The file descriptor is not necessarily associated with any of the 213underlying files used by the access method. 214No file descriptor is available for in memory databases. 215.Va \&Fd 216routines return -1 on error (setting 217.Va errno ) , 218and the file descriptor on success. 219.It Va get 220A pointer to a routine which is the interface for keyed retrieval from 221the database. 222The address and length of the data associated with the specified 223.Fa key 224are returned in the structure referenced by 225.Fa data . 226.Va get 227routines return -1 on error (setting 228.Va errno ) , 2290 on success, and 1 if the 230.Fa key 231was not in the file. 232.It Va put 233A pointer to a routine to store key/data pairs in the database. 234.Pp 235The 236.Fa flags 237argument 238may be set to one of the following values: 239.Bl -tag -width indent 240.It Dv R_CURSOR 241Replace the key/data pair referenced by the cursor. 242The cursor must have previously been initialized. 243.It Dv R_IAFTER 244Append the data immediately after the data referenced by 245.Fa key , 246creating a new key/data pair. 247The record number of the appended key/data pair is returned in the 248.Fa key 249structure. 250(Applicable only to the 251.Dv DB_RECNO 252access method.) 253.It Dv R_IBEFORE 254Insert the data immediately before the data referenced by 255.Fa key , 256creating a new key/data pair. 257The record number of the inserted key/data pair is returned in the 258.Fa key 259structure. 260(Applicable only to the 261.Dv DB_RECNO 262access method.) 263.It Dv R_NOOVERWRITE 264Enter the new key/data pair only if the key does not previously exist. 265.It Dv R_SETCURSOR 266Store the key/data pair, setting or initializing the position of the 267cursor to reference it. 268(Applicable only to the 269.Dv DB_BTREE 270and 271.Dv DB_RECNO 272access methods.) 273.El 274.Pp 275.Dv R_SETCURSOR 276is available only for the 277.Dv DB_BTREE 278and 279.Dv DB_RECNO 280access 281methods because it implies that the keys have an inherent order 282which does not change. 283.Pp 284.Dv R_IAFTER 285and 286.Dv R_IBEFORE 287are available only for the 288.Dv DB_RECNO 289access method because they each imply that the access method is able to 290create new keys. 291This is only true if the keys are ordered and independent, record numbers 292for example. 293.Pp 294The default behavior of the 295.Va put 296routines is to enter the new key/data pair, replacing any previously 297existing key. 298.Pp 299.Va put 300routines return -1 on error (setting 301.Va errno ) , 3020 on success, and 1 if the 303.Dv R_NOOVERWRITE 304flag 305was set and the key already exists in the file. 306.It Va seq 307A pointer to a routine which is the interface for sequential 308retrieval from the database. 309The address and length of the key are returned in the structure 310referenced by 311.Fa key , 312and the address and length of the data are returned in the 313structure referenced 314by 315.Fa data . 316.Pp 317Sequential key/data pair retrieval may begin at any time, and the 318position of the 319.Dq cursor 320is not affected by calls to the 321.Va del , 322.Va get , 323.Va put , 324or 325.Va sync 326routines. 327Modifications to the database during a sequential scan will be reflected 328in the scan, i.e., records inserted behind the cursor will not be returned 329while records inserted in front of the cursor will be returned. 330.Pp 331The 332.Fa flags 333argument 334.Em must 335be set to one of the following values: 336.Bl -tag -width indent 337.It Dv R_CURSOR 338The data associated with the specified key is returned. 339This differs from the 340.Va get 341routines in that it sets or initializes the cursor to the location of 342the key as well. 343(Note, for the 344.Dv DB_BTREE 345access method, the returned key is not necessarily an 346exact match for the specified key. 347The returned key is the smallest key greater than or equal to the specified 348key, permitting partial key matches and range searches.) 349.It Dv R_FIRST 350The first key/data pair of the database is returned, and the cursor 351is set or initialized to reference it. 352.It Dv R_LAST 353The last key/data pair of the database is returned, and the cursor 354is set or initialized to reference it. 355(Applicable only to the 356.Dv DB_BTREE 357and 358.Dv DB_RECNO 359access methods.) 360.It Dv R_NEXT 361Retrieve the key/data pair immediately after the cursor. 362If the cursor is not yet set, this is the same as the 363.Dv R_FIRST 364flag. 365.It Dv R_PREV 366Retrieve the key/data pair immediately before the cursor. 367If the cursor is not yet set, this is the same as the 368.Dv R_LAST 369flag. 370(Applicable only to the 371.Dv DB_BTREE 372and 373.Dv DB_RECNO 374access methods.) 375.El 376.Pp 377.Dv R_LAST 378and 379.Dv R_PREV 380are available only for the 381.Dv DB_BTREE 382and 383.Dv DB_RECNO 384access methods because they each imply that the keys have an inherent 385order which does not change. 386.Pp 387.Va seq 388routines return -1 on error (setting 389.Va errno ) , 3900 on success and 1 if there are no key/data pairs less than or greater 391than the specified or current key. 392If the 393.Dv DB_RECNO 394access method is being used, and if the database file 395is a character special file and no complete key/data pairs are currently 396available, the 397.Va seq 398routines return 2. 399.It Va sync 400A pointer to a routine to flush any cached information to disk. 401If the database is in memory only, the 402.Va sync 403routine has no effect and will always succeed. 404.Pp 405The 406.Fa flags 407argument may be set to the following value: 408.Bl -tag -width indent 409.It Dv R_RECNOSYNC 410If the 411.Dv DB_RECNO 412access method is being used, this flag causes 413the 414.Va sync 415routine to apply to the btree file which underlies the 416recno file, not the recno file itself. 417(See the 418.Va bfname 419field of the 420.Xr recno 3 421manual page for more information.) 422.El 423.Pp 424.Va sync 425routines return -1 on error (setting 426.Va errno ) 427and 0 on success. 428.El 429.Sh "KEY/DATA PAIRS" 430Access to all file types is based on key/data pairs. 431Both keys and data are represented by the following data structure: 432.Bd -literal 433typedef struct { 434 void *data; 435 size_t size; 436} DBT; 437.Ed 438.Pp 439The elements of the 440.Ft DBT 441structure are defined as follows: 442.Bl -tag -width "data" 443.It Va data 444A pointer to a byte string. 445.It Va size 446The length of the byte string. 447.El 448.Pp 449Key and data byte strings may reference strings of essentially unlimited 450length although any two of them must fit into available memory at the same 451time. 452It should be noted that the access methods provide no guarantees about 453byte string alignment. 454.Sh ERRORS 455The 456.Fn dbopen 457routine may fail and set 458.Va errno 459for any of the errors specified for the library routines 460.Xr open 2 461and 462.Xr malloc 3 463or the following: 464.Bl -tag -width Er 465.It Bq Er EFTYPE 466A file is incorrectly formatted. 467.It Bq Er EINVAL 468An argument has been specified (hash function, pad byte etc.) that is 469incompatible with the current file specification or which is not 470meaningful for the function (for example, use of the cursor without 471prior initialization) or there is a mismatch between the version 472number of file and the software. 473.El 474.Pp 475The 476.Va close 477routines may fail and set 478.Va errno 479for any of the errors specified for the library routines 480.Xr close 2 , 481.Xr read 2 , 482.Xr write 2 , 483.Xr free 3 , 484or 485.Xr fsync 2 . 486.Pp 487The 488.Va del , 489.Va get , 490.Va put 491and 492.Va seq 493routines may fail and set 494.Va errno 495for any of the errors specified for the library routines 496.Xr read 2 , 497.Xr write 2 , 498.Xr free 3 499or 500.Xr malloc 3 . 501.Pp 502The 503.Va fd 504routines will fail and set 505.Va errno 506to 507.Er ENOENT 508for in memory databases. 509.Pp 510The 511.Va sync 512routines may fail and set 513.Va errno 514for any of the errors specified for the library routine 515.Xr fsync 2 . 516.Sh SEE ALSO 517.Xr btree 3 , 518.Xr hash 3 , 519.Xr mpool 3 , 520.Xr recno 3 521.Rs 522.%T "LIBTP: Portable, Modular Transactions for UNIX" 523.%A Margo Seltzer 524.%A Michael Olson 525.%R "USENIX proceedings" 526.%D Winter 1992 527.Re 528.Sh BUGS 529The typedef 530.Ft DBT 531is a mnemonic for 532.Dq "data base thang" , 533and was used 534because noone could think of a reasonable name that was not already used. 535.Pp 536The file descriptor interface is a kluge and will be deleted in a 537future version of the interface. 538.Pp 539None of the access methods provide any form of concurrent access, 540locking, or transactions. 541