bt_search.c

Go to the documentation of this file.
00001 /*-
00002  * Copyright (c) 1990, 1993, 1994
00003  * The Regents of the University of California.  All rights reserved.
00004  *
00005  * This code is derived from software contributed to Berkeley by
00006  * Mike Olson.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 3. All advertising materials mentioning features or use of this software
00017  *    must display the following acknowledgement:
00018  * This product includes software developed by the University of
00019  * California, Berkeley and its contributors.
00020  * 4. Neither the name of the University nor the names of its contributors
00021  *    may be used to endorse or promote products derived from this software
00022  *    without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00025  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00027  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00030  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00031  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00033  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00034  * SUCH DAMAGE.
00035  */
00036 
00037 #if defined(LIBC_SCCS) && !defined(lint)
00038 static char sccsid[] = "@(#)bt_search.c   8.8 (Berkeley) 7/31/94";
00039 #endif /* LIBC_SCCS and not lint */
00040 
00041 #include <sys/types.h>
00042 
00043 #include <stdio.h>
00044 
00045 #include "../include/db.h"
00046 #include "btree.h"
00047 
00048 static int __bt_snext __P((BTREE *, PAGE *, const DBT *, int *));
00049 static int __bt_sprev __P((BTREE *, PAGE *, const DBT *, int *));
00050 
00051 /*
00052  * __bt_search --
00053  * Search a btree for a key.
00054  *
00055  * Parameters:
00056  * t: tree to search
00057  * key:  key to find
00058  * exactp:  pointer to exact match flag
00059  *
00060  * Returns:
00061  * The EPG for matching record, if any, or the EPG for the location
00062  * of the key, if it were inserted into the tree, is entered into
00063  * the bt_cur field of the tree.  A pointer to the field is returned.
00064  */
00065 EPG *
00066 __bt_search(t, key, exactp)
00067    BTREE *t;
00068    const DBT *key;
00069    int *exactp;
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 }
00136 
00137 /*
00138  * __bt_snext --
00139  * Check for an exact match after the key.
00140  *
00141  * Parameters:
00142  * t: tree
00143  * h: current page
00144  * key:  key
00145  * exactp:  pointer to exact match flag
00146  *
00147  * Returns:
00148  * If an exact match found.
00149  */
00150 static int
00151 __bt_snext(t, h, key, exactp)
00152    BTREE *t;
00153    PAGE *h;
00154    const DBT *key;
00155    int *exactp;
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 }
00175 
00176 /*
00177  * __bt_sprev --
00178  * Check for an exact match before the key.
00179  *
00180  * Parameters:
00181  * t: tree
00182  * h: current page
00183  * key:  key
00184  * exactp:  pointer to exact match flag
00185  *
00186  * Returns:
00187  * If an exact match found.
00188  */
00189 static int
00190 __bt_sprev(t, h, key, exactp)
00191    BTREE *t;
00192    PAGE *h;
00193    const DBT *key;
00194    int *exactp;
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 }

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