[PATCH v5 01/12] box: refactor key_def_find routine

Kirill Shcherbatov kshcherbatov at tarantool.org
Mon Oct 29 09:56:30 MSK 2018


Refactored key_def_find routine to use key_part as a second
argument. Introduced key_def_find_by_fieldno helper to use in
scenarios where no key_part exists.
New API is more convenient for complex key_part that will appear
with JSON paths introduction.

Need for #1012
---
 src/box/alter.cc     |  7 ++++---
 src/box/key_def.c    | 19 ++++++++++++++-----
 src/box/key_def.h    |  9 ++++++++-
 src/box/sql/build.c  |  2 +-
 src/box/sql/pragma.c |  2 +-
 5 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/src/box/alter.cc b/src/box/alter.cc
index a6bb5a0..fc4d44e 100644
--- a/src/box/alter.cc
+++ b/src/box/alter.cc
@@ -3952,9 +3952,10 @@ on_replace_dd_fk_constraint(struct trigger * /* trigger*/, void *event)
 				continue;
 			uint32_t j;
 			for (j = 0; j < fk_def->field_count; ++j) {
-				if (key_def_find(idx->def->key_def,
-						 fk_def->links[j].parent_field)
-				    == NULL)
+				if (key_def_find_by_fieldno(idx->def->key_def,
+							    fk_def->links[j].
+							    parent_field) ==
+							    NULL)
 					break;
 			}
 			if (j != fk_def->field_count)
diff --git a/src/box/key_def.c b/src/box/key_def.c
index 3a560bb..2119ca3 100644
--- a/src/box/key_def.c
+++ b/src/box/key_def.c
@@ -519,12 +519,21 @@ key_def_decode_parts(struct key_part_def *parts, uint32_t part_count,
 }
 
 const struct key_part *
-key_def_find(const struct key_def *key_def, uint32_t fieldno)
+key_def_find_by_fieldno(const struct key_def *key_def, uint32_t fieldno)
+{
+	struct key_part part;
+	memset(&part, 0, sizeof(struct key_part));
+	part.fieldno = fieldno;
+	return key_def_find(key_def, &part);
+}
+
+const struct key_part *
+key_def_find(const struct key_def *key_def, const struct key_part *to_find)
 {
 	const struct key_part *part = key_def->parts;
 	const struct key_part *end = part + key_def->part_count;
 	for (; part != end; part++) {
-		if (part->fieldno == fieldno)
+		if (part->fieldno == to_find->fieldno)
 			return part;
 	}
 	return NULL;
@@ -536,7 +545,7 @@ key_def_contains(const struct key_def *first, const struct key_def *second)
 	const struct key_part *part = second->parts;
 	const struct key_part *end = part + second->part_count;
 	for (; part != end; part++) {
-		if (key_def_find(first, part->fieldno) == NULL)
+		if (key_def_find(first, part) == NULL)
 			return false;
 	}
 	return true;
@@ -553,7 +562,7 @@ key_def_merge(const struct key_def *first, const struct key_def *second)
 	const struct key_part *part = second->parts;
 	const struct key_part *end = part + second->part_count;
 	for (; part != end; part++) {
-		if (key_def_find(first, part->fieldno))
+		if (key_def_find(first, part) != NULL)
 			--new_part_count;
 	}
 
@@ -584,7 +593,7 @@ key_def_merge(const struct key_def *first, const struct key_def *second)
 	part = second->parts;
 	end = part + second->part_count;
 	for (; part != end; part++) {
-		if (key_def_find(first, part->fieldno))
+		if (key_def_find(first, part) != NULL)
 			continue;
 		key_def_set_part(new_def, pos++, part->fieldno, part->type,
 				 part->nullable_action, part->coll,
diff --git a/src/box/key_def.h b/src/box/key_def.h
index 20e79f9..cfed3f1 100644
--- a/src/box/key_def.h
+++ b/src/box/key_def.h
@@ -320,7 +320,14 @@ key_def_decode_parts(struct key_part_def *parts, uint32_t part_count,
  * If fieldno is not in index_def->parts returns NULL.
  */
 const struct key_part *
-key_def_find(const struct key_def *key_def, uint32_t fieldno);
+key_def_find_by_fieldno(const struct key_def *key_def, uint32_t fieldno);
+
+/**
+ * Returns the part in index_def->parts for the specified key part.
+ * If to_find is not in index_def->parts returns NULL.
+ */
+const struct key_part *
+key_def_find(const struct key_def *key_def, const struct key_part *to_find);
 
 /**
  * Check if key definition @a first contains all parts of
diff --git a/src/box/sql/build.c b/src/box/sql/build.c
index fb5d61c..7556a78 100644
--- a/src/box/sql/build.c
+++ b/src/box/sql/build.c
@@ -145,7 +145,7 @@ sql_space_column_is_in_pk(struct space *space, uint32_t column)
 	if (column < 63)
 		return (pk_mask & (((uint64_t) 1) << column)) != 0;
 	else if ((pk_mask & (((uint64_t) 1) << 63)) != 0)
-		return key_def_find(key_def, column) != NULL;
+		return key_def_find_by_fieldno(key_def, column) != NULL;
 	return false;
 }
 
diff --git a/src/box/sql/pragma.c b/src/box/sql/pragma.c
index 9ece82a..cc99eb1 100644
--- a/src/box/sql/pragma.c
+++ b/src/box/sql/pragma.c
@@ -273,7 +273,7 @@ sql_pragma_table_info(struct Parse *parse, const char *tbl_name)
 			k = 1;
 		} else {
 			struct key_def *kdef = pk->def->key_def;
-			k = key_def_find(kdef, i) - kdef->parts + 1;
+			k = key_def_find_by_fieldno(kdef, i) - kdef->parts + 1;
 		}
 		sqlite3VdbeMultiLoad(v, 1, "issisi", i, field->name,
 				     field_type_strs[field->type],
-- 
2.7.4




More information about the Tarantool-patches mailing list