if the tree is being prefix compressed, it's possible to get a base
authorKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Mon, 11 Jan 1993 04:31:31 +0000 (20:31 -0800)
committerKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Mon, 11 Jan 1993 04:31:31 +0000 (20:31 -0800)
of zero.

SCCS-vsn: lib/libc/db/btree/bt_search.c 5.6

usr/src/lib/libc/db/btree/bt_search.c

index 669f24c..d0609dc 100644 (file)
@@ -9,7 +9,7 @@
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)bt_search.c        5.5 (Berkeley) %G%";
+static char sccsid[] = "@(#)bt_search.c        5.6 (Berkeley) %G%";
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/types.h>
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/types.h>
@@ -68,21 +68,22 @@ __bt_search(t, key, exactp)
                        }
                }
 
                        }
                }
 
-               /*
-                * No match found.  Base is the smallest index greater than
-                * key but may be an illegal index.  Use base if it's a leaf
-                * page, decrement it by one if it's an internal page.  This
-                * is safe because internal pages can't be empty.
-                */
-               index = h->flags & P_BLEAF ? base : base - 1;
-
                /* If it's a leaf page, we're done. */
                if (h->flags & P_BLEAF) {
                /* If it's a leaf page, we're done. */
                if (h->flags & P_BLEAF) {
-                       e.index = index;
+                       e.index = base;
                        *exactp = 0;
                        return (&e);
                }
 
                        *exactp = 0;
                        return (&e);
                }
 
+               /*
+                * No match found.  Base is the smallest index greater than
+                * key and may be zero or a last + 1 index.  If it's non-zero,
+                * decrement by one, and record the internal page which should
+                * be a parent page for the key.  If a split later occurs, the
+                * inserted page will be to the right of the saved page.
+                */
+               index = base ? base - 1 : base;
+
 next:          if (__bt_push(t, h->pgno, index) == RET_ERROR)
                        return (NULL);
                pg = GETBINTERNAL(h, index)->pgno;
 next:          if (__bt_push(t, h->pgno, index) == RET_ERROR)
                        return (NULL);
                pg = GETBINTERNAL(h, index)->pgno;