From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Date: Thu, 28 Mar 2019 05:01:47 +0300 From: Alexander Turenko Subject: Re: [PATCH v2 2/2] lua: add key_def lua module Message-ID: <20190328020146.lluz4mg5tacpghwv@tkn_work_nb> References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: To: Kirill Shcherbatov Cc: tarantool-patches@freelists.org, Vladimir Davydov List-ID: Thank you, it works like a charm. I added a fixup commit on top of your patchset (added a test case, updated comments in the test a bit). Also please consider comments below. Vladimir, I CCed you to ask a question at end of the email (the code is on kshch/gh-4025-lua-key-kef-methods branch). WBR, Alexander Turenko. On Wed, Mar 27, 2019 at 05:29:28PM +0300, Kirill Shcherbatov wrote: > There are several reasons to add this module: > > * Factor out key parts parsing code from the tuples merger (#3276). > * Support comparing a tuple with a key / a tuple, support merging > key_defs from Lua (#3398). > * Support extracting a key from a tuple (#4025). > > The format of `parts` parameter in the `key_def.new(parts)` call is > compatible with the following structures: > > * box.space[...].index[...].parts; > * net_box_conn.space[...].index[...].parts. > > A key_def instance has the following methods: > > * :extract_key(tuple) -> key (as tuple) > * :compare(tuple_a, tuple_b) -> number > * :compare_with_key(tuple, key) -> number > * :merge(another_key_def) -> new key_def instance > * :totable() -> table > I would add here 'Functions that accept tuple(s) also allow to pass Lua table(s) instead'. > +static int > +lbox_key_def_compare(struct lua_State *L) > +{ > + struct key_def *key_def; > + if (lua_gettop(L) != 3 || (key_def = check_key_def(L, 1)) == NULL) { > + return luaL_error(L, "Usage: key_def:" > + "compare(tuple_a, tuple_b)"); > + } > + > + struct tuple *tuple_a, *tuple_b; > + struct tuple_format *format = box_tuple_format_default(); > + if ((tuple_a = luaT_tuple_new(L, 2, format)) == NULL || > + tuple_validate_parts(key_def, tuple_a) != 0) > + return luaT_error(L); > + tuple_ref(tuple_a); > + if ((tuple_b = luaT_tuple_new(L, 3, format)) == NULL || > + tuple_validate_parts(key_def, tuple_b) != 0) { > + tuple_unref(tuple_a); > + return luaT_error(L); > + } > + tuple_ref(tuple_b); Consider the case when a user get tuples from a local space (or merger) and they have a format that allows to compare faster using precalculated offsets. I think we should not create a new tuple(s) in the case. Applicable for other functions too. > diff --git a/src/box/tuple.h b/src/box/tuple.h > index 8b12fd5a8..faa42fdf7 100644 > --- a/src/box/tuple.h > +++ b/src/box/tuple.h > @@ -672,6 +672,39 @@ tuple_field_by_part(const struct tuple *tuple, struct key_part *part) > tuple_field_map(tuple), part); > } > > +/** > + * Check that tuple match with the key definition. > + * @param key_def Key definition. > + * @param tuple Tuple for matching. > + * @param allow_nullable True if nullable parts are allowed. > + * > + * @retval 0 The tuple is valid. > + * @retval -1 The tuple is invalid. > + */ > +static inline int > +tuple_validate_parts(struct key_def *key_def, struct tuple *tuple) I don't sure it worth to inline this function: it is not so lightweight as, say, a structure field access. I'm tentative whether this function should be in tuple.[ch] or key_def.[ch]. What do you think? (If it is in tuple.[ch] maybe it is better to let a tuple being the first parameter?) Maybe we need to ask Vladimir (CCed).