Commit 0e56b133 authored by Kaarle Ritvanen's avatar Kaarle Ritvanen

ConfigObject.create: cache labeled objects

parent 0bfb89f3
......@@ -37,10 +37,19 @@ function M.ConfigObject:init(context, location)
self.root = context.objects
end
self.location = location
self.extraobjs = {}
self.uniqueids = {}
end
function M.ConfigObject:create(cls, params)
function M.ConfigObject:create(cls, params, label, index)
local key
if label then
key = label..(index or '')
local obj = self.extraobjs[key]
if obj then return obj end
end
if type(cls) == 'string' then
local name = cls
cls = loadclass(cls)
......@@ -49,11 +58,13 @@ function M.ConfigObject:create(cls, params)
end
end
if self.label then
params.label = self.label..(params.label and '-'..params.label or '')
end
local lbl = {self.label}
table.insert(lbl, label)
if lbl[1] then params.label = table.concat(lbl, '-') end
return cls.morph(params, self.context, self.location)
local obj = cls.morph(params, self.context, self.location)
if key then self.extraobjs[key] = obj end
return obj
end
function M.ConfigObject:uniqueid(key)
......
......@@ -108,8 +108,6 @@ function Filter:init(...)
end
self[limit].log = loadclass('log').get(self, self[limit].log, true)
end
self.extrarules = {}
end
function Filter:trules()
......@@ -117,24 +115,17 @@ function Filter:trules()
local function extrarules(label, cls, options)
options = options or {}
local key = label..(options.index or '')
local obj = self.extrarules[key]
if not obj then
local params = {label=label}
for i, attr in ipairs(
{'in', 'out', 'src', 'dest', 'dnat', 'ipset', 'ipsec', 'service'}
) do
params[attr] = (options.src or self)[attr]
end
util.update(params, options.update)
if options.discard then params[options.discard] = nil end
obj = self:create(cls, params)
self.extrarules[key] = obj
local params = {}
for i, attr in ipairs(
{'in', 'out', 'src', 'dest', 'dnat', 'ipset', 'ipsec', 'service'}
) do
params[attr] = (options.src or self)[attr]
end
extend(res, obj:trules())
util.update(params, options.update)
if options.discard then params[options.discard] = nil end
extend(res, self:create(cls, params, label, options.index):trules())
end
if self.dnat then
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment