From 9504642a39b5fe022232cf16c9f3a15faa514d7c Mon Sep 17 00:00:00 2001
From: Roman Tsisyk <roman@tsisyk.com>
Date: Wed, 4 Jun 2014 14:14:10 +0400
Subject: [PATCH] Fix #327: Convert box.counter to space:inc() and space:dec()

---
 src/box/lua/misc.lua   | 47 ------------------------------------------
 src/box/lua/schema.lua | 40 +++++++++++++++++++++++++++++++++++
 test/box/misc.result   | 15 +++++++-------
 test/box/misc.test.lua | 14 ++++++-------
 4 files changed, 54 insertions(+), 62 deletions(-)

diff --git a/src/box/lua/misc.lua b/src/box/lua/misc.lua
index b9172b05f7..c0356a249f 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 f952c14481..5320337f17 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 85762430ad..b6e031a742 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 d7dbf1b100..8b7ff88588 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()
 
-- 
GitLab