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