-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqxtable.lua
More file actions
121 lines (102 loc) · 2.32 KB
/
Copy pathqxtable.lua
File metadata and controls
121 lines (102 loc) · 2.32 KB
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
local c = require "qxtable.core"
local rawset = rawset
local rawget = rawget
local pairs = pairs
local select = select
local assert = assert
local setmetatable = setmetatable
local c_index = c.index
local c_next = c.next
local c_len = c.len
local cache_config = {}
local meta = {}
local weak_meta = {__mode = "v"}
local _M = {}
local function wrap_root(name, ud)
return setmetatable({__ud = assert(ud), __name = name}, meta)
end
local function wrap_table(ud)
return setmetatable({__ud = assert(ud)}, meta)
end
local function qxtable_next(t, k)
local nextk,nextv,nextud = c_next(t.__ud, k)
if nextud then
return nextk,t[nextk]
else
return nextk,nextv
end
end
meta.__index = function(t, k)
local v,ud = c_index(t.__ud, k)
if ud then
local value = wrap_table(ud)
rawset(t, k, value)
return value
else
return v
end
end
meta.__newindex = function(t, k, v)
error("Cannot modify a read-only table")
end
meta.__len = function(t)
return c_len(t.__ud)
end
meta.__pairs = function(t)
return qxtable_next,t,nil
end
meta.__ipairs = function(t)
return qxtable_next,t,nil
end
for k,v in pairs(meta) do
weak_meta[k] = v
end
function _M.reload()
local ud_list = c.reload()
for name,ud in pairs(ud_list) do
local cache = cache_config[name]
if not cache or cache.__ud ~= ud then
cache_config[name] = wrap_root(name, ud)
end
end
end
function _M.find(name, ...)
local conf = cache_config[name]
if not conf then
return
end
local val = conf
local n = select("#", ...)
for i =1,n do
val = val[select(i, ...)]
if val == nil then
return nil
end
end
return val
end
function _M.update(confs)
assert(type(confs) == "table")
for name,conf in pairs(confs) do
assert(type(name) == "string")
assert(type(conf) == "table")
end
c.update(confs)
end
function _M.md5(t)
local md5 = require "md5"
return md5.sumhexa(c.tostring(t.__ud))
end
function _M.memory()
return c.memory()
end
function _M.gc()
for _,cache in pairs(cache_config) do
setmetatable(cache, weak_meta)
end
collectgarbage("collect")
for _,cache in pairs(cache_config) do
setmetatable(cache, meta)
end
end
return _M