forked from avral/golosnotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
counters.lua
39 lines (35 loc) · 1.15 KB
/
counters.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
require 'table_utils'
row_len = 26
function migrate_tuple(space, account, cur_len)
if cur_len < row_len then
for i=cur_len+1,row_len do
space:update(account, {{'!', i, 0}})
end
end
end
function counter_add(account, scope)
-- print('counter_add -->', account, scope)
local space = box.space.counters
local res = space:select{account}
if #res > 0 then
local tuple = res[1]
migrate_tuple(space, account, #tuple)
space:update(account, {{'+', 2, 1}, {'+', scope + 2, 1}})
else
local tuple = {account, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
tuple[scope + 2] = 1;
space:insert(tuple)
end
end
function counter_read(account, scope)
-- print('counter_read -->', account, scope)
local space = box.space.counters
local res = space:select{account}
if #res == 0 then return nil end
local tuple = res[1]
local count = tuple[scope + 2]
if count == nil or count <= 0 then return tuple end
migrate_tuple(space, account, #tuple)
local res = space:update(account, {{'-', 2, count}, {'=', scope + 2, 0}})
return res
end