[Tarantool-patches] [PATCH 1/2] sql: refactor index drop code

Roman Khabibov roman.habibov at tarantool.org
Fri Jan 24 17:21:23 MSK 2020


Wrap index drop opcode emitting into single function.

Part of #4120
---
 src/box/sql/build.c | 52 ++++++++++++++++++++++++---------------------
 1 file changed, 28 insertions(+), 24 deletions(-)

diff --git a/src/box/sql/build.c b/src/box/sql/build.c
index bc50ecbfa..58a6a8a6b 100644
--- a/src/box/sql/build.c
+++ b/src/box/sql/build.c
@@ -1469,6 +1469,26 @@ vdbe_emit_stat_space_clear(struct Parse *parse, const char *stat_table_name,
 	sql_table_delete_from(parse, src_list, where);
 }
 
+/**
+ * Generate VDBE program to remove entry with @a index_id and @a
+ * space_id from _index space.
+ *
+ * Note: index_id_reg have to be allocated immediately after
+ * space_id_reg.
+ */
+static void
+vdbe_emit_index_drop(struct Parse *parse_context, uint32_t index_id,
+		     uint32_t index_id_reg, uint32_t space_id_reg)
+{
+	struct Vdbe *v = sqlGetVdbe(parse_context);
+	assert(v != NULL);
+	int record_reg = ++parse_context->nMem;
+	sqlVdbeAddOp2(v, OP_Integer, index_id, index_id_reg);
+	sqlVdbeAddOp3(v, OP_MakeRecord, space_id_reg, 2, record_reg);
+	sqlVdbeAddOp2(v, OP_SDelete, BOX_INDEX_ID, record_reg);
+	VdbeComment((v, "Remove index iid =  %u", index_id));
+}
+
 /**
  * Generate VDBE program to remove entry from _fk_constraint space.
  *
@@ -1680,23 +1700,14 @@ sql_code_drop_table(struct Parse *parse_context, struct space *space,
 			 * secondary exist.
 			 */
 			for (uint32_t i = 1; i < index_count; ++i) {
-				sqlVdbeAddOp2(v, OP_Integer,
-						  space->index[i]->def->iid,
-						  index_id_reg);
-				sqlVdbeAddOp3(v, OP_MakeRecord,
-						  space_id_reg, 2, idx_rec_reg);
-				sqlVdbeAddOp2(v, OP_SDelete, BOX_INDEX_ID,
-						  idx_rec_reg);
-				VdbeComment((v,
-					     "Remove secondary index iid = %u",
-					     space->index[i]->def->iid));
+				vdbe_emit_index_drop(parse_context,
+						     space->index[i]->def->iid,
+						     index_id_reg,
+						     space_id_reg);
 			}
 		}
-		sqlVdbeAddOp2(v, OP_Integer, 0, index_id_reg);
-		sqlVdbeAddOp3(v, OP_MakeRecord, space_id_reg, 2,
-				  idx_rec_reg);
-		sqlVdbeAddOp2(v, OP_SDelete, BOX_INDEX_ID, idx_rec_reg);
-		VdbeComment((v, "Remove primary index"));
+		vdbe_emit_index_drop(parse_context, 0, index_id_reg,
+				     space_id_reg);
 	}
 	/* Delete records about the space from the _truncate. */
 	sqlVdbeAddOp3(v, OP_MakeRecord, space_id_reg, 1, idx_rec_reg);
@@ -2704,18 +2715,11 @@ sql_drop_index(struct Parse *parse_context)
 		goto exit_drop_index;
 	}
 
-	/*
-	 * Generate code to remove entry from _index space
-	 * But firstly, delete statistics since schema
-	 * changes after DDL.
-	 */
-	int record_reg = ++parse_context->nMem;
 	int space_id_reg = ++parse_context->nMem;
 	int index_id_reg = ++parse_context->nMem;
 	sqlVdbeAddOp2(v, OP_Integer, space->def->id, space_id_reg);
-	sqlVdbeAddOp2(v, OP_Integer, index_id, index_id_reg);
-	sqlVdbeAddOp3(v, OP_MakeRecord, space_id_reg, 2, record_reg);
-	sqlVdbeAddOp2(v, OP_SDelete, BOX_INDEX_ID, record_reg);
+	vdbe_emit_index_drop(parse_context, index_id, index_id_reg,
+			     space_id_reg);
 	sqlVdbeChangeP5(v, OPFLAG_NCHANGE);
  exit_drop_index:
 	sqlSrcListDelete(db, table_list);
-- 
2.21.0 (Apple Git-122)



More information about the Tarantool-patches mailing list