+// set cursor to highest slot on highest page
+
+uint bt_lastkey (BtDb *bt)
+{
+uid page_no = bt_getid (bt->mgr->pagezero->alloc->left);
+BtPageSet set[1];
+uint slot;
+
+ if( set->latch = bt_pinlatch (bt, page_no, 1) )
+ set->page = bt_mappage (bt, set->latch);
+ else
+ return 0;
+
+ bt_lockpage(BtLockRead, set->latch);
+
+ memcpy (bt->cursor, set->page, bt->mgr->page_size);
+ slot = set->page->cnt;
+
+ bt_unlockpage(BtLockRead, set->latch);
+ bt_unpinlatch (set->latch);
+ return slot;
+}
+
+// return previous slot on cursor page
+
+uint bt_prevkey (BtDb *bt, uint slot)
+{
+uid ourright, next, us = bt->cursor_page;
+BtPageSet set[1];
+
+ if( --slot )
+ return slot;
+
+ ourright = bt_getid(bt->cursor->right);
+
+goleft:
+ if( !(next = bt_getid(bt->cursor->left)) )
+ return 0;
+
+findourself:
+ bt->cursor_page = next;
+
+ if( set->latch = bt_pinlatch (bt, next, 1) )
+ set->page = bt_mappage (bt, set->latch);
+ else
+ return 0;
+
+ bt_lockpage(BtLockRead, set->latch);
+ memcpy (bt->cursor, set->page, bt->mgr->page_size);
+ bt_unlockpage(BtLockRead, set->latch);
+ bt_unpinlatch (set->latch);
+
+ next = bt_getid (bt->cursor->right);
+
+ if( next != us )
+ if( next == ourright )
+ goto goleft;
+ else
+ goto findourself;
+
+ return bt->cursor->cnt;
+}
+