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