libs/uci: optimize get & set performance in luci.model.uci.bind, fix ambiguous case in uciop()

This commit is contained in:
Jo-Philipp Wich 2009-10-08 10:07:51 +00:00
parent 667e05a8b4
commit 740bb20850

View file

@ -90,14 +90,20 @@ bsection = utl.class()
function bsection.uciop(self, op, ...) function bsection.uciop(self, op, ...)
assert(self.bind and self.bind.uci, assert(self.bind and self.bind.uci,
"attempt to use unitialized binding: " .. type(self.sid)) "attempt to use unitialized binding")
return op and self.bind.uci[op](self.bind.uci, self.bind.cfg, ...) if op then
or self.bind.uci return self.bind.uci[op](self.bind.uci, self.bind.cfg, ...)
else
return self.bind.uci
end
end end
function bsection.get(self, k, c) function bsection.get(self, k, c)
local v local v
if type(c) == "string" then
v = self:uciop("get", c, k)
else
self:uciop("foreach", self.stype, self:uciop("foreach", self.stype,
function(s) function(s)
if type(c) == "table" then if type(c) == "table" then
@ -105,8 +111,6 @@ function bsection.get(self, k, c)
for ck, cv in pairs(c) do for ck, cv in pairs(c) do
if s[ck] ~= cv then return true end if s[ck] ~= cv then return true end
end end
elseif type(c) == "string" and s['.name'] ~= c then
return true
end end
if k ~= nil then if k ~= nil then
v = s[k] v = s[k]
@ -115,11 +119,15 @@ function bsection.get(self, k, c)
end end
return false return false
end) end)
end
return v return v
end end
function bsection.set(self, k, v, c) function bsection.set(self, k, v, c)
local stat local stat
if type(c) == "string" then
stat = self:uciop("set", c, k, v)
else
self:uciop("foreach", self.stype, self:uciop("foreach", self.stype,
function(s) function(s)
if type(c) == "table" then if type(c) == "table" then
@ -127,12 +135,11 @@ function bsection.set(self, k, v, c)
for ck, cv in pairs(c) do for ck, cv in pairs(c) do
if s[ck] ~= cv then return true end if s[ck] ~= cv then return true end
end end
elseif type(c) == "string" and s['.name'] ~= c then
return true
end end
stat = self:uciop("set", c, k, v) stat = self:uciop("set", c, k, v)
return false return false
end) end)
end
return stat or false return stat or false
end end