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