From: Roman Khabibov <roman.habibov@tarantool.org> To: tarantool-patches@dev.tarantool.org Cc: v.shpilevoy@tarantool.org Subject: [Tarantool-patches] [PATCH 1/2] box: introduce constraint names hash table Date: Tue, 12 Nov 2019 17:02:58 +0300 [thread overview] Message-ID: <49805fa2211dfa3e5d92a34a740a3f9cf488c327.1573566885.git.roman.habibov@tarantool.org> (raw) In-Reply-To: <cover.1573566885.git.roman.habibov@tarantool.org> Add hash table and API for interaction with it to struct space. This hash table is needed to keep constraint names of a table together and check their duplicates. Needed for #3503 --- src/box/space.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/box/space.h | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/src/box/space.c b/src/box/space.c index 94716a414..c15b9f5b4 100644 --- a/src/box/space.c +++ b/src/box/space.c @@ -45,6 +45,7 @@ #include "iproto_constants.h" #include "schema.h" #include "ck_constraint.h" +#include "assoc.h" int access_check_space(struct space *space, user_access_t access) @@ -202,6 +203,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 +265,7 @@ space_delete(struct space *space) trigger_destroy(&space->before_replace); trigger_destroy(&space->on_replace); space_def_delete(space->def); + mh_strnptr_delete(space->constraint_names); /* * SQL Triggers should be deleted with * on_replace_dd_trigger on deletion from _trigger. @@ -617,6 +626,43 @@ space_remove_ck_constraint(struct space *space, struct ck_constraint *ck) } } +bool +space_has_constraint(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 true; + return false; +} + +int +space_put_constraint_name(struct space *space, const char *name) +{ + uint32_t len = strlen(name); + uint32_t hash = mh_strn_hash(name, len); + char *name_copy = malloc(len); + memcpy(name_copy, name, len); + const struct mh_strnptr_node_t name_node = { name_copy, len, hash, + NULL }; + 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_name(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..36a767421 100644 --- a/src/box/space.h +++ b/src/box/space.h @@ -234,6 +234,11 @@ struct space { * of parent constraints as well as child ones. */ uint64_t fk_constraint_mask; + /** + * Hash table with constraint names. Used for non-system + * spaces only. + */ + struct mh_strnptr_t *constraint_names; }; /** Initialize a base space instance. */ @@ -516,6 +521,41 @@ space_add_ck_constraint(struct space *space, struct ck_constraint *ck); void space_remove_ck_constraint(struct space *space, struct ck_constraint *ck); +/** + * Check if a constraint with @a name exists within @a space. + * + * @param space Space. + * @param name Constraint name. + * + * @retval true The constraint with @a name exists. + * @retval false Doesn't exist respectively. + */ +bool +space_has_constraint(struct space *space, const char *name); + +/** + * Put node with @a name of a constraint the the hash table of @a + * space. + * + * @param space Space. + * @param name Constraint name. + * + * @retval 0 Success. + * @retval -1 Memory allocation error. + */ +int +space_put_constraint_name(struct space *space, const char *name); + +/** + * Remove node with @a name of a constraint from the hash table of + * @a space. + * + * @param space Space. + * @param name Constraint name. + */ +void +space_drop_constraint_name(struct space *space, const char *name); + /* * Virtual method stubs. */ -- 2.21.0 (Apple Git-122)
next prev parent reply other threads:[~2019-11-12 14:03 UTC|newest] Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top 2019-11-12 14:02 [Tarantool-patches] [PATCH 0/2] Add constraint names hash table to space Roman Khabibov 2019-11-12 14:02 ` Roman Khabibov [this message] 2019-11-12 14:17 ` [Tarantool-patches] [PATCH 1/2] box: introduce constraint names hash table Cyrill Gorcunov 2019-11-12 22:30 ` Roman Khabibov 2019-11-12 23:04 ` Vladislav Shpilevoy 2019-11-12 14:02 ` [Tarantool-patches] [PATCH 2/2] sql: make constraint operations transactional Roman Khabibov 2019-11-12 23:04 ` Vladislav Shpilevoy 2019-11-12 23:04 ` [Tarantool-patches] [PATCH 0/2] Add constraint names hash table to space Vladislav Shpilevoy
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=49805fa2211dfa3e5d92a34a740a3f9cf488c327.1573566885.git.roman.habibov@tarantool.org \ --to=roman.habibov@tarantool.org \ --cc=tarantool-patches@dev.tarantool.org \ --cc=v.shpilevoy@tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH 1/2] box: introduce constraint names hash table' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox