-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy.lua
337 lines (309 loc) · 9.15 KB
/
my.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
local function my_clean_dir()
os.execute('rm -rf *.snap *.xlog *.vylog ./512 ./513 ./514 ./515 ./516 ./517 ./518 ./519 ./520 ./521')
end
local function my_size(t)
if type(t) ~= 'table' and not box.tuple.is(t) then return 0 end
local count = 0;
for _ in pairs(t) do
count = count + 1
end
return count
end
function my_max_id(thing, default)
local res = default
for id in pairs(thing) do
if type(id) == 'number' and id > res then res = id end
end
return res
end
local function my_is_array(t)
if box.tuple.is(t) then return true end
if type(t) ~= 'table' then return false end
local count = 0
local max = 0;
for k,v in pairs(t) do
if type(k) ~= 'number' then return false end
if k ~= math.floor(k) or k < 1 then return false end
count = count + 1
max = k > max and k or max
end
return count == max
end
local function my_tostring(...)
local res = ''
local args = {...}
local main_first = true
for _,x in pairs(args) do
if main_first then main_first = false else res = res .. ', ' end
if type(x) == 'string' then
res = res .. '"' .. x .. '"'
elseif type(x) == 'table' and not my_is_array(x) then
res = res .. '{'
local first = true
for k,v in pairs(x) do
if first then first = false else res = res .. ', ' end
if type(k) == 'string' then
res = res .. k .. '=' .. my_tostring(v)
else
res = res .. '[' .. my_tostring(k) .. ']' .. '=' .. my_tostring(v)
end
end
res = res .. '}'
elseif my_is_array(x) then
res = res .. '{'
local size = #x
for i = 1,size do
if i > 1 then res = res .. ', ' end
res = res .. my_tostring(x[i])
end
res = res .. '}'
else
res = res .. tostring(x)
end
end
return res
end
local function my_equal(a, b)
local type_a = type(a)
if box.tuple.is(a) then type_a = 'table' end
local type_b = type(b)
if box.tuple.is(b) then type_b = 'table' end
if type_a ~= type_b then return false end
if type_a ~= 'table' then return a == b end
for k,v in pairs(a) do
if not my_equal(v, b[k]) then return false end
end
for k,v in pairs(b) do
if not my_equal(v, a[k]) then return false end
end
return true
end
local function my_print(...)
print(my_tostring(...))
end
local my_last_create_space = nil
local function my_create_space(...)
local name = 'test'
for i = 2,65536 do
if box.space[name] == nil then break end
name = 'test' .. i
end
my_last_create_space = box.schema.space.create(name, ...)
return my_last_create_space
end
local function my_create_index(...)
local name = 'test1'
for i = 2,256 do
if my_last_create_space.index[name] == nil then break end
name = 'test' .. i
end
return my_last_create_space:create_index(name, ...)
end
local function my_fselect(sp_or_ind, ...)
local t = sp_or_ind:select(...)
local n = #t
local max_width = 140
local min_col_width = 5
local s = box.space[sp_or_ind.space_id or sp_or_ind.id]
local cols = #s:format()
for i = 1,n do
cols = math.max(cols, #t[i])
end
local names = {}
for j = 1,cols do
table.insert(names, s:format()[j] and s:format()[j].name or tostring(j))
end
local widths = {}
local real_width = cols + 1
for j = 1,cols do
local width = names[j]:len()
for i = 1,n do
width = math.max(width, my_tostring(t[i][j]):len())
end
width = math.max(width, min_col_width)
real_width = real_width + width
table.insert(widths, width)
end
while (real_width > max_width) do
local max_j = 1
for j = 2,cols do
if widths[j] >= widths[max_j] then max_j = j end
end
widths[max_j] = widths[max_j] - 1
real_width = real_width - 1
end
local delim = '+'
for j = 1,cols do
delim = delim .. string.rep('-', widths[j]) .. '+'
end
delim = delim .. ' '
local tos = function(x, n, mod)
local tmp = mod and x or my_tostring(x)
local str
if tmp:len() <= n then
local add = n - tmp:len()
local add1 = math.floor(add/2)
local add2 = math.ceil(add/2)
str = string.rep(' ', add1) .. tmp .. string.rep(' ', add2)
else
str = tmp:sub(1, n)
end
return str:gsub("%s",string.char(0xC2) .. string.char(0xA0))
end
local res = {}
local ins = function(t, mod)
local str = '|'
local tlen = #t
for j = 1,cols do
str = str .. tos(t[j], widths[j], mod) .. '|'
end
str = str .. ' '
table.insert(res, str)
end
table.insert(res, delim)
ins(names, true)
table.insert(res, delim)
for i = 1,n do
ins(t[i], false)
end
table.insert(res, delim)
return res
end
local function my_find_space(name)
if name:sub(1, 1) ~= 's' then error('wrong usage') end
local ind = 1
if name ~= 's' then
ind = tonumber(name:sub(2))
if not ind then return nil end
if name ~= 's' .. ind then return nil end
end
local max_space_no = my_max_id(box.space, 0)
for i = 512,max_space_no do
if box.space[i] then ind = ind - 1 end
if ind == 0 then
return box.space[i]
end
end
return nil
end
local function my_find_index(name)
if name:sub(1, 1) ~= 'i' then error('wrong usage') end
local sp_ind = 1
local in_ind = 1
if name:len() > 3 then return nil end
if name:len() == 2 then
in_ind = tonumber(name:sub(2))
if not in_ind then return nil end
if name ~= 'i' .. in_ind then return nil end
elseif name:len() == 3 then
sp_ind = tonumber(name:sub(2, 2))
in_ind = tonumber(name:sub(3, 3))
if not sp_ind then return nil end
if not in_ind then return nil end
if name ~= 'i' .. sp_ind .. in_ind then return nil end
end
local max_space_no = my_max_id(box.space, 0)
for i = 512,max_space_no do
if box.space[i] then sp_ind = sp_ind - 1 end
if sp_ind == 0 then
return box.space[i].index[in_ind - 1]
end
end
return nil
end
local old_index_select = nil
local function new_index_select(index, key, opts)
if opts == nil then
opts = {}
else
opts = table.deepcopy(opts)
end
opts.fullscan = true
return old_index_select(index, key, opts)
end
local function my_init()
if type(box.cfg) ~= 'table' then
error('init must be called after box.cfg{}')
end
if not box.schema.space_mt['fselect'] then
box.schema.space_mt['fselect'] = my_fselect
end
local t = getmetatable(_G)
if not t then t = {} end
local old_index = t.__index
local new_index = function(self, key)
if type(key) == 'string' then
local res = nil
if key:sub(1, 1) == '_' then
res = box.space[key]
elseif key:sub(1, 1) == 's' then
res = my_find_space(key)
elseif key:sub(1, 1) == 'i' then
res = my_find_index(key)
end
if res then return res end
end
return old_index and old_index(self, key) or nil
end
t.__index = new_index
setmetatable(_G, t)
old_index_select = box.schema.memtx_index_mt.__index.select
box.schema.memtx_index_mt.__index.select = new_index_select
end
local function my_joinable(fib)
fib:set_joinable(true)
return fib
end
local function my_cfg(pattern)
local res = {}
for k,v in pairs(box.cfg) do
if string.find(k, pattern) then
res[k] = v
end
end
return res
end
local function my_restart()
local ffi = require('ffi')
ffi.cdef('int close(int); int execl(const char *pathname, const char *arg, ...);')
for i=3,16*1024 do ffi.C.close(i) end
os.execute('reset')
ffi.C.execl(arg[-1], arg[-1], './run.lua', box.NULL)
end
local function my_is_debug(val)
if val ~= nil and val ~= true and val ~= false and val ~= 0 and val ~= 1 then
error("Only nil, true, false, 0, 1 argument is accepted!")
end
local ffi = require('ffi')
ffi.cdef('int is_debug;')
local succ, curr = pcall(function() return ffi.C.is_debug end)
if not succ then
print("is_debug symbol was not found! call './is_debug.sh set make'")
return nil
end
if val == nil then
return curr
end
if val == 0 or val == false then
ffi.C.is_debug = 0
else
ffi.C.is_debug = 1
end
end
return {
clean_dir = my_clean_dir,
size = my_size,
max_id = my_max_id,
is_array = my_is_array,
equal = my_equal,
tostring = my_tostring,
print = my_print,
create_space = my_create_space,
create_index = my_create_index,
fselect = my_fselect,
joinable = my_joinable,
cfg = my_cfg,
is_debug = my_is_debug,
restart = my_restart,
init = my_init,
}