can't increment an lvalue, but gcc lets you anyway
[unix-history] / usr / src / lib / libc / db / man / btree.3
CommitLineData
f52ca292
KB
1.\" Copyright (c) 1990 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" %sccs.include.redist.man%
5.\"
4e3894e7 6.\" @(#)btree.3 5.4 (Berkeley) %G%
f52ca292
KB
7.\"
8.TH BTREE 3 ""
9.UC 7
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
71R_NOOVERWRITE flag is specified attempts to insert duplicate keys into
72the tree will fail.
73.IP
f52ca292
KB
74The order of retrieval of key/data pairs with duplicate keys is undefined,
75although a sequential retrieval call with the R_CURSOR flag set will always
76return the logical ``first'' of any group of duplicate keys.
77.RE
78.TP
79cachesize
80A suggested maximum size (in bytes) of the memory cache.
81This value is
82.B only
83advisory, and the access method will allocate more memory rather than fail.
84Since every search examines the root page of the tree, caching the most
85recently used pages substantially improves access time.
86In addition, physical writes are delayed as long as possible, so a moderate
87cache can reduce the number of I/O operations significantly.
88Obviously, using a cache increases (but only increases) the likelihood of
89corruption or lost data if the system crashes while a tree is being modified.
90If
91.I cachesize
92is 0 (no size is specified) a default cache is used.
93.TP
94psize
95Page size is the size (in bytes) of the pages used for nodes in the tree.
96The minimum page size is 512 bytes and the maximum page size is 64K.
97If
98.I psize
99is 0 (no page size is specified) a page size is chosen based on the
100underlying file system I/O block size.
101.TP
102lorder
103The byte order for integers in the stored database metadata.
104The number should represent the order as an integer; for example,
105big endian order would be the number 4,321.
106If
107.I lorder
108is 0 (no order is specified) the current host order is used.
8b4acb9e
KB
109.\" .TP
110.\" maxkeypage
111.\" The maximum number of keys which will be stored on any single page.
112.\" Because of the way the btree data structure works,
113.\" .I maxkeypage
114.\" must always be greater than or equal to 2.
115.\" If
116.\" .I maxkeypage
117.\" is 0 (no maximum number of keys is specified) the page fill factor is
118.\" made as large as possible (which is almost invariably what is wanted).
f52ca292
KB
119.TP
120minkeypage
121The minimum number of keys which will be stored on any single page.
122This value is used to determine which keys will be stored on overflow
123pages, i.e. if a key or data item is longer than the pagesize divided
124by the minkeypage value, it will be stored on overflow pages instead
125of in the page itself.
126If
127.I minkeypage
128is 0 (no minimum number of keys is specified) a value of 2 is used.
129.TP
130compare
131Compare is the key comparison function.
132It must return an integer less than, equal to, or greater than zero if the
133first key argument is considered to be respectively less than, equal to,
134or greater than the second key argument.
135The same comparison function must be used on a given tree every time it
136is opened.
137If
138.I compare
139is NULL (no comparison function is specified), the keys are compared
140lexically, with shorter keys considered less than longer keys.
141.TP
142prefix
143Prefix is the prefix comparison function.
144If specified, this routine must return the number of bytes of the second key
145argument which are necessary to determine that it is greater than the first
146key argument.
147If the keys are equal, the key length should be returned.
148Note, the usefulness of this routine is very data dependent, but, in some
149data sets can produce significantly reduced tree sizes and search times.
150If
151.I prefix
152is NULL (no prefix function is specified),
153.B and
154no comparison function is specified, a default lexical comparison routine
155is used.
156If
157.I prefix
158is NULL and a comparison routine is specified, no prefix comparison is
159done.
160.PP
161If the file already exists (and the O_TRUNC flag is not specified), the
162values specified for the parameters flags, lorder and psize are ignored
163in favor of the values used when the tree was created.
164.PP
165Forward sequential scans of a tree are from the least key to the greatest.
166.PP
167Space freed up by deleting key/data pairs from the tree is never reclaimed,
168although it is normally made available for reuse.
169This means that the btree storage structure is grow-only.
170The only solutions are to avoid excessive deletions, or to create a fresh
171tree periodically from a scan of an existing one.
172.PP
173Searches, insertions, and deletions in a btree will all complete in
174O lg base N where base is the average fill factor.
175Often, inserting ordered data into btrees results in a low fill factor.
176This implementation has been modified to make ordered insertion the best
177case, resulting in a much better than normal page fill factor.
178.SH "SEE ALSO"
179.IR dbopen (3),
180.IR hash (3),
181.IR mpool (3),
182.IR recno (3)
183.br
184.IR "The Ubiquitous B-tree" ,
185Douglas Comer, ACM Comput. Surv. 11, 2 (June 1979), 121-138.
186.br
187.IR "The Art of Computer Programming Vol. 3: Sorting and Searching" ,
188D.E. Knuth, 1968, pp 471-480.
189.SH BUGS
190Only big and little endian byte order is supported.