mdbx: refine mdbx_cursor_get().

This commit is contained in:
Leo Yuriev 2017-02-17 19:58:39 +03:00
parent abc7dca247
commit 44b378b8ea

142
mdbx.c
View File

@ -6513,86 +6513,72 @@ int mdbx_cursor_get(MDB_cursor *mc, MDB_val *key, MDB_val *data,
return MDB_BAD_TXN;
switch (op) {
case MDB_GET_CURRENT:
if (unlikely(!(mc->mc_flags & C_INITIALIZED))) {
rc = EINVAL;
case MDB_GET_CURRENT: {
if (unlikely(!(mc->mc_flags & C_INITIALIZED)))
return EINVAL;
MDB_page *mp = mc->mc_pg[mc->mc_top];
int nkeys = NUMKEYS(mp);
if (!nkeys || mc->mc_ki[mc->mc_top] >= nkeys) {
mc->mc_ki[mc->mc_top] = nkeys;
return MDB_NOTFOUND;
}
rc = MDB_SUCCESS;
if (IS_LEAF2(mp)) {
key->mv_size = mc->mc_db->md_xsize;
key->mv_data = LEAF2KEY(mp, mc->mc_ki[mc->mc_top], key->mv_size);
} else {
MDB_page *mp = mc->mc_pg[mc->mc_top];
int nkeys = NUMKEYS(mp);
if (!nkeys || mc->mc_ki[mc->mc_top] >= nkeys) {
mc->mc_ki[mc->mc_top] = nkeys;
rc = MDB_NOTFOUND;
break;
}
rc = MDB_SUCCESS;
if (IS_LEAF2(mp)) {
key->mv_size = mc->mc_db->md_xsize;
key->mv_data = LEAF2KEY(mp, mc->mc_ki[mc->mc_top], key->mv_size);
} else {
MDB_node *leaf = NODEPTR(mp, mc->mc_ki[mc->mc_top]);
MDB_GET_KEY(leaf, key);
if (data) {
if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
if (unlikely(
!(mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED))) {
mdbx_xcursor_init1(mc, leaf);
rc = mdbx_cursor_first(&mc->mc_xcursor->mx_cursor, data, NULL);
if (unlikely(rc))
break;
}
rc = mdbx_cursor_get(&mc->mc_xcursor->mx_cursor, data, NULL,
MDB_GET_CURRENT);
} else {
rc = mdbx_node_read(mc, leaf, data);
MDB_node *leaf = NODEPTR(mp, mc->mc_ki[mc->mc_top]);
MDB_GET_KEY(leaf, key);
if (data) {
if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
if (unlikely(!(mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED))) {
mdbx_xcursor_init1(mc, leaf);
rc = mdbx_cursor_first(&mc->mc_xcursor->mx_cursor, data, NULL);
if (unlikely(rc))
return rc;
}
rc = mdbx_cursor_get(&mc->mc_xcursor->mx_cursor, data, NULL,
MDB_GET_CURRENT);
} else {
rc = mdbx_node_read(mc, leaf, data);
}
if (unlikely(rc))
return rc;
}
}
break;
}
case MDB_GET_BOTH:
case MDB_GET_BOTH_RANGE:
if (unlikely(data == NULL)) {
rc = EINVAL;
break;
}
if (unlikely(mc->mc_xcursor == NULL)) {
rc = MDB_INCOMPATIBLE;
break;
}
if (unlikely(data == NULL))
return EINVAL;
if (unlikely(mc->mc_xcursor == NULL))
return MDB_INCOMPATIBLE;
/* FALLTHRU */
case MDB_SET:
case MDB_SET_KEY:
case MDB_SET_RANGE:
if (unlikely(key == NULL)) {
rc = EINVAL;
} else {
rc = mdbx_cursor_set(mc, key, data, op,
op == MDB_SET_RANGE ? NULL : &exact);
}
if (unlikely(key == NULL))
return EINVAL;
rc =
mdbx_cursor_set(mc, key, data, op, op == MDB_SET_RANGE ? NULL : &exact);
break;
case MDB_GET_MULTIPLE:
if (unlikely(data == NULL || !(mc->mc_flags & C_INITIALIZED))) {
rc = EINVAL;
break;
}
if (unlikely(!(mc->mc_db->md_flags & MDB_DUPFIXED))) {
rc = MDB_INCOMPATIBLE;
break;
}
if (unlikely(data == NULL || !(mc->mc_flags & C_INITIALIZED)))
return EINVAL;
if (unlikely(!(mc->mc_db->md_flags & MDB_DUPFIXED)))
return MDB_INCOMPATIBLE;
rc = MDB_SUCCESS;
if (!(mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) ||
(mc->mc_xcursor->mx_cursor.mc_flags & C_EOF))
break;
goto fetchm;
case MDB_NEXT_MULTIPLE:
if (unlikely(data == NULL)) {
rc = EINVAL;
break;
}
if (unlikely(!(mc->mc_db->md_flags & MDB_DUPFIXED))) {
rc = MDB_INCOMPATIBLE;
break;
}
if (unlikely(data == NULL))
return EINVAL;
if (unlikely(!(mc->mc_db->md_flags & MDB_DUPFIXED)))
return MDB_INCOMPATIBLE;
rc = mdbx_cursor_next(mc, key, data, MDB_NEXT_DUP);
if (rc == MDB_SUCCESS) {
if (mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) {
@ -6608,18 +6594,13 @@ int mdbx_cursor_get(MDB_cursor *mc, MDB_val *key, MDB_val *data,
}
break;
case MDB_PREV_MULTIPLE:
if (data == NULL) {
rc = EINVAL;
break;
}
if (!(mc->mc_db->md_flags & MDB_DUPFIXED)) {
rc = MDB_INCOMPATIBLE;
break;
}
if (data == NULL)
return EINVAL;
if (!(mc->mc_db->md_flags & MDB_DUPFIXED))
return MDB_INCOMPATIBLE;
rc = MDB_SUCCESS;
if (!(mc->mc_flags & C_INITIALIZED))
rc = mdbx_cursor_last(mc, key, data);
else
rc = MDB_SUCCESS;
if (rc == MDB_SUCCESS) {
MDB_cursor *mx = &mc->mc_xcursor->mx_cursor;
if (mx->mc_flags & C_INITIALIZED) {
@ -6647,14 +6628,10 @@ int mdbx_cursor_get(MDB_cursor *mc, MDB_val *key, MDB_val *data,
case MDB_FIRST_DUP:
mfunc = mdbx_cursor_first;
mmove:
if (unlikely(data == NULL || !(mc->mc_flags & C_INITIALIZED))) {
rc = EINVAL;
break;
}
if (unlikely(mc->mc_xcursor == NULL)) {
rc = MDB_INCOMPATIBLE;
break;
}
if (unlikely(data == NULL || !(mc->mc_flags & C_INITIALIZED)))
return EINVAL;
if (unlikely(mc->mc_xcursor == NULL))
return MDB_INCOMPATIBLE;
{
MDB_node *leaf = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
if (!F_ISSET(leaf->mn_flags, F_DUPDATA)) {
@ -6663,10 +6640,8 @@ int mdbx_cursor_get(MDB_cursor *mc, MDB_val *key, MDB_val *data,
break;
}
}
if (unlikely(!(mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED))) {
rc = EINVAL;
break;
}
if (unlikely(!(mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED)))
return EINVAL;
rc = mfunc(&mc->mc_xcursor->mx_cursor, data, NULL);
break;
case MDB_LAST:
@ -6677,8 +6652,7 @@ int mdbx_cursor_get(MDB_cursor *mc, MDB_val *key, MDB_val *data,
goto mmove;
default:
mdbx_debug("unhandled/unimplemented cursor operation %u", op);
rc = EINVAL;
break;
return EINVAL;
}
mc->mc_flags &= ~C_DEL;