From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Date: Thu, 4 Apr 2019 12:05:45 +0300 From: Vladimir Davydov Subject: Re: [tarantool-patches] Re: [PATCH v2 2/2] lua: add key_def lua module Message-ID: <20190404090544.ya6nvcpygtxe4eiw@esperanza> References: <20190328020146.lluz4mg5tacpghwv@tkn_work_nb> <35ed4661-9789-7cf1-6627-2ced2a821939@tarantool.org> <6d915212-e80f-4a6d-d884-b838bf25f8a7@tarantool.org> <20190328112158.kpxsk6b55noicbes@tkn_work_nb> <20190403111003.x7vq7olda55tthgi@esperanza> <20190404050733.2xuobbezfzbs47l4@tkn_work_nb> <1a4f5fe8-c2d3-47eb-f596-a2104de182fb@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1a4f5fe8-c2d3-47eb-f596-a2104de182fb@tarantool.org> To: Kirill Shcherbatov Cc: tarantool-patches@freelists.org, Alexander Turenko List-ID: 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.