1.\" $OpenBSD: btree.3,v 1.15 2001/06/23 14:42:12 deraadt Exp $ 2.\" $NetBSD: btree.3,v 1.6 1996/05/03 21:26:48 cgd Exp $ 3.\" 4.\" Copyright (c) 1997, Phillip F Knaack. All rights reserved. 5.\" 6.\" Copyright (c) 1990, 1993 7.\" The Regents of the University of California. All rights reserved. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 3. All advertising materials mentioning features or use of this software 18.\" must display the following acknowledgement: 19.\" This product includes software developed by the University of 20.\" California, Berkeley and its contributors. 21.\" 4. Neither the name of the University nor the names of its contributors 22.\" may be used to endorse or promote products derived from this software 23.\" without specific prior written permission. 24.\" 25.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35.\" SUCH DAMAGE. 36.\" 37.\" @(#)btree.3 8.4 (Berkeley) 8/18/94 38.\" 39.Dd August 18, 1994 40.Dt BTREE 3 41.Os 42.Sh NAME 43.Nm btree 44.Nd btree database access method 45.Sh SYNOPSIS 46.Fd #include <sys/types.h> 47.Fd #include <db.h> 48.Sh DESCRIPTION 49The 50.Fn dbopen 51routine is the library interface to database files. 52One of the supported file formats is btree files. 53The general description of the database access methods is in 54.Xr dbopen 3 . 55This manual page describes only the btree specific information. 56.Pp 57The btree data structure is a sorted, balanced tree structure storing 58associated key/data pairs. 59.Pp 60The btree access method specific data structure provided to 61.Fn dbopen 62is defined in the 63.Aq Pa db.h 64include file as follows: 65.Pp 66.Bl -item -compact 67typedef struct { 68.It 69.Bl -item -compact -inset -offset indent 70.It 71u_long flags; 72.It 73u_int cachesize; 74.It 75int maxkeypage; 76.It 77int minkeypage; 78.It 79u_int psize; 80.It 81int (*compare)(const DBT *key1, const DBT *key2); 82.It 83size_t (*prefix)(const DBT *key1, const DBT *key2); 84.It 85int lorder; 86.El 87.It 88} BTREEINFO; 89.El 90.Pp 91The elements of this structure are as follows: 92.Bl -tag -width "XXXXXX" 93.It Fa flags 94The flag value is specified by 95.Tn OR Ns 'ing 96any of the following values: 97.Bl -tag -width XXXXX 98.It Dv R_DUP 99Permit duplicate keys in the tree, i.e., permit insertion if the key to be 100inserted already exists in the tree. 101The default behavior, as described in 102.Xr dbopen 3 , 103is to overwrite a matching key when inserting a new key or to fail if 104the 105.Dv R_NOOVERWRITE 106flag is specified. 107The 108.Dv R_DUP 109flag is overridden by the 110.Dv R_NOOVERWRITE 111flag, and if the 112.Dv R_NOOVERWRITE 113flag is specified, attempts to insert duplicate keys into 114the tree will fail. 115.Pp 116If the database contains duplicate keys, the order of retrieval of 117key/data pairs is undefined if the 118.Fn get 119routine is used; however, 120.Fn seq 121routine calls with the 122.Dv R_CURSOR 123flag set will always return the logical 124``first'' of any group of duplicate keys. 125.El 126.It Fa cachesize 127A suggested maximum size (in bytes) of the memory cache. 128This value is 129.Em only 130advisory, and the access method will allocate more memory rather than fail. 131Since every search examines the root page of the tree, caching the most 132recently used pages substantially improves access time. 133In addition, physical writes are delayed as long as possible, so a moderate 134cache can reduce the number of I/O operations significantly. 135Obviously, using a cache increases (but only increases) the likelihood of 136corruption or lost data if the system crashes while a tree is being modified. 137If 138.Fa cachesize 139is 0 (no size is specified) a default cache is used. 140.It Fa maxkeypage 141The maximum number of keys which will be stored on any single page. 142Not currently implemented. 143.It Fa minkeypage 144The minimum number of keys which will be stored on any single page. 145This value is used to determine which keys will be stored on overflow 146pages, i.e., if a key or data item is longer than the pagesize divided 147by the minkeypage value, it will be stored on overflow pages instead 148of in the page itself. 149If 150.Fa minkeypage 151is 0 (no minimum number of keys is specified) a value of 2 is used. 152.It Fa psize 153Page size is the size (in bytes) of the pages used for nodes in the tree. 154The minimum page size is 512 bytes and the maximum page size is 64K. 155If 156.Fa psize 157is 0 (no page size is specified) a page size is chosen based on the 158underlying file system I/O block size. 159.It Fa compare 160Compare is the key comparison function. 161It must return an integer less than, equal to, or greater than zero if the 162first key argument is considered to be respectively less than, equal to, 163or greater than the second key argument. 164The same comparison function must be used on a given tree every time it 165is opened. 166If 167.Fa compare 168is 169.Dv NULL 170(no comparison function is specified), the keys are compared 171lexically, with shorter keys considered less than longer keys. 172.It Fa prefix 173Prefix is the prefix comparison function. 174If specified, this routine must return the number of bytes of the second key 175argument which are necessary to determine that it is greater than the first 176key argument. 177If the keys are equal, the key length should be returned. 178Note, the usefulness of this routine is very data dependent, but in some 179data sets can produce significantly reduced tree sizes and search times. 180If 181.Fa prefix 182is 183.Dv NULL 184(no prefix function is specified), 185.Em and 186no comparison function is specified, a default lexical comparison routine 187is used. 188If 189.Fa prefix 190is 191.Dv NULL 192and a comparison routine is specified, no prefix comparison is done. 193.It Fa lorder 194The byte order for integers in the stored database metadata. 195The number should represent the order as an integer; for example, 196big endian order would be the number 4,321. 197If 198.Fa lorder 199is 0 (no order is specified) the current host order is used. 200.El 201.Pp 202If the file already exists (and the 203.Dv O_TRUNC 204flag is not specified), the 205values specified for the parameters 206.Fa flags , 207.Fa lorder , 208and 209.Fa psize 210are ignored in favor of the values used when the tree was created. 211.Pp 212Forward sequential scans of a tree are from the least key to the greatest. 213.Pp 214Space freed up by deleting key/data pairs from the tree is never reclaimed, 215although it is normally made available for reuse. 216This means that the btree storage structure is grow-only. 217The only solutions are to avoid excessive deletions, or to create a fresh 218tree periodically from a scan of an existing one. 219.Pp 220Searches, insertions, and deletions in a btree will all complete in 221O(lg\ base\ N) where base is the average fill factor. 222Often, inserting ordered data into btrees results in a low fill factor. 223This implementation has been modified to make ordered insertion the best 224case, resulting in a much better than normal page fill factor. 225.Sh ERRORS 226The 227.Nm 228access method routines may fail and set 229.Va errno 230for any of the errors specified for the library routine 231.Xr dbopen 3 . 232.Sh SEE ALSO 233.Xr dbopen 3 , 234.Xr hash 3 , 235.Xr mpool 3 , 236.Xr recno 3 237.Rs 238.%T "The Ubiquitous B-tree" 239.%A Douglas Comer 240.%J ACM Comput. Surv. 11 241.%D June 1979 242.%P pp 121-138 243.Re 244.Rs 245.%T "Prefix B-trees" 246.%A Rudolf Bayer 247.%A Karl Unterauer 248.%J ACM Transactions on Database Systems 249.%V Vol. 2 , 1 250.%D March 1977 251.%P pp 11-26 252.Re 253.Rs 254.%B "The Art of Computer Programming Vol. 3: Sorting and Searching" 255.%A D. E. Knuth 256.%D 1968 257.%P pp 471-480 258.Re 259.Sh BUGS 260Only big and little endian byte order is supported. 261