+ if( set->latch = bt_pinleaf (mgr, next, thread_no) )
+ set->page = bt_mappage (mgr, set->latch);
+ else
+ return 0;
+
+ bt_lockpage(BtLockRead, set->latch, thread_no, __LINE__);
+ next = bt_getid (set->page->right);
+
+ } while( next != us );
+
+ slot = set->page->cnt + 1;
+ }
+}
+
+// advance to previous key
+
+BTERR bt_prevkey (BtDb *bt)
+{
+int cmp;
+
+ // first advance last key(s) one previous slot
+
+ while( 1 ) {
+ switch( bt->phase ) {
+ case 0:
+ bt->cacheslot = bt_prevslot (bt->mgr, bt->cacheset, bt->cacheslot, bt->thread_no);
+ break;
+ case 1:
+ bt->mainslot = bt_prevslot (bt->main, bt->mainset, bt->mainslot, bt->thread_no);
+ break;
+ case 2:
+ bt->cacheslot = bt_prevslot (bt->mgr, bt->cacheset, bt->cacheslot, bt->thread_no);
+ bt->mainslot = bt_prevslot (bt->main, bt->mainset, bt->mainslot, bt->thread_no);
+ break;
+ }
+
+ // return next key
+
+ if( bt->cacheslot ) {
+ bt->cachenode = slotptr(bt->cacheset->page, bt->cacheslot);
+ bt->cachekey = keyptr(bt->cacheset->page, bt->cacheslot);
+ bt->cacheval = valptr(bt->cacheset->page, bt->cacheslot);
+ }
+
+ if( bt->mainslot ) {
+ bt->mainnode = slotptr(bt->mainset->page, bt->mainslot);
+ bt->mainkey = keyptr(bt->mainset->page, bt->mainslot);
+ bt->mainval = valptr(bt->mainset->page, bt->mainslot);
+ }
+
+ if( bt->mainslot && bt->cacheslot )
+ cmp = keycmp (bt->cachekey, bt->mainkey->key, bt->mainkey->len);
+ else if( bt->cacheslot )
+ cmp = 1;
+ else if( bt->mainslot )
+ cmp = -1;