Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladimir Davydov <vdavydov.dev@gmail.com>
To: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
Cc: tarantool-patches@freelists.org, kshcherbatov@tarantool.org
Subject: Re: [PATCH v3 3/3] Lua: implement json path access to tuple fields
Date: Fri, 13 Apr 2018 14:33:26 +0300	[thread overview]
Message-ID: <20180413113326.3lijwwzy6dwzrgev@esperanza> (raw)
In-Reply-To: <c3b64b7b4e638970864995c895bbd5f736282560.1523349020.git.v.shpilevoy@tarantool.org>

On Tue, Apr 10, 2018 at 11:31:46AM +0300, Vladislav Shpilevoy wrote:
> From: Kirill Shcherbatov <kshcherbatov@tarantool.org>
> 
> New tuple_field_raw_by_path and tuple_field_by_path APIs.

Would be nice to say a few words here about the new API.

> 
> Resolves #1285
> ---
>  src/box/CMakeLists.txt     |   2 +-
>  src/box/lua/tuple.c        |  63 +++++++++----
>  src/box/lua/tuple.lua      |  49 +++-------
>  src/box/tuple.h            |  21 +++++
>  src/box/tuple_format.c     | 164 ++++++++++++++++++++++++++++++++
>  src/box/tuple_format.h     |  19 ++++
>  test/engine/tuple.result   | 229 +++++++++++++++++++++++++++++++++++++++++++++
>  test/engine/tuple.test.lua |  67 +++++++++++++
>  8 files changed, 558 insertions(+), 56 deletions(-)
> 
> diff --git a/src/box/CMakeLists.txt b/src/box/CMakeLists.txt
> index ef7225d10..ae156a2f5 100644
> --- a/src/box/CMakeLists.txt
> +++ b/src/box/CMakeLists.txt
> @@ -47,7 +47,7 @@ add_library(tuple STATIC
>      field_def.c
>      opt_def.c
>  )
> -target_link_libraries(tuple box_error core ${MSGPUCK_LIBRARIES} ${ICU_LIBRARIES} misc bit)
> +target_link_libraries(tuple json_path box_error core ${MSGPUCK_LIBRARIES} ${ICU_LIBRARIES} misc bit)
>  
>  add_library(xlog STATIC xlog.c)
>  target_link_libraries(xlog core box_error crc32 ${ZSTD_LIBRARIES})
> diff --git a/src/box/lua/tuple.c b/src/box/lua/tuple.c
> index a0b4e4a79..c10e9b253 100644
> --- a/src/box/lua/tuple.c
> +++ b/src/box/lua/tuple.c
> @@ -41,6 +41,7 @@
>  #include "box/tuple_convert.h"
>  #include "box/errcode.h"
>  #include "box/memtx_tuple.h"
> +#include "json/path.h"
>  
>  /** {{{ box.tuple Lua library
>   *
> @@ -420,36 +421,58 @@ lbox_tuple_transform(struct lua_State *L)
>  }
>  
>  /**
> - * Find a tuple field using its name.
> + * Find a tuple field by JSON path. If a field was not found and a
> + * path contains JSON syntax errors, then an exception is raised.
>   * @param L Lua state.
> - * @param tuple 1-th argument on lua stack, tuple to get field
> + * @param tuple 1-th argument on a lua stack, tuple to get field
>   *        from.
> - * @param field_name 2-th argument on lua stack, field name to
> - *        get.
> + * @param path 2-th argument on lua stack. Can be field name,
> + *        JSON path to a field or a field number.
>   *
> - * @retval If a field was not found, return -1 and nil to lua else
> - *         return 0 and decoded field.
> + * @retval not nil Found field value.
> + * @retval     nil A field is NULL or does not exist.
>   */
>  static int
> -lbox_tuple_field_by_name(struct lua_State *L)
> +lbox_tuple_field_by_path(struct lua_State *L)
>  {
>  	struct tuple *tuple = luaT_istuple(L, 1);
>  	/* Is checked in Lua wrapper. */
>  	assert(tuple != NULL);
> -	assert(lua_isstring(L, 2));
> -	size_t name_len;
> -	const char *name = lua_tolstring(L, 2, &name_len);
> -	uint32_t name_hash = lua_hashstring(L, 2);
> -	const char *field =
> -		tuple_field_by_name(tuple, name, name_len, name_hash);
> -	if (field == NULL) {
> -		lua_pushinteger(L, -1);
> -		lua_pushnil(L);
> -		return 2;
> +	const char *field = NULL;
> +	if (lua_isnumber(L, 2)) {
> +		double dbl_index = lua_tonumber(L, 2);
> +		if (dbl_index != floor(dbl_index))
> +			goto usage_error;
> +		int index = (int) floor(dbl_index) - TUPLE_INDEX_BASE;
> +		if (index >= 0) {
> +			field = tuple_field(tuple, index);
> +			if (field == NULL) {
> +				lua_pushnil(L);
> +				return 1;
> +			}
> +		} else {
> +			lua_pushnil(L);
> +			return 1;
> +		}

Why did you move handling of integer index from Lua to C? AFAIU having
it in Lua should be faster. Besides, this new function is kinda
difficult to follow IMO as it does two things now. I'd prefer it to do
just one thing - looking up a tuple field by path.

Other than this, the patch looks good to me.

> +	} else if (lua_isstring(L, 2)) {
> +		size_t len;
> +		const char *path = lua_tolstring(L, 2, &len);
> +		if (len == 0)
> +			goto usage_error;
> +		if (tuple_field_by_path(tuple, path, (uint32_t) len,
> +					lua_hashstring(L, 2), &field) != 0) {
> +			return luaT_error(L);
> +		} else if (field == NULL) {
> +			lua_pushnil(L);
> +			return 1;
> +		}
> +	} else {
> +usage_error:
> +		return luaL_error(L, "Usage: tuple[<path> or number >= 1]");
>  	}
> -	lua_pushinteger(L, 0);
> +	assert(field != NULL);
>  	luamp_decode(L, luaL_msgpack_default, &field);
> -	return 2;
> +	return 1;
>  }

  parent reply	other threads:[~2018-04-13 11:33 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-10  8:31 [PATCH v3 0/3] Implement " Vladislav Shpilevoy
2018-04-10  8:31 ` [PATCH v3 1/3] Allow gcov on Mac Vladislav Shpilevoy
2018-04-10 10:49   ` Vladimir Davydov
     [not found] ` <f1302082b4457a03b5d2b3c44526815428de4a0e.1523349020.git.v.shpilevoy@tarantool.org>
2018-04-10 16:36   ` [PATCH v3 2/3] Introduce json_path_parser with Unicode support Vladimir Davydov
2018-04-10 18:42     ` [tarantool-patches] " Vladislav Shpilevoy
2018-04-11  9:20       ` Vladimir Davydov
     [not found] ` <c3b64b7b4e638970864995c895bbd5f736282560.1523349020.git.v.shpilevoy@tarantool.org>
2018-04-13 11:33   ` Vladimir Davydov [this message]
2018-04-13 21:51     ` [tarantool-patches] Re: [PATCH v3 3/3] Lua: implement json path access to tuple fields Vladislav Shpilevoy
2018-04-16  8:35       ` Vladimir Davydov
2018-04-16 10:02         ` Vladislav Shpilevoy
2018-04-22 14:21         ` Vladislav Shpilevoy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180413113326.3lijwwzy6dwzrgev@esperanza \
    --to=vdavydov.dev@gmail.com \
    --cc=kshcherbatov@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [PATCH v3 3/3] Lua: implement json path access to tuple fields' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox