[tarantool-patches] Re: [PATCH v2 2/2] lua: add key_def lua module

Vladimir Davydov vdavydov.dev at gmail.com
Thu Apr 4 12:05:45 MSK 2019


On Thu, Apr 04, 2019 at 11:04:10AM +0300, Kirill Shcherbatov wrote:
> >> The code checking a tuple against key_def should live somewhere in
> >> src/box - chances are high that we miss lua/key_def.c when we extend
> >> key_def struct again.> 
> > Can you suggest where it is better to place this code: src/box/key_def.c
> > or src/box/tuple.c?
> Due to the fact that this code needs table_field_by_part and
> tuple_format_min_field_count, it cannot be placed in key_def.c
> Placing it in tuple.h is not quite correct either; that's why we've got it inlined.

Well, you could put it in tuple_format.c.

> 
> >> Probably, we should reuse tuple_validate() for checking a tuple against
> >> a key_def so as not to implement the same code again.
> 
> Unfortunately tuple_validate() is designed for format validation while we don't
> have format here and I don't like create it for validation event in case of error.

Creating a format on each call to 'compare' is prohibitive from
performance pov, you're right. However, we could attach a format to
Lua's key_def object. This would also speed up comparisons, as tuples
created for this format would have fieldmap.

> 
> 
> As for assertion you've found, that is my fault; required fix doesn't increase
> code complexity (not sure about error message):
> 
> @@ -243,8 +242,11 @@ lbox_key_def_check_tuple(struct lua_State *L, struct key_def *key_def, int idx)
>  		struct key_part *part = &key_def->parts[idx];
>  		const char *field = tuple_field_by_part(tuple, part);
>  		if (field == NULL) {
> -			assert(key_def->has_optional_parts);
> -			continue;
> +			if (key_part_is_nullable(part))
> +				continue;
> +			diag_set(IllegalParams, "tuple doesn't match key "
> +						"definition (part %d)", idx);
> +			return NULL;

Again, mixing IllegalParams and ClientError (from key_part_validate)
doesn't look good.

Also, after this fix you don't need the min_field_count check.

However, what I don't like is that in case a tuple is created from a Lua
table, you'll have to access all fields twice - on validation and
comparison - which is costly without a fieldmap. One more argument for
attaching a format to Lua's key_def object.

>  		}
>  		if (key_part_validate(part->type, field, idx,
>  				      key_part_is_nullable(part)) != 0)
> 
> 
> >> Mixing ER_WRONG_INDEX_OPTIONS and IllegalParams looks ugly. Please fix.
> 
> @@ -143,8 +143,7 @@ luaT_key_def_set_part(struct lua_State *L, struct key_part_def *parts,
>  		size_t path_len;
>  		const char *path = lua_tolstring(L, -1, &path_len);
>  		if (json_path_validate(path, path_len, TUPLE_INDEX_BASE) != 0) {
> -			diag_set(ClientError, ER_WRONG_INDEX_OPTIONS,
> -				 part_idx + TUPLE_INDEX_BASE, "invalid path");
> +			diag_set(IllegalParams, "invalid path: \"%s\"", path);

Then you don't need to pass parts+part_idx to this function -
you can pass only the part you need to set, as it was initially
done by Alexander.



More information about the Tarantool-patches mailing list