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