bt_seq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*-
  2. * Copyright (c) 1990, 1993, 1994
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * Mike Olson.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. All advertising materials mentioning features or use of this software
  17. * must display the following acknowledgement:
  18. * This product includes software developed by the University of
  19. * California, Berkeley and its contributors.
  20. * 4. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. */
  36. #if defined(LIBC_SCCS) && !defined(lint)
  37. static char sccsid[] = "@(#)bt_seq.c 8.7 (Berkeley) 7/20/94";
  38. #endif /* LIBC_SCCS and not lint */
  39. #include <sys/types.h>
  40. #include <errno.h>
  41. #include <stddef.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include "../include/db.h"
  45. #include "btree.h"
  46. static int __bt_first __P((BTREE *, const DBT *, EPG *, int *));
  47. static int __bt_seqadv __P((BTREE *, EPG *, int));
  48. static int __bt_seqset __P((BTREE *, EPG *, DBT *, int));
  49. /*
  50. * Sequential scan support.
  51. *
  52. * The tree can be scanned sequentially, starting from either end of the
  53. * tree or from any specific key. A scan request before any scanning is
  54. * done is initialized as starting from the least node.
  55. */
  56. /*
  57. * __bt_seq --
  58. * Btree sequential scan interface.
  59. *
  60. * Parameters:
  61. * dbp: pointer to access method
  62. * key: key for positioning and return value
  63. * data: data return value
  64. * flags: R_CURSOR, R_FIRST, R_LAST, R_NEXT, R_PREV.
  65. *
  66. * Returns:
  67. * RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key.
  68. */
  69. int
  70. __bt_seq(dbp, key, data, flags)
  71. const DB *dbp;
  72. DBT *key, *data;
  73. u_int flags;
  74. {
  75. BTREE *t;
  76. EPG e;
  77. int status;
  78. t = dbp->internal;
  79. /* Toss any page pinned across calls. */
  80. if (t->bt_pinned != NULL) {
  81. mpool_put(t->bt_mp, t->bt_pinned, 0);
  82. t->bt_pinned = NULL;
  83. }
  84. /*
  85. * If scan uninitialized as yet, or starting at a specific record, set
  86. * the scan to a specific key. Both __bt_seqset and __bt_seqadv pin
  87. * the page the cursor references if they're successful.
  88. */
  89. switch (flags) {
  90. case R_NEXT:
  91. case R_PREV:
  92. if (F_ISSET(&t->bt_cursor, CURS_INIT)) {
  93. status = __bt_seqadv(t, &e, flags);
  94. break;
  95. }
  96. /* FALLTHROUGH */
  97. case R_FIRST:
  98. case R_LAST:
  99. case R_CURSOR:
  100. status = __bt_seqset(t, &e, key, flags);
  101. break;
  102. default:
  103. errno = EINVAL;
  104. return (RET_ERROR);
  105. }
  106. if (status == RET_SUCCESS) {
  107. __bt_setcur(t, e.page->pgno, e.index);
  108. status =
  109. __bt_ret(t, &e, key, &t->bt_rkey, data, &t->bt_rdata, 0);
  110. /*
  111. * If the user is doing concurrent access, we copied the
  112. * key/data, toss the page.
  113. */
  114. if (F_ISSET(t, B_DB_LOCK))
  115. mpool_put(t->bt_mp, e.page, 0);
  116. else
  117. t->bt_pinned = e.page;
  118. }
  119. return (status);
  120. }
  121. /*
  122. * __bt_seqset --
  123. * Set the sequential scan to a specific key.
  124. *
  125. * Parameters:
  126. * t: tree
  127. * ep: storage for returned key
  128. * key: key for initial scan position
  129. * flags: R_CURSOR, R_FIRST, R_LAST, R_NEXT, R_PREV
  130. *
  131. * Side effects:
  132. * Pins the page the cursor references.
  133. *
  134. * Returns:
  135. * RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key.
  136. */
  137. static int
  138. __bt_seqset(t, ep, key, flags)
  139. BTREE *t;
  140. EPG *ep;
  141. DBT *key;
  142. int flags;
  143. {
  144. PAGE *h;
  145. pgno_t pg;
  146. int exact;
  147. /*
  148. * Find the first, last or specific key in the tree and point the
  149. * cursor at it. The cursor may not be moved until a new key has
  150. * been found.
  151. */
  152. switch (flags) {
  153. case R_CURSOR: /* Keyed scan. */
  154. /*
  155. * Find the first instance of the key or the smallest key
  156. * which is greater than or equal to the specified key.
  157. */
  158. if (key->data == NULL || key->size == 0) {
  159. errno = EINVAL;
  160. return (RET_ERROR);
  161. }
  162. return (__bt_first(t, key, ep, &exact));
  163. case R_FIRST: /* First record. */
  164. case R_NEXT:
  165. /* Walk down the left-hand side of the tree. */
  166. for (pg = P_ROOT;;) {
  167. if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
  168. return (RET_ERROR);
  169. /* Check for an empty tree. */
  170. if (NEXTINDEX(h) == 0) {
  171. mpool_put(t->bt_mp, h, 0);
  172. return (RET_SPECIAL);
  173. }
  174. if (h->flags & (P_BLEAF | P_RLEAF))
  175. break;
  176. pg = GETBINTERNAL(h, 0)->pgno;
  177. mpool_put(t->bt_mp, h, 0);
  178. }
  179. ep->page = h;
  180. ep->index = 0;
  181. break;
  182. case R_LAST: /* Last record. */
  183. case R_PREV:
  184. /* Walk down the right-hand side of the tree. */
  185. for (pg = P_ROOT;;) {
  186. if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
  187. return (RET_ERROR);
  188. /* Check for an empty tree. */
  189. if (NEXTINDEX(h) == 0) {
  190. mpool_put(t->bt_mp, h, 0);
  191. return (RET_SPECIAL);
  192. }
  193. if (h->flags & (P_BLEAF | P_RLEAF))
  194. break;
  195. pg = GETBINTERNAL(h, NEXTINDEX(h) - 1)->pgno;
  196. mpool_put(t->bt_mp, h, 0);
  197. }
  198. ep->page = h;
  199. ep->index = NEXTINDEX(h) - 1;
  200. break;
  201. }
  202. return (RET_SUCCESS);
  203. }
  204. /*
  205. * __bt_seqadvance --
  206. * Advance the sequential scan.
  207. *
  208. * Parameters:
  209. * t: tree
  210. * flags: R_NEXT, R_PREV
  211. *
  212. * Side effects:
  213. * Pins the page the new key/data record is on.
  214. *
  215. * Returns:
  216. * RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key.
  217. */
  218. static int
  219. __bt_seqadv(t, ep, flags)
  220. BTREE *t;
  221. EPG *ep;
  222. int flags;
  223. {
  224. CURSOR *c;
  225. PAGE *h;
  226. indx_t index = 0;
  227. pgno_t pg;
  228. int exact;
  229. /*
  230. * There are a couple of states that we can be in. The cursor has
  231. * been initialized by the time we get here, but that's all we know.
  232. */
  233. c = &t->bt_cursor;
  234. /*
  235. * The cursor was deleted where there weren't any duplicate records,
  236. * so the key was saved. Find out where that key would go in the
  237. * current tree. It doesn't matter if the returned key is an exact
  238. * match or not -- if it's an exact match, the record was added after
  239. * the delete so we can just return it. If not, as long as there's
  240. * a record there, return it.
  241. */
  242. if (F_ISSET(c, CURS_ACQUIRE))
  243. return (__bt_first(t, &c->key, ep, &exact));
  244. /* Get the page referenced by the cursor. */
  245. if ((h = mpool_get(t->bt_mp, c->pg.pgno, 0)) == NULL)
  246. return (RET_ERROR);
  247. /*
  248. * Find the next/previous record in the tree and point the cursor at
  249. * it. The cursor may not be moved until a new key has been found.
  250. */
  251. switch (flags) {
  252. case R_NEXT: /* Next record. */
  253. /*
  254. * The cursor was deleted in duplicate records, and moved
  255. * forward to a record that has yet to be returned. Clear
  256. * that flag, and return the record.
  257. */
  258. if (F_ISSET(c, CURS_AFTER))
  259. goto usecurrent;
  260. index = c->pg.index;
  261. if (++index == NEXTINDEX(h)) {
  262. pg = h->nextpg;
  263. mpool_put(t->bt_mp, h, 0);
  264. if (pg == P_INVALID)
  265. return (RET_SPECIAL);
  266. if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
  267. return (RET_ERROR);
  268. index = 0;
  269. }
  270. break;
  271. case R_PREV: /* Previous record. */
  272. /*
  273. * The cursor was deleted in duplicate records, and moved
  274. * backward to a record that has yet to be returned. Clear
  275. * that flag, and return the record.
  276. */
  277. if (F_ISSET(c, CURS_BEFORE)) {
  278. usecurrent: F_CLR(c, CURS_AFTER | CURS_BEFORE);
  279. ep->page = h;
  280. ep->index = c->pg.index;
  281. return (RET_SUCCESS);
  282. }
  283. index = c->pg.index;
  284. if (index == 0) {
  285. pg = h->prevpg;
  286. mpool_put(t->bt_mp, h, 0);
  287. if (pg == P_INVALID)
  288. return (RET_SPECIAL);
  289. if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
  290. return (RET_ERROR);
  291. index = NEXTINDEX(h) - 1;
  292. } else
  293. --index;
  294. break;
  295. }
  296. ep->page = h;
  297. ep->index = index;
  298. return (RET_SUCCESS);
  299. }
  300. /*
  301. * __bt_first --
  302. * Find the first entry.
  303. *
  304. * Parameters:
  305. * t: the tree
  306. * key: the key
  307. * erval: return EPG
  308. * exactp: pointer to exact match flag
  309. *
  310. * Returns:
  311. * The first entry in the tree greater than or equal to key,
  312. * or RET_SPECIAL if no such key exists.
  313. */
  314. static int
  315. __bt_first(t, key, erval, exactp)
  316. BTREE *t;
  317. const DBT *key;
  318. EPG *erval;
  319. int *exactp;
  320. {
  321. PAGE *h;
  322. EPG *ep, save;
  323. pgno_t pg;
  324. /*
  325. * Find any matching record; __bt_search pins the page.
  326. *
  327. * If it's an exact match and duplicates are possible, walk backwards
  328. * in the tree until we find the first one. Otherwise, make sure it's
  329. * a valid key (__bt_search may return an index just past the end of a
  330. * page) and return it.
  331. */
  332. if ((ep = __bt_search(t, key, exactp)) == NULL)
  333. return (RET_SPECIAL);
  334. if (*exactp) {
  335. if (F_ISSET(t, B_NODUPS)) {
  336. *erval = *ep;
  337. return (RET_SUCCESS);
  338. }
  339. /*
  340. * Walk backwards, as long as the entry matches and there are
  341. * keys left in the tree. Save a copy of each match in case
  342. * we go too far.
  343. */
  344. save = *ep;
  345. h = ep->page;
  346. do {
  347. if (save.page->pgno != ep->page->pgno) {
  348. mpool_put(t->bt_mp, save.page, 0);
  349. save = *ep;
  350. } else
  351. save.index = ep->index;
  352. /*
  353. * Don't unpin the page the last (or original) match
  354. * was on, but make sure it's unpinned if an error
  355. * occurs.
  356. */
  357. if (ep->index == 0) {
  358. if (h->prevpg == P_INVALID)
  359. break;
  360. if (h->pgno != save.page->pgno)
  361. mpool_put(t->bt_mp, h, 0);
  362. if ((h = mpool_get(t->bt_mp,
  363. h->prevpg, 0)) == NULL) {
  364. if (h->pgno == save.page->pgno)
  365. mpool_put(t->bt_mp,
  366. save.page, 0);
  367. return (RET_ERROR);
  368. }
  369. ep->page = h;
  370. ep->index = NEXTINDEX(h);
  371. }
  372. --ep->index;
  373. } while (__bt_cmp(t, key, ep) == 0);
  374. /*
  375. * Reach here with the last page that was looked at pinned,
  376. * which may or may not be the same as the last (or original)
  377. * match page. If it's not useful, release it.
  378. */
  379. if (h->pgno != save.page->pgno)
  380. mpool_put(t->bt_mp, h, 0);
  381. *erval = save;
  382. return (RET_SUCCESS);
  383. }
  384. /* If at the end of a page, find the next entry. */
  385. if (ep->index == NEXTINDEX(ep->page)) {
  386. h = ep->page;
  387. pg = h->nextpg;
  388. mpool_put(t->bt_mp, h, 0);
  389. if (pg == P_INVALID)
  390. return (RET_SPECIAL);
  391. if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
  392. return (RET_ERROR);
  393. ep->index = 0;
  394. ep->page = h;
  395. }
  396. *erval = *ep;
  397. return (RET_SUCCESS);
  398. }
  399. /*
  400. * __bt_setcur --
  401. * Set the cursor to an entry in the tree.
  402. *
  403. * Parameters:
  404. * t: the tree
  405. * pgno: page number
  406. * index: page index
  407. */
  408. void
  409. __bt_setcur(t, pgno, index)
  410. BTREE *t;
  411. pgno_t pgno;
  412. u_int index;
  413. {
  414. /* Lose any already deleted key. */
  415. if (t->bt_cursor.key.data != NULL) {
  416. free(t->bt_cursor.key.data);
  417. t->bt_cursor.key.size = 0;
  418. t->bt_cursor.key.data = NULL;
  419. }
  420. F_CLR(&t->bt_cursor, CURS_ACQUIRE | CURS_AFTER | CURS_BEFORE);
  421. /* Update the cursor. */
  422. t->bt_cursor.pg.pgno = pgno;
  423. t->bt_cursor.pg.index = index;
  424. F_SET(&t->bt_cursor, CURS_INIT);
  425. }