1 .\" Copyright (c) 1990, 1993
2 .\" The Regents of the University of California. All rights reserved.
4 .\" SPDX-License-Identifier: BSD-4-Clause-UC
6 .\" @(#)btree.3 8.4 (Berkeley) 8/18/94
8 .TH btree 3 (date) "Linux man-pages (unreleased)"
11 btree \- btree database access method
14 .RI ( libc ", " \-lc )
18 #include <sys/types.h>
24 This page documents interfaces provided up until glibc 2.1.
25 Since glibc 2.2, glibc no longer provides these interfaces.
26 Probably, you are looking for the APIs provided by the
32 is the library interface to database files.
33 One of the supported file formats is btree files.
34 The general description of the database access methods is in
36 this manual page describes only the btree-specific information.
38 The btree data structure is a sorted, balanced tree structure storing
39 associated key/data pairs.
41 The btree access-method-specific data structure provided to
45 include file as follows:
51 unsigned int cachesize;
55 int (*compare)(const DBT *key1, const DBT *key2);
56 size_t (*prefix)(const DBT *key1, const DBT *key2);
62 The elements of this structure are as follows:
65 The flag value is specified by ORing any of the following values:
69 Permit duplicate keys in the tree, that is,
70 permit insertion if the key to be
71 inserted already exists in the tree.
72 The default behavior, as described in
74 is to overwrite a matching key when inserting a new key or to fail if
80 flag is overridden by the
84 flag is specified, attempts to insert duplicate keys into
87 If the database contains duplicate keys, the order of retrieval of
88 key/data pairs is undefined if the
90 routine is used, however,
92 routine calls with the
94 flag set will always return the logical
95 "first" of any group of duplicate keys.
99 A suggested maximum size (in bytes) of the memory cache.
102 advisory, and the access method will allocate more memory rather than fail.
103 Since every search examines the root page of the tree, caching the most
104 recently used pages substantially improves access time.
105 In addition, physical writes are delayed as long as possible, so a moderate
106 cache can reduce the number of I/O operations significantly.
107 Obviously, using a cache increases (but only increases) the likelihood of
108 corruption or lost data if the system crashes while a tree is being modified.
111 is 0 (no size is specified), a default cache is used.
114 The maximum number of keys which will be stored on any single page.
115 Not currently implemented.
116 .\" The maximum number of keys which will be stored on any single page.
117 .\" Because of the way the btree data structure works,
119 .\" must always be greater than or equal to 2.
122 .\" is 0 (no maximum number of keys is specified), the page fill factor is
123 .\" made as large as possible (which is almost invariably what is wanted).
126 The minimum number of keys which will be stored on any single page.
127 This value is used to determine which keys will be stored on overflow
128 pages, that is, if a key or data item is longer than the pagesize divided
129 by the minkeypage value, it will be stored on overflow pages instead
130 of in the page itself.
133 is 0 (no minimum number of keys is specified), a value of 2 is used.
136 Page size is the size (in bytes) of the pages used for nodes in the tree.
137 The minimum page size is 512 bytes and the maximum page size is 64\ KiB.
140 is 0 (no page size is specified), a page size is chosen based on the
141 underlying filesystem I/O block size.
144 Compare is the key comparison function.
145 It must return an integer less than, equal to, or greater than zero if the
146 first key argument is considered to be respectively less than, equal to,
147 or greater than the second key argument.
148 The same comparison function must be used on a given tree every time it
152 is NULL (no comparison function is specified), the keys are compared
153 lexically, with shorter keys considered less than longer keys.
156 Prefix is the prefix comparison function.
157 If specified, this routine must return the number of bytes of the second key
158 argument which are necessary to determine that it is greater than the first
160 If the keys are equal, the key length should be returned.
161 Note, the usefulness of this routine is very data-dependent, but, in some
162 data sets can produce significantly reduced tree sizes and search times.
165 is NULL (no prefix function is specified),
167 no comparison function is specified, a default lexical comparison routine
171 is NULL and a comparison routine is specified, no prefix comparison is
175 The byte order for integers in the stored database metadata.
176 The number should represent the order as an integer; for example,
177 big endian order would be the number 4,321.
180 is 0 (no order is specified), the current host order is used.
182 If the file already exists (and the
184 flag is not specified), the
185 values specified for the arguments
191 in favor of the values used when the tree was created.
193 Forward sequential scans of a tree are from the least key to the greatest.
195 Space freed up by deleting key/data pairs from the tree is never reclaimed,
196 although it is normally made available for reuse.
197 This means that the btree storage structure is grow-only.
198 The only solutions are to avoid excessive deletions, or to create a fresh
199 tree periodically from a scan of an existing one.
201 Searches, insertions, and deletions in a btree will all complete in
202 O lg base N where base is the average fill factor.
203 Often, inserting ordered data into btrees results in a low fill factor.
204 This implementation has been modified to make ordered insertion the best
205 case, resulting in a much better than normal page fill factor.
209 access method routines may fail and set
211 for any of the errors specified for the library routine
214 Only big and little endian byte order is supported.
221 .IR "The Ubiquitous B-tree" ,
222 Douglas Comer, ACM Comput. Surv. 11, 2 (June 1979), 121-138.
224 .IR "Prefix B-trees" ,
225 Bayer and Unterauer, ACM Transactions on Database Systems, Vol. 2, 1
228 .IR "The Art of Computer Programming Vol. 3: Sorting and Searching" ,
229 D.E. Knuth, 1968, pp 471-480.