mdbx: backport - fix tracking around mdbx_cursor_del().

This commit is contained in:
Leo Yuriev 2018-09-11 16:55:22 +03:00 committed by Leonid Yuriev
parent f57ffc987c
commit e32ca55258

View File

@ -9347,6 +9347,7 @@ static int mdbx_cursor_del0(MDBX_cursor *mc) {
MDBX_cursor *m2, *m3; MDBX_cursor *m2, *m3;
MDBX_dbi dbi = mc->mc_dbi; MDBX_dbi dbi = mc->mc_dbi;
mdbx_cassert(mc, IS_LEAF(mc->mc_pg[mc->mc_top]));
ki = mc->mc_ki[mc->mc_top]; ki = mc->mc_ki[mc->mc_top];
mp = mc->mc_pg[mc->mc_top]; mp = mc->mc_pg[mc->mc_top];
mdbx_node_del(mc, mc->mc_db->md_xsize); mdbx_node_del(mc, mc->mc_db->md_xsize);
@ -9355,9 +9356,9 @@ static int mdbx_cursor_del0(MDBX_cursor *mc) {
/* Adjust other cursors pointing to mp */ /* Adjust other cursors pointing to mp */
for (m2 = mc->mc_txn->mt_cursors[dbi]; m2; m2 = m2->mc_next) { for (m2 = mc->mc_txn->mt_cursors[dbi]; m2; m2 = m2->mc_next) {
m3 = (mc->mc_flags & C_SUB) ? &m2->mc_xcursor->mx_cursor : m2; m3 = (mc->mc_flags & C_SUB) ? &m2->mc_xcursor->mx_cursor : m2;
if (!(m2->mc_flags & m3->mc_flags & C_INITIALIZED)) if (m3 == mc || !(m2->mc_flags & m3->mc_flags & C_INITIALIZED))
continue; continue;
if (m3 == mc || m3->mc_snum < mc->mc_snum) if (m3->mc_snum < mc->mc_snum)
continue; continue;
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) {
@ -9388,24 +9389,25 @@ static int mdbx_cursor_del0(MDBX_cursor *mc) {
return rc; return rc;
} }
ki = mc->mc_ki[mc->mc_top];
mp = mc->mc_pg[mc->mc_top]; mp = mc->mc_pg[mc->mc_top];
mdbx_cassert(mc, IS_LEAF(mc->mc_pg[mc->mc_top]));
nkeys = NUMKEYS(mp); nkeys = NUMKEYS(mp);
mdbx_cassert(mc, (mc->mc_db->md_entries > 0 && nkeys > 0) || mdbx_cassert(mc, (mc->mc_db->md_entries > 0 && nkeys > 0) ||
((mc->mc_flags & C_SUB) && ((mc->mc_flags & C_SUB) &&
mc->mc_db->md_entries == 0 && nkeys == 0)); mc->mc_db->md_entries == 0 && nkeys == 0));
/* Adjust other cursors pointing to mp */ /* Adjust THIS and other cursors pointing to mp */
for (m2 = mc->mc_txn->mt_cursors[dbi]; !rc && m2; m2 = m2->mc_next) { for (m2 = mc->mc_txn->mt_cursors[dbi]; m2; m2 = m2->mc_next) {
m3 = (mc->mc_flags & C_SUB) ? &m2->mc_xcursor->mx_cursor : m2; m3 = (mc->mc_flags & C_SUB) ? &m2->mc_xcursor->mx_cursor : m2;
if (!(m2->mc_flags & m3->mc_flags & C_INITIALIZED)) if (m3 == mc || !(m2->mc_flags & m3->mc_flags & C_INITIALIZED))
continue; continue;
if (m3->mc_snum < mc->mc_snum) if (m3->mc_snum < mc->mc_snum)
continue; continue;
if (m3->mc_pg[mc->mc_top] == mp) { if (m3->mc_pg[mc->mc_top] == mp) {
/* if m3 points past last node in page, find next sibling */ /* if m3 points past last node in page, find next sibling */
if (m3->mc_ki[mc->mc_top] >= mc->mc_ki[mc->mc_top]) {
if (m3->mc_ki[mc->mc_top] >= nkeys) { if (m3->mc_ki[mc->mc_top] >= nkeys) {
rc = mdbx_cursor_sibling(m3, 1); rc = mdbx_cursor_sibling(m3, true);
if (rc == MDBX_NOTFOUND) { if (rc == MDBX_NOTFOUND) {
m3->mc_flags |= C_EOF; m3->mc_flags |= C_EOF;
rc = MDBX_SUCCESS; rc = MDBX_SUCCESS;
@ -9413,7 +9415,9 @@ static int mdbx_cursor_del0(MDBX_cursor *mc) {
} else if (unlikely(rc != MDBX_SUCCESS)) } else if (unlikely(rc != MDBX_SUCCESS))
break; break;
} }
if (mc->mc_db->md_flags & MDBX_DUPSORT) { if (m3->mc_ki[mc->mc_top] >= ki || m3->mc_pg[mc->mc_top] != mp) {
if ((mc->mc_db->md_flags & MDBX_DUPSORT) != 0 &&
(m3->mc_flags & C_EOF) == 0) {
MDBX_node *node = MDBX_node *node =
NODEPTR(m3->mc_pg[m3->mc_top], m3->mc_ki[m3->mc_top]); NODEPTR(m3->mc_pg[m3->mc_top], m3->mc_ki[m3->mc_top]);
/* If this node has dupdata, it may need to be reinited /* If this node has dupdata, it may need to be reinited
@ -9426,7 +9430,8 @@ static int mdbx_cursor_del0(MDBX_cursor *mc) {
m3->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(node); m3->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(node);
} else { } else {
rc = mdbx_xcursor_init1(m3, node); rc = mdbx_xcursor_init1(m3, node);
if (likely(rc == MDBX_SUCCESS)) if (unlikely(rc != MDBX_SUCCESS))
break;
m3->mc_xcursor->mx_cursor.mc_flags |= C_DEL; m3->mc_xcursor->mx_cursor.mc_flags |= C_DEL;
} }
} }
@ -9434,6 +9439,32 @@ static int mdbx_cursor_del0(MDBX_cursor *mc) {
} }
} }
} }
if (mc->mc_ki[mc->mc_top] >= nkeys) {
rc = mdbx_cursor_sibling(mc, true);
if (rc == MDBX_NOTFOUND) {
mc->mc_flags |= C_EOF;
rc = MDBX_SUCCESS;
}
}
if ((mc->mc_db->md_flags & MDBX_DUPSORT) != 0 &&
(mc->mc_flags & C_EOF) == 0) {
MDBX_node *node = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
/* If this node has dupdata, it may need to be reinited
* because its data has moved.
* If the xcursor was not initd it must be reinited.
* Else if node points to a subDB, nothing is needed. */
if (node->mn_flags & F_DUPDATA) {
if (mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) {
if (!(node->mn_flags & F_SUBDATA))
mc->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(node);
} else {
rc = mdbx_xcursor_init1(mc, node);
if (likely(rc != MDBX_SUCCESS))
mc->mc_xcursor->mx_cursor.mc_flags |= C_DEL;
}
}
}
mc->mc_flags |= C_DEL; mc->mc_flags |= C_DEL;
} }