From: olegrok@tarantool.org To: tarantool-patches@dev.tarantool.org, v.shpilevoy@tarantool.org, korablev@tarantool.org Cc: Oleg Babin <babinoleg@mail.ru> Subject: [Tarantool-patches] [PATCH 2/2] box: introduce "current" for sequence Date: Fri, 6 Mar 2020 14:46:43 +0300 [thread overview] Message-ID: <dd357243382b6dcd82245d7ecfcaf9eda9d5deb6.1583494653.git.babinoleg@mail.ru> (raw) In-Reply-To: <cover.1583494653.git.babinoleg@mail.ru> From: Oleg Babin <babinoleg@mail.ru> This patch introduces "current" function for sequences. It returns current value of specified sequence or throws an error if sequence is not initialized ("next" called at least once). Closes #4752 @TarantoolBot document Title: sequence:current() New function returns current value of sequence of throws an error if used before sequence initialization. Example ```lua sq = box.schema.sequence.create('test') --- ... sq:current() --- - error: Sequence is not initialized yet ... sq:next() --- - 1 ... sq:current() --- - 1 ... sq:set(42) --- ... sq:current() --- - 42 ... ``` --- Issue: https://github.com/tarantool/tarantool/issues/4752 Branch: https://github.com/tarantool/tarantool/tree/olegrok/4752-sequence-current src/box/box.cc | 14 ++++++++++++++ src/box/box.h | 12 ++++++++++++ src/box/lua/schema.lua | 4 ++++ src/box/lua/sequence.c | 12 ++++++++++++ test/box/sequence.result | 32 ++++++++++++++++++++++++++++++++ test/box/sequence.test.lua | 12 ++++++++++++ 6 files changed, 86 insertions(+) diff --git a/src/box/box.cc b/src/box/box.cc index 09dd67ab4..a5052dba4 100644 --- a/src/box/box.cc +++ b/src/box/box.cc @@ -1414,6 +1414,20 @@ box_sequence_next(uint32_t seq_id, int64_t *result) *result = value; return 0; } + +int +box_sequence_current(uint32_t seq_id, int64_t *result) +{ + struct sequence *seq = sequence_cache_find(seq_id); + if (seq == NULL) + return -1; + if (access_check_sequence(seq) != 0) + return -1; + if (sequence_get_value(seq, result) != 0) + return -1; + return 0; +} + int box_sequence_set(uint32_t seq_id, int64_t value) { diff --git a/src/box/box.h b/src/box/box.h index f37a945eb..8fb723630 100644 --- a/src/box/box.h +++ b/src/box/box.h @@ -423,6 +423,18 @@ box_truncate(uint32_t space_id); API_EXPORT int box_sequence_next(uint32_t seq_id, int64_t *result); +/** + * Returns current value of sequence. + * + * \param seq_id sequence identifier + * \param[out] result pointer to a variable where the current sequence + * value will be stored on success + * \retval -1 on error (check box_error_last()) + * \retval 0 on success + */ +API_EXPORT int +box_sequence_current(uint32_t seq_id, int64_t *result); + /** * Set a sequence value. * diff --git a/src/box/lua/schema.lua b/src/box/lua/schema.lua index f537c3cec..0cd747350 100644 --- a/src/box/lua/schema.lua +++ b/src/box/lua/schema.lua @@ -1812,6 +1812,10 @@ sequence_mt.next = function(self) return internal.sequence.next(self.id) end +sequence_mt.current = function(self) + return internal.sequence.current(self.id) +end + sequence_mt.set = function(self, value) return internal.sequence.set(self.id, value) end diff --git a/src/box/lua/sequence.c b/src/box/lua/sequence.c index bf0714c1a..ae4b5d3ce 100644 --- a/src/box/lua/sequence.c +++ b/src/box/lua/sequence.c @@ -49,6 +49,17 @@ lbox_sequence_next(struct lua_State *L) return 1; } +static int +lbox_sequence_current(struct lua_State *L) +{ + uint32_t seq_id = luaL_checkinteger(L, 1); + int64_t result; + if (box_sequence_current(seq_id, &result) != 0) + luaT_error(L); + luaL_pushint64(L, result); + return 1; +} + static int lbox_sequence_set(struct lua_State *L) { @@ -174,6 +185,7 @@ box_lua_sequence_init(struct lua_State *L) { static const struct luaL_Reg sequence_internal_lib[] = { {"next", lbox_sequence_next}, + {"current", lbox_sequence_current}, {"set", lbox_sequence_set}, {"reset", lbox_sequence_reset}, {NULL, NULL} diff --git a/test/box/sequence.result b/test/box/sequence.result index 0a6cfee2c..5a4cc8153 100644 --- a/test/box/sequence.result +++ b/test/box/sequence.result @@ -2289,3 +2289,35 @@ box.space._space_sequence:update({s.id}, {{'=', 2, t[2]}}) s:drop() --- ... +-- +-- gh-4752 current value of sequence +-- +sq = box.schema.sequence.create('test') +--- +... +sq:current() +--- +- error: Sequence is not initialized yet +... +sq:next() +--- +- 1 +... +sq:current() +--- +- 1 +... +sq:set(42) +--- +... +sq:current() +--- +- 42 +... +sq:current() +--- +- 42 +... +sq:drop() +--- +... diff --git a/test/box/sequence.test.lua b/test/box/sequence.test.lua index 8e00571e5..54011225e 100644 --- a/test/box/sequence.test.lua +++ b/test/box/sequence.test.lua @@ -780,3 +780,15 @@ pk = s:create_index('pk', {sequence = true}) t = box.space._space_sequence:get({s.id}) box.space._space_sequence:update({s.id}, {{'=', 2, t[2]}}) s:drop() + +-- +-- gh-4752 current value of sequence +-- +sq = box.schema.sequence.create('test') +sq:current() +sq:next() +sq:current() +sq:set(42) +sq:current() +sq:current() +sq:drop() -- 2.23.0
next prev parent reply other threads:[~2020-03-06 11:48 UTC|newest] Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-03-06 11:46 [Tarantool-patches] [PATCH 0/2] Introcude sequence.current olegrok 2020-03-06 11:46 ` [Tarantool-patches] [PATCH 1/2] box: introduce internal sequence error olegrok 2020-03-06 12:55 ` Nikita Pettik 2020-03-06 16:37 ` Oleg Babin 2020-03-06 11:46 ` olegrok [this message] 2020-03-06 13:00 ` [Tarantool-patches] [PATCH 2/2] box: introduce "current" for sequence Nikita 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=dd357243382b6dcd82245d7ecfcaf9eda9d5deb6.1583494653.git.babinoleg@mail.ru \ --to=olegrok@tarantool.org \ --cc=babinoleg@mail.ru \ --cc=korablev@tarantool.org \ --cc=tarantool-patches@dev.tarantool.org \ --cc=v.shpilevoy@tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH 2/2] box: introduce "current" for sequence' \ /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