bt_search.c File Reference

#include <sys/types.h>
#include <stdio.h>
#include "../include/db.h"
#include "btree.h"

Include dependency graph for bt_search.c:

Go to the source code of this file.

Functions

EPG__bt_search (BTREE *t, const DBT *key, int *exactp)
static int __bt_snext (BTREE *t, PAGE *h, const DBT *key, int *exactp)
static int __bt_sprev (BTREE *t, PAGE *h, const DBT *key, int *exactp)
static int __bt_snext __P ((BTREE *, PAGE *, const DBT *, int *))


Function Documentation

EPG* __bt_search ( BTREE t,
const DBT key,
int *  exactp 
)

Definition at line 66 of file bt_search.c.

References __bt_cmp(), __bt_snext(), __bt_sprev(), B_NODUPS, BT_CLR, BT_PUSH, F_ISSET, _page::flags, GETBINTERNAL, h, mpool_get, mpool_put, NEXTINDEX, _page::nextpg, NULL, P_BLEAF, P_INVALID, P_ROOT, _page::pgno, and _page::prevpg.

Referenced by __bt_bdelete(), __bt_first(), __bt_get(), __bt_put(), and __bt_stkacq().

00070 {
00071    PAGE *h;
00072    indx_t base, index, lim;
00073    pgno_t pg;
00074    int cmp;
00075 
00076    BT_CLR(t);
00077    for (pg = P_ROOT;;) {
00078       if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
00079          return (NULL);
00080 
00081       /* Do a binary search on the current page. */
00082       t->bt_cur.page = h;
00083       for (base = 0, lim = NEXTINDEX(h); lim; lim >>= 1) {
00084          t->bt_cur.index = index = base + (lim >> 1);
00085          if ((cmp = __bt_cmp(t, key, &t->bt_cur)) == 0) {
00086             if (h->flags & P_BLEAF) {
00087                *exactp = 1;
00088                return (&t->bt_cur);
00089             }
00090             goto next;
00091          }
00092          if (cmp > 0) {
00093             base = index + 1;
00094             --lim;
00095          }
00096       }
00097 
00098       /*
00099        * If it's a leaf page, we're almost done.  If no duplicates
00100        * are allowed, or we have an exact match, we're done.  Else,
00101        * it's possible that there were matching keys on this page,
00102        * which later deleted, and we're on a page with no matches
00103        * while there are matches on other pages.  If at the start or
00104        * end of a page, check the adjacent page.
00105        */
00106       if (h->flags & P_BLEAF) {
00107          if (!F_ISSET(t, B_NODUPS)) {
00108             if (base == 0 &&
00109                 h->prevpg != P_INVALID &&
00110                 __bt_sprev(t, h, key, exactp))
00111                return (&t->bt_cur);
00112             if (base == NEXTINDEX(h) &&
00113                 h->nextpg != P_INVALID &&
00114                 __bt_snext(t, h, key, exactp))
00115                return (&t->bt_cur);
00116          }
00117          *exactp = 0;
00118          t->bt_cur.index = base;
00119          return (&t->bt_cur);
00120       }
00121 
00122       /*
00123        * No match found.  Base is the smallest index greater than
00124        * key and may be zero or a last + 1 index.  If it's non-zero,
00125        * decrement by one, and record the internal page which should
00126        * be a parent page for the key.  If a split later occurs, the
00127        * inserted page will be to the right of the saved page.
00128        */
00129       index = base ? base - 1 : base;
00130 
00131 next:    BT_PUSH(t, h->pgno, index);
00132       pg = GETBINTERNAL(h, index)->pgno;
00133       mpool_put(t->bt_mp, h, 0);
00134    }
00135 }

static int __bt_snext ( BTREE t,
PAGE h,
const DBT key,
int *  exactp 
) [static]

Definition at line 151 of file bt_search.c.

References __bt_cmp(), e, _epg::index, mpool_get, mpool_put, _page::nextpg, NULL, and _epg::page.

Referenced by __bt_search().

00156 {
00157    EPG e;
00158 
00159    /*
00160     * Get the next page.  The key is either an exact
00161     * match, or not as good as the one we already have.
00162     */
00163    if ((e.page = mpool_get(t->bt_mp, h->nextpg, 0)) == NULL)
00164       return (0);
00165    e.index = 0;
00166    if (__bt_cmp(t, key, &e) == 0) {
00167       mpool_put(t->bt_mp, h, 0);
00168       t->bt_cur = e;
00169       *exactp = 1;
00170       return (1);
00171    }
00172    mpool_put(t->bt_mp, e.page, 0);
00173    return (0);
00174 }

static int __bt_sprev ( BTREE t,
PAGE h,
const DBT key,
int *  exactp 
) [static]

Definition at line 190 of file bt_search.c.

References __bt_cmp(), e, _epg::index, mpool_get, mpool_put, NEXTINDEX, NULL, _epg::page, and _page::prevpg.

Referenced by __bt_search().

00195 {
00196    EPG e;
00197 
00198    /*
00199     * Get the previous page.  The key is either an exact
00200     * match, or not as good as the one we already have.
00201     */
00202    if ((e.page = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL)
00203       return (0);
00204    e.index = NEXTINDEX(e.page) - 1;
00205    if (__bt_cmp(t, key, &e) == 0) {
00206       mpool_put(t->bt_mp, h, 0);
00207       t->bt_cur = e;
00208       *exactp = 1;
00209       return (1);
00210    }
00211    mpool_put(t->bt_mp, e.page, 0);
00212    return (0);
00213 }

static int __bt_sprev __P ( (BTREE *, PAGE *, const DBT *, int *)   )  [static]


Generated on Thu Apr 16 06:29:15 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6