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 D5474238C3 for ; Tue, 24 Jul 2018 07:05:18 -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 bhPHkuI_B_qW for ; Tue, 24 Jul 2018 07:05:18 -0400 (EDT) Received: from smtpng1.m.smailru.net (smtpng1.m.smailru.net [94.100.181.251]) (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 1330C23795 for ; Tue, 24 Jul 2018 07:05:17 -0400 (EDT) From: Kirill Shcherbatov Subject: [tarantool-patches] [PATCH v1 1/2] sql: use schema API to get index info in analyze Date: Tue, 24 Jul 2018 14:05:12 +0300 Message-Id: In-Reply-To: <5899c7db-b1c0-d099-989e-dcb41b801cdd@tarantool.org> References: <5899c7db-b1c0-d099-989e-dcb41b801cdd@tarantool.org> 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: tarantool-patches@freelists.org Cc: v.shpilevoy@tarantool.org, korablev@tarantool.org, Kirill Shcherbatov We would like to avoid starting transactions in ANALYZE so we need to use schema API that is more tolerant. Part of #3551. --- src/box/sql/analyze.c | 82 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 34 deletions(-) diff --git a/src/box/sql/analyze.c b/src/box/sql/analyze.c index 00d96d2..c7b85dc 100644 --- a/src/box/sql/analyze.c +++ b/src/box/sql/analyze.c @@ -1219,6 +1219,26 @@ decode_stat_string(const char *stat_string, int stat_size, tRowcnt *stat_exact, } /** + * Find index with specified @name in specified @space. + * @param space to lookup. + * @param name to use in comparation. + * @param len lenth of @name string. + * @retval NULL on nothing found. + * @retval not NULL pointer on index AST else. + */ +static struct index * +space_index_by_name(struct space *space, const char *name, uint32_t len) +{ + uint32_t idx_cnt = space->index_count; + for (uint32_t i = 0; i < idx_cnt; i++) { + const char *idx_name = space->index[i]->def->name; + if (strlen(idx_name) == len && memcmp(idx_name, name, len) == 0) + return space->index[i]; + } + return NULL; +} + +/** * This callback is invoked once for each index when reading the * _sql_stat1 table. * @@ -1246,20 +1266,20 @@ analysis_loader(void *data, int argc, char **argv, char **unused) struct analysis_index_info *info = (struct analysis_index_info *) data; assert(info->stats != NULL); struct index_stat *stat = &info->stats[info->index_count++]; - uint32_t space_id = box_space_id_by_name(argv[0], strlen(argv[0])); - if (space_id == BOX_ID_NIL) + uint32_t space_id = BOX_ID_NIL; + int rc = schema_find_id(BOX_SPACE_ID, 2, argv[0], strlen(argv[0]), + &space_id); + if (rc != 0 || space_id == BOX_ID_NIL) return -1; struct space *space = space_by_id(space_id); assert(space != NULL); - struct index *index; - uint32_t iid = box_index_id_by_name(space_id, argv[1], strlen(argv[1])); + struct index *index = + space_index_by_name(space, argv[1], strlen(argv[1])); /* * Convention is if index's name matches with space's * one, then it is primary index. */ - if (iid != BOX_ID_NIL) { - index = space_index(space, iid); - } else { + if (index == NULL) { if (sqlite3_stricmp(argv[0], argv[1]) != 0) return -1; index = space_index(space, 0); @@ -1417,19 +1437,17 @@ load_stat_from_space(struct sqlite3 *db, const char *sql_select_prepare, if (index_name == NULL) continue; uint32_t sample_count = sqlite3_column_int(stmt, 2); - uint32_t space_id = box_space_id_by_name(space_name, - strlen(space_name)); - assert(space_id != BOX_ID_NIL); + uint32_t space_id = BOX_ID_NIL; + rc = schema_find_id(BOX_SPACE_ID, 2, space_name, + strlen(space_name), &space_id); + assert(rc == 0 && space_id != BOX_ID_NIL); struct space *space = space_by_id(space_id); assert(space != NULL); - struct index *index; - uint32_t iid = box_index_id_by_name(space_id, index_name, - strlen(index_name)); + struct index *index = + space_index_by_name(space, index_name, strlen(index_name)); if (sqlite3_stricmp(space_name, index_name) == 0 && - iid == BOX_ID_NIL) + index == NULL) index = space_index(space, 0); - else - index = space_index(space, iid); assert(index != NULL); uint32_t column_count = index->def->key_def->part_count; struct index_stat *stat = &stats[current_idx_count]; @@ -1488,17 +1506,15 @@ load_stat_from_space(struct sqlite3 *db, const char *sql_select_prepare, const char *index_name = (char *)sqlite3_column_text(stmt, 1); if (index_name == NULL) continue; - uint32_t space_id = box_space_id_by_name(space_name, - strlen(space_name)); - assert(space_id != BOX_ID_NIL); + uint32_t space_id = BOX_ID_NIL; + int rc = schema_find_id(BOX_SPACE_ID, 2, space_name, + strlen(space_name), &space_id); + assert(rc == 0 && space_id != BOX_ID_NIL); struct space *space = space_by_id(space_id); assert(space != NULL); - struct index *index; - uint32_t iid = box_index_id_by_name(space_id, index_name, - strlen(index_name)); - if (iid != BOX_ID_NIL) { - index = space_index(space, iid); - } else { + struct index *index = + space_index_by_name(space, index_name, strlen(index_name)); + if (index == NULL) { if (sqlite3_stricmp(space_name, index_name) != 0) return -1; index = space_index(space, 0); @@ -1572,18 +1588,16 @@ load_stat_to_index(struct sqlite3 *db, const char *sql_select_load, const char *index_name = (char *)sqlite3_column_text(stmt, 1); if (index_name == NULL) continue; - uint32_t space_id = box_space_id_by_name(space_name, - strlen(space_name)); - if (space_id == BOX_ID_NIL) + uint32_t space_id = BOX_ID_NIL; + int rc = schema_find_id(BOX_SPACE_ID, 2, space_name, strlen(space_name), + &space_id); + if (rc != 0 || space_id == BOX_ID_NIL) return -1; struct space *space = space_by_id(space_id); assert(space != NULL); - struct index *index; - uint32_t iid = box_index_id_by_name(space_id, index_name, - strlen(index_name)); - if (iid != BOX_ID_NIL) { - index = space_index(space, iid); - } else { + struct index *index = + space_index_by_name(space, index_name, strlen(index_name)); + if (index == NULL) { if (sqlite3_stricmp(space_name, index_name) != 0) return -1; index = space_index(space, 0); -- 2.7.4