From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 9FFEA290E3 for ; Mon, 3 Sep 2018 06:32:52 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id NQ6mB-5lf923 for ; Mon, 3 Sep 2018 06:32:52 -0400 (EDT) Received: from smtp54.i.mail.ru (smtp54.i.mail.ru [217.69.128.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 5B3EF290DF for ; Mon, 3 Sep 2018 06:32:52 -0400 (EDT) Subject: [tarantool-patches] Re: [PATCH v3 4/4] box: specify indexes in user-friendly form References: From: Vladislav Shpilevoy Message-ID: <1d55c8a2-d52c-36e3-d0f2-856992977b1a@tarantool.org> Date: Mon, 3 Sep 2018 13:32:50 +0300 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-subscribe: List-owner: List-post: List-archive: To: Kirill Shcherbatov , tarantool-patches@freelists.org Thanks for the fixes! See 3 comments below. > diff --git a/src/box/lua/index.c b/src/box/lua/index.c > index ef89c39..b2b8ca2 100644 > --- a/src/box/lua/index.c > +++ b/src/box/lua/index.c > @@ -328,6 +331,76 @@ lbox_index_compact(lua_State *L) > return 0; > } > > +static int > +lbox_index_resolve_path(struct lua_State *L) > +{ > + if (lua_gettop(L) != 3 || > + !lua_isnumber(L, 1) || !lua_isnumber(L, 2) || !lua_isstring(L, 3)) { > + return luaL_error(L, "Usage box.internal." > + "path_resolve(part_id, space_id, path)"); > + } > + uint32_t part_id = lua_tonumber(L, 1); > + uint32_t space_id = lua_tonumber(L, 2); > + const char *path = lua_tostring(L, 3); > + size_t path_len = strlen(path); > + struct space *space = space_cache_find(space_id); > + if (space == NULL) > + return luaT_error(L); > + struct json_path_parser parser; > + struct json_path_node node; > + json_path_parser_create(&parser, path, path_len); > + int rc = json_path_next(&parser, &node); > + if (rc != 0) { > + const char *err_msg = > + tt_sprintf("Illegal parameters, options.parts[%d]: " > + "error in path on position %d", part_id, rc); > + diag_set(IllegalParams, err_msg); 1. It is box sub-module so use ClientError please. > + return luaT_error(L); > + } > + assert(space->format != NULL && space->format->dict != NULL); > + uint32_t fieldno = node.num - TUPLE_INDEX_BASE;; 2. How do you use node.num before checking that it is num? And why referencing a field out of format is forbidden? I am free to define indexes on non-formatted fields. > + if (node.type == JSON_PATH_NUM && > + fieldno >= space->format->field_count) { > + const char *err_msg = > + tt_sprintf("Illegal parameters, options.parts[%d]: " > + "field '%d' referenced in path is greater " > + "than format field count %d", part_id, > + fieldno + TUPLE_INDEX_BASE, > + space->format->field_count); > + diag_set(IllegalParams, err_msg); > + return luaT_error(L); > + } else if (node.type == JSON_PATH_STR && > + tuple_fieldno_by_name(space->format->dict, node.str, node.len, > + field_name_hash(node.str, node.len), > + &fieldno) != 0) { > + const char *err_msg = > + tt_sprintf("Illegal parameters, options.parts[%d]: " > + "field was not found by name '%.*s'", > + part_id, node.len, node.str); > + diag_set(IllegalParams, err_msg); > + return luaT_error(L); > + } > + fieldno += TUPLE_INDEX_BASE; > + > + char *path_resolved = region_alloc(&fiber()->gc, path_len + 1); > + if (path_resolved == NULL) { > + diag_set(OutOfMemory, path_len + 1, "region_alloc", > + "path_resolved"); > + return luaT_error(L); > + } > + > + path = path_resolved; > + path_resolved += > + sprintf(path_resolved, "[%d]", fieldno); 3. Fits in one line. > + memcpy(path_resolved, parser.src + parser.offset, > + parser.src_len - parser.offset); > + path_resolved[parser.src_len - parser.offset] = '\0'; > + > + lua_pushnumber(L, fieldno); > + lua_pushstring(L, path); > + return 2; > +} > + > /* }}} */ > > void