[tarantool-patches] [PATCH v1 1/2] sql: use schema API to get index info in analyze

Kirill Shcherbatov kshcherbatov at tarantool.org
Tue Jul 24 14:05:12 MSK 2018


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





More information about the Tarantool-patches mailing list