btree.h 14 KB

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