Tarantool development patches archive
 help / color / mirror / Atom feed
From: Kirill Shcherbatov <kshcherbatov@tarantool.org>
To: tarantool-patches@freelists.org
Cc: v.shpilevoy@tarantool.org,
	Kirill Shcherbatov <kshcherbatov@tarantool.org>
Subject: [tarantool-patches] [PATCH v3 04/10] box: port schema_find_id to C
Date: Thu, 14 Jun 2018 20:32:22 +0300	[thread overview]
Message-ID: <b3c0df52affb05177e04913f28693bd48abc3e89.1528997527.git.kshcherbatov@tarantool.org> (raw)
In-Reply-To: <cover.1528997527.git.kshcherbatov@tarantool.org>
In-Reply-To: <cover.1528997527.git.kshcherbatov@tarantool.org>

Part of #3273.
---
 src/box/schema.cc | 54 ++++++++++++++++++++++++++++++++++++++----------------
 src/box/schema.h  | 23 ++++++++++++++++-------
 src/box/user.cc   |  4 +++-
 3 files changed, 57 insertions(+), 24 deletions(-)

diff --git a/src/box/schema.cc b/src/box/schema.cc
index 2ddf920..ccdc744 100644
--- a/src/box/schema.cc
+++ b/src/box/schema.cc
@@ -222,30 +222,52 @@ sc_space_new(uint32_t id, const char *name, struct key_def *key_def,
 	trigger_run_xc(&on_alter_space, space);
 }
 
-uint32_t
+int
 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 *object_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);
+	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", "key");
+		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);
-	if (tuple) {
+	struct iterator *it = index_create_iterator(index, ITER_EQ, key, 1);
+	if (it == NULL) {
+		region_truncate(region, used);
+		return -1;
+	}
+	struct tuple *tuple;
+	int rc = iterator_next(it, &tuple);
+	if (rc == 0) {
 		/* id is always field #1 */
-		return tuple_field_u32_xc(tuple, 0);
+		if (tuple == NULL)
+			*object_id = BOX_ID_NIL;
+		else if (tuple_field_u32(tuple, 0, object_id) != 0)
+			rc = -1;
 	}
-	return BOX_ID_NIL;
+	iterator_delete(it);
+	region_truncate(region, used);
+	return rc;
 }
 
 /**
diff --git a/src/box/schema.h b/src/box/schema.h
index 1f7414f..3e76604 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 object id by name in specified system space with index.
+ *
+ * @param system_space_id identifier of the system object.
+ * @param index_id identifier of the index to lookup.
+ * @param name of object to lookup.
+ * @param len length of a name.
+ * @param object_id[out] object_id or BOX_ID_NIL - not found.
+ *
+ * @retval 0 on success.
+ * @retval -1 on error.
+ */
+int
+schema_find_id(uint32_t system_space_id, uint32_t index_id, const char *name,
+	       uint32_t len, uint32_t *object_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));
-- 
2.7.4

  parent reply	other threads:[~2018-06-14 17:32 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-14 17:32 [tarantool-patches] [PATCH v3 00/10] sql: remove Triggers to server Kirill Shcherbatov
2018-06-14 17:32 ` [tarantool-patches] [PATCH v3 01/10] box: move db->pShchema init to sql_init Kirill Shcherbatov
2018-06-14 17:32 ` [tarantool-patches] [PATCH v3 10/10] sql: VDBE tests for trigger existence Kirill Shcherbatov
2018-06-14 19:27   ` [tarantool-patches] " Vladislav Shpilevoy
2018-06-15 16:21     ` Kirill Shcherbatov
2018-06-18 15:42       ` Vladislav Shpilevoy
2018-06-18 19:22         ` Kirill Shcherbatov
2018-06-19 10:24           ` Vladislav Shpilevoy
2018-06-19 15:12             ` Kirill Shcherbatov
2018-06-19 15:23               ` Vladislav Shpilevoy
2018-06-20  6:38                 ` Kirill Shcherbatov
2018-06-20  8:10                   ` Vladislav Shpilevoy
2018-06-20  8:24                     ` Kirill Shcherbatov
2018-06-14 17:32 ` [tarantool-patches] [PATCH v3 02/10] sql: fix leak on CREATE TABLE and resolve self ref Kirill Shcherbatov
2018-06-14 22:46   ` [tarantool-patches] " n.pettik
2018-06-15  9:25     ` Vladislav Shpilevoy
2018-06-14 17:32 ` [tarantool-patches] [PATCH v3 03/10] sql: fix sql len in tarantoolSqlite3RenameTrigger Kirill Shcherbatov
2018-06-14 17:32 ` Kirill Shcherbatov [this message]
2018-06-14 19:27   ` [tarantool-patches] Re: [PATCH v3 04/10] box: port schema_find_id to C Vladislav Shpilevoy
2018-06-14 22:46     ` n.pettik
2018-06-15  9:25       ` Vladislav Shpilevoy
2018-06-14 17:32 ` [tarantool-patches] [PATCH v3 05/10] sql: refactor sql_expr_compile to return AST Kirill Shcherbatov
2018-06-14 19:27   ` [tarantool-patches] " Vladislav Shpilevoy
2018-06-15 16:21     ` Kirill Shcherbatov
2018-06-14 17:32 ` [tarantool-patches] [PATCH v3 06/10] sql: move sqlite3DeleteTrigger to sql.h Kirill Shcherbatov
2018-06-14 19:27   ` [tarantool-patches] " Vladislav Shpilevoy
2018-06-14 17:32 ` [tarantool-patches] [PATCH v3 07/10] box: sort error codes in misc.test Kirill Shcherbatov
2018-06-14 17:32 ` [tarantool-patches] [PATCH v3 08/10] sql: new _trigger space format with space_id Kirill Shcherbatov
2018-06-14 19:27   ` [tarantool-patches] " Vladislav Shpilevoy
2018-06-15 16:21     ` Kirill Shcherbatov
2018-06-14 17:32 ` [tarantool-patches] [PATCH v3 09/10] sql: move Triggers to server Kirill Shcherbatov
2018-06-14 19:27   ` [tarantool-patches] " Vladislav Shpilevoy
2018-06-15 16:21     ` Kirill Shcherbatov
2018-06-18 15:42       ` Vladislav Shpilevoy
2018-06-18 19:22         ` Kirill Shcherbatov
2018-06-14 17:34 ` [tarantool-patches] Re: [PATCH v3 00/10] sql: remove " Kirill Shcherbatov
2018-06-20  8:35 ` Vladislav Shpilevoy
2018-06-28 15:47   ` n.pettik

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=b3c0df52affb05177e04913f28693bd48abc3e89.1528997527.git.kshcherbatov@tarantool.org \
    --to=kshcherbatov@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [tarantool-patches] [PATCH v3 04/10] box: port schema_find_id to C' \
    /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