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 08B97247E9 for ; Wed, 9 Jan 2019 07:13:30 -0500 (EST) 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 b9gi1VfygL_y for ; Wed, 9 Jan 2019 07:13:29 -0500 (EST) Received: from smtp50.i.mail.ru (smtp50.i.mail.ru [94.100.177.110]) (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 B486C247D6 for ; Wed, 9 Jan 2019 07:13:29 -0500 (EST) From: Nikita Pettik Subject: [tarantool-patches] [PATCH 4/6] sql: refactor getNewIid() function Date: Wed, 9 Jan 2019 14:13:18 +0200 Message-Id: In-Reply-To: References: In-Reply-To: References: 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, Nikita Pettik This commit includes no functional changes. Lets simply rewrite getNewIid() function according to Tarantool codestyle. Part of #3914 --- src/box/sql/build.c | 72 ++++++++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/src/box/sql/build.c b/src/box/sql/build.c index 9d31cb736..ca9d469fd 100644 --- a/src/box/sql/build.c +++ b/src/box/sql/build.c @@ -2044,45 +2044,43 @@ sql_drop_foreign_key(struct Parse *parse_context, struct Token *constraint) sqlite3VdbeChangeP5(sqlite3GetVdbe(parse_context), OPFLAG_NCHANGE); } -/* - * Generate code to determine next free Iid in the space identified by - * the iSpaceId. Return register number holding the result. +/** + * Generate code to determine next free index id in + * the space identified by the @space_id. + * Return register holding the result. + * + * Overall VDBE program logic is following: + * + * 1 Seek for space id in _index, goto l1 if seeks fails. + * 2 Goto l2. + * 3 l1: Halt. + * 4 l2: Fetch index id from _index record. */ static int -getNewIid(Parse * pParse, int iSpaceId, int iCursor) +generate_index_id(struct Parse *parse, uint32_t space_id, int cursor) { - Vdbe *v = sqlite3GetVdbe(pParse); - int iRes = ++pParse->nMem; - int iKey = ++pParse->nMem; - int iSeekInst, iGotoInst; - - sqlite3VdbeAddOp2(v, OP_Integer, iSpaceId, iKey); - iSeekInst = sqlite3VdbeAddOp4Int(v, OP_SeekLE, iCursor, 0, iKey, 1); - sqlite3VdbeAddOp4Int(v, OP_IdxLT, iCursor, 0, iKey, 1); - - /* - * If SeekLE succeeds, the control falls through here, skipping - * IdxLt. - * - * If it fails (no entry with the given key prefix: invalid spaceId) - * VDBE jumps to the next code block (jump target is IMM, fixed up - * later with sqlite3VdbeJumpHere()). - */ - iGotoInst = sqlite3VdbeAddOp0(v, OP_Goto); /* Jump over Halt */ - - /* Invalid spaceId detected. Halt now. */ - sqlite3VdbeJumpHere(v, iSeekInst); - sqlite3VdbeJumpHere(v, iSeekInst + 1); - sqlite3VdbeAddOp4(v, - OP_Halt, SQLITE_ERROR, ON_CONFLICT_ACTION_FAIL, 0, - sqlite3MPrintf(pParse->db, "Invalid space id: %d", - iSpaceId), P4_DYNAMIC); - - /* Fetch iid from the row and ++it. */ - sqlite3VdbeJumpHere(v, iGotoInst); - sqlite3VdbeAddOp3(v, OP_Column, iCursor, 1, iRes); - sqlite3VdbeAddOp2(v, OP_AddImm, iRes, 1); - return iRes; + struct Vdbe *v = sqlite3GetVdbe(parse); + int key_reg = ++parse->nMem; + + sqlite3VdbeAddOp2(v, OP_Integer, space_id, key_reg); + int seek_adr = sqlite3VdbeAddOp4Int(v, OP_SeekLE, cursor, 0, + key_reg, 1); + sqlite3VdbeAddOp4Int(v, OP_IdxLT, cursor, 0, key_reg, 1); + /* Jump over Halt block. */ + int goto_succ_addr = sqlite3VdbeAddOp0(v, OP_Goto); + /* Invalid space id handling block starts here. */ + sqlite3VdbeJumpHere(v, seek_adr); + sqlite3VdbeJumpHere(v, seek_adr + 1); + sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_ERROR, ON_CONFLICT_ACTION_FAIL, 0, + sqlite3MPrintf(parse->db, "Invalid space id: %d", + space_id), P4_DYNAMIC); + + sqlite3VdbeJumpHere(v, goto_succ_addr); + /* Fetch iid from the row and increment it. */ + int iid_reg = ++parse->nMem; + sqlite3VdbeAddOp3(v, OP_Column, cursor, 1, iid_reg); + sqlite3VdbeAddOp2(v, OP_AddImm, iid_reg, 1); + return iid_reg; } /** @@ -2523,7 +2521,7 @@ sql_create_index(struct Parse *parse, struct Token *token, P4_SPACEPTR); sqlite3VdbeChangeP5(vdbe, OPFLAG_SEEKEQ); - int index_id = getNewIid(parse, def->id, cursor); + int index_id = generate_index_id(parse, def->id, cursor); sqlite3VdbeAddOp1(vdbe, OP_Close, cursor); vdbe_emit_create_index(parse, def, index->def, def->id, index_id); -- 2.15.1