mdbx: Merge branch 'devel' into 'pt' branch.

This commit is contained in:
Leo Yuriev 2016-12-06 21:20:23 +03:00
commit 9775b409bc
2 changed files with 53 additions and 61 deletions

102
mdb.c
View File

@ -981,6 +981,21 @@ typedef struct MDB_xcursor {
unsigned char mx_dbflag; unsigned char mx_dbflag;
} MDB_xcursor; } MDB_xcursor;
/** Check if there is an inited xcursor, so #XCURSOR_REFRESH() is proper */
#define XCURSOR_INITED(mc) \
((mc)->mc_xcursor && ((mc)->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED))
/** Update sub-page pointer, if any, in \b mc->mc_xcursor. Needed
* when the node which contains the sub-page may have moved. Called
* with \b mp = mc->mc_pg[mc->mc_top], \b ki = mc->mc_ki[mc->mc_top].
*/
#define XCURSOR_REFRESH(mc, mp, ki) do { \
MDB_page *xr_pg = (mp); \
MDB_node *xr_node = NODEPTR(xr_pg, ki); \
if ((xr_node->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA) \
(mc)->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(xr_node); \
} while (0)
/** State of FreeDB old pages, stored in the MDB_env */ /** State of FreeDB old pages, stored in the MDB_env */
typedef struct MDB_pgstate { typedef struct MDB_pgstate {
pgno_t *mf_pghead; /**< Reclaimed freeDB pages, or NULL before use */ pgno_t *mf_pghead; /**< Reclaimed freeDB pages, or NULL before use */
@ -1453,7 +1468,7 @@ mdb_cursor_chk(MDB_cursor *mc)
} }
if (unlikely(mc->mc_ki[i] >= NUMKEYS(mc->mc_pg[i]))) if (unlikely(mc->mc_ki[i] >= NUMKEYS(mc->mc_pg[i])))
mdb_print("ack!\n"); mdb_print("ack!\n");
if (mc->mc_xcursor && (mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED)) { if (XCURSOR_INITED(mc)) {
node = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]); node = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
if (((node->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA) && if (((node->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA) &&
mc->mc_xcursor->mx_cursor.mc_pg[0] != NODEDATA(node)) { mc->mc_xcursor->mx_cursor.mc_pg[0] != NODEDATA(node)) {
@ -2552,14 +2567,8 @@ done:
if (m2 == mc) continue; if (m2 == mc) continue;
if (m2->mc_pg[mc->mc_top] == mp) { if (m2->mc_pg[mc->mc_top] == mp) {
m2->mc_pg[mc->mc_top] = np; m2->mc_pg[mc->mc_top] = np;
if ((mc->mc_db->md_flags & MDB_DUPSORT) && if (XCURSOR_INITED(m2) && IS_LEAF(np))
IS_LEAF(np) && XCURSOR_REFRESH(m2, np, m2->mc_ki[mc->mc_top]);
(m2->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED))
{
MDB_node *leaf = NODEPTR(np, m2->mc_ki[mc->mc_top]);
if ((leaf->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA)
m2->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(leaf);
}
} }
} }
} }
@ -6946,11 +6955,8 @@ new_sub:
if (m3->mc_ki[i] >= mc->mc_ki[i] && insert_key) { if (m3->mc_ki[i] >= mc->mc_ki[i] && insert_key) {
m3->mc_ki[i]++; m3->mc_ki[i]++;
} }
if (m3->mc_xcursor && (m3->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED)) { if (XCURSOR_INITED(m3))
MDB_node *n2 = NODEPTR(mp, m3->mc_ki[i]); XCURSOR_REFRESH(m3, mp, m3->mc_ki[i]);
if ((n2->mn_flags & (F_SUBDATA|F_DUPDATA)) == F_DUPDATA)
m3->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(n2);
}
} }
} }
} }
@ -7001,9 +7007,7 @@ put_sub:
if (m2->mc_ki[i] == mc->mc_ki[i]) { if (m2->mc_ki[i] == mc->mc_ki[i]) {
mdb_xcursor_init2(m2, mx, new_dupdata); mdb_xcursor_init2(m2, mx, new_dupdata);
} else if (!insert_key && m2->mc_ki[i] < nkeys) { } else if (!insert_key && m2->mc_ki[i] < nkeys) {
MDB_node *n2 = NODEPTR(mp, m2->mc_ki[i]); XCURSOR_REFRESH(m2, mp, m2->mc_ki[i]);
if ((n2->mn_flags & (F_SUBDATA|F_DUPDATA)) == F_DUPDATA)
m2->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(n2);
} }
} }
} }
@ -7114,13 +7118,12 @@ mdb_cursor_del(MDB_cursor *mc, unsigned flags)
if (m2 == mc || m2->mc_snum < mc->mc_snum) continue; if (m2 == mc || m2->mc_snum < mc->mc_snum) continue;
if (!(m2->mc_flags & C_INITIALIZED)) continue; if (!(m2->mc_flags & C_INITIALIZED)) continue;
if (m2->mc_pg[mc->mc_top] == mp) { if (m2->mc_pg[mc->mc_top] == mp) {
if (m2->mc_ki[mc->mc_top] == mc->mc_ki[mc->mc_top]) { MDB_node *n2 = leaf;
m2->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(leaf); if (m2->mc_ki[mc->mc_top] != mc->mc_ki[mc->mc_top]) {
} else { n2 = NODEPTR(mp, m2->mc_ki[mc->mc_top]);
MDB_node *n2 = NODEPTR(mp, m2->mc_ki[mc->mc_top]); if (n2->mn_flags & F_SUBDATA) continue;
if (!(n2->mn_flags & F_SUBDATA))
m2->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(n2);
} }
m2->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(n2);
} }
} }
} }
@ -7565,7 +7568,6 @@ mdb_xcursor_init1(MDB_cursor *mc, MDB_node *node)
if (mx->mx_dbx.md_cmp == mdb_cmp_int && mx->mx_db.md_pad == sizeof(size_t)) if (mx->mx_dbx.md_cmp == mdb_cmp_int && mx->mx_db.md_pad == sizeof(size_t))
mx->mx_dbx.md_cmp = mdb_cmp_clong; mx->mx_dbx.md_cmp = mdb_cmp_clong;
#endif */ #endif */
mc->mc_signature = MDBX_MC_SIGNATURE;
} }
@ -7604,6 +7606,7 @@ mdb_xcursor_init2(MDB_cursor *mc, MDB_xcursor *src_mx, int new_dupdata)
static void static void
mdb_cursor_init(MDB_cursor *mc, MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx) mdb_cursor_init(MDB_cursor *mc, MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx)
{ {
mc->mc_signature = MDBX_MC_SIGNATURE;
mc->mc_next = NULL; mc->mc_next = NULL;
mc->mc_backup = NULL; mc->mc_backup = NULL;
mc->mc_dbi = dbi; mc->mc_dbi = dbi;
@ -7627,7 +7630,6 @@ mdb_cursor_init(MDB_cursor *mc, MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx)
if (*mc->mc_dbflag & DB_STALE) { if (*mc->mc_dbflag & DB_STALE) {
mdb_page_search(mc, NULL, MDB_PS_ROOTONLY); mdb_page_search(mc, NULL, MDB_PS_ROOTONLY);
} }
mc->mc_signature = MDBX_MC_SIGNATURE;
} }
int int
@ -7982,12 +7984,8 @@ mdb_node_move(MDB_cursor *csrc, MDB_cursor *cdst, int fromleft)
m3->mc_ki[csrc->mc_top] = cdst->mc_ki[cdst->mc_top]; m3->mc_ki[csrc->mc_top] = cdst->mc_ki[cdst->mc_top];
m3->mc_ki[csrc->mc_top-1]++; m3->mc_ki[csrc->mc_top-1]++;
} }
if (m3->mc_xcursor && (m3->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) && if (XCURSOR_INITED(m3) && IS_LEAF(mps))
IS_LEAF(mps)) { XCURSOR_REFRESH(m3, m3->mc_pg[csrc->mc_top], m3->mc_ki[csrc->mc_top]);
MDB_node *node = NODEPTR(m3->mc_pg[csrc->mc_top], m3->mc_ki[csrc->mc_top]);
if ((node->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA)
m3->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(node);
}
} }
} else } else
/* Adding on the right, bump others down */ /* Adding on the right, bump others down */
@ -8008,12 +8006,8 @@ mdb_node_move(MDB_cursor *csrc, MDB_cursor *cdst, int fromleft)
} else { } else {
m3->mc_ki[csrc->mc_top]--; m3->mc_ki[csrc->mc_top]--;
} }
if (m3->mc_xcursor && (m3->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) && if (XCURSOR_INITED(m3) && IS_LEAF(mps))
IS_LEAF(mps)) { XCURSOR_REFRESH(m3, m3->mc_pg[csrc->mc_top], m3->mc_ki[csrc->mc_top]);
MDB_node *node = NODEPTR(m3->mc_pg[csrc->mc_top], m3->mc_ki[csrc->mc_top]);
if ((node->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA)
m3->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(node);
}
} }
} }
} }
@ -8212,12 +8206,8 @@ mdb_page_merge(MDB_cursor *csrc, MDB_cursor *cdst)
m3->mc_ki[top-1] > csrc->mc_ki[top-1]) { m3->mc_ki[top-1] > csrc->mc_ki[top-1]) {
m3->mc_ki[top-1]--; m3->mc_ki[top-1]--;
} }
if (m3->mc_xcursor && (m3->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) && if (XCURSOR_INITED(m3) && IS_LEAF(psrc))
IS_LEAF(psrc)) { XCURSOR_REFRESH(m3, m3->mc_pg[top], m3->mc_ki[top]);
MDB_node *node = NODEPTR(m3->mc_pg[top], m3->mc_ki[top]);
if ((node->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA)
m3->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(node);
}
} }
} }
{ {
@ -8471,14 +8461,16 @@ mdb_cursor_del0(MDB_cursor *mc)
if (m3->mc_pg[mc->mc_top] == mp) { if (m3->mc_pg[mc->mc_top] == mp) {
if (m3->mc_ki[mc->mc_top] == ki) { if (m3->mc_ki[mc->mc_top] == ki) {
m3->mc_flags |= C_DEL; m3->mc_flags |= C_DEL;
if (mc->mc_db->md_flags & MDB_DUPSORT) {
/* Sub-cursor referred into dataset which is gone */
m3->mc_xcursor->mx_cursor.mc_flags &= ~(C_INITIALIZED|C_EOF);
}
continue;
} else if (m3->mc_ki[mc->mc_top] > ki) { } else if (m3->mc_ki[mc->mc_top] > ki) {
m3->mc_ki[mc->mc_top]--; m3->mc_ki[mc->mc_top]--;
} }
if (m3->mc_xcursor && (m3->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED)) { if (XCURSOR_INITED(m3))
MDB_node *node = NODEPTR(m3->mc_pg[mc->mc_top], m3->mc_ki[mc->mc_top]); XCURSOR_REFRESH(m3, m3->mc_pg[mc->mc_top], m3->mc_ki[mc->mc_top]);
if ((node->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA)
m3->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(node);
}
} }
} }
} }
@ -8516,9 +8508,13 @@ mdb_cursor_del0(MDB_cursor *mc)
if (mc->mc_db->md_flags & MDB_DUPSORT) { if (mc->mc_db->md_flags & MDB_DUPSORT) {
MDB_node *node = NODEPTR(m3->mc_pg[m3->mc_top], m3->mc_ki[m3->mc_top]); MDB_node *node = NODEPTR(m3->mc_pg[m3->mc_top], m3->mc_ki[m3->mc_top]);
/* If this node is a fake page, it needs to be reinited /* If this node is a fake page, it needs to be reinited
* because its data has moved. * because its data has moved. But just reset mc_pg[0]
* if the xcursor is already live.
*/ */
if ((node->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA) { if ((node->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA) {
if (m3->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED)
m3->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(node);
else
mdb_xcursor_init1(m3, node); mdb_xcursor_init1(m3, node);
} }
} }
@ -9008,12 +9004,8 @@ mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno
m3->mc_ki[ptop] >= mc->mc_ki[ptop]) { m3->mc_ki[ptop] >= mc->mc_ki[ptop]) {
m3->mc_ki[ptop]++; m3->mc_ki[ptop]++;
} }
if (m3->mc_xcursor && (m3->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) && if (XCURSOR_INITED(m3) && IS_LEAF(mp))
IS_LEAF(mp)) { XCURSOR_REFRESH(m3, m3->mc_pg[mc->mc_top], m3->mc_ki[mc->mc_top]);
MDB_node *node = NODEPTR(m3->mc_pg[mc->mc_top], m3->mc_ki[mc->mc_top]);
if ((node->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA)
m3->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(node);
}
} }
} }
mdb_debug("mp left: %d, rp left: %d", SIZELEFT(mp), SIZELEFT(rp)); mdb_debug("mp left: %d, rp left: %d", SIZELEFT(mp), SIZELEFT(rp));

View File

@ -52,7 +52,7 @@ flagbit dbflags[] = {
static volatile sig_atomic_t gotsignal; static volatile sig_atomic_t gotsignal;
static void signal_hanlder( int sig ) { static void signal_handler( int sig ) {
(void) sig; (void) sig;
gotsignal = 1; gotsignal = 1;
} }
@ -660,13 +660,13 @@ int main(int argc, char *argv[])
usage(prog); usage(prog);
#ifdef SIGPIPE #ifdef SIGPIPE
signal(SIGPIPE, signal_hanlder); signal(SIGPIPE, signal_handler);
#endif #endif
#ifdef SIGHUP #ifdef SIGHUP
signal(SIGHUP, signal_hanlder); signal(SIGHUP, signal_handler);
#endif #endif
signal(SIGINT, signal_hanlder); signal(SIGINT, signal_handler);
signal(SIGTERM, signal_hanlder); signal(SIGTERM, signal_handler);
envname = argv[optind]; envname = argv[optind];
print("Running mdbx_chk for '%s' in %s mode...\n", print("Running mdbx_chk for '%s' in %s mode...\n",