diff --git a/src/box/lua/misc.lua b/src/box/lua/misc.lua index b9172b05f7fa933283a95f970df5445303e75748..c0356a249f523d19210c635dfa3f5fc65ebe5751 100644 --- a/src/box/lua/misc.lua +++ b/src/box/lua/misc.lua @@ -1,52 +1,5 @@ -- misc.lua (internal file) --- --- Simple counter. --- -box.counter = {} - --- --- Increment counter identified by primary key. --- Create counter if not exists. --- Returns updated value of the counter. --- -function box.counter.inc(spaceno, key) - local cnt_index = #key - local s = box.space[spaceno] - - local tuple - while true do - tuple = s:update(key, {{'+', cnt_index, 1}}) - if tuple ~= nil then break end - local data = key - table.insert(data, 1) - tuple = s:insert(data) - if tuple ~= nil then break end - end - return tuple[cnt_index] -end - --- --- Decrement counter identified by primary key. --- Delete counter if it decreased to zero. --- Returns updated value of the counter. --- -function box.counter.dec(spaceno, key) - local cnt_index = #key - local s = box.space[spaceno] - - local tuple = s:get(key) - if tuple == nil then return 0 end - if tuple[cnt_index] == 1 then - s:delete(key) - return 0 - else - tuple = s:update(key, {{'-', cnt_index, 1}}) - return tuple[cnt_index] - end -end - - -- This function automatically called by console client -- on help command. function help() diff --git a/src/box/lua/schema.lua b/src/box/lua/schema.lua index f952c1448193eb0c6a899c517740e7f5d7ab1658..5320337f1720b88d9617078132a07e99a85a02d7 100644 --- a/src/box/lua/schema.lua +++ b/src/box/lua/schema.lua @@ -485,6 +485,46 @@ function box.schema.space.bless(space) table.insert(tuple, 1, max + 1) return space:insert(tuple) end + + -- + -- Increment counter identified by primary key. + -- Create counter if not exists. + -- Returns updated value of the counter. + -- + space_mt.inc = function(space, key) + local key = keify(key) + local cnt_index = #key + local tuple + while true do + tuple = space:update(key, {{'+', cnt_index, 1}}) + if tuple ~= nil then break end + local data = key + table.insert(data, 1) + tuple = space:insert(data) + if tuple ~= nil then break end + end + return tuple[cnt_index] + end + + -- + -- Decrement counter identified by primary key. + -- Delete counter if it decreased to zero. + -- Returns updated value of the counter. + -- + space_mt.dec = function(space, key) + local key = keify(key) + local cnt_index = #key + local tuple = space:get(key) + if tuple == nil then return 0 end + if tuple[cnt_index] == 1 then + space:delete(key) + return 0 + else + tuple = space:update(key, {{'-', cnt_index, 1}}) + return tuple[cnt_index] + end + end + space_mt.pairs = function(space, key) if space.index[0] == nil then -- empty space without indexes, return empty iterator diff --git a/test/box/misc.result b/test/box/misc.result index 85762430ad92893e9fbfd7569f4a6ddb1f249caf..b6e031a74281772f9dace5fa5c7324534b7d6a3c 100644 --- a/test/box/misc.result +++ b/test/box/misc.result @@ -17,7 +17,6 @@ t = {} for n in pairs(box) do table.insert(t, tostring(n)) end table.sort(t) t --- - - cfg - - counter - error - index - info @@ -401,11 +400,11 @@ bit.bor(1, 2) --- - 3 ... --- A test case for box.counter +-- A test case for space:inc and space:dec space = box.space.tweedledum --- ... -box.counter.inc(space.id, {1}) +space:inc{1} --- - 1 ... @@ -413,11 +412,11 @@ space:get{1} --- - [1, 1] ... -box.counter.inc(space.id, {1}) +space:inc{1} --- - 2 ... -box.counter.inc(space.id, {1}) +space:inc{1} --- - 3 ... @@ -425,15 +424,15 @@ space:get{1} --- - [1, 3] ... -box.counter.dec(space.id, {1}) +space:dec{1} --- - 2 ... -box.counter.dec(space.id, {1}) +space:dec{1} --- - 1 ... -box.counter.dec(space.id, {1}) +space:dec{1} --- - 0 ... diff --git a/test/box/misc.test.lua b/test/box/misc.test.lua index d7dbf1b100f731cccae64d3e333fbb2be897c58f..8b7ff88588fa88d2edda430ab482c8fb1c7cb77e 100644 --- a/test/box/misc.test.lua +++ b/test/box/misc.test.lua @@ -129,16 +129,16 @@ bit.lshift(1, 32) bit.band(1, 3) bit.bor(1, 2) --- A test case for box.counter +-- A test case for space:inc and space:dec space = box.space.tweedledum -box.counter.inc(space.id, {1}) +space:inc{1} space:get{1} -box.counter.inc(space.id, {1}) -box.counter.inc(space.id, {1}) +space:inc{1} +space:inc{1} space:get{1} -box.counter.dec(space.id, {1}) -box.counter.dec(space.id, {1}) -box.counter.dec(space.id, {1}) +space:dec{1} +space:dec{1} +space:dec{1} space:get{1} space:truncate()