bt_rsearch.c
Upload User: tsgydb
Upload Date: 2007-04-14
Package Size: 10674k
Code Size: 11k
Category:

MySQL

Development Platform:

Visual C++

  1. /*-
  2.  * See the file LICENSE for redistribution information.
  3.  *
  4.  * Copyright (c) 1996, 1997, 1998, 1999, 2000
  5.  * Sleepycat Software.  All rights reserved.
  6.  */
  7. /*
  8.  * Copyright (c) 1990, 1993, 1994, 1995, 1996
  9.  * Keith Bostic.  All rights reserved.
  10.  */
  11. /*
  12.  * Copyright (c) 1990, 1993
  13.  * The Regents of the University of California.  All rights reserved.
  14.  *
  15.  * Redistribution and use in source and binary forms, with or without
  16.  * modification, are permitted provided that the following conditions
  17.  * are met:
  18.  * 1. Redistributions of source code must retain the above copyright
  19.  *    notice, this list of conditions and the following disclaimer.
  20.  * 2. Redistributions in binary form must reproduce the above copyright
  21.  *    notice, this list of conditions and the following disclaimer in the
  22.  *    documentation and/or other materials provided with the distribution.
  23.  * 3. Neither the name of the University nor the names of its contributors
  24.  *    may be used to endorse or promote products derived from this software
  25.  *    without specific prior written permission.
  26.  *
  27.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  28.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  31.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  36.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  37.  * SUCH DAMAGE.
  38.  */
  39. #include "db_config.h"
  40. #ifndef lint
  41. static const char revid[] = "$Id: bt_rsearch.c,v 11.21 2000/03/28 21:50:04 ubell Exp $";
  42. #endif /* not lint */
  43. #ifndef NO_SYSTEM_INCLUDES
  44. #include <sys/types.h>
  45. #endif
  46. #include "db_int.h"
  47. #include "db_page.h"
  48. #include "btree.h"
  49. #include "db_shash.h"
  50. #include "lock.h"
  51. /*
  52.  * __bam_rsearch --
  53.  * Search a btree for a record number.
  54.  *
  55.  * PUBLIC: int __bam_rsearch __P((DBC *, db_recno_t *, u_int32_t, int, int *));
  56.  */
  57. int
  58. __bam_rsearch(dbc, recnop, flags, stop, exactp)
  59. DBC *dbc;
  60. db_recno_t *recnop;
  61. u_int32_t flags;
  62. int stop, *exactp;
  63. {
  64. BINTERNAL *bi;
  65. BTREE_CURSOR *cp;
  66. DB *dbp;
  67. DB_LOCK lock;
  68. PAGE *h;
  69. RINTERNAL *ri;
  70. db_indx_t adjust, deloffset, indx, top;
  71. db_lockmode_t lock_mode;
  72. db_pgno_t pg;
  73. db_recno_t recno, t_recno, total;
  74. int ret, stack;
  75. dbp = dbc->dbp;
  76. cp = (BTREE_CURSOR *)dbc->internal;
  77. BT_STK_CLR(cp);
  78. /*
  79.  * There are several ways we search a btree tree.  The flags argument
  80.  * specifies if we're acquiring read or write locks and if we are
  81.  * locking pairs of pages.  In addition, if we're adding or deleting
  82.  * an item, we have to lock the entire tree, regardless.  See btree.h
  83.  * for more details.
  84.  *
  85.  * If write-locking pages, we need to know whether or not to acquire a
  86.  * write lock on a page before getting it.  This depends on how deep it
  87.  * is in tree, which we don't know until we acquire the root page.  So,
  88.  * if we need to lock the root page we may have to upgrade it later,
  89.  * because we won't get the correct lock initially.
  90.  *
  91.  * Retrieve the root page.
  92.  */
  93. pg = cp->root;
  94. stack = LF_ISSET(S_STACK);
  95. lock_mode = stack ? DB_LOCK_WRITE : DB_LOCK_READ;
  96. if ((ret = __db_lget(dbc, 0, pg, lock_mode, 0, &lock)) != 0)
  97. return (ret);
  98. if ((ret = memp_fget(dbp->mpf, &pg, 0, &h)) != 0) {
  99. /* Did not read it, so we can release the lock */
  100. (void)__LPUT(dbc, lock);
  101. return (ret);
  102. }
  103. /*
  104.  * Decide if we need to save this page; if we do, write lock it.
  105.  * We deliberately don't lock-couple on this call.  If the tree
  106.  * is tiny, i.e., one page, and two threads are busily updating
  107.  * the root page, we're almost guaranteed deadlocks galore, as
  108.  * each one gets a read lock and then blocks the other's attempt
  109.  * for a write lock.
  110.  */
  111. if (!stack &&
  112.     ((LF_ISSET(S_PARENT) && (u_int8_t)(stop + 1) >= h->level) ||
  113.     (LF_ISSET(S_WRITE) && h->level == LEAFLEVEL))) {
  114. (void)memp_fput(dbp->mpf, h, 0);
  115. (void)__LPUT(dbc, lock);
  116. lock_mode = DB_LOCK_WRITE;
  117. if ((ret = __db_lget(dbc, 0, pg, lock_mode, 0, &lock)) != 0)
  118. return (ret);
  119. if ((ret = memp_fget(dbp->mpf, &pg, 0, &h)) != 0) {
  120. /* Did not read it, so we can release the lock */
  121. (void)__LPUT(dbc, lock);
  122. return (ret);
  123. }
  124. stack = 1;
  125. }
  126. /*
  127.  * If appending to the tree, set the record number now -- we have the
  128.  * root page locked.
  129.  *
  130.  * Delete only deletes exact matches, read only returns exact matches.
  131.  * Note, this is different from __bam_search(), which returns non-exact
  132.  * matches for read.
  133.  *
  134.  * The record may not exist.  We can only return the correct location
  135.  * for the record immediately after the last record in the tree, so do
  136.  * a fast check now.
  137.  */
  138. total = RE_NREC(h);
  139. if (LF_ISSET(S_APPEND)) {
  140. *exactp = 0;
  141. *recnop = recno = total + 1;
  142. } else {
  143. recno = *recnop;
  144. if (recno <= total)
  145. *exactp = 1;
  146. else {
  147. *exactp = 0;
  148. if (!LF_ISSET(S_PAST_EOF) || recno > total + 1) {
  149. /*
  150.  * Keep the page locked for serializability.
  151.  *
  152.  * XXX
  153.  * This leaves the root page locked, which will
  154.  * eliminate any concurrency.  A possible fix
  155.  * would be to lock the last leaf page instead.
  156.  */
  157. (void)memp_fput(dbp->mpf, h, 0);
  158. (void)__TLPUT(dbc, lock);
  159. return (DB_NOTFOUND);
  160. }
  161. }
  162. }
  163. /*
  164.  * !!!
  165.  * Record numbers in the tree are 0-based, but the recno is
  166.  * 1-based.  All of the calculations below have to take this
  167.  * into account.
  168.  */
  169. for (total = 0;;) {
  170. switch (TYPE(h)) {
  171. case P_LBTREE:
  172. case P_LDUP:
  173. recno -= total;
  174. /*
  175.  * There may be logically deleted records on the page.
  176.  * If there are enough, the record may not exist.
  177.  */
  178. if (TYPE(h) == P_LBTREE) {
  179. adjust = P_INDX;
  180. deloffset = O_INDX;
  181. } else {
  182. adjust = O_INDX;
  183. deloffset = 0;
  184. }
  185. for (t_recno = 0, indx = 0;; indx += adjust) {
  186. if (indx >= NUM_ENT(h)) {
  187. *exactp = 0;
  188. if (!LF_ISSET(S_PAST_EOF) ||
  189.     recno > t_recno + 1) {
  190. ret = DB_NOTFOUND;
  191. goto err;
  192. }
  193. }
  194. if (!B_DISSET(
  195.     GET_BKEYDATA(h, indx + deloffset)->type) &&
  196.     ++t_recno == recno)
  197. break;
  198. }
  199. /* Correct from 1-based to 0-based for a page offset. */
  200. BT_STK_ENTER(dbp->dbenv,
  201.     cp, h, indx, lock, lock_mode, ret);
  202. if (ret != 0)
  203. goto err;
  204. return (0);
  205. case P_IBTREE:
  206. for (indx = 0, top = NUM_ENT(h);;) {
  207. bi = GET_BINTERNAL(h, indx);
  208. if (++indx == top || total + bi->nrecs >= recno)
  209. break;
  210. total += bi->nrecs;
  211. }
  212. pg = bi->pgno;
  213. break;
  214. case P_LRECNO:
  215. recno -= total;
  216. /* Correct from 1-based to 0-based for a page offset. */
  217. --recno;
  218. BT_STK_ENTER(dbp->dbenv,
  219.     cp, h, recno, lock, lock_mode, ret);
  220. if (ret != 0)
  221. goto err;
  222. return (0);
  223. case P_IRECNO:
  224. for (indx = 0, top = NUM_ENT(h);;) {
  225. ri = GET_RINTERNAL(h, indx);
  226. if (++indx == top || total + ri->nrecs >= recno)
  227. break;
  228. total += ri->nrecs;
  229. }
  230. pg = ri->pgno;
  231. break;
  232. default:
  233. return (__db_pgfmt(dbp, h->pgno));
  234. }
  235. --indx;
  236. if (stack) {
  237. /* Return if this is the lowest page wanted. */
  238. if (LF_ISSET(S_PARENT) && stop == h->level) {
  239. BT_STK_ENTER(dbp->dbenv,
  240.     cp, h, indx, lock, lock_mode, ret);
  241. if (ret != 0)
  242. goto err;
  243. return (0);
  244. }
  245. BT_STK_PUSH(dbp->dbenv,
  246.     cp, h, indx, lock, lock_mode, ret);
  247. if (ret != 0)
  248. goto err;
  249. lock_mode = DB_LOCK_WRITE;
  250. if ((ret =
  251.     __db_lget(dbc, 0, pg, lock_mode, 0, &lock)) != 0)
  252. goto err;
  253. } else {
  254. /*
  255.  * Decide if we want to return a pointer to the next
  256.  * page in the stack.  If we do, write lock it and
  257.  * never unlock it.
  258.  */
  259. if ((LF_ISSET(S_PARENT) &&
  260.     (u_int8_t)(stop + 1) >= (u_int8_t)(h->level - 1)) ||
  261.     (h->level - 1) == LEAFLEVEL)
  262. stack = 1;
  263. (void)memp_fput(dbp->mpf, h, 0);
  264. lock_mode = stack &&
  265.     LF_ISSET(S_WRITE) ? DB_LOCK_WRITE : DB_LOCK_READ;
  266. if ((ret = __db_lget(dbc,
  267.     LCK_COUPLE, pg, lock_mode, 0, &lock)) != 0) {
  268. /*
  269.  * If we fail, discard the lock we held.  This
  270.  * is OK because this only happens when we are
  271.  * descending the tree holding read-locks.
  272.  */
  273. __LPUT(dbc, lock);
  274. goto err;
  275. }
  276. }
  277. if ((ret = memp_fget(dbp->mpf, &pg, 0, &h)) != 0)
  278. goto err;
  279. }
  280. /* NOTREACHED */
  281. err: BT_STK_POP(cp);
  282. __bam_stkrel(dbc, 0);
  283. return (ret);
  284. }
  285. /*
  286.  * __bam_adjust --
  287.  * Adjust the tree after adding or deleting a record.
  288.  *
  289.  * PUBLIC: int __bam_adjust __P((DBC *, int32_t));
  290.  */
  291. int
  292. __bam_adjust(dbc, adjust)
  293. DBC *dbc;
  294. int32_t adjust;
  295. {
  296. BTREE_CURSOR *cp;
  297. DB *dbp;
  298. EPG *epg;
  299. PAGE *h;
  300. db_pgno_t root_pgno;
  301. int ret;
  302. dbp = dbc->dbp;
  303. cp = (BTREE_CURSOR *)dbc->internal;
  304. root_pgno = cp->root;
  305. /* Update the record counts for the tree. */
  306. for (epg = cp->sp; epg <= cp->csp; ++epg) {
  307. h = epg->page;
  308. if (TYPE(h) == P_IBTREE || TYPE(h) == P_IRECNO) {
  309. if (DB_LOGGING(dbc) &&
  310.     (ret = __bam_cadjust_log(dbp->dbenv,
  311.     dbc->txn, &LSN(h), 0, dbp->log_fileid,
  312.     PGNO(h), &LSN(h), (u_int32_t)epg->indx, adjust,
  313.     PGNO(h) == root_pgno ? CAD_UPDATEROOT : 0)) != 0)
  314. return (ret);
  315. if (TYPE(h) == P_IBTREE)
  316. GET_BINTERNAL(h, epg->indx)->nrecs += adjust;
  317. else
  318. GET_RINTERNAL(h, epg->indx)->nrecs += adjust;
  319. if (PGNO(h) == root_pgno)
  320. RE_NREC_ADJ(h, adjust);
  321. if ((ret = memp_fset(dbp->mpf, h, DB_MPOOL_DIRTY)) != 0)
  322. return (ret);
  323. }
  324. }
  325. return (0);
  326. }
  327. /*
  328.  * __bam_nrecs --
  329.  * Return the number of records in the tree.
  330.  *
  331.  * PUBLIC: int __bam_nrecs __P((DBC *, db_recno_t *));
  332.  */
  333. int
  334. __bam_nrecs(dbc, rep)
  335. DBC *dbc;
  336. db_recno_t *rep;
  337. {
  338. DB *dbp;
  339. DB_LOCK lock;
  340. PAGE *h;
  341. db_pgno_t pgno;
  342. int ret;
  343. dbp = dbc->dbp;
  344. pgno = dbc->internal->root;
  345. if ((ret = __db_lget(dbc, 0, pgno, DB_LOCK_READ, 0, &lock)) != 0)
  346. return (ret);
  347. if ((ret = memp_fget(dbp->mpf, &pgno, 0, &h)) != 0)
  348. return (ret);
  349. *rep = RE_NREC(h);
  350. (void)memp_fput(dbp->mpf, h, 0);
  351. (void)__TLPUT(dbc, lock);
  352. return (0);
  353. }
  354. /*
  355.  * __bam_total --
  356.  * Return the number of records below a page.
  357.  *
  358.  * PUBLIC: db_recno_t __bam_total __P((PAGE *));
  359.  */
  360. db_recno_t
  361. __bam_total(h)
  362. PAGE *h;
  363. {
  364. db_recno_t nrecs;
  365. db_indx_t indx, top;
  366. nrecs = 0;
  367. top = NUM_ENT(h);
  368. switch (TYPE(h)) {
  369. case P_LBTREE:
  370. /* Check for logically deleted records. */
  371. for (indx = 0; indx < top; indx += P_INDX)
  372. if (!B_DISSET(GET_BKEYDATA(h, indx + O_INDX)->type))
  373. ++nrecs;
  374. break;
  375. case P_LDUP:
  376. /* Check for logically deleted records. */
  377. for (indx = 0; indx < top; indx += O_INDX)
  378. if (!B_DISSET(GET_BKEYDATA(h, indx)->type))
  379. ++nrecs;
  380. break;
  381. case P_IBTREE:
  382. for (indx = 0; indx < top; indx += O_INDX)
  383. nrecs += GET_BINTERNAL(h, indx)->nrecs;
  384. break;
  385. case P_LRECNO:
  386. nrecs = NUM_ENT(h);
  387. break;
  388. case P_IRECNO:
  389. for (indx = 0; indx < top; indx += O_INDX)
  390. nrecs += GET_RINTERNAL(h, indx)->nrecs;
  391. break;
  392. }
  393. return (nrecs);
  394. }