BSD 4_4 development
[unix-history] / .ref-6294d633e80db8e89697db796e6f6025d5af0cae / usr / src / lib / libc / db / man / btree.3
CommitLineData
81b62568
KB
1.\" Copyright (c) 1990, 1993
2.\" The Regents of the University of California. All rights reserved.
f52ca292
KB
3.\"
4.\" %sccs.include.redist.man%
5.\"
81b62568 6.\" @(#)btree.3 8.1 (Berkeley) %G%
f52ca292
KB
7.\"
8.TH BTREE 3 ""
c38379a5 9.\".UC 7
f52ca292
KB
10.SH NAME
11btree \- btree database access method
12.SH SYNOPSIS
13.nf
14.ft B
15#include <sys/types.h>
16#include <db.h>
17.ft R
18.fi
19.SH DESCRIPTION
20The routine
21.IR dbopen
22is the library interface to database files.
23One of the supported file formats is btree files.
24The general description of the database access methods is in
25.IR dbopen (3),
26this manual page describes only the btree specific information.
27.PP
28The btree data structure is a sorted, balanced tree structure storing
29associated key/data pairs.
30.PP
31The btree access method specific data structure provided to
32.I dbopen
33is defined in the <db.h> include file as follows:
34.PP
35typedef struct {
36.RS
37u_long flags;
38.br
39u_int cachesize;
40.br
41index_t psize;
42.br
43int lorder;
8b4acb9e
KB
44.\" .br
45.\" int maxkeypage;
f52ca292
KB
46.br
47int minkeypage;
48.br
49int (*compare)(const DBT *key1, const DBT *key2);
50.br
51int (*prefix)(const DBT *key1, const DBT *key2);
52.RE
53} BTREEINFO;
54.PP
55The elements of this structure are as follows:
56.TP
57flags
58The flag value is specified by
59.IR or 'ing
60any of the following values:
61.RS
62.TP
63R_DUP
d6d37eec
KB
64Permit duplicate keys in the tree, i.e. permit insertion if the key to be
65inserted already exists in the tree.
66The default behavior, as described in
4e3894e7 67.IR dbopen (3),
d6d37eec
KB
68is to overwrite a matching key when inserting a new key or to fail if
69the R_NOOVERWRITE flag is specified.
70The R_DUP flag is overridden by the R_NOOVERWRITE flag, and if the
c38379a5 71R_NOOVERWRITE flag is specified, attempts to insert duplicate keys into
d6d37eec
KB
72the tree will fail.
73.IP
c38379a5
KB
74If the database contains duplicate keys, the order of retrieval of
75key/data pairs is undefined if the
76.I get
77routine is used, however,
78.I seq
79routine calls with the R_CURSOR flag set will always return the logical
80``first'' of any group of duplicate keys.
f52ca292
KB
81.RE
82.TP
83cachesize
84A suggested maximum size (in bytes) of the memory cache.
85This value is
86.B only
87advisory, and the access method will allocate more memory rather than fail.
88Since every search examines the root page of the tree, caching the most
89recently used pages substantially improves access time.
90In addition, physical writes are delayed as long as possible, so a moderate
91cache can reduce the number of I/O operations significantly.
92Obviously, using a cache increases (but only increases) the likelihood of
93corruption or lost data if the system crashes while a tree is being modified.
94If
95.I cachesize
96is 0 (no size is specified) a default cache is used.
97.TP
98psize
99Page size is the size (in bytes) of the pages used for nodes in the tree.
100The minimum page size is 512 bytes and the maximum page size is 64K.
101If
102.I psize
103is 0 (no page size is specified) a page size is chosen based on the
104underlying file system I/O block size.
105.TP
106lorder
107The byte order for integers in the stored database metadata.
108The number should represent the order as an integer; for example,
109big endian order would be the number 4,321.
110If
111.I lorder
112is 0 (no order is specified) the current host order is used.
8b4acb9e
KB
113.\" .TP
114.\" maxkeypage
115.\" The maximum number of keys which will be stored on any single page.
116.\" Because of the way the btree data structure works,
117.\" .I maxkeypage
118.\" must always be greater than or equal to 2.
119.\" If
120.\" .I maxkeypage
121.\" is 0 (no maximum number of keys is specified) the page fill factor is
122.\" made as large as possible (which is almost invariably what is wanted).
f52ca292
KB
123.TP
124minkeypage
125The minimum number of keys which will be stored on any single page.
126This value is used to determine which keys will be stored on overflow
127pages, i.e. if a key or data item is longer than the pagesize divided
128by the minkeypage value, it will be stored on overflow pages instead
129of in the page itself.
130If
131.I minkeypage
132is 0 (no minimum number of keys is specified) a value of 2 is used.
133.TP
134compare
135Compare is the key comparison function.
136It must return an integer less than, equal to, or greater than zero if the
137first key argument is considered to be respectively less than, equal to,
138or greater than the second key argument.
139The same comparison function must be used on a given tree every time it
140is opened.
141If
142.I compare
143is NULL (no comparison function is specified), the keys are compared
144lexically, with shorter keys considered less than longer keys.
145.TP
146prefix
147Prefix is the prefix comparison function.
148If specified, this routine must return the number of bytes of the second key
149argument which are necessary to determine that it is greater than the first
150key argument.
151If the keys are equal, the key length should be returned.
152Note, the usefulness of this routine is very data dependent, but, in some
153data sets can produce significantly reduced tree sizes and search times.
154If
155.I prefix
156is NULL (no prefix function is specified),
157.B and
158no comparison function is specified, a default lexical comparison routine
159is used.
160If
161.I prefix
162is NULL and a comparison routine is specified, no prefix comparison is
163done.
164.PP
165If the file already exists (and the O_TRUNC flag is not specified), the
166values specified for the parameters flags, lorder and psize are ignored
167in favor of the values used when the tree was created.
168.PP
169Forward sequential scans of a tree are from the least key to the greatest.
170.PP
171Space freed up by deleting key/data pairs from the tree is never reclaimed,
172although it is normally made available for reuse.
173This means that the btree storage structure is grow-only.
174The only solutions are to avoid excessive deletions, or to create a fresh
175tree periodically from a scan of an existing one.
176.PP
177Searches, insertions, and deletions in a btree will all complete in
178O lg base N where base is the average fill factor.
179Often, inserting ordered data into btrees results in a low fill factor.
180This implementation has been modified to make ordered insertion the best
181case, resulting in a much better than normal page fill factor.
182.SH "SEE ALSO"
183.IR dbopen (3),
184.IR hash (3),
185.IR mpool (3),
186.IR recno (3)
48c2722a 187.sp
f52ca292
KB
188.IR "The Ubiquitous B-tree" ,
189Douglas Comer, ACM Comput. Surv. 11, 2 (June 1979), 121-138.
48c2722a
KB
190.sp
191.IR "Prefix B-trees" ,
192Bayer and Unterauer, ACM Transactions on Database Systems, Vol. 2, 1
193(March 1977), 11-26.
194.sp
f52ca292
KB
195.IR "The Art of Computer Programming Vol. 3: Sorting and Searching" ,
196D.E. Knuth, 1968, pp 471-480.
197.SH BUGS
198Only big and little endian byte order is supported.