Simplify last commits

vmware
Hallvard Furuseth 11 years ago
parent 45c4ed1805
commit 392be3a7c6
  1. 121
      libraries/liblmdb/mdb.c

@ -1354,19 +1354,24 @@ mdb_dlist_free(MDB_txn *txn)
dl[0].mid = 0; dl[0].mid = 0;
} }
/* Set or clear P_KEEP in non-overflow, non-sub pages in this txn's cursors. /* Set or clear P_KEEP in dirty, non-overflow, non-sub pages watched by txn.
* @param[in] mc A cursor handle for the current operation. * @param[in] mc A cursor handle for the current operation.
* @param[in] pflags Flags of the pages to update: * @param[in] pflags Flags of the pages to update:
* P_DIRTY to set P_KEEP, P_DIRTY|P_KEEP to clear it. * P_DIRTY to set P_KEEP, P_DIRTY|P_KEEP to clear it.
* @param[in] all No shortcuts. Needed except after a full #mdb_page_flush().
* @return 0 on success, non-zero on failure.
*/ */
static void static int
mdb_cursorpages_mark(MDB_cursor *mc, unsigned pflags) mdb_pages_xkeep(MDB_cursor *mc, unsigned pflags, int all)
{ {
MDB_txn *txn = mc->mc_txn; MDB_txn *txn = mc->mc_txn;
MDB_cursor *m3; MDB_cursor *m3;
MDB_xcursor *mx; MDB_xcursor *mx;
MDB_page *dp;
unsigned i, j; unsigned i, j;
int rc = MDB_SUCCESS, level;
/* Mark pages seen by cursors */
if (mc->mc_flags & C_UNTRACK) if (mc->mc_flags & C_UNTRACK)
mc = NULL; /* will find mc in mt_cursors */ mc = NULL; /* will find mc in mt_cursors */
for (i = txn->mt_numdbs;; mc = txn->mt_cursors[--i]) { for (i = txn->mt_numdbs;; mc = txn->mt_cursors[--i]) {
@ -1384,9 +1389,26 @@ mdb_cursorpages_mark(MDB_cursor *mc, unsigned pflags)
if (i == 0) if (i == 0)
break; break;
} }
if (all) {
/* Mark dirty root pages */
for (i=0; i<txn->mt_numdbs; i++) {
if (txn->mt_dbflags[i] & DB_DIRTY) {
pgno_t pgno = txn->mt_dbs[i].md_root;
if (pgno == P_INVALID)
continue;
if ((rc = mdb_page_get(txn, pgno, &dp, &level)) != MDB_SUCCESS)
break;
if ((dp->mp_flags & (P_DIRTY|P_KEEP)) == pflags && level <= 1)
dp->mp_flags ^= P_KEEP;
}
}
}
return rc;
} }
static int mdb_page_flush(MDB_txn *txn); static int mdb_page_flush(MDB_txn *txn, int keep);
/** Spill pages from the dirty list back to disk. /** Spill pages from the dirty list back to disk.
* This is intended to prevent running into #MDB_TXN_FULL situations, * This is intended to prevent running into #MDB_TXN_FULL situations,
@ -1429,8 +1451,8 @@ mdb_page_spill(MDB_cursor *m0, MDB_val *key, MDB_val *data)
MDB_txn *txn = m0->mc_txn; MDB_txn *txn = m0->mc_txn;
MDB_page *dp; MDB_page *dp;
MDB_ID2L dl = txn->mt_u.dirty_list; MDB_ID2L dl = txn->mt_u.dirty_list;
unsigned int i, j, k, need; unsigned int i, j, need;
int rc, level; int rc;
if (m0->mc_flags & C_SUB) if (m0->mc_flags & C_SUB)
return MDB_SUCCESS; return MDB_SUCCESS;
@ -1455,21 +1477,9 @@ mdb_page_spill(MDB_cursor *m0, MDB_val *key, MDB_val *data)
return ENOMEM; return ENOMEM;
} }
/* Mark all the dirty root pages we want to preserve */ /* Preserve pages which may soon be dirtied again */
for (i=0; i<txn->mt_numdbs; i++) { if ((rc = mdb_pages_xkeep(m0, P_DIRTY, 1)) != MDB_SUCCESS)
if (txn->mt_dbflags[i] & DB_DIRTY) {
pgno_t pgno = txn->mt_dbs[i].md_root;
if (pgno == P_INVALID)
continue;
if ((rc = mdb_page_get(txn, pgno, &dp, &level)) != MDB_SUCCESS)
goto done; goto done;
if ((dp->mp_flags & P_DIRTY) && level <= 1)
dp->mp_flags |= P_KEEP;
}
}
/* Preserve pages used by cursors */
mdb_cursorpages_mark(m0, P_DIRTY);
/* Less aggressive spill - we originally spilled the entire dirty list, /* Less aggressive spill - we originally spilled the entire dirty list,
* with a few exceptions for cursor pages and DB root pages. But this * with a few exceptions for cursor pages and DB root pages. But this
@ -1478,13 +1488,12 @@ mdb_page_spill(MDB_cursor *m0, MDB_val *key, MDB_val *data)
* of the dirty pages. Testing revealed this to be a good tradeoff, * of the dirty pages. Testing revealed this to be a good tradeoff,
* better than 1/2, 1/4, or 1/10. * better than 1/2, 1/4, or 1/10.
*/ */
k = 0;
if (need < MDB_IDL_UM_MAX / 8) if (need < MDB_IDL_UM_MAX / 8)
need = MDB_IDL_UM_MAX / 8; need = MDB_IDL_UM_MAX / 8;
/* Save the page IDs of all the pages we're flushing */ /* Save the page IDs of all the pages we're flushing */
/* flush from the tail forward, this saves a lot of shifting later on. */ /* flush from the tail forward, this saves a lot of shifting later on. */
for (i=dl[0].mid; i>0; i--) { for (i=dl[0].mid; i && need; i--) {
dp = dl[i].mptr; dp = dl[i].mptr;
if (dp->mp_flags & P_KEEP) if (dp->mp_flags & P_KEEP)
continue; continue;
@ -1507,51 +1516,16 @@ mdb_page_spill(MDB_cursor *m0, MDB_val *key, MDB_val *data)
} }
if ((rc = mdb_midl_append(&txn->mt_spill_pgs, dl[i].mid))) if ((rc = mdb_midl_append(&txn->mt_spill_pgs, dl[i].mid)))
goto done; goto done;
k++; need--;
if (k > need)
break;
} }
mdb_midl_sort(txn->mt_spill_pgs); mdb_midl_sort(txn->mt_spill_pgs);
/* Since we're only doing the tail 1/8th of the dirty list, /* Flush the spilled part of dirty list */
* fake a dirty list to reflect this. if ((rc = mdb_page_flush(txn, i)) != MDB_SUCCESS)
*/
{
MDB_ID2 old;
if (i) {
k = dl[0].mid - i + 1;
old = dl[i-1];
dl[i-1].mid = k;
txn->mt_u.dirty_list = &dl[i-1];
}
rc = mdb_page_flush(txn);
if (i) {
/* reset back to the real list */
dl[0].mid -= k;
dl[0].mid += dl[i-1].mid;
dl[i-1] = old;
txn->mt_u.dirty_list = dl;
}
}
mdb_cursorpages_mark(m0, P_DIRTY|P_KEEP);
if (i) {
/* Reset any dirty root pages we kept that page_flush didn't see */
for (i=0; i<txn->mt_numdbs; i++) {
if (txn->mt_dbflags[i] & DB_DIRTY) {
pgno_t pgno = txn->mt_dbs[i].md_root;
if (pgno == P_INVALID)
continue;
if ((rc = mdb_page_get(txn, pgno, &dp, &level)) != MDB_SUCCESS)
goto done; goto done;
if (dp->mp_flags & P_KEEP)
dp->mp_flags ^= P_KEEP; /* Reset any dirty pages we kept that page_flush didn't see */
} rc = mdb_pages_xkeep(m0, P_DIRTY|P_KEEP, i);
}
}
done: done:
if (rc == 0) { if (rc == 0) {
@ -2626,10 +2600,13 @@ mdb_freelist_save(MDB_txn *txn)
return rc; return rc;
} }
/** Flush dirty pages to the map, after clearing their dirty flag. /** Flush (some) dirty pages to the map, after clearing their dirty flag.
* @param[in] txn the transaction that's being committed
* @param[in] keep number of initial pages in dirty_list to keep dirty.
* @return 0 on success, non-zero on failure.
*/ */
static int static int
mdb_page_flush(MDB_txn *txn) mdb_page_flush(MDB_txn *txn, int keep)
{ {
MDB_env *env = txn->mt_env; MDB_env *env = txn->mt_env;
MDB_ID2L dl = txn->mt_u.dirty_list; MDB_ID2L dl = txn->mt_u.dirty_list;
@ -2647,10 +2624,11 @@ mdb_page_flush(MDB_txn *txn)
int n = 0; int n = 0;
#endif #endif
j = 0; j = i = keep;
if (env->me_flags & MDB_WRITEMAP) { if (env->me_flags & MDB_WRITEMAP) {
/* Clear dirty flags */ /* Clear dirty flags */
for (i=1; i<=pagecount; i++) { while (++i <= pagecount) {
dp = dl[i].mptr; dp = dl[i].mptr;
/* Don't flush this page yet */ /* Don't flush this page yet */
if (dp->mp_flags & P_KEEP) { if (dp->mp_flags & P_KEEP) {
@ -2665,8 +2643,8 @@ mdb_page_flush(MDB_txn *txn)
} }
/* Write the pages */ /* Write the pages */
for (i = 1;; i++) { for (;;) {
if (i <= pagecount) { if (++i <= pagecount) {
dp = dl[i].mptr; dp = dl[i].mptr;
/* Don't flush this page yet */ /* Don't flush this page yet */
if (dp->mp_flags & P_KEEP) { if (dp->mp_flags & P_KEEP) {
@ -2745,8 +2723,7 @@ mdb_page_flush(MDB_txn *txn)
#endif /* _WIN32 */ #endif /* _WIN32 */
} }
j = 0; for (i = keep; ++i <= pagecount; ) {
for (i=1; i<=pagecount; i++) {
dp = dl[i].mptr; dp = dl[i].mptr;
/* This is a page we skipped above */ /* This is a page we skipped above */
if (!dl[i].mid) { if (!dl[i].mid) {
@ -2949,7 +2926,7 @@ mdb_txn_commit(MDB_txn *txn)
mdb_audit(txn); mdb_audit(txn);
#endif #endif
if ((rc = mdb_page_flush(txn)) || if ((rc = mdb_page_flush(txn, 0)) ||
(rc = mdb_env_sync(env, 0)) || (rc = mdb_env_sync(env, 0)) ||
(rc = mdb_env_write_meta(txn))) (rc = mdb_env_write_meta(txn)))
goto fail; goto fail;

Loading…
Cancel
Save