1 /*- 2 * Copyright (c) 1990, 1993 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 37 #if defined(LIBC_SCCS) && !defined(lint) 38 /* from: static char sccsid[] = "@(#)bt_get.c 8.2 (Berkeley) 9/7/93"; */ 39 static char *rcsid = "$Id: bt_get.c,v 1.4 1993/09/09 02:41:24 cgd Exp $"; 40 #endif /* LIBC_SCCS and not lint */ 41 42 #include <sys/types.h> 43 44 #include <errno.h> 45 #include <stddef.h> 46 #include <stdio.h> 47 48 #include <db.h> 49 #include "btree.h" 50 51 /* 52 * __BT_GET -- Get a record from the btree. 53 * 54 * Parameters: 55 * dbp: pointer to access method 56 * key: key to find 57 * data: data to return 58 * flag: currently unused 59 * 60 * Returns: 61 * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found. 62 */ 63 int 64 __bt_get(dbp, key, data, flags) 65 const DB *dbp; 66 const DBT *key; 67 DBT *data; 68 u_int flags; 69 { 70 BTREE *t; 71 EPG *e; 72 int exact, status; 73 74 t = dbp->internal; 75 76 /* Toss any page pinned across calls. */ 77 if (t->bt_pinned != NULL) { 78 mpool_put(t->bt_mp, t->bt_pinned, 0); 79 t->bt_pinned = NULL; 80 } 81 82 /* Get currently doesn't take any flags. */ 83 if (flags) { 84 errno = EINVAL; 85 return (RET_ERROR); 86 } 87 88 if ((e = __bt_search(t, key, &exact)) == NULL) 89 return (RET_ERROR); 90 if (!exact) { 91 mpool_put(t->bt_mp, e->page, 0); 92 return (RET_SPECIAL); 93 } 94 95 /* 96 * A special case is if we found the record but it's flagged for 97 * deletion. In this case, we want to find another record with the 98 * same key, if it exists. Rather than look around the tree we call 99 * __bt_first and have it redo the search, as __bt_first will not 100 * return keys marked for deletion. Slow, but should never happen. 101 */ 102 if (ISSET(t, B_DELCRSR) && e->page->pgno == t->bt_bcursor.pgno && 103 e->index == t->bt_bcursor.index) { 104 mpool_put(t->bt_mp, e->page, 0); 105 if ((e = __bt_first(t, key, &exact)) == NULL) 106 return (RET_ERROR); 107 if (!exact) 108 return (RET_SPECIAL); 109 } 110 111 status = __bt_ret(t, e, NULL, data); 112 /* 113 * If the user is doing concurrent access, we copied the 114 * key/data, toss the page. 115 */ 116 if (ISSET(t, B_DB_LOCK)) 117 mpool_put(t->bt_mp, e->page, 0); 118 else 119 t->bt_pinned = e->page; 120 return (status); 121 } 122 123 /* 124 * __BT_FIRST -- Find the first entry. 125 * 126 * Parameters: 127 * t: the tree 128 * key: the key 129 * 130 * Returns: 131 * The first entry in the tree greater than or equal to key. 132 */ 133 EPG * 134 __bt_first(t, key, exactp) 135 BTREE *t; 136 const DBT *key; 137 int *exactp; 138 { 139 register PAGE *h; 140 register EPG *e; 141 EPG save; 142 pgno_t cpgno, pg; 143 indx_t cindex; 144 int found; 145 146 /* 147 * Find any matching record; __bt_search pins the page. Only exact 148 * matches are tricky, otherwise just return the location of the key 149 * if it were to be inserted into the tree. 150 */ 151 if ((e = __bt_search(t, key, exactp)) == NULL) 152 return (NULL); 153 if (!*exactp) 154 return (e); 155 156 if (ISSET(t, B_DELCRSR)) { 157 cpgno = t->bt_bcursor.pgno; 158 cindex = t->bt_bcursor.index; 159 } else { 160 cpgno = P_INVALID; 161 cindex = 0; /* GCC thinks it's uninitialized. */ 162 } 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 /* 205 * Reach here with the last page that was looked at pinned, which may 206 * or may not be the same as the last (or original) match page. If 207 * it's not useful, release it. 208 */ 209 done1: if (h->pgno != save.page->pgno) 210 mpool_put(t->bt_mp, h, 0); 211 212 /* 213 * If still haven't found a record, the only possibility left is the 214 * next one. Move forward one slot, skipping empty pages and check. 215 */ 216 if (!found) { 217 h = save.page; 218 if (++save.index == NEXTINDEX(h)) { 219 do { 220 pg = h->nextpg; 221 mpool_put(t->bt_mp, h, 0); 222 if (pg == P_INVALID) { 223 *exactp = 0; 224 return (e); 225 } 226 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) 227 return (NULL); 228 } while ((save.index = NEXTINDEX(h)) == 0); 229 save.page = h; 230 } 231 if (__bt_cmp(t, key, &save) != 0) { 232 *exactp = 0; 233 return (e); 234 } 235 } 236 *e = save; 237 *exactp = 1; 238 return (e); 239 } 240