btree.h 14 KB

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