xref: /openbsd-src/lib/libc/db/man/btree.3 (revision d4741794dd2f512d997014f8bd85fbb24d935059)
1.\"	$OpenBSD: btree.3,v 1.23 2015/09/10 10:20:55 jmc 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: September 10 2015 $
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
78.Tn OR Ns 'ing
79any of the following values:
80.Bl -tag -width XXXXX
81.It Dv R_DUP
82Permit duplicate keys in the tree, i.e., permit insertion if the key to be
83inserted already exists in the tree.
84The default behavior, as described in
85.Xr dbopen 3 ,
86is to overwrite a matching key when inserting a new key or to fail if
87the
88.Dv R_NOOVERWRITE
89flag is specified.
90The
91.Dv R_DUP
92flag is overridden by the
93.Dv R_NOOVERWRITE
94flag, and if the
95.Dv R_NOOVERWRITE
96flag is specified, attempts to insert duplicate keys into
97the tree will fail.
98.Pp
99If the database contains duplicate keys, the order of retrieval of
100key/data pairs is undefined if the
101.Fn get
102routine is used; however,
103.Fn seq
104routine calls with the
105.Dv R_CURSOR
106flag set will always return the logical
107.Dq first
108of any group of duplicate keys.
109.El
110.It Fa cachesize
111A suggested maximum size (in bytes) of the memory cache.
112This value is
113.Em only
114advisory, and the access method will allocate more memory rather than fail.
115Since every search examines the root page of the tree, caching the most
116recently used pages substantially improves access time.
117In addition, physical writes are delayed as long as possible, so a moderate
118cache can reduce the number of I/O operations significantly.
119Obviously, using a cache increases (but only increases) the likelihood of
120corruption or lost data if the system crashes while a tree is being modified.
121If
122.Fa cachesize
123is 0 (no size is specified) a default cache is used.
124.It Fa maxkeypage
125The maximum number of keys which will be stored on any single page.
126Not currently implemented.
127.It Fa minkeypage
128The minimum number of keys which will be stored on any single page.
129This value is used to determine which keys will be stored on overflow
130pages, i.e., if a key or data item is longer than the pagesize divided
131by the minkeypage value, it will be stored on overflow pages instead
132of in the page itself.
133If
134.Fa minkeypage
135is 0 (no minimum number of keys is specified) a value of 2 is used.
136.It Fa psize
137Page size is the size (in bytes) of the pages used for nodes in the tree.
138The minimum page size is 512 bytes and the maximum page size is 64K.
139If
140.Fa psize
141is 0 (no page size is specified) a page size is chosen based on the
142underlying file system I/O block size.
143.It Fa compare
144Compare is the key comparison function.
145It must return an integer less than, equal to, or greater than zero if the
146first key argument is considered to be respectively less than, equal to,
147or greater than the second key argument.
148The same comparison function must be used on a given tree every time it
149is opened.
150If
151.Fa compare
152is
153.Dv NULL
154(no comparison function is specified), the keys are compared
155lexically, with shorter keys considered less than longer keys.
156.It Fa prefix
157Prefix is the prefix comparison function.
158If specified, this routine must return the number of bytes of the second key
159argument which are necessary to determine that it is greater than the first
160key argument.
161If the keys are equal, the key length should be returned.
162Note, the usefulness of this routine is very data dependent, but in some
163data sets it can produce significantly reduced tree sizes and search times.
164If
165.Fa prefix
166is
167.Dv NULL
168(no prefix function is specified),
169.Em and
170no comparison function is specified, a default lexical comparison routine
171is used.
172If
173.Fa prefix
174is
175.Dv NULL
176and a comparison routine is specified, no prefix comparison is done.
177.It Fa lorder
178The byte order for integers in the stored database metadata.
179The number should represent the order as an integer; for example,
180big endian order would be the number 4,321.
181If
182.Fa lorder
183is 0 (no order is specified) the current host order is used.
184.El
185.Pp
186If the file already exists (and the
187.Dv O_TRUNC
188flag is not specified), the
189values specified for the parameters
190.Fa flags ,
191.Fa lorder ,
192and
193.Fa psize
194are ignored in favor of the values used when the tree was created.
195.Pp
196Forward sequential scans of a tree are from the least key to the greatest.
197.Pp
198Space freed up by deleting key/data pairs from the tree is never reclaimed,
199although it is normally made available for reuse.
200This means that the btree storage structure is grow-only.
201The only solutions are to avoid excessive deletions, or to create a fresh
202tree periodically from a scan of an existing one.
203.Pp
204Searches, insertions, and deletions in a btree will all complete in
205O(lg\ base\ N) where base is the average fill factor.
206Often, inserting ordered data into btrees results in a low fill factor.
207This implementation has been modified to make ordered insertion the best
208case, resulting in a much better than normal page fill factor.
209.Sh ERRORS
210The
211.Nm
212access method routines may fail and set
213.Va errno
214for any of the errors specified for the library routine
215.Xr dbopen 3 .
216.Sh SEE ALSO
217.Xr dbopen 3 ,
218.Xr hash 3 ,
219.Xr recno 3
220.Rs
221.%T "The Ubiquitous B-tree"
222.%A Douglas Comer
223.%J ACM Comput. Surv. 11
224.%D June 1979
225.%P pp 121-138
226.Re
227.Rs
228.%T "Prefix B-trees"
229.%A Rudolf Bayer
230.%A Karl Unterauer
231.%J ACM Transactions on Database Systems
232.%V Vol. 2 , 1
233.%D March 1977
234.%P pp 11-26
235.Re
236.Rs
237.%B "The Art of Computer Programming Vol. 3: Sorting and Searching"
238.%A D. E. Knuth
239.%D 1968
240.%P pp 471-480
241.Re
242.Sh BUGS
243Only big and little endian byte order is supported.
244