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