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