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