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