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