Update.
[glibc.git] / db / btree / btree.h
blob45f7c94ed465674a9fc25d7de0f136c1c9739a03
1 /*-
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Mike Olson.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
36 * @(#)btree.h 8.11 (Berkeley) 8/17/94
39 /* Macros to set/clear/test flags. */
40 #define F_SET(p, f) (p)->flags |= (f)
41 #define F_CLR(p, f) (p)->flags &= ~(f)
42 #define F_ISSET(p, f) ((p)->flags & (f))
44 #include <mpool.h>
46 #ifdef _LIBC
47 /* In the GNU C library we must not pollute the namespace because libdb is
48 needed by libnss_db. */
49 #define mpool_open __mpool_open
50 #define mpool_filter __mpool_filter
51 #define mpool_new __mpool_new
52 #define mpool_get __mpool_get
53 #define mpool_put __mpool_put
54 #define mpool_sync __mpool_sync
55 #define mpool_close __mpool_close
56 #endif
58 #define DEFMINKEYPAGE (2) /* Minimum keys per page */
59 #define MINCACHE (5) /* Minimum cached pages */
60 #define MINPSIZE (512) /* Minimum page size */
63 * Page 0 of a btree file contains a copy of the meta-data. This page is also
64 * used as an out-of-band page, i.e. page pointers that point to nowhere point
65 * to page 0. Page 1 is the root of the btree.
67 #define P_INVALID 0 /* Invalid tree page number. */
68 #define P_META 0 /* Tree metadata page number. */
69 #define P_ROOT 1 /* Tree root page number. */
72 * There are five page layouts in the btree: btree internal pages (BINTERNAL),
73 * btree leaf pages (BLEAF), recno internal pages (RINTERNAL), recno leaf pages
74 * (RLEAF) and overflow pages. All five page types have a page header (PAGE).
75 * This implementation requires that values within structures NOT be padded.
76 * (ANSI C permits random padding.) If your compiler pads randomly you'll have
77 * to do some work to get this package to run.
79 typedef struct _page {
80 pgno_t pgno; /* this page's page number */
81 pgno_t prevpg; /* left sibling */
82 pgno_t nextpg; /* right sibling */
84 #define P_BINTERNAL 0x01 /* btree internal page */
85 #define P_BLEAF 0x02 /* leaf page */
86 #define P_OVERFLOW 0x04 /* overflow page */
87 #define P_RINTERNAL 0x08 /* recno internal page */
88 #define P_RLEAF 0x10 /* leaf page */
89 #define P_TYPE 0x1f /* type mask */
90 #define P_PRESERVE 0x20 /* never delete this chain of pages */
91 u_int32_t flags;
93 indx_t lower; /* lower bound of free space on page */
94 indx_t upper; /* upper bound of free space on page */
95 indx_t linp[1]; /* indx_t-aligned VAR. LENGTH DATA */
96 } PAGE;
98 /* First and next index. */
99 #define BTDATAOFF \
100 (sizeof(pgno_t) + sizeof(pgno_t) + sizeof(pgno_t) + \
101 sizeof(u_int32_t) + sizeof(indx_t) + sizeof(indx_t))
102 #define NEXTINDEX(p) (((p)->lower - BTDATAOFF) / sizeof(indx_t))
105 * For pages other than overflow pages, there is an array of offsets into the
106 * rest of the page immediately following the page header. Each offset is to
107 * an item which is unique to the type of page. The h_lower offset is just
108 * past the last filled-in index. The h_upper offset is the first item on the
109 * page. Offsets are from the beginning of the page.
111 * If an item is too big to store on a single page, a flag is set and the item
112 * is a { page, size } pair such that the page is the first page of an overflow
113 * chain with size bytes of item. Overflow pages are simply bytes without any
114 * external structure.
116 * The page number and size fields in the items are pgno_t-aligned so they can
117 * be manipulated without copying. (This presumes that 32 bit items can be
118 * manipulated on this system.)
120 #define LALIGN(n) (((n) + sizeof(pgno_t) - 1) & ~(sizeof(pgno_t) - 1))
121 #define NOVFLSIZE (sizeof(pgno_t) + sizeof(u_int32_t))
124 * For the btree internal pages, the item is a key. BINTERNALs are {key, pgno}
125 * pairs, such that the key compares less than or equal to all of the records
126 * on that page. For a tree without duplicate keys, an internal page with two
127 * consecutive keys, a and b, will have all records greater than or equal to a
128 * and less than b stored on the page associated with a. Duplicate keys are
129 * somewhat special and can cause duplicate internal and leaf page records and
130 * some minor modifications of the above rule.
132 typedef struct _binternal {
133 u_int32_t ksize; /* key size */
134 pgno_t pgno; /* page number stored on */
135 #define P_BIGDATA 0x01 /* overflow data */
136 #define P_BIGKEY 0x02 /* overflow key */
137 u_char flags;
138 char bytes[1]; /* data */
139 } BINTERNAL;
141 /* Get the page's BINTERNAL structure at index indx. */
142 #define GETBINTERNAL(pg, indx) \
143 ((BINTERNAL *)((char *)(pg) + (pg)->linp[indx]))
145 /* Get the number of bytes in the entry. */
146 #define NBINTERNAL(len) \
147 LALIGN(sizeof(u_int32_t) + sizeof(pgno_t) + sizeof(u_char) + (len))
149 /* Copy a BINTERNAL entry to the page. */
150 #define WR_BINTERNAL(p, size, pgno, flags) { \
151 *(u_int32_t *)p = size; \
152 p += sizeof(u_int32_t); \
153 *(pgno_t *)p = pgno; \
154 p += sizeof(pgno_t); \
155 *(u_char *)p = flags; \
156 p += sizeof(u_char); \
160 * For the recno internal pages, the item is a page number with the number of
161 * keys found on that page and below.
163 typedef struct _rinternal {
164 recno_t nrecs; /* number of records */
165 pgno_t pgno; /* page number stored below */
166 } RINTERNAL;
168 /* Get the page's RINTERNAL structure at index indx. */
169 #define GETRINTERNAL(pg, indx) \
170 ((RINTERNAL *)((char *)(pg) + (pg)->linp[indx]))
172 /* Get the number of bytes in the entry. */
173 #define NRINTERNAL \
174 LALIGN(sizeof(recno_t) + sizeof(pgno_t))
176 /* Copy a RINTERNAL entry to the page. */
177 #define WR_RINTERNAL(p, nrecs, pgno) { \
178 *(recno_t *)p = nrecs; \
179 p += sizeof(recno_t); \
180 *(pgno_t *)p = pgno; \
183 /* For the btree leaf pages, the item is a key and data pair. */
184 typedef struct _bleaf {
185 u_int32_t ksize; /* size of key */
186 u_int32_t dsize; /* size of data */
187 u_char flags; /* P_BIGDATA, P_BIGKEY */
188 char bytes[1]; /* data */
189 } BLEAF;
191 /* Get the page's BLEAF structure at index indx. */
192 #define GETBLEAF(pg, indx) \
193 ((BLEAF *)((char *)(pg) + (pg)->linp[indx]))
195 /* Get the number of bytes in the entry. */
196 #define NBLEAF(p) NBLEAFDBT((p)->ksize, (p)->dsize)
198 /* Get the number of bytes in the user's key/data pair. */
199 #define NBLEAFDBT(ksize, dsize) \
200 LALIGN(sizeof(u_int32_t) + sizeof(u_int32_t) + sizeof(u_char) + \
201 (ksize) + (dsize))
203 /* Copy a BLEAF entry to the page. */
204 #define WR_BLEAF(p, key, data, flags) { \
205 *(u_int32_t *)p = key->size; \
206 p += sizeof(u_int32_t); \
207 *(u_int32_t *)p = data->size; \
208 p += sizeof(u_int32_t); \
209 *(u_char *)p = flags; \
210 p += sizeof(u_char); \
211 memmove(p, key->data, key->size); \
212 p += key->size; \
213 memmove(p, data->data, data->size); \
216 /* For the recno leaf pages, the item is a data entry. */
217 typedef struct _rleaf {
218 u_int32_t dsize; /* size of data */
219 u_char flags; /* P_BIGDATA */
220 char bytes[1];
221 } RLEAF;
223 /* Get the page's RLEAF structure at index indx. */
224 #define GETRLEAF(pg, indx) \
225 ((RLEAF *)((char *)(pg) + (pg)->linp[indx]))
227 /* Get the number of bytes in the entry. */
228 #define NRLEAF(p) NRLEAFDBT((p)->dsize)
230 /* Get the number of bytes from the user's data. */
231 #define NRLEAFDBT(dsize) \
232 LALIGN(sizeof(u_int32_t) + sizeof(u_char) + (dsize))
234 /* Copy a RLEAF entry to the page. */
235 #define WR_RLEAF(p, data, flags) { \
236 *(u_int32_t *)p = data->size; \
237 p += sizeof(u_int32_t); \
238 *(u_char *)p = flags; \
239 p += sizeof(u_char); \
240 memmove(p, data->data, data->size); \
244 * A record in the tree is either a pointer to a page and an index in the page
245 * or a page number and an index. These structures are used as a cursor, stack
246 * entry and search returns as well as to pass records to other routines.
248 * One comment about searches. Internal page searches must find the largest
249 * record less than key in the tree so that descents work. Leaf page searches
250 * must find the smallest record greater than key so that the returned index
251 * is the record's correct position for insertion.
253 typedef struct _epgno {
254 pgno_t pgno; /* the page number */
255 indx_t index; /* the index on the page */
256 } EPGNO;
258 typedef struct _epg {
259 PAGE *page; /* the (pinned) page */
260 indx_t index; /* the index on the page */
261 } EPG;
264 * About cursors. The cursor (and the page that contained the key/data pair
265 * that it referenced) can be deleted, which makes things a bit tricky. If
266 * there are no duplicates of the cursor key in the tree (i.e. B_NODUPS is set
267 * or there simply aren't any duplicates of the key) we copy the key that it
268 * referenced when it's deleted, and reacquire a new cursor key if the cursor
269 * is used again. If there are duplicates keys, we move to the next/previous
270 * key, and set a flag so that we know what happened. NOTE: if duplicate (to
271 * the cursor) keys are added to the tree during this process, it is undefined
272 * if they will be returned or not in a cursor scan.
274 * The flags determine the possible states of the cursor:
276 * CURS_INIT The cursor references *something*.
277 * CURS_ACQUIRE The cursor was deleted, and a key has been saved so that
278 * we can reacquire the right position in the tree.
279 * CURS_AFTER, CURS_BEFORE
280 * The cursor was deleted, and now references a key/data pair
281 * that has not yet been returned, either before or after the
282 * deleted key/data pair.
283 * XXX
284 * This structure is broken out so that we can eventually offer multiple
285 * cursors as part of the DB interface.
287 typedef struct _cursor {
288 EPGNO pg; /* B: Saved tree reference. */
289 DBT key; /* B: Saved key, or key.data == NULL. */
290 recno_t rcursor; /* R: recno cursor (1-based) */
292 #define CURS_ACQUIRE 0x01 /* B: Cursor needs to be reacquired. */
293 #define CURS_AFTER 0x02 /* B: Unreturned cursor after key. */
294 #define CURS_BEFORE 0x04 /* B: Unreturned cursor before key. */
295 #define CURS_INIT 0x08 /* RB: Cursor initialized. */
296 u_int8_t flags;
297 } CURSOR;
300 * The metadata of the tree. The nrecs field is used only by the RECNO code.
301 * This is because the btree doesn't really need it and it requires that every
302 * put or delete call modify the metadata.
304 typedef struct _btmeta {
305 u_int32_t magic; /* magic number */
306 u_int32_t version; /* version */
307 u_int32_t psize; /* page size */
308 u_int32_t free; /* page number of first free page */
309 u_int32_t nrecs; /* R: number of records */
311 #define SAVEMETA (B_NODUPS | R_RECNO)
312 u_int32_t flags; /* bt_flags & SAVEMETA */
313 } BTMETA;
315 /* The in-memory btree/recno data structure. */
316 typedef struct _btree {
317 MPOOL *bt_mp; /* memory pool cookie */
319 DB *bt_dbp; /* pointer to enclosing DB */
321 EPG bt_cur; /* current (pinned) page */
322 PAGE *bt_pinned; /* page pinned across calls */
324 CURSOR bt_cursor; /* cursor */
326 #define BT_PUSH(t, p, i) { \
327 t->bt_sp->pgno = p; \
328 t->bt_sp->index = i; \
329 ++t->bt_sp; \
331 #define BT_POP(t) (t->bt_sp == t->bt_stack ? NULL : --t->bt_sp)
332 #define BT_CLR(t) (t->bt_sp = t->bt_stack)
333 EPGNO bt_stack[50]; /* stack of parent pages */
334 EPGNO *bt_sp; /* current stack pointer */
336 DBT bt_rkey; /* returned key */
337 DBT bt_rdata; /* returned data */
339 int bt_fd; /* tree file descriptor */
341 pgno_t bt_free; /* next free page */
342 u_int32_t bt_psize; /* page size */
343 indx_t bt_ovflsize; /* cut-off for key/data overflow */
344 int bt_lorder; /* byte order */
345 /* sorted order */
346 enum { NOT, BACK, FORWARD } bt_order;
347 EPGNO bt_last; /* last insert */
349 /* B: key comparison function */
350 int (*bt_cmp) __P((const DBT *, const DBT *));
351 /* B: prefix comparison function */
352 size_t (*bt_pfx) __P((const DBT *, const DBT *));
353 /* R: recno input function */
354 int (*bt_irec) __P((struct _btree *, recno_t));
356 FILE *bt_rfp; /* R: record FILE pointer */
357 int bt_rfd; /* R: record file descriptor */
359 caddr_t bt_cmap; /* R: current point in mapped space */
360 caddr_t bt_smap; /* R: start of mapped space */
361 caddr_t bt_emap; /* R: end of mapped space */
362 size_t bt_msize; /* R: size of mapped region. */
364 recno_t bt_nrecs; /* R: number of records */
365 size_t bt_reclen; /* R: fixed record length */
366 u_char bt_bval; /* R: delimiting byte/pad character */
369 * NB:
370 * B_NODUPS and R_RECNO are stored on disk, and may not be changed.
372 #define B_INMEM 0x00001 /* in-memory tree */
373 #define B_METADIRTY 0x00002 /* need to write metadata */
374 #define B_MODIFIED 0x00004 /* tree modified */
375 #define B_NEEDSWAP 0x00008 /* if byte order requires swapping */
376 #define B_RDONLY 0x00010 /* read-only tree */
378 #define B_NODUPS 0x00020 /* no duplicate keys permitted */
379 #define R_RECNO 0x00080 /* record oriented tree */
381 #define R_CLOSEFP 0x00040 /* opened a file pointer */
382 #define R_EOF 0x00100 /* end of input file reached. */
383 #define R_FIXLEN 0x00200 /* fixed length records */
384 #define R_MEMMAPPED 0x00400 /* memory mapped file. */
385 #define R_INMEM 0x00800 /* in-memory file */
386 #define R_MODIFIED 0x01000 /* modified file */
387 #define R_RDONLY 0x02000 /* read-only file */
389 #define B_DB_LOCK 0x04000 /* DB_LOCK specified. */
390 #define B_DB_SHMEM 0x08000 /* DB_SHMEM specified. */
391 #define B_DB_TXN 0x10000 /* DB_TXN specified. */
392 u_int32_t flags;
393 } BTREE;
395 #include "extern.h"