[Tarantool-patches] [PATCH v2 1/2] box: introduce constraint names hash table
Roman Khabibov
roman.habibov at tarantool.org
Thu Nov 28 21:34:03 MSK 2019
Add hash table and API for interaction with it to struct space.
This hash table is needed to keep constraint of a table together
and check their duplicates by name.
Part of #3503
---
src/box/CMakeLists.txt | 1 +
src/box/constraint_def.c | 59 ++++++++++++++++++++++++++++
src/box/constraint_def.h | 83 ++++++++++++++++++++++++++++++++++++++++
src/box/space.c | 56 +++++++++++++++++++++++++++
src/box/space.h | 39 +++++++++++++++++++
5 files changed, 238 insertions(+)
create mode 100755 src/box/constraint_def.c
create mode 100755 src/box/constraint_def.h
diff --git a/src/box/CMakeLists.txt b/src/box/CMakeLists.txt
index 5cd5cba81..bf3895262 100644
--- a/src/box/CMakeLists.txt
+++ b/src/box/CMakeLists.txt
@@ -102,6 +102,7 @@ add_library(box STATIC
sequence.c
ck_constraint.c
fk_constraint.c
+ constraint_def.c
func.c
func_def.c
key_list.c
diff --git a/src/box/constraint_def.c b/src/box/constraint_def.c
new file mode 100755
index 000000000..914b36da3
--- /dev/null
+++ b/src/box/constraint_def.c
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2010-2019, Tarantool AUTHORS, please see AUTHORS
+ * file.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the
+ * following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "constraint_def.h"
+#include "assoc.h"
+#include "errcode.h"
+#include "diag.h"
+#include <string.h>
+#include <stdlib.h>
+
+struct constraint_def *
+constraint_def_new(uint32_t space_id, enum constraint_type type,
+ const char *name) {
+ uint32_t len = strlen(name);
+ uint32_t size = sizeof(struct constraint_def) + len + 1;
+ struct constraint_def *ret = malloc(size);
+ if (ret == NULL) {
+ diag_set(OutOfMemory, size, "malloc", "constraint_def");
+ return NULL;
+ }
+ ret->space_id = space_id;
+ ret->type = type;
+ memcpy(ret->name, name, len + 1);
+ return ret;
+}
+
+void
+constraint_def_free(struct constraint_def *def) {
+ free(def);
+}
diff --git a/src/box/constraint_def.h b/src/box/constraint_def.h
new file mode 100755
index 000000000..ef238eb46
--- /dev/null
+++ b/src/box/constraint_def.h
@@ -0,0 +1,83 @@
+#ifndef INCLUDES_BOX_CONSTRAINT_DEF_H
+#define INCLUDES_BOX_CONSTRAINT_DEF_H
+/*
+ * Copyright 2010-2019, Tarantool AUTHORS, please see AUTHORS
+ * file.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the
+ * following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <stdint.h>
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+enum constraint_type {
+ CONSTRAINT_TYPE_PK = 0,
+ CONSTRAINT_TYPE_UNIQUE = 1,
+ CONSTRAINT_TYPE_FK = 2,
+ CONSTRAINT_TYPE_CK = 3
+};
+
+struct constraint_def {
+ /** Space id. */
+ uint32_t space_id;
+ /** Constraint type. */
+ enum constraint_type type;
+ /** Zero-terminated string with name. */
+ char name[0];
+};
+
+/**
+ * Allocate memory and construct constraint def object.
+ *
+ * @param space_id Space id.
+ * @param type Constraint type.
+ * @param name Constraint name.
+ *
+ * @retval ptr Constraint def object.
+ * @retval NULL Memory allocation error.
+ */
+struct constraint_def *
+constraint_def_new(uint32_t space_id, enum constraint_type type,
+ const char *name);
+
+/**
+ * Free memory of constraint def object.
+ *
+ * @param def Constraint def.
+ */
+void
+constraint_def_free(struct constraint_def *def);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
diff --git a/src/box/space.c b/src/box/space.c
index 94716a414..91a7d575b 100644
--- a/src/box/space.c
+++ b/src/box/space.c
@@ -45,6 +45,8 @@
#include "iproto_constants.h"
#include "schema.h"
#include "ck_constraint.h"
+#include "assoc.h"
+#include "constraint_def.h"
int
access_check_space(struct space *space, user_access_t access)
@@ -202,6 +204,13 @@ space_create(struct space *space, struct engine *engine,
}
}
}
+
+ space->constraint_names = mh_strnptr_new();
+ if (space->constraint_names == NULL) {
+ diag_set(OutOfMemory, sizeof(*space->constraint_names),
+ "malloc", "constraint_names");
+ goto fail;
+ }
return 0;
fail_free_indexes:
@@ -257,6 +266,20 @@ space_delete(struct space *space)
trigger_destroy(&space->before_replace);
trigger_destroy(&space->on_replace);
space_def_delete(space->def);
+
+ /**
+ * Free memory of the constraint hash table. Destroy every
+ * constraint def object.
+ */
+ struct mh_strnptr_t *mh = space->constraint_names;
+ while (mh_size(mh) > 0) {
+ mh_int_t i = mh_first(mh);
+ struct constraint_def *def =
+ (struct constraint_def *) mh_strnptr_node(mh, i)->val;
+ constraint_def_free(def);
+ }
+ mh_strnptr_delete(mh);
+
/*
* SQL Triggers should be deleted with
* on_replace_dd_trigger on deletion from _trigger.
@@ -617,6 +640,39 @@ space_remove_ck_constraint(struct space *space, struct ck_constraint *ck)
}
}
+struct constraint_def *
+space_constraint_def_by_name(struct space *space, const char *name) {
+ uint32_t len = strlen(name);
+ mh_int_t pos = mh_strnptr_find_inp(space->constraint_names, name, len);
+ if (pos == mh_end(space->constraint_names))
+ return NULL;
+ return (struct constraint_def *)
+ mh_strnptr_node(space->constraint_names, pos)->val;
+}
+
+int
+space_put_constraint(struct space *space, struct constraint_def *def)
+{
+ uint32_t len = strlen(def->name);
+ uint32_t hash = mh_strn_hash(def->name, len);
+ const struct mh_strnptr_node_t name_node = { def->name, len, hash, def};
+ if (mh_strnptr_put(space->constraint_names, &name_node, NULL, NULL) ==
+ mh_end(space->constraint_names)) {
+ diag_set(OutOfMemory, sizeof(name_node), "malloc",
+ "constraint_names");
+ return -1;
+ }
+ return 0;
+}
+
+void
+space_drop_constraint(struct space *space, const char *name)
+{
+ mh_int_t pos = mh_strnptr_find_inp(space->constraint_names, name,
+ strlen(name));
+ mh_strnptr_del(space->constraint_names, pos, NULL);
+}
+
/* {{{ Virtual method stubs */
size_t
diff --git a/src/box/space.h b/src/box/space.h
index 7926aa65e..96396311e 100644
--- a/src/box/space.h
+++ b/src/box/space.h
@@ -52,6 +52,7 @@ struct port;
struct tuple;
struct tuple_format;
struct ck_constraint;
+struct constraint_def;
struct space_vtab {
/** Free a space instance. */
@@ -234,6 +235,8 @@ struct space {
* of parent constraints as well as child ones.
*/
uint64_t fk_constraint_mask;
+ /** Hash table with constraint names. */
+ struct mh_strnptr_t *constraint_names;
};
/** Initialize a base space instance. */
@@ -516,6 +519,42 @@ space_add_ck_constraint(struct space *space, struct ck_constraint *ck);
void
space_remove_ck_constraint(struct space *space, struct ck_constraint *ck);
+/**
+ * Find node with @a name in the constraint hash table of @a
+ * space.
+ *
+ * @param space Space.
+ * @param name Constraint name.
+ *
+ * @retval constraint_def object.
+ * @retval NULL Constraint doesn't exist.
+ */
+struct constraint_def *
+space_constraint_def_by_name(struct space *space, const char *name);
+
+/**
+ * Put node with @a def to the constraint hash table of @a space.
+ *
+ * @param space Space.
+ * @param def Constraint def.
+ *
+ * @retval 0 Success.
+ * @retval -1 Memory allocation error.
+ */
+int
+space_put_constraint(struct space *space, struct constraint_def *def);
+
+/**
+ * Remove node with @a name from the constraint hash table of @a
+ * space. But don't destroy the constraint def object binded to
+ * this @a name.
+ *
+ * @param space Space.
+ * @param name Constraint name.
+ */
+void
+space_drop_constraint(struct space *space, const char *name);
+
/*
* Virtual method stubs.
*/
--
2.21.0 (Apple Git-122)
More information about the Tarantool-patches
mailing list