]> git.saurik.com Git - apple/libc.git/blob - db.subproj/btree.subproj/btree.h
Libc-167.tar.gz
[apple/libc.git] / db.subproj / btree.subproj / btree.h
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright (c) 1991, 1993
24 * The Regents of the University of California. All rights reserved.
25 *
26 * This code is derived from software contributed to Berkeley by
27 * Mike Olson.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
57
58 #include <mpool.h>
59
60 #define DEFMINKEYPAGE (2) /* Minimum keys per page */
61 #define MINCACHE (5) /* Minimum cached pages */
62 #define MINPSIZE (512) /* Minimum page size */
63
64 /*
65 * Page 0 of a btree file contains a copy of the meta-data. This page is also
66 * used as an out-of-band page, i.e. page pointers that point to nowhere point
67 * to page 0. Page 1 is the root of the btree.
68 */
69 #define P_INVALID 0 /* Invalid tree page number. */
70 #define P_META 0 /* Tree metadata page number. */
71 #define P_ROOT 1 /* Tree root page number. */
72
73 /*
74 * There are five page layouts in the btree: btree internal pages (BINTERNAL),
75 * btree leaf pages (BLEAF), recno internal pages (RINTERNAL), recno leaf pages
76 * (RLEAF) and overflow pages. All five page types have a page header (PAGE).
77 * This implementation requires that values within structures NOT be padded.
78 * (ANSI C permits random padding.) If your compiler pads randomly you'll have
79 * to do some work to get this package to run.
80 */
81 typedef struct _page {
82 pgno_t pgno; /* this page's page number */
83 pgno_t prevpg; /* left sibling */
84 pgno_t nextpg; /* right sibling */
85
86 #define P_BINTERNAL 0x01 /* btree internal page */
87 #define P_BLEAF 0x02 /* leaf page */
88 #define P_OVERFLOW 0x04 /* overflow page */
89 #define P_RINTERNAL 0x08 /* recno internal page */
90 #define P_RLEAF 0x10 /* leaf page */
91 #define P_TYPE 0x1f /* type mask */
92 #define P_PRESERVE 0x20 /* never delete this chain of pages */
93 u_int32_t flags;
94
95 indx_t lower; /* lower bound of free space on page */
96 indx_t upper; /* upper bound of free space on page */
97 indx_t linp[1]; /* indx_t-aligned VAR. LENGTH DATA */
98 } PAGE;
99
100 /* First and next index. */
101 #define BTDATAOFF (sizeof(pgno_t) + sizeof(pgno_t) + sizeof(pgno_t) + \
102 sizeof(u_int32_t) + sizeof(indx_t) + sizeof(indx_t))
103 #define NEXTINDEX(p) (((p)->lower - BTDATAOFF) / sizeof(indx_t))
104
105 /*
106 * For pages other than overflow pages, there is an array of offsets into the
107 * rest of the page immediately following the page header. Each offset is to
108 * an item which is unique to the type of page. The h_lower offset is just
109 * past the last filled-in index. The h_upper offset is the first item on the
110 * page. Offsets are from the beginning of the page.
111 *
112 * If an item is too big to store on a single page, a flag is set and the item
113 * is a { page, size } pair such that the page is the first page of an overflow
114 * chain with size bytes of item. Overflow pages are simply bytes without any
115 * external structure.
116 *
117 * The page number and size fields in the items are pgno_t-aligned so they can
118 * be manipulated without copying. (This presumes that 32 bit items can be
119 * manipulated on this system.)
120 */
121 #define LALIGN(n) \
122 (((n) + sizeof(pgno_t) - 1) & ~(sizeof(pgno_t) - 1))
123 #define NOVFLSIZE (sizeof(pgno_t) + sizeof(size_t))
124
125 /*
126 * For the btree internal pages, the item is a key. BINTERNALs are {key, pgno}
127 * pairs, such that the key compares less than or equal to all of the records
128 * on that page. For a tree without duplicate keys, an internal page with two
129 * consecutive keys, a and b, will have all records greater than or equal to a
130 * and less than b stored on the page associated with a. Duplicate keys are
131 * somewhat special and can cause duplicate internal and leaf page records and
132 * some minor modifications of the above rule.
133 */
134 typedef struct _binternal {
135 size_t ksize; /* key size */
136 pgno_t pgno; /* page number stored on */
137 #define P_BIGDATA 0x01 /* overflow data */
138 #define P_BIGKEY 0x02 /* overflow key */
139 u_char flags;
140 char bytes[1]; /* data */
141 } BINTERNAL;
142
143 /* Get the page's BINTERNAL structure at index indx. */
144 #define GETBINTERNAL(pg, indx) \
145 ((BINTERNAL *)((char *)(pg) + (pg)->linp[indx]))
146
147 /* Get the number of bytes in the entry. */
148 #define NBINTERNAL(len) \
149 LALIGN(sizeof(size_t) + sizeof(pgno_t) + sizeof(u_char) + (len))
150
151 /* Copy a BINTERNAL entry to the page. */
152 #define WR_BINTERNAL(p, size, pgno, flags) { \
153 *(size_t *)p = size; \
154 p += sizeof(size_t); \
155 *(pgno_t *)p = pgno; \
156 p += sizeof(pgno_t); \
157 *(u_char *)p = flags; \
158 p += sizeof(u_char); \
159 }
160
161 /*
162 * For the recno internal pages, the item is a page number with the number of
163 * keys found on that page and below.
164 */
165 typedef struct _rinternal {
166 recno_t nrecs; /* number of records */
167 pgno_t pgno; /* page number stored below */
168 } RINTERNAL;
169
170 /* Get the page's RINTERNAL structure at index indx. */
171 #define GETRINTERNAL(pg, indx) \
172 ((RINTERNAL *)((char *)(pg) + (pg)->linp[indx]))
173
174 /* Get the number of bytes in the entry. */
175 #define NRINTERNAL \
176 LALIGN(sizeof(recno_t) + sizeof(pgno_t))
177
178 /* Copy a RINTERAL entry to the page. */
179 #define WR_RINTERNAL(p, nrecs, pgno) { \
180 *(recno_t *)p = nrecs; \
181 p += sizeof(recno_t); \
182 *(pgno_t *)p = pgno; \
183 }
184
185 /* For the btree leaf pages, the item is a key and data pair. */
186 typedef struct _bleaf {
187 size_t ksize; /* size of key */
188 size_t dsize; /* size of data */
189 u_char flags; /* P_BIGDATA, P_BIGKEY */
190 char bytes[1]; /* data */
191 } BLEAF;
192
193 /* Get the page's BLEAF structure at index indx. */
194 #define GETBLEAF(pg, indx) \
195 ((BLEAF *)((char *)(pg) + (pg)->linp[indx]))
196
197 /* Get the number of bytes in the entry. */
198 #define NBLEAF(p) NBLEAFDBT((p)->ksize, (p)->dsize)
199
200 /* Get the number of bytes in the user's key/data pair. */
201 #define NBLEAFDBT(ksize, dsize) \
202 LALIGN(sizeof(size_t) + sizeof(size_t) + sizeof(u_char) + \
203 (ksize) + (dsize))
204
205 /* Copy a BLEAF entry to the page. */
206 #define WR_BLEAF(p, key, data, flags) { \
207 *(size_t *)p = key->size; \
208 p += sizeof(size_t); \
209 *(size_t *)p = data->size; \
210 p += sizeof(size_t); \
211 *(u_char *)p = flags; \
212 p += sizeof(u_char); \
213 memmove(p, key->data, key->size); \
214 p += key->size; \
215 memmove(p, data->data, data->size); \
216 }
217
218 /* For the recno leaf pages, the item is a data entry. */
219 typedef struct _rleaf {
220 size_t dsize; /* size of data */
221 u_char flags; /* P_BIGDATA */
222 char bytes[1];
223 } RLEAF;
224
225 /* Get the page's RLEAF structure at index indx. */
226 #define GETRLEAF(pg, indx) \
227 ((RLEAF *)((char *)(pg) + (pg)->linp[indx]))
228
229 /* Get the number of bytes in the entry. */
230 #define NRLEAF(p) NRLEAFDBT((p)->dsize)
231
232 /* Get the number of bytes from the user's data. */
233 #define NRLEAFDBT(dsize) \
234 LALIGN(sizeof(size_t) + sizeof(u_char) + (dsize))
235
236 /* Copy a RLEAF entry to the page. */
237 #define WR_RLEAF(p, data, flags) { \
238 *(size_t *)p = data->size; \
239 p += sizeof(size_t); \
240 *(u_char *)p = flags; \
241 p += sizeof(u_char); \
242 memmove(p, data->data, data->size); \
243 }
244
245 /*
246 * A record in the tree is either a pointer to a page and an index in the page
247 * or a page number and an index. These structures are used as a cursor, stack
248 * entry and search returns as well as to pass records to other routines.
249 *
250 * One comment about searches. Internal page searches must find the largest
251 * record less than key in the tree so that descents work. Leaf page searches
252 * must find the smallest record greater than key so that the returned index
253 * is the record's correct position for insertion.
254 *
255 * One comment about cursors. The cursor key is never removed from the tree,
256 * even if deleted. This is because it is quite difficult to decide where the
257 * cursor should be when other keys have been inserted/deleted in the tree;
258 * duplicate keys make it impossible. This scheme does require extra work
259 * though, to make sure that we don't perform an operation on a deleted key.
260 */
261 typedef struct _epgno {
262 pgno_t pgno; /* the page number */
263 indx_t index; /* the index on the page */
264 } EPGNO;
265
266 typedef struct _epg {
267 PAGE *page; /* the (pinned) page */
268 indx_t index; /* the index on the page */
269 } EPG;
270
271 /*
272 * The metadata of the tree. The m_nrecs field is used only by the RECNO code.
273 * This is because the btree doesn't really need it and it requires that every
274 * put or delete call modify the metadata.
275 */
276 typedef struct _btmeta {
277 u_int32_t m_magic; /* magic number */
278 u_int32_t m_version; /* version */
279 u_int32_t m_psize; /* page size */
280 u_int32_t m_free; /* page number of first free page */
281 u_int32_t m_nrecs; /* R: number of records */
282 #define SAVEMETA (B_NODUPS | R_RECNO)
283 u_int32_t m_flags; /* bt_flags & SAVEMETA */
284 u_int32_t m_unused; /* unused */
285 } BTMETA;
286
287 /* The in-memory btree/recno data structure. */
288 typedef struct _btree {
289 MPOOL *bt_mp; /* memory pool cookie */
290
291 DB *bt_dbp; /* pointer to enclosing DB */
292
293 EPG bt_cur; /* current (pinned) page */
294 PAGE *bt_pinned; /* page pinned across calls */
295
296 EPGNO bt_bcursor; /* B: btree cursor */
297 recno_t bt_rcursor; /* R: recno cursor (1-based) */
298
299 #define BT_POP(t) (t->bt_sp ? t->bt_stack + --t->bt_sp : NULL)
300 #define BT_CLR(t) (t->bt_sp = 0)
301 EPGNO *bt_stack; /* stack of parent pages */
302 u_int bt_sp; /* current stack pointer */
303 u_int bt_maxstack; /* largest stack */
304
305 char *bt_kbuf; /* key buffer */
306 size_t bt_kbufsz; /* key buffer size */
307 char *bt_dbuf; /* data buffer */
308 size_t bt_dbufsz; /* data buffer size */
309
310 int bt_fd; /* tree file descriptor */
311
312 pgno_t bt_free; /* next free page */
313 u_int32_t bt_psize; /* page size */
314 indx_t bt_ovflsize; /* cut-off for key/data overflow */
315 int bt_lorder; /* byte order */
316 /* sorted order */
317 enum { NOT, BACK, FORWARD } bt_order;
318 EPGNO bt_last; /* last insert */
319
320 /* B: key comparison function */
321 int (*bt_cmp) __P((const DBT *, const DBT *));
322 /* B: prefix comparison function */
323 size_t (*bt_pfx) __P((const DBT *, const DBT *));
324 /* R: recno input function */
325 int (*bt_irec) __P((struct _btree *, recno_t));
326
327 FILE *bt_rfp; /* R: record FILE pointer */
328 int bt_rfd; /* R: record file descriptor */
329
330 caddr_t bt_cmap; /* R: current point in mapped space */
331 caddr_t bt_smap; /* R: start of mapped space */
332 caddr_t bt_emap; /* R: end of mapped space */
333 size_t bt_msize; /* R: size of mapped region. */
334
335 recno_t bt_nrecs; /* R: number of records */
336 size_t bt_reclen; /* R: fixed record length */
337 u_char bt_bval; /* R: delimiting byte/pad character */
338
339 /*
340 * NB:
341 * B_NODUPS and R_RECNO are stored on disk, and may not be changed.
342 */
343 #define B_DELCRSR 0x00001 /* cursor has been deleted */
344 #define B_INMEM 0x00002 /* in-memory tree */
345 #define B_METADIRTY 0x00004 /* need to write metadata */
346 #define B_MODIFIED 0x00008 /* tree modified */
347 #define B_NEEDSWAP 0x00010 /* if byte order requires swapping */
348 #define B_NODUPS 0x00020 /* no duplicate keys permitted */
349 #define B_RDONLY 0x00040 /* read-only tree */
350 #define R_RECNO 0x00080 /* record oriented tree */
351 #define B_SEQINIT 0x00100 /* sequential scan initialized */
352
353 #define R_CLOSEFP 0x00200 /* opened a file pointer */
354 #define R_EOF 0x00400 /* end of input file reached. */
355 #define R_FIXLEN 0x00800 /* fixed length records */
356 #define R_MEMMAPPED 0x01000 /* memory mapped file. */
357 #define R_INMEM 0x02000 /* in-memory file */
358 #define R_MODIFIED 0x04000 /* modified file */
359 #define R_RDONLY 0x08000 /* read-only file */
360
361 #define B_DB_LOCK 0x10000 /* DB_LOCK specified. */
362 #define B_DB_SHMEM 0x20000 /* DB_SHMEM specified. */
363 #define B_DB_TXN 0x40000 /* DB_TXN specified. */
364
365 u_int32_t bt_flags; /* btree state */
366 } BTREE;
367
368 #define SET(t, f) ((t)->bt_flags |= (f))
369 #define CLR(t, f) ((t)->bt_flags &= ~(f))
370 #define ISSET(t, f) ((t)->bt_flags & (f))
371
372 #include "bt_extern.h"