From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 71C1929170 for ; Fri, 1 Jun 2018 16:24:47 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id s-cuch54sBi3 for ; Fri, 1 Jun 2018 16:24:47 -0400 (EDT) Received: from smtpng3.m.smailru.net (smtpng3.m.smailru.net [94.100.177.149]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 293F4290CC for ; Fri, 1 Jun 2018 16:24:47 -0400 (EDT) Subject: [tarantool-patches] Re: [PATCH v1 3/4] box: introduce box_space_id_by_name References: <95c32aa69c7040aa5a63164e19de8ac8f06d9ce3.1527765756.git.kshcherbatov@tarantool.org> <91765db9-575c-9d10-c678-ff1f3b230735@tarantool.org> From: Kirill Shcherbatov Message-ID: Date: Fri, 1 Jun 2018 23:24:45 +0300 MIME-Version: 1.0 In-Reply-To: <91765db9-575c-9d10-c678-ff1f3b230735@tarantool.org> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-subscribe: List-owner: List-post: List-archive: To: tarantool-patches@freelists.org Cc: "v.shpilevoy@tarantool.org" > 1. The comment now is wrong - space_id_by_name will be used exactly for > non-public API. > 2. space_id_by_name must not take system_space_id as an argument. It must > always use BOX_SPACE_ID. > * Return 0 on success, -1 on error. > * @param[out] space_id BOX_ID_NIL - not found. Else space_id is > * saved. > 3. Not _vspace. > 4. space_id_by_name is not public function. You must not put it inside > /** \cond public */. > 5. Why can not you just port schema_find_id to C? I've ported schema_find_id to C. This required change it's signature. diff --git a/src/box/schema.cc b/src/box/schema.cc index 2ddf920..c5055ee 100644 --- a/src/box/schema.cc +++ b/src/box/schema.cc @@ -224,28 +224,53 @@ sc_space_new(uint32_t id, const char *name, struct key_def *key_def, uint32_t schema_find_id(uint32_t system_space_id, uint32_t index_id, - const char *name, uint32_t len) + const char *name, uint32_t len, uint32_t *space_id) { - if (len > BOX_NAME_MAX) - return BOX_ID_NIL; - struct space *space = space_cache_find_xc(system_space_id); - struct index *index = index_find_system_xc(space, index_id); + *space_id = BOX_ID_NIL; + if (len > BOX_NAME_MAX) { + diag_set(SystemError, + "name length %d is greater than BOX_NAME_MAX", len); + return -1; + } + struct space *space = space_cache_find(system_space_id); + if (space == NULL) + return -1; + if (!space_is_memtx(space)) { + diag_set(ClientError, ER_UNSUPPORTED, + space->engine->name, "system data"); + return -1; + } + struct index *index = index_find(space, index_id); + if (index == NULL) + return -1; uint32_t size = mp_sizeof_str(len); struct region *region = &fiber()->gc; uint32_t used = region_used(region); - char *key = (char *) region_alloc_xc(region, size); - auto guard = make_scoped_guard([=] { region_truncate(region, used); }); + char *key = (char *)region_alloc(region, size); + if (key == NULL) { + diag_set(OutOfMemory, size, "region", "new slab"); + return -1; + } mp_encode_str(key, name, len); - - struct iterator *it = index_create_iterator_xc(index, ITER_EQ, key, 1); - IteratorGuard iter_guard(it); - - struct tuple *tuple = iterator_next_xc(it); + struct iterator *it = index_create_iterator(index, ITER_EQ, key, 1); + if (it == NULL) { + region_truncate(region, used); + return -1; + } + int rc = 0; + struct tuple *tuple; + if (iterator_next(it, &tuple) != 0) { + rc = -1; + goto cleanup; + } if (tuple) { /* id is always field #1 */ - return tuple_field_u32_xc(tuple, 0); + *space_id = tuple_field_u32_xc(tuple, 0); } - return BOX_ID_NIL; +cleanup: + iterator_delete(it); + region_truncate(region, used); + return rc; } /** diff --git a/src/box/schema.h b/src/box/schema.h index 1f7414f..099f9b0 100644 --- a/src/box/schema.h +++ b/src/box/schema.h @@ -102,6 +102,22 @@ space_is_system(struct space *space); struct sequence * sequence_by_id(uint32_t id); +/** + * Find space id by name in specified system space with index. + * + * @param system_space_id identifier of the system space. + * @param index_id identifier of the index to lookup. + * @param name space to lookup name. + * @param len length of a name. + * @param space_id[out] space_id BOX_ID_NIL - not found. + * + * @retval 0 on success. + * @retval -1 on error. + */ +uint32_t +schema_find_id(uint32_t system_space_id, uint32_t index_id, + const char *name, uint32_t len, uint32_t *space_id); + #if defined(__cplusplus) } /* extern "C" */ @@ -134,13 +150,6 @@ schema_free(); struct space *schema_space(uint32_t id); -/* - * Find object id by object name. - */ -uint32_t -schema_find_id(uint32_t system_space_id, uint32_t index_id, - const char *name, uint32_t len); - /** * Insert a new function or update the old one. * diff --git a/src/box/user.cc b/src/box/user.cc index 7fa66da..3e7f466 100644 --- a/src/box/user.cc +++ b/src/box/user.cc @@ -450,7 +450,9 @@ user_find(uint32_t uid) struct user * user_find_by_name(const char *name, uint32_t len) { - uint32_t uid = schema_find_id(BOX_USER_ID, 2, name, len); + uint32_t uid; + if (schema_find_id(BOX_USER_ID, 2, name, len, &uid) != 0) + diag_raise(); struct user *user = user_by_id(uid); if (user == NULL || user->def->type != SC_USER) { diag_set(ClientError, ER_NO_SUCH_USER, tt_cstr(name, len));