[PATCH v3 2/7] lib: introduce a new json_path_multikey_offset helper

Vladimir Davydov vdavydov.dev at gmail.com
Wed Apr 3 15:56:01 MSK 2019


On Tue, Apr 02, 2019 at 06:49:33PM +0300, Kirill Shcherbatov wrote:
> Introduced a new procedure json_path_multikey_offset. This helper
> scans the JSON path string and returns the offset of the first character
> [*] (the array index placeholder).
> 
> We need this function in the scope of the multikey index patchset to
> extract the number of keys to be inserted into the index
> using JSON subpath that has json_path_multikey_offset() length.
> 
> Needed for #1257
> ---
>  src/lib/json/json.c   | 18 ++++++++++++++++++
>  src/lib/json/json.h   | 10 ++++++++++
>  test/unit/json.c      | 32 +++++++++++++++++++++++++++++++-
>  test/unit/json.result | 12 +++++++++++-
>  4 files changed, 70 insertions(+), 2 deletions(-)
> 
> diff --git a/src/lib/json/json.c b/src/lib/json/json.c
> index 854158f63..2d3c35f4b 100644
> --- a/src/lib/json/json.c
> +++ b/src/lib/json/json.c
> @@ -321,6 +321,24 @@ json_path_validate(const char *path, int path_len, int index_base)
>  	return rc;
>  }
>  
> +int
> +json_path_multikey_offset(const char *path, int path_len, int index_base)
> +{
> +	struct json_lexer lexer;
> +	json_lexer_create(&lexer, path, path_len, index_base);
> +	struct json_token token;
> +	int rc, last_lexer_offset = 0;
> +	while ((rc = json_lexer_next_token(&lexer, &token)) == 0) {
> +		if (token.type == JSON_TOKEN_ANY)
> +			return last_lexer_offset;
> +		else if (token.type == JSON_TOKEN_END)
> +			break;
> +		last_lexer_offset = lexer.offset;
> +	}
> +	assert(rc == 0);
> +	return -1;
> +}

IMO returning path_len if no multikey token is found would be more
common (think of STL methods, which return end() iterator if not found).
Doesn't really matter though.

Other than that looks good to me.



More information about the Tarantool-patches mailing list