-
Notifications
You must be signed in to change notification settings - Fork 23
/
rico.lua
155 lines (128 loc) · 5.37 KB
/
rico.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
--
-- rico.lua
--
--
-- Remote Imap collector COunters. Stores some scheduling information needed for collectors.
--
--
-- Space 0: Stores last collect time, last success collect time and so on.
-- Tuple: { coll_id (NUM), last_time (NUM), last_ok (NUM), old_threshold (NUM), last_fullsync (NUM), success_collects_num (NUM) }
-- Index 0: TREE { coll_id }
--
-- Space 1: Stores last collect time, last success collect time and so on for another type of collectors (pop3).
-- Tuple: { coll_id (NUM), last_time (NUM), last_ok (NUM), old_threshold (NUM), last_fullsync (NUM), success_collects_num (NUM) }
-- Index 0: TREE { coll_id }
--
function rico_get(coll_id)
coll_id = box.unpack('i', coll_id)
local t = box.select_limit(0, 0, 0, 1, coll_id)
if t ~=nil then return t:transform(0,1) end
end
function rico_get_lasttimes(from_coll_id, to_coll_id)
from_coll_id = box.unpack('i', from_coll_id)
to_coll_id = box.unpack('i', to_coll_id)
local result = {}
for t in box.space[0].index[0]:iterator(box.index.GE, from_coll_id) do
if box.unpack('i', t[0]) >= to_coll_id then break end
table.insert(result, { t:slice(0, 2) })
end
if #result > 0 then return result end
return unpack(result)
end
function rico_reset(coll_id)
coll_id = box.unpack('i', coll_id)
box.replace(0, coll_id, box.time(), box.time(), box.time(), box.time(), 0)
end
function rico_update_success(coll_id, need_update_old_threshold, need_update_last_fullsync)
coll_id = box.unpack('i', coll_id)
need_update_old_threshold = box.unpack('i', need_update_old_threshold)
need_update_last_fullsync = box.unpack('i', need_update_last_fullsync)
local status, t
if need_update_old_threshold ~= 0 and need_update_last_fullsync ~= 0 then
status, t = pcall(box.update, 0, { coll_id }, "=p=p=p=p+p", 1, box.time(), 2, box.time(), 3, box.time(), 4, box.time(), 5, 1)
elseif need_update_old_threshold ~= 0 then
status, t = pcall(box.update, 0, { coll_id }, "=p=p=p+p", 1, box.time(), 2, box.time(), 3, box.time(), 5, 1)
elseif need_update_last_fullsync ~= 0 then
status, t = pcall(box.update, 0, { coll_id }, "=p=p=p+p", 1, box.time(), 2, box.time(), 4, box.time(), 5, 1)
else
status, t = pcall(box.update, 0, { coll_id }, "=p=p+p", 1, box.time(), 2, box.time(), 5, 1)
end
if not status or t == nil then
box.replace(0, coll_id, box.time(), box.time(), box.time(), box.time(), 1)
end
end
function rico_update_failure(coll_id, need_update_last_fullsync)
coll_id = box.unpack('i', coll_id)
need_update_last_fullsync = box.unpack('i', need_update_last_fullsync)
local status, t
if need_update_last_fullsync ~= 0 then
status, t = pcall(box.update, 0, { coll_id }, "=p=p", 1, box.time(), 4, box.time())
else
status, t = pcall(box.update, 0, { coll_id }, "=p", 1, box.time())
end
if not status or t == nil then
box.replace(0, coll_id, box.time(), box.time(), box.time(), box.time(), 0)
end
end
function rico_drop(coll_id)
coll_id = box.unpack('i', coll_id)
local t = box.delete(0, coll_id)
end
------------------------------
-- POP3 --
------------------------------
function rico_get_pop3(coll_id)
coll_id = box.unpack('i', coll_id)
local t = box.select_limit(1, 0, 0, 1, coll_id)
if t ~=nil then return t:transform(0,1) end
end
function rico_get_lasttimes_pop3(from_coll_id, to_coll_id)
from_coll_id = box.unpack('i', from_coll_id)
to_coll_id = box.unpack('i', to_coll_id)
local result = {}
for t in box.space[1].index[0]:iterator(box.index.GE, from_coll_id) do
if box.unpack('i', t[0]) >= to_coll_id then break end
table.insert(result, { t:slice(0, 2) })
end
if #result > 0 then return result end
return unpack(result)
end
function rico_reset_pop3(coll_id)
coll_id = box.unpack('i', coll_id)
box.replace(1, coll_id, box.time(), box.time(), box.time(), box.time(), 0)
end
function rico_update_success_pop3(coll_id, need_update_old_threshold, need_update_last_fullsync)
coll_id = box.unpack('i', coll_id)
need_update_old_threshold = box.unpack('i', need_update_old_threshold)
need_update_last_fullsync = box.unpack('i', need_update_last_fullsync)
local status, t
if need_update_old_threshold ~= 0 and need_update_last_fullsync ~= 0 then
status, t = pcall(box.update, 1, { coll_id }, "=p=p=p=p+p", 1, box.time(), 2, box.time(), 3, box.time(), 4, box.time(), 5, 1)
elseif need_update_old_threshold ~= 0 then
status, t = pcall(box.update, 1, { coll_id }, "=p=p=p+p", 1, box.time(), 2, box.time(), 3, box.time(), 5, 1)
elseif need_update_last_fullsync ~= 0 then
status, t = pcall(box.update, 1, { coll_id }, "=p=p=p+p", 1, box.time(), 2, box.time(), 4, box.time(), 5, 1)
else
status, t = pcall(box.update, 1, { coll_id }, "=p=p+p", 1, box.time(), 2, box.time(), 5, 1)
end
if not status or t == nil then
box.replace(1, coll_id, box.time(), box.time(), box.time(), box.time(), 1)
end
end
function rico_update_failure_pop3(coll_id, need_update_last_fullsync)
coll_id = box.unpack('i', coll_id)
need_update_last_fullsync = box.unpack('i', need_update_last_fullsync)
local status, t
if need_update_last_fullsync ~= 0 then
status, t = pcall(box.update, 1, { coll_id }, "=p=p", 1, box.time(), 4, box.time())
else
status, t = pcall(box.update, 1, { coll_id }, "=p", 1, box.time())
end
if not status or t == nil then
box.replace(1, coll_id, box.time(), box.time(), box.time(), box.time(), 0)
end
end
function rico_drop_pop3(coll_id)
coll_id = box.unpack('i', coll_id)
local t = box.delete(1, coll_id)
end