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