commit 5b7cc2945717570c6bf0a13e6207303e798dc30b from: Kirill Yukhin via: Vladimir Davydov date: Fri Oct 26 20:33:09 2018 UTC sql: use space_by_name in SQL Since hash, which maps space name to space pointer was introduced in previous patch, use it in SQL front-end as it is heavily needed. @locker: removed a couple of useless assertions and variable renames commit - 030d28f55fa855a7cd967078e61a55aced523e30 commit + 5b7cc2945717570c6bf0a13e6207303e798dc30b blob - 3d72e311a987b1817a41486900ac46fc1247f6de blob + 89e51eb30db7821fb2176a152fd8ffb0f80c2508 --- src/box/sql/alter.c +++ src/box/sql/alter.c @@ -47,19 +47,16 @@ sql_alter_table_rename(struct Parse *parse, struct Src if (new_name == NULL) goto exit_rename_table; /* Check that new name isn't occupied by another table. */ - uint32_t space_id = box_space_id_by_name(new_name, strlen(new_name)); - if (space_id != BOX_ID_NIL) { + if (space_by_name(new_name) != NULL) { diag_set(ClientError, ER_SPACE_EXISTS, new_name); goto tnt_error; } const char *tbl_name = src_tab->a[0].zName; - space_id = box_space_id_by_name(tbl_name, strlen(tbl_name)); - if (space_id == BOX_ID_NIL) { + struct space *space = space_by_name(tbl_name); + if (space == NULL) { diag_set(ClientError, ER_NO_SUCH_SPACE, tbl_name); goto tnt_error; } - struct space *space = space_by_id(space_id); - assert(space != NULL); if (space->def->opts.is_view) { diag_set(ClientError, ER_ALTER_SPACE, tbl_name, "view may not be altered"); @@ -68,7 +65,7 @@ sql_alter_table_rename(struct Parse *parse, struct Src sql_set_multi_write(parse, false); /* Drop and reload the internal table schema. */ struct Vdbe *v = sqlite3GetVdbe(parse); - sqlite3VdbeAddOp4(v, OP_RenameTable, space_id, 0, 0, new_name, + sqlite3VdbeAddOp4(v, OP_RenameTable, space->def->id, 0, 0, new_name, P4_DYNAMIC); exit_rename_table: sqlite3SrcListDelete(db, src_tab); blob - 674d53dae9e52f2b8c2da3367520660cba685f54 blob + 97884ca7117a02f842bca0d46de88a1eb858da82 --- src/box/sql/analyze.c +++ src/box/sql/analyze.c @@ -1114,10 +1114,8 @@ sqlite3Analyze(Parse * pParse, Token * pName) /* Form 2: Analyze table named */ char *z = sqlite3NameFromToken(db, pName); if (z != NULL) { - uint32_t space_id = box_space_id_by_name(z, strlen(z)); - if (space_id != BOX_ID_NIL) { - struct space *sp = space_by_id(space_id); - assert(sp != NULL); + struct space *sp = space_by_name(z); + if (sp != NULL) { if (sp->def->opts.is_view) { sqlite3ErrorMsg(pParse, "VIEW isn't "\ "allowed to be "\ @@ -1224,13 +1222,12 @@ analysis_loader(void *data, int argc, char **argv, cha 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) + struct space *space = space_by_name(argv[0]); + if (space == NULL) 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])); + uint32_t iid = box_index_id_by_name(space->def->id, argv[1], + strlen(argv[1])); /* * Convention is if index's name matches with space's * one, then it is primary index. @@ -1395,13 +1392,10 @@ load_stat_from_space(struct sqlite3 *db, const char *s 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); - struct space *space = space_by_id(space_id); + struct space *space = space_by_name(space_name); assert(space != NULL); struct index *index; - uint32_t iid = box_index_id_by_name(space_id, index_name, + uint32_t iid = box_index_id_by_name(space->def->id, index_name, strlen(index_name)); if (sqlite3_stricmp(space_name, index_name) == 0 && iid == BOX_ID_NIL) @@ -1466,13 +1460,10 @@ load_stat_from_space(struct sqlite3 *db, const char *s 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); - struct space *space = space_by_id(space_id); + struct space *space = space_by_name(space_name); assert(space != NULL); struct index *index; - uint32_t iid = box_index_id_by_name(space_id, index_name, + uint32_t iid = box_index_id_by_name(space->def->id, index_name, strlen(index_name)); if (iid != BOX_ID_NIL) { index = space_index(space, iid); @@ -1550,14 +1541,10 @@ load_stat_to_index(struct sqlite3 *db, const char *sql 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) - return -1; - struct space *space = space_by_id(space_id); + struct space *space = space_by_name(space_name); assert(space != NULL); struct index *index; - uint32_t iid = box_index_id_by_name(space_id, index_name, + uint32_t iid = box_index_id_by_name(space->def->id, index_name, strlen(index_name)); if (iid != BOX_ID_NIL) { index = space_index(space, iid); blob - a806fb4b3cdbeed85717fdc036f4a59783c2ae93 blob + fb5d61c210462bf6f562ef5bd81c136395ee4c8a --- src/box/sql/build.c +++ src/box/sql/build.c @@ -329,8 +329,8 @@ sqlite3StartTable(Parse *pParse, Token *pName, int noE if (sqlite3CheckIdentifierName(pParse, zName) != SQLITE_OK) goto cleanup; - uint32_t space_id = box_space_id_by_name(zName, strlen(zName)); - if (space_id != BOX_ID_NIL) { + struct space *space = space_by_name(zName); + if (space != NULL) { if (!noErr) { sqlite3ErrorMsg(pParse, "table %s already exists", zName); @@ -1863,9 +1863,8 @@ sql_drop_table(struct Parse *parse_context, struct Src assert(parse_context->nErr == 0); assert(table_name_list->nSrc == 1); const char *space_name = table_name_list->a[0].zName; - uint32_t space_id = box_space_id_by_name(space_name, - strlen(space_name)); - if (space_id == BOX_ID_NIL) { + struct space *space = space_by_name(space_name); + if (space == NULL) { if (!is_view && !if_exists) sqlite3ErrorMsg(parse_context, "no such table: %s", space_name); @@ -1874,8 +1873,6 @@ sql_drop_table(struct Parse *parse_context, struct Src space_name); goto exit_drop_table; } - struct space *space = space_by_id(space_id); - assert(space != NULL); /* * Ensure DROP TABLE is not used on a view, * and DROP VIEW is not used on a table. @@ -1990,17 +1987,13 @@ sql_create_foreign_key(struct Parse *parse_context, st } assert(!is_alter || (child != NULL && child->nSrc == 1)); struct space *child_space = NULL; - uint32_t child_id = 0; if (is_alter) { const char *child_name = child->a[0].zName; - child_id = box_space_id_by_name(child_name, - strlen(child_name)); - if (child_id == BOX_ID_NIL) { + child_space = space_by_name(child_name); + if (child_space == NULL) { diag_set(ClientError, ER_NO_SUCH_SPACE, child_name); goto tnt_error; } - child_space = space_by_id(child_id); - assert(child_space != NULL); } else { struct fkey_parse *fk = region_alloc(&parse_context->region, sizeof(*fk)); @@ -2016,8 +2009,6 @@ sql_create_foreign_key(struct Parse *parse_context, st parent_name = sqlite3NameFromToken(db, parent); if (parent_name == NULL) goto exit_create_fk; - uint32_t parent_id = box_space_id_by_name(parent_name, - strlen(parent_name)); /* * Within ALTER TABLE ADD CONSTRAINT FK also can be * self-referenced, but in this case parent (which is @@ -2025,9 +2016,8 @@ sql_create_foreign_key(struct Parse *parse_context, st */ is_self_referenced = !is_alter && strcmp(parent_name, new_tab->def->name) == 0; - struct space *parent_space; - if (parent_id == BOX_ID_NIL) { - parent_space = NULL; + struct space *parent_space = space_by_name(parent_name); + if (parent_space == NULL) { if (is_self_referenced) { struct fkey_parse *fk = rlist_first_entry(&parse_context->new_fkey, @@ -2039,8 +2029,6 @@ sql_create_foreign_key(struct Parse *parse_context, st goto tnt_error; } } else { - parent_space = space_by_id(parent_id); - assert(parent_space != NULL); if (parent_space->def->opts.is_view) { sqlite3ErrorMsg(parse_context, "referenced table can't be view"); @@ -2092,8 +2080,8 @@ sql_create_foreign_key(struct Parse *parse_context, st goto tnt_error; } fk->field_count = child_cols_count; - fk->child_id = child_id; - fk->parent_id = parent_id; + fk->child_id = child_space != NULL ? child_space->def->id : 0; + fk->parent_id = parent_space != NULL ? parent_space->def->id : 0; fk->is_deferred = is_deferred; fk->match = (enum fkey_match) ((actions >> 16) & 0xff); fk->on_update = (enum fkey_action) ((actions >> 8) & 0xff); @@ -2184,9 +2172,8 @@ sql_drop_foreign_key(struct Parse *parse_context, stru { assert(table != NULL && table->nSrc == 1); const char *table_name = table->a[0].zName; - uint32_t child_id = box_space_id_by_name(table_name, - strlen(table_name)); - if (child_id == BOX_ID_NIL) { + struct space *child = space_by_name(table_name); + if (child == NULL) { diag_set(ClientError, ER_NO_SUCH_SPACE, table_name); parse_context->rc = SQL_TARANTOOL_ERROR; parse_context->nErr++; @@ -2195,7 +2182,8 @@ sql_drop_foreign_key(struct Parse *parse_context, stru char *constraint_name = sqlite3NameFromToken(parse_context->db, constraint); if (constraint_name != NULL) - vdbe_emit_fkey_drop(parse_context, constraint_name, child_id); + vdbe_emit_fkey_drop(parse_context, constraint_name, + child->def->id); } /* @@ -2415,8 +2403,8 @@ sql_create_index(struct Parse *parse, struct Token *to if (tbl_name != NULL) { assert(token != NULL && token->z != NULL); const char *name = tbl_name->a[0].zName; - uint32_t space_id = box_space_id_by_name(name, strlen(name)); - if (space_id == BOX_ID_NIL) { + space = space_by_name(name); + if (space == NULL) { if (! if_not_exist) { diag_set(ClientError, ER_NO_SUCH_SPACE, name); parse->rc = SQL_TARANTOOL_ERROR; @@ -2424,8 +2412,6 @@ sql_create_index(struct Parse *parse, struct Token *to } goto exit_create_index; } - space = space_by_id(space_id); - assert(space != NULL); def = space->def; } else { if (parse->pNewTable == NULL) @@ -2738,16 +2724,15 @@ sql_drop_index(struct Parse *parse_context, struct Src sqlite3VdbeCountChanges(v); assert(index_name_list->nSrc == 1); assert(table_token->n > 0); - uint32_t space_id = box_space_id_by_name(table_name, - strlen(table_name)); - if (space_id == BOX_ID_NIL) { + struct space *space = space_by_name(table_name); + if (space == NULL) { if (!if_exists) sqlite3ErrorMsg(parse_context, "no such space: %s", table_name); goto exit_drop_index; } const char *index_name = index_name_list->a[0].zName; - uint32_t index_id = box_index_id_by_name(space_id, index_name, + uint32_t index_id = box_index_id_by_name(space->def->id, index_name, strlen(index_name)); if (index_id == BOX_ID_NIL) { if (!if_exists) @@ -2755,8 +2740,6 @@ sql_drop_index(struct Parse *parse_context, struct Src table_name, index_name); goto exit_drop_index; } - struct space *space = space_by_id(space_id); - assert(space != NULL); struct index *index = space_index(space, index_id); assert(index != NULL); /* @@ -2779,7 +2762,7 @@ sql_drop_index(struct Parse *parse_context, struct Src sql_clear_stat_spaces(parse_context, table_name, index->def->name); int record_reg = ++parse_context->nMem; int space_id_reg = ++parse_context->nMem; - sqlite3VdbeAddOp2(v, OP_Integer, space_id, space_id_reg); + sqlite3VdbeAddOp2(v, OP_Integer, space->def->id, space_id_reg); sqlite3VdbeAddOp2(v, OP_Integer, index_id, space_id_reg + 1); sqlite3VdbeAddOp3(v, OP_MakeRecord, space_id_reg, 2, record_reg); sqlite3VdbeAddOp2(v, OP_SDelete, BOX_INDEX_ID, record_reg); blob - 8f6c76f684bc952ab082558becb9d9e4bfe51708 blob + 116832f906fa0daaaf7d326a432866e4f7567d95 --- src/box/sql/delete.c +++ src/box/sql/delete.c @@ -40,10 +40,8 @@ sql_lookup_table(struct Parse *parse, struct SrcList_i { assert(tbl_name != NULL); assert(tbl_name->pTab == NULL); - uint32_t space_id = box_space_id_by_name(tbl_name->zName, - strlen(tbl_name->zName)); - struct space *space = space_by_id(space_id); - if (space_id == BOX_ID_NIL || space == NULL) { + struct space *space = space_by_name(tbl_name->zName); + if (space == NULL) { sqlite3ErrorMsg(parse, "no such table: %s", tbl_name->zName); return NULL; } @@ -98,14 +96,11 @@ sql_table_truncate(struct Parse *parse, struct SrcList goto cleanup; const char *tab_name = tab_list->a->zName; - uint32_t space_id = box_space_id_by_name(tab_name, strlen(tab_name)); - if (space_id == BOX_ID_NIL) { + struct space *space = space_by_name(tab_name); + if (space == NULL) { diag_set(ClientError, ER_NO_SUCH_SPACE, tab_name); goto tarantool_error; } - struct space *space = space_cache_find(space_id); - assert(space != NULL); - if (! rlist_empty(&space->parent_fkey)) { const char *err_msg = tt_sprintf("can not truncate space '%s' because other " blob - 5862917f010d902d49d9afb4329c0becbcf32393 blob + eef27bcb92e86869100beef651697b896587b405 --- src/box/sql/insert.c +++ src/box/sql/insert.c @@ -1197,13 +1197,10 @@ xferOptimization(Parse * pParse, /* Parser context */ * we have to check the semantics. */ pItem = pSelect->pSrc->a; - uint32_t src_id = box_space_id_by_name(pItem->zName, - strlen(pItem->zName)); + struct space *src = space_by_name(pItem->zName); /* FROM clause does not contain a real table. */ - if (src_id == BOX_ID_NIL) + if (src == NULL) return 0; - struct space *src = space_by_id(src_id); - assert(src != NULL); /* Src and dest may not be the same table. */ if (src->def->id == dest->def->id) return 0; blob - 2e007f9423272e56f29ec60e0511f7ba1bbba633 blob + 9ece82acd95f3329b05ba195cdb24b23725030c0 --- src/box/sql/pragma.c +++ src/box/sql/pragma.c @@ -257,11 +257,9 @@ sql_pragma_table_info(struct Parse *parse, const char { if (tbl_name == NULL) return; - uint32_t space_id = box_space_id_by_name(tbl_name, strlen(tbl_name)); - if (space_id == BOX_ID_NIL) + struct space *space = space_by_name(tbl_name); + if (space == NULL) return; - struct space *space = space_by_id(space_id); - assert(space != NULL); struct index *pk = space_index(space, 0); parse->nMem = 6; if (space->def->opts.is_view) @@ -336,14 +334,12 @@ sql_pragma_index_info(struct Parse *parse, const Pragm { if (idx_name == NULL || tbl_name == NULL) return; - uint32_t space_id = box_space_id_by_name(tbl_name, strlen(tbl_name)); - if (space_id == BOX_ID_NIL) + struct space *space = space_by_name(tbl_name); + if (space == NULL) return; - struct space *space = space_by_id(space_id); - assert(space != NULL); if (space->def->opts.sql == NULL) return; - uint32_t iid = box_index_id_by_name(space_id, idx_name, + uint32_t iid = box_index_id_by_name(space->def->id, idx_name, strlen(idx_name)); if (iid == BOX_ID_NIL) return; @@ -390,11 +386,9 @@ sql_pragma_index_list(struct Parse *parse, const char { if (tbl_name == NULL) return; - uint32_t space_id = box_space_id_by_name(tbl_name, strlen(tbl_name)); - if (space_id == BOX_ID_NIL) + struct space *space = space_by_name(tbl_name); + if (space == NULL) return; - struct space *space = space_by_id(space_id); - assert(space != NULL); parse->nMem = 5; struct Vdbe *v = sqlite3GetVdbe(parse); for (uint32_t i = 0; i < space->index_count; ++i) { @@ -516,15 +510,13 @@ sqlite3Pragma(Parse * pParse, Token * pId, /* First pa case PragTyp_COLLATION_LIST:{ int i = 0; - uint32_t space_id; - space_id = box_space_id_by_name("_collation", - (uint32_t) strlen("_collation")); + struct space *space = space_by_name("_collation"); char key_buf[16]; /* 16 is enough to encode 0 len array */ char *key_end = key_buf; key_end = mp_encode_array(key_end, 0); box_tuple_t *tuple; box_iterator_t* iter; - iter = box_index_iterator(space_id, 0,ITER_ALL, key_buf, key_end); + iter = box_index_iterator(space->def->id, 0,ITER_ALL, key_buf, key_end); int rc = box_iterator_next(iter, &tuple); (void) rc; assert(rc == 0); @@ -544,11 +536,9 @@ sqlite3Pragma(Parse * pParse, Token * pId, /* First pa case PragTyp_FOREIGN_KEY_LIST:{ if (zRight == NULL) break; - uint32_t space_id = box_space_id_by_name(zRight, - strlen(zRight)); - if (space_id == BOX_ID_NIL) + struct space *space = space_by_name(zRight); + if (space == NULL) break; - struct space *space = space_by_id(space_id); int i = 0; pParse->nMem = 8; struct fkey *fkey; blob - 60ba88fea61089d584267ca67bd4d9d4a8dad5e8 blob + af1607db7412a10e5fd2a35f9412ee829da9a51f --- test/box/stat.result +++ test/box/stat.result @@ -24,7 +24,7 @@ box.stat.REPLACE.total ... box.stat.SELECT.total --- -- 2 +- 1 ... box.stat.ERROR.total --- @@ -59,7 +59,7 @@ box.stat.REPLACE.total ... box.stat.SELECT.total --- -- 5 +- 4 ... -- check exceptions space:get('Impossible value') @@ -118,7 +118,7 @@ box.stat.REPLACE.total ... box.stat.SELECT.total --- -- 2 +- 1 ... box.stat.ERROR.total ---