[tarantool-patches] [PATCH v3 1/1] lua: add key_def lua module

Konstantin Osipov kostja.osipov at gmail.com
Wed Apr 24 21:13:02 MSK 2019


* Kirill Shcherbatov <kshcherbatov at tarantool.org> [19/04/22 17:40]:
> --- a/src/box/errcode.h
> +++ b/src/box/errcode.h
> @@ -246,6 +246,7 @@ struct errcode_record {
>  	/*191 */_(ER_SQL_PARSER_LIMIT,		"%s %d exceeds the limit (%d)") \
>  	/*192 */_(ER_INDEX_DEF_UNSUPPORTED,	"%s are prohibited in an index definition") \
>  	/*193 */_(ER_CK_DEF_UNSUPPORTED,	"%s are prohibited in a CHECK constraint definition") \
> +	/*194 */_(ER_TUPLE_KEY_PART_MISSED,	"Supplied tuple field for part %u does not exists") \

I don't understand this error message. 
Besides key part number, the message should contain field name or
number.

Besides, we already have ER_NO_SUCH_FIELD_NO and
why not use it?


ER_NO_FIELD_FOR_KEY_PART "The supplied tuple has no field %s for key part %u"

> +
> +static uint32_t key_def_type_id = 0;

Please rename to key_def_ctype_id

> +static int
> +lbox_key_def_compare(struct lua_State *L)
> +{
> +	struct key_def *key_def;
> +	if (lua_gettop(L) != 3 ||
> +	    (key_def = luaT_check_key_def(L, 1)) == NULL) {
> +		return luaL_error(L, "Usage: key_def:"
> +				     "compare(tuple_a, tuple_b)");
> +	}
> +
> +	struct tuple *tuple_a, *tuple_b;
> +	if ((tuple_a = luaT_key_def_check_tuple(L, key_def, 2)) == NULL)
> +		return luaT_error(L);
> +	if ((tuple_b = luaT_key_def_check_tuple(L, key_def, 3)) == NULL) {
> +		tuple_unref(tuple_a);
> +		return luaT_error(L);
> +	}

Invoking tuple_validate and possibly tuple_new on each compare is
awfully slow.
> +static int
> +lbox_key_def_compare_with_key(struct lua_State *L)
> +{
> +	struct key_def *key_def;
> +	if (lua_gettop(L) != 3 ||
> +	    (key_def = luaT_check_key_def(L, 1)) == NULL) {
> +		return luaL_error(L, "Usage: key_def:"
> +				     "compare_with_key(tuple, key)");
> +	}
> +
> +	struct tuple *tuple = luaT_key_def_check_tuple(L, key_def, 2);
> +	if (tuple == NULL)
> +		return luaT_error(L);
> +
> +	size_t key_len;
> +	const char *key = lbox_encode_tuple_on_gc(L, 3, &key_len);
> +	uint32_t part_count = mp_decode_array(&key);
> +	if (key_validate_parts(key_def, key, part_count, true) != 0) {
> +		tuple_unref(tuple);
> +		return luaT_error(L);
> +	}
> +
> +	int rc = tuple_compare_with_key(tuple, key, part_count, key_def);
> +	tuple_unref(tuple);
> +	lua_pushinteger(L, rc);
> +	return 1;
> +}

This also looks as a terribly inefficient implementation for
compare.

Overall, the API looks good to me, while the implementation seems
to be too inefficient. I would consider changing extract_key() to
return char *, not struct tuple, the buffer could be allocated on
transaction region. I also think that compare should not allocate
memory or create tuples, and it should not call tuple_validate()
either.

If this is urgent, I would push since the code quality is very
good and the api would stable, but I don't see how soo inefficient
compare functions could be useful.

-- 
Konstantin Osipov, Moscow, Russia, +7 903 626 22 32
http://tarantool.io - www.twitter.com/kostja_osipov



More information about the Tarantool-patches mailing list