isbtree2.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. /*%% (c) Copyright 1993, 1994 Hewlett-Packard Company */
  24. /*%% (c) Copyright 1993, 1994 International Business Machines Corp. */
  25. /*%% (c) Copyright 1993, 1994 Sun Microsystems, Inc. */
  26. /*%% (c) Copyright 1993, 1994 Novell, Inc. */
  27. /*%% $XConsortium: isbtree2.c /main/3 1995/10/23 11:36:02 rswiston $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * isbtree2.c
  33. *
  34. * Description:
  35. * B-tree operations: INSERT
  36. *
  37. */
  38. #include "isam_impl.h"
  39. extern int _iskeycmp();
  40. void leftkey_up(Btree *, int);
  41. static void insert_key(Btree *, char *, int, char *, Blkno);
  42. static void splitblock(Btree *, char *, char *, int);
  43. /* _isbtree_insert() - Insert entry into B-tree ----------------------------*/
  44. void
  45. _isbtree_insert(Btree *btree, char *key)
  46. {
  47. Keydesc2 *pkeydesc2 = btree->keydesc2;
  48. int keylength = pkeydesc2->k2_len;
  49. int nkeys; /* Number of keys in the page */
  50. int capac = 0;
  51. char keybuf[MAXKEYSIZE];
  52. int i;
  53. Blkno blkno;
  54. char *pkp = NULL, *pkp2, *pkp3;
  55. Bufhdr *kp2bhdr, *kp3bhdr;
  56. Blkno blkno2, blkno3;
  57. int level;
  58. int halfcapac;
  59. /*
  60. * Set key comparison function.
  61. */
  62. _iskeycmp_set(pkeydesc2, pkeydesc2->k2_nparts+1); /* +1 for recno field */
  63. /*
  64. * Initialize data structure before main loop.
  65. */
  66. blkno = NULL_BLKNO;
  67. memcpy( keybuf,key, keylength);
  68. /*
  69. * Main loop:
  70. * Starting from the leaf, insert key after current search position.
  71. * If necessary make block split and propage changes to upper levels.
  72. * Root split is handled differently because we don't want to change
  73. * root block number.
  74. */
  75. for (i = btree->depth - 1; i >= 0; i--) {
  76. /* We have to fix the block for update. */
  77. btree->bufhdr[i] = _isdisk_refix(btree->bufhdr[i], ISFIXWRITE);
  78. pkp = btree->bufhdr[i]->isb_buffer;
  79. level = ldshort(pkp + BT_LEVEL_OFF);
  80. capac = ldshort(pkp + BT_CAPAC_OFF); /* Block capacity */
  81. nkeys = ldshort(pkp + BT_NKEYS_OFF); /* Number of keys in block */
  82. assert(level + i + 1 == btree->depth);
  83. assert(nkeys <= capac);
  84. if (nkeys == capac) {
  85. /*
  86. * This block must be split.
  87. * Allocate new block, move half of the entries into the new
  88. * block, and insert the key into the appropriate
  89. * block. If this block is not root, the loop will iterate
  90. * to update upper levels.
  91. */
  92. /* Get new page. */
  93. kp2bhdr = _allockpage(btree->fcb, capac, level, &blkno2);
  94. pkp2 = kp2bhdr->isb_buffer;
  95. halfcapac = (nkeys+1) >> 1; /* Same as nkeys/2 + 1 */
  96. splitblock(btree, pkp, pkp2, btree->curpos[i]);
  97. if (btree->curpos[i] > halfcapac - 2) {
  98. /* Insert entry into right block. */
  99. insert_key(btree, pkp2, btree->curpos[i] - halfcapac, keybuf,
  100. blkno);
  101. }
  102. else {
  103. /* Insert entry into left block. */
  104. insert_key(btree, pkp, btree->curpos[i], keybuf, blkno);
  105. if (btree->curpos[i] == -1) /* Leftmost entry inserted - */
  106. leftkey_up(btree, i); /* propagate to upper levels. */
  107. }
  108. /* Set variables for next loop iteration. */
  109. memcpy( keybuf,pkp2 + BT_KEYS_OFF, keylength); /* Leftmost key */
  110. blkno = blkno2;
  111. /*
  112. * Next loop iteration will insert entry pointing to
  113. * new allocated page into next upper level.
  114. */
  115. }
  116. else {
  117. /*
  118. * Block split is not necessary. Simply insert key into
  119. * the block. If the inserted key becomes the leftmost entry
  120. * in the block, update upper levels.
  121. */
  122. insert_key(btree, pkp, btree->curpos[i], keybuf, blkno);
  123. if (btree->curpos[i] == -1 && i > 0)
  124. leftkey_up(btree, i);
  125. break; /* from main loop */
  126. }
  127. }
  128. if (i < 0) {
  129. /*
  130. * Root was split.
  131. * Allocate new page. However, to keep root block the same,
  132. * the new page is used for the left son of root.
  133. *
  134. * pkp is the new root, pkp3 is the left son,
  135. * and pkp3 is the right of the root.
  136. */
  137. kp3bhdr = _allockpage(btree->fcb, capac, 1, &blkno3);
  138. pkp3 = kp3bhdr->isb_buffer;
  139. memcpy( pkp3,pkp, ISPAGESIZE);
  140. stshort((short) getkeyspernode(keylength), pkp + BT_CAPAC_OFF);
  141. stshort((short) 0, pkp + BT_NKEYS_OFF);
  142. stshort((short)btree->depth, pkp + BT_LEVEL_OFF);
  143. insert_key(btree, pkp, -1, pkp3 + BT_KEYS_OFF, blkno3);
  144. insert_key(btree, pkp, 0 , keybuf, blkno);
  145. }
  146. }
  147. /*--------- insert supporting local functions -------------------------------*/
  148. /* leftkey_up() - Update upper levels with new leftmost entry -----------*/
  149. void
  150. leftkey_up(Btree *btree, int level)
  151. {
  152. int keylength = btree->keydesc2->k2_len;
  153. char *pkp;
  154. char *key;
  155. pkp = btree->bufhdr[level]->isb_buffer;
  156. key = pkp + BT_KEYS_OFF; /* Leftmost key */
  157. while (--level >= 0) {
  158. btree->bufhdr[level] = _isdisk_refix(btree->bufhdr[level], ISFIXWRITE);
  159. pkp = btree->bufhdr[level]->isb_buffer;
  160. memcpy( pkp + BT_KEYS_OFF + (btree->curpos[level] * keylength),key,
  161. keylength);
  162. if (btree->curpos[level] > 0)
  163. break;
  164. }
  165. }
  166. /* insert_key - Insert key into block ------------------------*/
  167. static void
  168. insert_key(Btree *btree, char *pkp, int pos, char *key, Blkno blkno)
  169. {
  170. int keylength = btree->keydesc2->k2_len;
  171. int nkeys = ldshort(pkp + BT_NKEYS_OFF);
  172. int capac = ldshort(pkp + BT_CAPAC_OFF);
  173. int level = ldshort(pkp + BT_LEVEL_OFF);
  174. assert(nkeys < capac);
  175. /* Shift nkeys - pos - 1 entries to the right. */
  176. /* memmove() handle overlaps correctly */
  177. memmove(pkp + BT_KEYS_OFF + (pos + 2) * keylength,
  178. pkp + BT_KEYS_OFF + (pos + 1) * keylength,
  179. (nkeys - pos - 1) * keylength);
  180. /* Copy new key entry into the block. */
  181. memcpy( pkp + BT_KEYS_OFF + (pos + 1) * keylength,key, keylength);
  182. /* For non-leaf nodes, insert block number into table of down pointers. */
  183. if (level > 0) {
  184. memcpy(pkp + ISPAGESIZE - (nkeys + 1) * BLKNOSIZE,
  185. pkp + ISPAGESIZE - nkeys * BLKNOSIZE,
  186. (nkeys - pos - 1) * BLKNOSIZE);
  187. stblkno(blkno, pkp + ISPAGESIZE - (pos + 2) * BLKNOSIZE);
  188. }
  189. stshort((short) (nkeys + 1), pkp + BT_NKEYS_OFF);
  190. }
  191. /* splitblock() - Split block into two -----------------------------*/
  192. static void
  193. splitblock(Btree *btree, char *fullpage, char *newpage, int pos)
  194. {
  195. int keylength = btree->keydesc2->k2_len;
  196. int nkeys, capac, level;
  197. int halfcapac;
  198. int newpage_nkeys;
  199. int fullpage_nkeys;
  200. nkeys = ldshort(fullpage + BT_NKEYS_OFF);
  201. capac = ldshort(fullpage + BT_NKEYS_OFF);
  202. level = ldshort(fullpage + BT_LEVEL_OFF);
  203. assert(nkeys == capac);
  204. halfcapac = (capac + 1) >> 1; /* same as capac/2 + 1 */
  205. if (pos > halfcapac - 2) {
  206. /* New entry will go into right page(fullpage). */
  207. fullpage_nkeys = halfcapac;
  208. newpage_nkeys = halfcapac - 1;
  209. }
  210. else {
  211. /* New entry will go into left page (newpage). */
  212. fullpage_nkeys = halfcapac - 1;
  213. newpage_nkeys = halfcapac;
  214. }
  215. /* Move newpage_nkeys keys into newpage. */
  216. memcpy(newpage + BT_KEYS_OFF,
  217. fullpage + BT_KEYS_OFF + fullpage_nkeys * keylength,
  218. keylength * newpage_nkeys);
  219. /* If non-leaf, move corresponding entries from block number table. */
  220. if (level > 0) {
  221. memcpy(newpage + ISPAGESIZE - newpage_nkeys * BLKNOSIZE,
  222. fullpage + ISPAGESIZE - nkeys * BLKNOSIZE,
  223. newpage_nkeys * BLKNOSIZE);
  224. }
  225. stshort((short) fullpage_nkeys, fullpage + BT_NKEYS_OFF);
  226. stshort((short) newpage_nkeys, newpage + BT_NKEYS_OFF);
  227. }