mirror of
https://github.com/isar/libmdbx.git
synced 2024-10-30 11:29:19 +08:00
mdbx: minor refine node_add()
.
This commit is contained in:
parent
1215bda188
commit
c05a3b7bb9
63
src/core.c
63
src/core.c
@ -3273,17 +3273,13 @@ struct node_result {
|
|||||||
static struct node_result mdbx_node_search(MDBX_cursor *mc,
|
static struct node_result mdbx_node_search(MDBX_cursor *mc,
|
||||||
const MDBX_val *key);
|
const MDBX_val *key);
|
||||||
|
|
||||||
static int __must_check_result mdbx_node_add_branch(MDBX_cursor *mc,
|
static int __must_check_result node_add_branch(MDBX_cursor *mc, unsigned indx,
|
||||||
unsigned indx,
|
|
||||||
const MDBX_val *key,
|
const MDBX_val *key,
|
||||||
pgno_t pgno);
|
pgno_t pgno);
|
||||||
static int __must_check_result mdbx_node_add_leaf(MDBX_cursor *mc,
|
static int __must_check_result node_add_leaf(MDBX_cursor *mc, unsigned indx,
|
||||||
unsigned indx,
|
|
||||||
const MDBX_val *key,
|
const MDBX_val *key,
|
||||||
MDBX_val *data,
|
MDBX_val *data, unsigned flags);
|
||||||
unsigned flags);
|
static int __must_check_result node_add_leaf2(MDBX_cursor *mc, unsigned indx,
|
||||||
static int __must_check_result mdbx_node_add_leaf2(MDBX_cursor *mc,
|
|
||||||
unsigned indx,
|
|
||||||
const MDBX_val *key);
|
const MDBX_val *key);
|
||||||
|
|
||||||
static void node_del(MDBX_cursor *mc, size_t ksize);
|
static void node_del(MDBX_cursor *mc, size_t ksize);
|
||||||
@ -15848,9 +15844,9 @@ new_sub:;
|
|||||||
if (IS_LEAF2(mc->mc_pg[mc->mc_top])) {
|
if (IS_LEAF2(mc->mc_pg[mc->mc_top])) {
|
||||||
mdbx_cassert(mc, !(naf & (F_BIGDATA | F_SUBDATA | F_DUPDATA)) &&
|
mdbx_cassert(mc, !(naf & (F_BIGDATA | F_SUBDATA | F_DUPDATA)) &&
|
||||||
rdata->iov_len == 0);
|
rdata->iov_len == 0);
|
||||||
rc = mdbx_node_add_leaf2(mc, mc->mc_ki[mc->mc_top], key);
|
rc = node_add_leaf2(mc, mc->mc_ki[mc->mc_top], key);
|
||||||
} else
|
} else
|
||||||
rc = mdbx_node_add_leaf(mc, mc->mc_ki[mc->mc_top], key, rdata, naf);
|
rc = node_add_leaf(mc, mc->mc_ki[mc->mc_top], key, rdata, naf);
|
||||||
if (likely(rc == 0)) {
|
if (likely(rc == 0)) {
|
||||||
/* Adjust other cursors pointing to mp */
|
/* Adjust other cursors pointing to mp */
|
||||||
const MDBX_dbi dbi = mc->mc_dbi;
|
const MDBX_dbi dbi = mc->mc_dbi;
|
||||||
@ -16155,7 +16151,7 @@ static pgr_t page_new_large(MDBX_cursor *mc, const unsigned npages) {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int __must_check_result mdbx_node_add_leaf2(MDBX_cursor *mc,
|
__hot static int __must_check_result node_add_leaf2(MDBX_cursor *mc,
|
||||||
unsigned indx,
|
unsigned indx,
|
||||||
const MDBX_val *key) {
|
const MDBX_val *key) {
|
||||||
MDBX_page *mp = mc->mc_pg[mc->mc_top];
|
MDBX_page *mp = mc->mc_pg[mc->mc_top];
|
||||||
@ -16192,8 +16188,7 @@ static int __must_check_result mdbx_node_add_leaf2(MDBX_cursor *mc,
|
|||||||
return MDBX_SUCCESS;
|
return MDBX_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int __must_check_result mdbx_node_add_branch(MDBX_cursor *mc,
|
static int __must_check_result node_add_branch(MDBX_cursor *mc, unsigned indx,
|
||||||
unsigned indx,
|
|
||||||
const MDBX_val *key,
|
const MDBX_val *key,
|
||||||
pgno_t pgno) {
|
pgno_t pgno) {
|
||||||
MDBX_page *mp = mc->mc_pg[mc->mc_top];
|
MDBX_page *mp = mc->mc_pg[mc->mc_top];
|
||||||
@ -16236,7 +16231,7 @@ static int __must_check_result mdbx_node_add_branch(MDBX_cursor *mc,
|
|||||||
return MDBX_SUCCESS;
|
return MDBX_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int __must_check_result mdbx_node_add_leaf(MDBX_cursor *mc,
|
__hot static int __must_check_result node_add_leaf(MDBX_cursor *mc,
|
||||||
unsigned indx,
|
unsigned indx,
|
||||||
const MDBX_val *key,
|
const MDBX_val *key,
|
||||||
MDBX_val *data,
|
MDBX_val *data,
|
||||||
@ -16313,22 +16308,19 @@ static int __must_check_result mdbx_node_add_leaf(MDBX_cursor *mc,
|
|||||||
|
|
||||||
void *nodedata = node_data(node);
|
void *nodedata = node_data(node);
|
||||||
if (likely(largepage == NULL)) {
|
if (likely(largepage == NULL)) {
|
||||||
if (unlikely(flags & F_BIGDATA))
|
if (unlikely(flags & F_BIGDATA)) {
|
||||||
memcpy(nodedata, data->iov_base, sizeof(pgno_t));
|
memcpy(nodedata, data->iov_base, sizeof(pgno_t));
|
||||||
else if (unlikely(flags & MDBX_RESERVE))
|
return MDBX_SUCCESS;
|
||||||
data->iov_base = nodedata;
|
}
|
||||||
else if (likely(nodedata != data->iov_base &&
|
|
||||||
data->iov_len /* to avoid UBSAN traps*/ != 0))
|
|
||||||
memcpy(nodedata, data->iov_base, data->iov_len);
|
|
||||||
} else {
|
} else {
|
||||||
poke_pgno(nodedata, largepage->mp_pgno);
|
poke_pgno(nodedata, largepage->mp_pgno);
|
||||||
nodedata = page_data(largepage);
|
nodedata = page_data(largepage);
|
||||||
|
}
|
||||||
if (unlikely(flags & MDBX_RESERVE))
|
if (unlikely(flags & MDBX_RESERVE))
|
||||||
data->iov_base = nodedata;
|
data->iov_base = nodedata;
|
||||||
else if (likely(nodedata != data->iov_base &&
|
else if (likely(nodedata != data->iov_base &&
|
||||||
data->iov_len /* to avoid UBSAN traps*/ != 0))
|
data->iov_len /* to avoid UBSAN traps*/ != 0))
|
||||||
memcpy(nodedata, data->iov_base, data->iov_len);
|
memcpy(nodedata, data->iov_base, data->iov_len);
|
||||||
}
|
|
||||||
return MDBX_SUCCESS;
|
return MDBX_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -17112,8 +17104,7 @@ static int mdbx_node_move(MDBX_cursor *csrc, MDBX_cursor *cdst, bool fromleft) {
|
|||||||
"branch", csrc->mc_ki[csrc->mc_top], DKEY_DEBUG(&key4move),
|
"branch", csrc->mc_ki[csrc->mc_top], DKEY_DEBUG(&key4move),
|
||||||
psrc->mp_pgno, cdst->mc_ki[cdst->mc_top], pdst->mp_pgno);
|
psrc->mp_pgno, cdst->mc_ki[cdst->mc_top], pdst->mp_pgno);
|
||||||
/* Add the node to the destination page. */
|
/* Add the node to the destination page. */
|
||||||
rc =
|
rc = node_add_branch(cdst, cdst->mc_ki[cdst->mc_top], &key4move, srcpg);
|
||||||
mdbx_node_add_branch(cdst, cdst->mc_ki[cdst->mc_top], &key4move, srcpg);
|
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case P_LEAF: {
|
case P_LEAF: {
|
||||||
@ -17133,7 +17124,7 @@ static int mdbx_node_move(MDBX_cursor *csrc, MDBX_cursor *cdst, bool fromleft) {
|
|||||||
"leaf", csrc->mc_ki[csrc->mc_top], DKEY_DEBUG(&key4move),
|
"leaf", csrc->mc_ki[csrc->mc_top], DKEY_DEBUG(&key4move),
|
||||||
psrc->mp_pgno, cdst->mc_ki[cdst->mc_top], pdst->mp_pgno);
|
psrc->mp_pgno, cdst->mc_ki[cdst->mc_top], pdst->mp_pgno);
|
||||||
/* Add the node to the destination page. */
|
/* Add the node to the destination page. */
|
||||||
rc = mdbx_node_add_leaf(cdst, cdst->mc_ki[cdst->mc_top], &key4move, &data,
|
rc = node_add_leaf(cdst, cdst->mc_ki[cdst->mc_top], &key4move, &data,
|
||||||
node_flags(srcnode));
|
node_flags(srcnode));
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
@ -17151,7 +17142,7 @@ static int mdbx_node_move(MDBX_cursor *csrc, MDBX_cursor *cdst, bool fromleft) {
|
|||||||
"leaf2", csrc->mc_ki[csrc->mc_top], DKEY_DEBUG(&key4move),
|
"leaf2", csrc->mc_ki[csrc->mc_top], DKEY_DEBUG(&key4move),
|
||||||
psrc->mp_pgno, cdst->mc_ki[cdst->mc_top], pdst->mp_pgno);
|
psrc->mp_pgno, cdst->mc_ki[cdst->mc_top], pdst->mp_pgno);
|
||||||
/* Add the node to the destination page. */
|
/* Add the node to the destination page. */
|
||||||
rc = mdbx_node_add_leaf2(cdst, cdst->mc_ki[cdst->mc_top], &key4move);
|
rc = node_add_leaf2(cdst, cdst->mc_ki[cdst->mc_top], &key4move);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -17339,7 +17330,7 @@ static int mdbx_page_merge(MDBX_cursor *csrc, MDBX_cursor *cdst) {
|
|||||||
key.iov_base = page_data(psrc);
|
key.iov_base = page_data(psrc);
|
||||||
unsigned i = 0;
|
unsigned i = 0;
|
||||||
do {
|
do {
|
||||||
rc = mdbx_node_add_leaf2(cdst, j++, &key);
|
rc = node_add_leaf2(cdst, j++, &key);
|
||||||
if (unlikely(rc != MDBX_SUCCESS))
|
if (unlikely(rc != MDBX_SUCCESS))
|
||||||
return rc;
|
return rc;
|
||||||
key.iov_base = (char *)key.iov_base + key.iov_len;
|
key.iov_base = (char *)key.iov_base + key.iov_len;
|
||||||
@ -17387,10 +17378,10 @@ static int mdbx_page_merge(MDBX_cursor *csrc, MDBX_cursor *cdst) {
|
|||||||
MDBX_val data;
|
MDBX_val data;
|
||||||
data.iov_len = node_ds(srcnode);
|
data.iov_len = node_ds(srcnode);
|
||||||
data.iov_base = node_data(srcnode);
|
data.iov_base = node_data(srcnode);
|
||||||
rc = mdbx_node_add_leaf(cdst, j++, &key, &data, node_flags(srcnode));
|
rc = node_add_leaf(cdst, j++, &key, &data, node_flags(srcnode));
|
||||||
} else {
|
} else {
|
||||||
mdbx_cassert(csrc, node_flags(srcnode) == 0);
|
mdbx_cassert(csrc, node_flags(srcnode) == 0);
|
||||||
rc = mdbx_node_add_branch(cdst, j++, &key, node_pgno(srcnode));
|
rc = node_add_branch(cdst, j++, &key, node_pgno(srcnode));
|
||||||
}
|
}
|
||||||
if (unlikely(rc != MDBX_SUCCESS))
|
if (unlikely(rc != MDBX_SUCCESS))
|
||||||
return rc;
|
return rc;
|
||||||
@ -18541,7 +18532,7 @@ static int page_split(MDBX_cursor *mc, const MDBX_val *const newkey,
|
|||||||
foliage = mc->mc_db->md_depth++;
|
foliage = mc->mc_db->md_depth++;
|
||||||
|
|
||||||
/* Add left (implicit) pointer. */
|
/* Add left (implicit) pointer. */
|
||||||
rc = mdbx_node_add_branch(mc, 0, NULL, mp->mp_pgno);
|
rc = node_add_branch(mc, 0, NULL, mp->mp_pgno);
|
||||||
if (unlikely(rc != MDBX_SUCCESS)) {
|
if (unlikely(rc != MDBX_SUCCESS)) {
|
||||||
/* undo the pre-push */
|
/* undo the pre-push */
|
||||||
mc->mc_pg[0] = mc->mc_pg[1];
|
mc->mc_pg[0] = mc->mc_pg[1];
|
||||||
@ -18836,8 +18827,8 @@ static int page_split(MDBX_cursor *mc, const MDBX_val *const newkey,
|
|||||||
ptop_page->mp_pgno, mc->mc_ki[ptop], sister->mp_pgno,
|
ptop_page->mp_pgno, mc->mc_ki[ptop], sister->mp_pgno,
|
||||||
DKEY(mc->mc_ki[ptop] ? newkey : NULL));
|
DKEY(mc->mc_ki[ptop] ? newkey : NULL));
|
||||||
mc->mc_top--;
|
mc->mc_top--;
|
||||||
rc = mdbx_node_add_branch(mc, mc->mc_ki[ptop],
|
rc = node_add_branch(mc, mc->mc_ki[ptop], mc->mc_ki[ptop] ? newkey : NULL,
|
||||||
mc->mc_ki[ptop] ? newkey : NULL, sister->mp_pgno);
|
sister->mp_pgno);
|
||||||
mdbx_cassert(mc, mp == mc->mc_pg[ptop + 1] &&
|
mdbx_cassert(mc, mp == mc->mc_pg[ptop + 1] &&
|
||||||
newindx == mc->mc_ki[ptop + 1] && ptop == mc->mc_top);
|
newindx == mc->mc_ki[ptop + 1] && ptop == mc->mc_top);
|
||||||
|
|
||||||
@ -18865,7 +18856,7 @@ static int page_split(MDBX_cursor *mc, const MDBX_val *const newkey,
|
|||||||
mn.mc_top--;
|
mn.mc_top--;
|
||||||
mdbx_trace("add-to-parent the right-entry[%u] for new sibling-page",
|
mdbx_trace("add-to-parent the right-entry[%u] for new sibling-page",
|
||||||
mn.mc_ki[ptop]);
|
mn.mc_ki[ptop]);
|
||||||
rc = mdbx_node_add_branch(&mn, mn.mc_ki[ptop], &sepkey, sister->mp_pgno);
|
rc = node_add_branch(&mn, mn.mc_ki[ptop], &sepkey, sister->mp_pgno);
|
||||||
mn.mc_top++;
|
mn.mc_top++;
|
||||||
if (unlikely(rc != MDBX_SUCCESS))
|
if (unlikely(rc != MDBX_SUCCESS))
|
||||||
goto done;
|
goto done;
|
||||||
@ -18877,12 +18868,12 @@ static int page_split(MDBX_cursor *mc, const MDBX_val *const newkey,
|
|||||||
switch (PAGETYPE_WHOLE(sister)) {
|
switch (PAGETYPE_WHOLE(sister)) {
|
||||||
case P_LEAF: {
|
case P_LEAF: {
|
||||||
mdbx_cassert(mc, newpgno == 0 || newpgno == P_INVALID);
|
mdbx_cassert(mc, newpgno == 0 || newpgno == P_INVALID);
|
||||||
rc = mdbx_node_add_leaf(mc, 0, newkey, newdata, naf);
|
rc = node_add_leaf(mc, 0, newkey, newdata, naf);
|
||||||
} break;
|
} break;
|
||||||
case P_LEAF | P_LEAF2: {
|
case P_LEAF | P_LEAF2: {
|
||||||
mdbx_cassert(mc, (naf & (F_BIGDATA | F_SUBDATA | F_DUPDATA)) == 0);
|
mdbx_cassert(mc, (naf & (F_BIGDATA | F_SUBDATA | F_DUPDATA)) == 0);
|
||||||
mdbx_cassert(mc, newpgno == 0 || newpgno == P_INVALID);
|
mdbx_cassert(mc, newpgno == 0 || newpgno == P_INVALID);
|
||||||
rc = mdbx_node_add_leaf2(mc, 0, newkey);
|
rc = node_add_leaf2(mc, 0, newkey);
|
||||||
} break;
|
} break;
|
||||||
default:
|
default:
|
||||||
rc = bad_page(sister, "wrong page-type %u\n", PAGETYPE_WHOLE(sister));
|
rc = bad_page(sister, "wrong page-type %u\n", PAGETYPE_WHOLE(sister));
|
||||||
@ -18949,12 +18940,12 @@ static int page_split(MDBX_cursor *mc, const MDBX_val *const newkey,
|
|||||||
case P_BRANCH: {
|
case P_BRANCH: {
|
||||||
mdbx_cassert(mc, 0 == (uint16_t)flags);
|
mdbx_cassert(mc, 0 == (uint16_t)flags);
|
||||||
/* First branch index doesn't need key data. */
|
/* First branch index doesn't need key data. */
|
||||||
rc = mdbx_node_add_branch(mc, n, n ? &rkey : NULL, pgno);
|
rc = node_add_branch(mc, n, n ? &rkey : NULL, pgno);
|
||||||
} break;
|
} break;
|
||||||
case P_LEAF: {
|
case P_LEAF: {
|
||||||
mdbx_cassert(mc, pgno == 0);
|
mdbx_cassert(mc, pgno == 0);
|
||||||
mdbx_cassert(mc, rdata != NULL);
|
mdbx_cassert(mc, rdata != NULL);
|
||||||
rc = mdbx_node_add_leaf(mc, n, &rkey, rdata, flags);
|
rc = node_add_leaf(mc, n, &rkey, rdata, flags);
|
||||||
} break;
|
} break;
|
||||||
/* case P_LEAF | P_LEAF2: {
|
/* case P_LEAF | P_LEAF2: {
|
||||||
mdbx_cassert(mc, (nflags & (F_BIGDATA | F_SUBDATA | F_DUPDATA)) == 0);
|
mdbx_cassert(mc, (nflags & (F_BIGDATA | F_SUBDATA | F_DUPDATA)) == 0);
|
||||||
|
Loading…
Reference in New Issue
Block a user