* Copyright (c) 1990 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid
[] = "@(#)search.c 5.2 (Berkeley) 4/8/91";
#endif /* LIBC_SCCS and not lint */
* _BT_FIRST -- Find the first item in the tree that matches the supplied
* This routine supports deletion. When the user supplies a key to
* be deleted, we find the first one, and iteratively delete all the
* matching ones that follow it.
* t -- btree in which to find first occurrence
* The BTITEM for the matching item. If there's no match,
* this may point to the first item > than the supplied key,
* or off the end of the page.
* The BTITEM returned is in static space and will be overwritten
* by the next search of any kind in any btree.
/* find any matching item */
item
= _bt_search(t
, key
);
/* if we're off the end of the page, search failed and we're done */
if (item
->bti_index
>= next
)
/* as long as we have an exact match, walk backwards */
while ((r
= _bt_cmp(t
, key
->data
, item
->bti_index
)) == 0) {
if (item
->bti_index
== 0) {
/* if no prev page, we're done */
if (h
->h_prevpg
== P_NONE
)
/* walk backward, skipping empty pages */
if (_bt_getpage(t
, h
->h_prevpg
) == RET_ERROR
)
return ((BTITEM
*) NULL
);
} while (NEXTINDEX(h
) == 0 && h
->h_prevpg
!= P_NONE
);
item
->bti_index
= NEXTINDEX(h
) - 1;
item
->bti_pgno
= h
->h_pgno
;
/* if we went too far backwards, step forward one entry */
if (++(item
->bti_index
) >= NEXTINDEX(h
)
&& h
->h_nextpg
!= P_NONE
) {
/* walk forward, skipping empty pages */
if (_bt_getpage(t
, h
->h_nextpg
) == RET_ERROR
)
return ((BTITEM
*) NULL
);
} while (h
->h_nextpg
!= P_NONE
&& NEXTINDEX(h
) == 0);
item
->bti_pgno
= h
->h_pgno
;
* _BT_SEARCH, _BT_SEARCHR -- Search for a particular key in the tree.
* t -- btree in which to search
* BTITEM for matching item, if any, or the BTITEM for the
* location of the key, if it were in the tree.
* The BTITEM returned is in static memory, and will be
* overwritten by the next search of any kind in any tree.
/* we want to start all of our searches at the root */
if (_bt_getpage(t
, (pgno_t
) P_ROOT
) == RET_ERROR
)
return ((BTITEM
*) NULL
);
return (_bt_searchr(t
, key
));
BTHEADER
*h
= t
->bt_curpage
;
/* do a binary search on the current page */
index
= _bt_binsrch(t
, key
->data
);
* At this point, the binary search terminated because the endpoints
* got too close together, or we have a match. Figure out which
* case applies and decide what to do based on the page type.
if (h
->h_flags
& F_LEAF
) {
item
.bti_pgno
= h
->h_pgno
;
if (index
< NEXTINDEX(h
))
d
= (DATUM
*) GETDATUM(h
,index
);
id
= (IDATUM
*) GETDATUM(h
, index
);
if (_bt_push(t
, h
->h_pgno
) == RET_ERROR
)
return ((BTITEM
*) NULL
);
if (_bt_getpage(t
, id
->i_pgno
) == RET_ERROR
)
return ((BTITEM
*) NULL
);
return (_bt_searchr(t
, key
));
* _BT_BINSRCH -- Do a binary search for a given key on the current page.
* Searches on internal pages are handled slightly differently from
* searches on leaf pages. This is because internal page searches
* find the largest item <= key in the tree, and leaf searches find
* the smallest item >= key. This guarantees that leaf page searches
* leave us pointing at the item's correct position, and internal
* searches descend the tree correctly.
* key -- key we're looking for
* Index of the line pointer array entry for the (closest)
* match to key on the current page, with "closest" as defined
index_t lbound
, ubound
, cur
;
BTHEADER
*h
= t
->bt_curpage
;
/* do a binary search on the current page */
while ((ubound
- lbound
) > 1) {
cur
= lbound
+ ((ubound
- lbound
) / 2);
r
= _bt_cmp(t
, key
, cur
);
* At this point, the binary search terminated because the endpoints
* got too close together, or we have a match. Figure out which
* case applies, decide what to do based on the page type (leaf or
* internal), and do the right thing.
} else if (ubound
!= lbound
) {
if (h
->h_flags
& F_LEAF
) {
r
= _bt_cmp(t
, key
, lbound
);
r
= _bt_cmp(t
, key
, ubound
);
/* for internal nodes, move as far left as possible */
r
= _bt_cmp(t
, key
, lbound
);
if (h
->h_flags
& F_LEAF
) {
if (ubound
< NEXTINDEX(h
)) {
r
= _bt_cmp(t
, key
, ubound
);
/* for internal pages, move as far left as possible */
if (ubound
== NEXTINDEX(h
))
while (_bt_cmp(t
, key
, ubound
) < 0)