isbtree.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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: isbtree.c /main/3 1995/10/23 11:35:52 rswiston $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * isbtree.c
  33. *
  34. * Description:
  35. * B-tree operations: SEARCH
  36. *
  37. */
  38. #include <stdlib.h>
  39. #include "isam_impl.h"
  40. /*
  41. * _isbtree_create()
  42. *
  43. * Create a B-tree path object that will used in subsequent operations.
  44. */
  45. Btree *
  46. _isbtree_create(Fcb *fcb, Keydesc2 *pkeydesc2)
  47. {
  48. Btree *p;
  49. p = (Btree *) _ismalloc(sizeof(*p));
  50. memset((char *)p, 0, sizeof(*p));
  51. p->fcb = fcb;
  52. p->keydesc2 = pkeydesc2;
  53. return (p);
  54. }
  55. /*
  56. * _isbtr_destroy()
  57. *
  58. * Destroy B-tree path object
  59. */
  60. void
  61. _isbtree_destroy(Btree *btree)
  62. {
  63. int i;
  64. for (i = 0; i < btree->depth;i++) {
  65. _isdisk_unfix(btree->bufhdr[i]);
  66. }
  67. free((char *)btree);
  68. }
  69. /*
  70. * _isbtree_search()
  71. *
  72. * Descend the B-tree, position pointer on or before the matched key.
  73. */
  74. void
  75. _isbtree_search( Btree *btree, char *key)
  76. {
  77. Keydesc2 *pkeydesc2 = btree->keydesc2;
  78. Blkno rootblkno = pkeydesc2->k2_rootnode;
  79. int keylength = pkeydesc2->k2_len;
  80. int index; /* Index to tables in btree */
  81. /* Has value of 1, next 2, etc. */
  82. int elevation; /* Level - leaves have value 0 */
  83. char *p; /* Pointer to key page */
  84. int nkeys; /* Number of keys in the page */
  85. char *key2; /* Equal or next lower key */
  86. int curpos; /* index of key2 in key page */
  87. Blkno blkno;
  88. /* Set comparison function. */
  89. _iskeycmp_set(pkeydesc2, pkeydesc2->k2_nparts + 1); /* +1 for recno field */
  90. index = 0;
  91. blkno = rootblkno;
  92. do {
  93. btree->bufhdr[index] =
  94. _isdisk_fix(btree->fcb, btree->fcb->indfd, blkno, ISFIXREAD);
  95. p = btree->bufhdr[index]->isb_buffer; /* pointer to buffer */
  96. /* Load some fields from the key page. */
  97. nkeys = ldshort(p+BT_NKEYS_OFF); /* Number of keys in the page */
  98. elevation = ldshort(p+BT_LEVEL_OFF); /* Level of the page */
  99. /* Binary search in the key page to find equal or next lowere key. */
  100. key2 = _isbsearch(key, p+BT_KEYS_OFF, nkeys, keylength, _iskeycmp);
  101. curpos = (key2) ? ((key2 - p - BT_NKEYS_OFF) / keylength) : 0;
  102. btree->curpos[index] =
  103. (key2 == (char *)0 && elevation==0)? -1 : curpos;
  104. if (elevation > 0)
  105. blkno = ldblkno(p + ISPAGESIZE - (curpos + 1) * BLKNOSIZE);
  106. index++;
  107. } while (elevation > 0);
  108. btree->depth = index;
  109. }
  110. /*
  111. * _isbtree_current()
  112. *
  113. * Get pointer to the current key
  114. */
  115. char *
  116. _isbtree_current(Btree *btree)
  117. {
  118. int curpos;
  119. assert(btree->depth > 0);
  120. if ((curpos = btree->curpos[btree->depth - 1]) == -1)
  121. return (NULL);
  122. else
  123. return (btree->bufhdr[btree->depth - 1]->isb_buffer
  124. + BT_KEYS_OFF + curpos * btree->keydesc2->k2_len);
  125. }
  126. /*
  127. * _isbtree_next()
  128. *
  129. * Get pointer to the next key
  130. */
  131. char *
  132. _isbtree_next(Btree *btree)
  133. {
  134. int curpos;
  135. int depth = btree->depth;
  136. char *p;
  137. int level;
  138. Blkno blkno;
  139. assert(depth > 0);
  140. /*
  141. * Move up along the path, find first block where we can move to the right.
  142. */
  143. for (level = depth - 1; level >= 0; level--) {
  144. p = btree->bufhdr[level]->isb_buffer;
  145. if (btree->curpos[level] < ldshort(p + BT_NKEYS_OFF) - 1)
  146. break;
  147. }
  148. if (level < 0) {
  149. /* Logical end of the index file. No next record. */
  150. return (NULL);
  151. }
  152. curpos = ++(btree->curpos[level]);
  153. while (++level < depth) {
  154. /* Get block number to block in next lower level. */
  155. if (level > 0)
  156. blkno = ldblkno(p + ISPAGESIZE - (curpos + 1) * BLKNOSIZE);
  157. /* Unfix page in this level, fetch its right brother. */
  158. _isdisk_unfix(btree->bufhdr[level]);
  159. btree->bufhdr[level] =
  160. _isdisk_fix(btree->fcb, btree->fcb->indfd, blkno, ISFIXREAD);
  161. p = btree->bufhdr[level]->isb_buffer;
  162. curpos = btree->curpos[level] = 0;
  163. }
  164. return (p + BT_KEYS_OFF + curpos * btree->keydesc2->k2_len);
  165. }