+ MDB_xcursor mx;
+ MDB_pageparent mp2;
+
+ mdb_xcursor_init0(txn, dbi, &mx);
+ mdb_xcursor_init1(txn, dbi, &mx, leaf);
+ if (flags == MDB_DEL_DUP) {
+ rc = mdb_del(&mx.mx_txn, mx.mx_cursor.mc_dbi, data, NULL, 0);
+ mdb_xcursor_fini(txn, dbi, &mx);
+ if (rc != MDB_SUCCESS)
+ return rc;
+ /* If sub-DB still has entries, we're done */
+ if (mx.mx_txn.mt_dbs[mx.mx_cursor.mc_dbi].md_root != P_INVALID) {
+ memcpy(NODEDATA(leaf), &mx.mx_txn.mt_dbs[mx.mx_cursor.mc_dbi],
+ sizeof(MDB_db));
+ return rc;
+ }
+ /* otherwise fall thru and delete the sub-DB */
+ } else {
+ /* add all the child DB's pages to the free list */
+ rc = mdb_search_page(&mx.mx_txn, mx.mx_cursor.mc_dbi,
+ NULL, &mx.mx_cursor, 0, &mp2);
+ if (rc == MDB_SUCCESS) {
+ MDB_ppage *top, *parent;
+ MDB_node *ni;
+ unsigned int i;
+
+ cursor_pop_page(&mx.mx_cursor);
+ top = CURSOR_TOP(&mx.mx_cursor);
+ if (top != NULL) {
+ parent = SLIST_NEXT(top, mp_entry);
+ while (parent != NULL) {
+ for (i=0; i<NUMKEYS(top->mp_page); i++) {
+ ni = NODEPTR(top->mp_page, i);
+ mdb_idl_insert(txn->mt_free_pgs, ni->mn_pgno);
+ }
+ if (parent) {
+ parent->mp_ki++;
+ if (parent->mp_ki >= NUMKEYS(parent->mp_page)) {
+ cursor_pop_page(&mx.mx_cursor);
+ top = CURSOR_TOP(&mx.mx_cursor);
+ parent = SLIST_NEXT(top, mp_entry);
+ } else {
+ ni = NODEPTR(parent->mp_page, parent->mp_ki);
+ top->mp_page = mdb_get_page(&mx.mx_txn, ni->mn_pgno);
+ }
+ }
+ }
+ }
+ mdb_idl_insert(txn->mt_free_pgs, mx.mx_txn.mt_dbs[mx.mx_cursor.mc_dbi].md_root);
+ }
+ }