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.\" @(#)btree.3 8.4 (Berkeley) 8/18/94 29.\" 30.Dd August 18, 1994 31.Dt BTREE 3 32.Os 33.Sh NAME 34.Nm btree 35.Nd "btree database access method" 36.Sh SYNOPSIS 37.In sys/types.h 38.In db.h 39.Sh DESCRIPTION 40The routine 41.Fn dbopen 42is the library interface to database files. 43One of the supported file formats is 44.Nm 45files. 46The general description of the database access methods is in 47.Xr dbopen 3 , 48this manual page describes only the 49.Nm 50specific information. 51.Pp 52The 53.Nm 54data structure is a sorted, balanced tree structure storing 55associated key/data pairs. 56.Pp 57The 58.Nm 59access method specific data structure provided to 60.Fn dbopen 61is defined in the 62.In db.h 63include file as follows: 64.Bd -literal 65typedef struct { 66 u_long flags; 67 u_int cachesize; 68 int maxkeypage; 69 int minkeypage; 70 u_int psize; 71 int (*compare)(const DBT *key1, const DBT *key2); 72 size_t (*prefix)(const DBT *key1, const DBT *key2); 73 int lorder; 74} BTREEINFO; 75.Ed 76.Pp 77The elements of this structure are as follows: 78.Bl -tag -width indent 79.It Va flags 80The flag value is specified by 81.Em or Ns 'ing 82any of the following values: 83.Bl -tag -width indent 84.It Dv R_DUP 85Permit duplicate keys in the tree, i.e., permit insertion if the key to be 86inserted already exists in the tree. 87The default behavior, as described in 88.Xr dbopen 3 , 89is to overwrite a matching key when inserting a new key or to fail if 90the 91.Dv R_NOOVERWRITE 92flag is specified. 93The 94.Dv R_DUP 95flag is overridden by the 96.Dv R_NOOVERWRITE 97flag, and if the 98.Dv R_NOOVERWRITE 99flag is specified, attempts to insert duplicate keys into 100the tree will fail. 101.Pp 102If the database contains duplicate keys, the order of retrieval of 103key/data pairs is undefined if the 104.Va get 105routine is used, however, 106.Va seq 107routine calls with the 108.Dv R_CURSOR 109flag set will always return the logical 110.Dq first 111of any group of duplicate keys. 112.El 113.It Va cachesize 114A suggested maximum size (in bytes) of the memory cache. 115This value is 116.Em only 117advisory, and the access method will allocate more memory rather than fail. 118Since every search examines the root page of the tree, caching the most 119recently used pages substantially improves access time. 120In addition, physical writes are delayed as long as possible, so a moderate 121cache can reduce the number of I/O operations significantly. 122Obviously, using a cache increases (but only increases) the likelihood of 123corruption or lost data if the system crashes while a tree is being modified. 124If 125.Va cachesize 126is 0 (no size is specified) a default cache is used. 127.It Va maxkeypage 128The maximum number of keys which will be stored on any single page. 129Not currently implemented. 130.\" The maximum number of keys which will be stored on any single page. 131.\" Because of the way the 132.\" .Nm 133.\" data structure works, 134.\" .Va maxkeypage 135.\" must always be greater than or equal to 2. 136.\" If 137.\" .Va maxkeypage 138.\" is 0 (no maximum number of keys is specified) the page fill factor is 139.\" made as large as possible (which is almost invariably what is wanted). 140.It Va minkeypage 141The minimum number of keys which will be stored on any single page. 142This value is used to determine which keys will be stored on overflow 143pages, i.e., if a key or data item is longer than the pagesize divided 144by the minkeypage value, it will be stored on overflow pages instead 145of in the page itself. 146If 147.Va minkeypage 148is 0 (no minimum number of keys is specified) a value of 2 is used. 149.It Va psize 150Page size is the size (in bytes) of the pages used for nodes in the tree. 151The minimum page size is 512 bytes and the maximum page size is 64K. 152If 153.Va psize 154is 0 (no page size is specified) a page size is chosen based on the 155underlying file system I/O block size. 156.It Va compare 157Compare is the key comparison function. 158It must return an integer less than, equal to, or greater than zero if the 159first key argument is considered to be respectively less than, equal to, 160or greater than the second key argument. 161The same comparison function must be used on a given tree every time it 162is opened. 163If 164.Va compare 165is 166.Dv NULL 167(no comparison function is specified), the keys are compared 168lexically, with shorter keys considered less than longer keys. 169.It Va prefix 170The 171.Va prefix 172element 173is 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.Va 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.Va prefix 190is 191.Dv NULL 192and a comparison routine is specified, no prefix comparison is 193done. 194.It Va lorder 195The byte order for integers in the stored database metadata. 196The number should represent the order as an integer; for example, 197big endian order would be the number 4,321. 198If 199.Va lorder 200is 0 (no order is specified) the current host order is used. 201.El 202.Pp 203If the file already exists (and the 204.Dv O_TRUNC 205flag is not specified), the 206values specified for the 207.Va flags , lorder 208and 209.Va psize 210arguments 211are ignored 212in favor of the values used when the tree was created. 213.Pp 214Forward sequential scans of a tree are from the least key to the greatest. 215.Pp 216Space freed up by deleting key/data pairs from the tree is never reclaimed, 217although it is normally made available for reuse. 218This means that the 219.Nm 220storage structure is grow-only. 221The only solutions are to avoid excessive deletions, or to create a fresh 222tree periodically from a scan of an existing one. 223.Pp 224Searches, insertions, and deletions in a 225.Nm 226will all complete in 227O lg base N where base is the average fill factor. 228Often, inserting ordered data into 229.Nm Ns s 230results in a low fill factor. 231This implementation has been modified to make ordered insertion the best 232case, resulting in a much better than normal page fill factor. 233.Sh ERRORS 234The 235.Nm 236access method routines may fail and set 237.Va errno 238for any of the errors specified for the library routine 239.Xr dbopen 3 . 240.Sh SEE ALSO 241.Xr dbopen 3 , 242.Xr hash 3 , 243.Xr mpool 3 , 244.Xr recno 3 245.Rs 246.%T "The Ubiquitous B-tree" 247.%A Douglas Comer 248.%J "ACM Comput. Surv. 11" 249.%N 2 250.%D June 1979 251.%P 121-138 252.Re 253.Rs 254.%A Bayer 255.%A Unterauer 256.%T "Prefix B-trees" 257.%J "ACM Transactions on Database Systems" 258.%N 1 259.%V Vol. 2 260.%D March 1977 261.%P 11-26 262.Re 263.Rs 264.%B "The Art of Computer Programming Vol. 3: Sorting and Searching" 265.%A D. E. Knuth 266.%D 1968 267.%P 471-480 268.Re 269.Sh BUGS 270Only big and little endian byte order is supported. 271