xref: /openbsd/lib/libc/db/man/btree.3 (revision 898184e3)
1.\"	$OpenBSD: btree.3,v 1.19 2007/05/31 19:19:27 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: May 31 2007 $
36.Dt BTREE 3
37.Os
38.Sh NAME
39.Nm btree
40.Nd btree database access method
41.Sh SYNOPSIS
42.Fd #include <sys/types.h>
43.Fd #include <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.Aq Pa 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``first'' of 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 fail.
114Since every search examines the root page of the tree, caching the most
115recently used pages substantially improves access time.
116In addition, physical writes are delayed as long as possible, so a moderate
117cache can reduce the number of I/O operations significantly.
118Obviously, using a cache increases (but only increases) the likelihood of
119corruption or lost data if the system crashes while a tree is being modified.
120If
121.Fa cachesize
122is 0 (no size is specified) a default cache is used.
123.It Fa maxkeypage
124The maximum number of keys which will be stored on any single page.
125Not currently implemented.
126.It Fa minkeypage
127The minimum number of keys which will be stored on any single page.
128This value is used to determine which keys will be stored on overflow
129pages, i.e., if a key or data item is longer than the pagesize divided
130by the minkeypage value, it will be stored on overflow pages instead
131of in the page itself.
132If
133.Fa minkeypage
134is 0 (no minimum number of keys is specified) a value of 2 is used.
135.It Fa psize
136Page size is the size (in bytes) of the pages used for nodes in the tree.
137The minimum page size is 512 bytes and the maximum page size is 64K.
138If
139.Fa psize
140is 0 (no page size is specified) a page size is chosen based on the
141underlying file system I/O block size.
142.It Fa compare
143Compare is the key comparison function.
144It must return an integer less than, equal to, or greater than zero if the
145first key argument is considered to be respectively less than, equal to,
146or greater than the second key argument.
147The same comparison function must be used on a given tree every time it
148is opened.
149If
150.Fa compare
151is
152.Dv NULL
153(no comparison function is specified), the keys are compared
154lexically, with shorter keys considered less than longer keys.
155.It Fa prefix
156Prefix is the prefix comparison function.
157If specified, this routine must return the number of bytes of the second key
158argument which are necessary to determine that it is greater than the first
159key argument.
160If the keys are equal, the key length should be returned.
161Note, the usefulness of this routine is very data dependent, but in some
162data sets it can produce significantly reduced tree sizes and search times.
163If
164.Fa prefix
165is
166.Dv NULL
167(no prefix function is specified),
168.Em and
169no comparison function is specified, a default lexical comparison routine
170is used.
171If
172.Fa prefix
173is
174.Dv NULL
175and a comparison routine is specified, no prefix comparison is done.
176.It Fa lorder
177The byte order for integers in the stored database metadata.
178The number should represent the order as an integer; for example,
179big endian order would be the number 4,321.
180If
181.Fa lorder
182is 0 (no order is specified) the current host order is used.
183.El
184.Pp
185If the file already exists (and the
186.Dv O_TRUNC
187flag is not specified), the
188values specified for the parameters
189.Fa flags ,
190.Fa lorder ,
191and
192.Fa psize
193are ignored in favor of the values used when the tree was created.
194.Pp
195Forward sequential scans of a tree are from the least key to the greatest.
196.Pp
197Space freed up by deleting key/data pairs from the tree is never reclaimed,
198although it is normally made available for reuse.
199This means that the btree storage structure is grow-only.
200The only solutions are to avoid excessive deletions, or to create a fresh
201tree periodically from a scan of an existing one.
202.Pp
203Searches, insertions, and deletions in a btree will all complete in
204O(lg\ base\ N) where base is the average fill factor.
205Often, inserting ordered data into btrees results in a low fill factor.
206This implementation has been modified to make ordered insertion the best
207case, resulting in a much better than normal page fill factor.
208.Sh ERRORS
209The
210.Nm
211access method routines may fail and set
212.Va errno
213for any of the errors specified for the library routine
214.Xr dbopen 3 .
215.Sh SEE ALSO
216.Xr dbopen 3 ,
217.Xr hash 3 ,
218.Xr mpool 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