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