OSDN Git Service

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