bt_get.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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: bt_get.c /main/3 1996/06/11 17:12:34 cde-hal $ */
  24. /*-
  25. * Copyright (c) 1990, 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. #if defined(LIBC_SCCS) && !defined(lint)
  60. static char sccsid[] = "@(#)bt_get.c 8.2 (Berkeley) 9/7/93";
  61. #endif /* LIBC_SCCS and not lint */
  62. #include <sys/types.h>
  63. #include <errno.h>
  64. #include <stddef.h>
  65. #include <stdio.h>
  66. #include <db.h>
  67. #include "btree.h"
  68. /*
  69. * __BT_GET -- Get a record from the btree.
  70. *
  71. * Parameters:
  72. * dbp: pointer to access method
  73. * key: key to find
  74. * data: data to return
  75. * flag: currently unused
  76. *
  77. * Returns:
  78. * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
  79. */
  80. int
  81. __bt_get(const DB *dbp, const DBT *key, DBT *data, u_int flags)
  82. {
  83. BTREE *t;
  84. EPG *e;
  85. int exact, status;
  86. t = dbp->internal;
  87. /* Toss any page pinned across calls. */
  88. if (t->bt_pinned != NULL) {
  89. mpool_put(t->bt_mp, t->bt_pinned, 0);
  90. t->bt_pinned = NULL;
  91. }
  92. /* Get currently doesn't take any flags. */
  93. if (flags) {
  94. errno = EINVAL;
  95. return (RET_ERROR);
  96. }
  97. if ((e = __bt_search(t, key, &exact)) == NULL)
  98. return (RET_ERROR);
  99. if (!exact) {
  100. mpool_put(t->bt_mp, e->page, 0);
  101. return (RET_SPECIAL);
  102. }
  103. /*
  104. * A special case is if we found the record but it's flagged for
  105. * deletion. In this case, we want to find another record with the
  106. * same key, if it exists. Rather than look around the tree we call
  107. * __bt_first and have it redo the search, as __bt_first will not
  108. * return keys marked for deletion. Slow, but should never happen.
  109. */
  110. if (ISSET(t, B_DELCRSR) && e->page->pgno == t->bt_bcursor.pgno &&
  111. e->index == t->bt_bcursor.index) {
  112. mpool_put(t->bt_mp, e->page, 0);
  113. if ((e = __bt_first(t, key, &exact)) == NULL)
  114. return (RET_ERROR);
  115. if (!exact)
  116. return (RET_SPECIAL);
  117. }
  118. status = __bt_ret(t, e, NULL, data);
  119. /*
  120. * If the user is doing concurrent access, we copied the
  121. * key/data, toss the page.
  122. */
  123. if (ISSET(t, B_DB_LOCK))
  124. mpool_put(t->bt_mp, e->page, 0);
  125. else
  126. t->bt_pinned = e->page;
  127. return (status);
  128. }
  129. /*
  130. * __BT_FIRST -- Find the first entry.
  131. *
  132. * Parameters:
  133. * t: the tree
  134. * key: the key
  135. *
  136. * Returns:
  137. * The first entry in the tree greater than or equal to key.
  138. */
  139. EPG *
  140. __bt_first(BTREE *t, const DBT *key, int *exactp)
  141. {
  142. PAGE *h;
  143. EPG *e;
  144. EPG save;
  145. pgno_t cpgno, pg;
  146. indx_t cindex;
  147. int found;
  148. /*
  149. * Find any matching record; __bt_search pins the page. Only exact
  150. * matches are tricky, otherwise just return the location of the key
  151. * if it were to be inserted into the tree.
  152. */
  153. if ((e = __bt_search(t, key, exactp)) == NULL)
  154. return (NULL);
  155. if (!*exactp)
  156. return (e);
  157. if (ISSET(t, B_DELCRSR)) {
  158. cpgno = t->bt_bcursor.pgno;
  159. cindex = t->bt_bcursor.index;
  160. } else {
  161. cpgno = P_INVALID;
  162. cindex = 0; /* GCC thinks it's uninitialized. */
  163. }
  164. /*
  165. * Walk backwards, skipping empty pages, as long as the entry matches
  166. * and there are keys left in the tree. Save a copy of each match in
  167. * case we go too far. A special case is that we don't return a match
  168. * on records that the cursor references that have already been flagged
  169. * for deletion.
  170. */
  171. save = *e;
  172. h = e->page;
  173. found = 0;
  174. do {
  175. if (cpgno != h->pgno || cindex != e->index) {
  176. if (save.page->pgno != e->page->pgno) {
  177. mpool_put(t->bt_mp, save.page, 0);
  178. save = *e;
  179. } else
  180. save.index = e->index;
  181. found = 1;
  182. }
  183. /*
  184. * Make a special effort not to unpin the page the last (or
  185. * original) match was on, but also make sure it's unpinned
  186. * if an error occurs.
  187. */
  188. while (e->index == 0) {
  189. if (h->prevpg == P_INVALID)
  190. goto done1;
  191. if (h->pgno != save.page->pgno)
  192. mpool_put(t->bt_mp, h, 0);
  193. if ((h = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL) {
  194. if (h->pgno == save.page->pgno)
  195. mpool_put(t->bt_mp, save.page, 0);
  196. return (NULL);
  197. }
  198. e->page = h;
  199. e->index = NEXTINDEX(h);
  200. }
  201. --e->index;
  202. } while (__bt_cmp(t, key, e) == 0);
  203. /*
  204. * Reach here with the last page that was looked at pinned, which may
  205. * or may not be the same as the last (or original) match page. If
  206. * it's not useful, release it.
  207. */
  208. done1: if (h->pgno != save.page->pgno)
  209. mpool_put(t->bt_mp, h, 0);
  210. /*
  211. * If still haven't found a record, the only possibility left is the
  212. * next one. Move forward one slot, skipping empty pages and check.
  213. */
  214. if (!found) {
  215. h = save.page;
  216. if (++save.index == NEXTINDEX(h)) {
  217. do {
  218. pg = h->nextpg;
  219. mpool_put(t->bt_mp, h, 0);
  220. if (pg == P_INVALID) {
  221. *exactp = 0;
  222. return (e);
  223. }
  224. if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
  225. return (NULL);
  226. } while ((save.index = NEXTINDEX(h)) == 0);
  227. save.page = h;
  228. }
  229. if (__bt_cmp(t, key, &save) != 0) {
  230. *exactp = 0;
  231. return (e);
  232. }
  233. }
  234. *e = save;
  235. *exactp = 1;
  236. return (e);
  237. }