[Tarantool-patches] [PATCH 1.10 01/16] collation: allow to find a collation by a name

Alexander Turenko alexander.turenko at tarantool.org
Wed Sep 23 04:40:14 MSK 2020


Module API key part structure, which will be introduced in a next
commit, holds a collation name. We need to transform it to a collation
id in order to create a key definition.

The change is backported from commit 1.8.3-58-g8629bd897 ('Add search
collations by name') with aware of changes made after the commit (up to
2.6.0-78-gc1f72aeb7).

Part of #5273

(backported from commit 8629bd8979927f8befede9a028eed0564a92a558)
---
 src/box/coll_id_cache.c | 50 ++++++++++++++++++++++++++++++++++++-----
 src/box/coll_id_cache.h |  6 +++++
 2 files changed, 50 insertions(+), 6 deletions(-)

diff --git a/src/box/coll_id_cache.c b/src/box/coll_id_cache.c
index 122863937..45d295eb9 100644
--- a/src/box/coll_id_cache.c
+++ b/src/box/coll_id_cache.c
@@ -33,6 +33,8 @@
 #include "diag.h"
 #include "assoc.h"
 
+/** mhash table (name, len -> collation) */
+static struct mh_strnptr_t *coll_cache_name = NULL;
 /** mhash table (id -> collation) */
 static struct mh_i32ptr_t *coll_id_cache = NULL;
 
@@ -45,12 +47,20 @@ coll_id_cache_init()
 			 "coll_id_cache");
 		return -1;
 	}
+	coll_cache_name = mh_strnptr_new();
+	if (coll_cache_name == NULL) {
+		diag_set(OutOfMemory, sizeof(*coll_cache_name), "malloc",
+			 "coll_cache_name");
+		mh_i32ptr_delete(coll_id_cache);
+		return -1;
+	}
 	return 0;
 }
 
 void
 coll_id_cache_destroy()
 {
+	mh_strnptr_delete(coll_cache_name);
 	mh_i32ptr_delete(coll_id_cache);
 }
 
@@ -60,12 +70,27 @@ coll_id_cache_replace(struct coll_id *coll_id, struct coll_id **replaced_id)
 	const struct mh_i32ptr_node_t id_node = {coll_id->id, coll_id};
 	struct mh_i32ptr_node_t repl_id_node = {0, NULL};
 	struct mh_i32ptr_node_t *prepl_id_node = &repl_id_node;
-	if (mh_i32ptr_put(coll_id_cache, &id_node, &prepl_id_node, NULL) ==
-	    mh_end(coll_id_cache)) {
+	mh_int_t i =
+		mh_i32ptr_put(coll_id_cache, &id_node, &prepl_id_node, NULL);
+	if (i == mh_end(coll_id_cache)) {
 		diag_set(OutOfMemory, sizeof(id_node), "malloc",
 			 "coll_id_cache");
 		return -1;
 	}
+
+	uint32_t hash = mh_strn_hash(coll_id->name, coll_id->name_len);
+	const struct mh_strnptr_node_t name_node =
+		{ coll_id->name, coll_id->name_len, hash, coll_id };
+	struct mh_strnptr_node_t repl_name_node = { NULL, 0, 0, NULL };
+	struct mh_strnptr_node_t *prepl_node_name = &repl_name_node;
+	if (mh_strnptr_put(coll_cache_name, &name_node, &prepl_node_name,
+			   NULL) == mh_end(coll_id_cache)) {
+		diag_set(OutOfMemory, sizeof(name_node), "malloc",
+			 "coll_cache_name");
+		mh_i32ptr_del(coll_id_cache, i, NULL);
+		return -1;
+	}
+	assert(repl_id_node.val == repl_name_node.val);
 	assert(repl_id_node.val == NULL);
 	*replaced_id = repl_id_node.val;
 	return 0;
@@ -74,10 +99,11 @@ coll_id_cache_replace(struct coll_id *coll_id, struct coll_id **replaced_id)
 void
 coll_id_cache_delete(const struct coll_id *coll_id)
 {
-	mh_int_t i = mh_i32ptr_find(coll_id_cache, coll_id->id, NULL);
-	if (i == mh_end(coll_id_cache))
-		return;
-	mh_i32ptr_del(coll_id_cache, i, NULL);
+	mh_int_t id_i = mh_i32ptr_find(coll_id_cache, coll_id->id, NULL);
+	mh_i32ptr_del(coll_id_cache, id_i, NULL);
+	mh_int_t name_i = mh_strnptr_find_inp(coll_cache_name, coll_id->name,
+					      coll_id->name_len);
+	mh_strnptr_del(coll_cache_name, name_i, NULL);
 }
 
 struct coll_id *
@@ -88,3 +114,15 @@ coll_by_id(uint32_t id)
 		return NULL;
 	return mh_i32ptr_node(coll_id_cache, pos)->val;
 }
+
+/**
+ * Find a collation object by its name.
+ */
+struct coll_id *
+coll_by_name(const char *name, uint32_t len)
+{
+	mh_int_t pos = mh_strnptr_find_inp(coll_cache_name, name, len);
+	if (pos == mh_end(coll_cache_name))
+		return NULL;
+	return mh_strnptr_node(coll_cache_name, pos)->val;
+}
diff --git a/src/box/coll_id_cache.h b/src/box/coll_id_cache.h
index 4bbbc85de..e3115ddb5 100644
--- a/src/box/coll_id_cache.h
+++ b/src/box/coll_id_cache.h
@@ -71,6 +71,12 @@ coll_id_cache_delete(const struct coll_id *coll_id);
 struct coll_id *
 coll_by_id(uint32_t id);
 
+/**
+ * Find a collation object by its name.
+ */
+struct coll_id *
+coll_by_name(const char *name, uint32_t len);
+
 #if defined(__cplusplus)
 } /* extern "C" */
 #endif /* defined(__cplusplus) */
-- 
2.25.0



More information about the Tarantool-patches mailing list