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) ...@@ -37,10 +37,19 @@ function M.ConfigObject:init(context, location)
self.root = context.objects self.root = context.objects
end end
self.location = location self.location = location
self.extraobjs = {}
self.uniqueids = {} self.uniqueids = {}
end 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 if type(cls) == 'string' then
local name = cls local name = cls
cls = loadclass(cls) cls = loadclass(cls)
...@@ -49,11 +58,13 @@ function M.ConfigObject:create(cls, params) ...@@ -49,11 +58,13 @@ function M.ConfigObject:create(cls, params)
end end
end end
if self.label then local lbl = {self.label}
params.label = self.label..(params.label and '-'..params.label or '') table.insert(lbl, label)
end 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 end
function M.ConfigObject:uniqueid(key) function M.ConfigObject:uniqueid(key)
......
...@@ -108,8 +108,6 @@ function Filter:init(...) ...@@ -108,8 +108,6 @@ function Filter:init(...)
end end
self[limit].log = loadclass('log').get(self, self[limit].log, true) self[limit].log = loadclass('log').get(self, self[limit].log, true)
end end
self.extrarules = {}
end end
function Filter:trules() function Filter:trules()
...@@ -117,24 +115,17 @@ function Filter:trules() ...@@ -117,24 +115,17 @@ function Filter:trules()
local function extrarules(label, cls, options) local function extrarules(label, cls, options)
options = options or {} 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) local params = {}
self.extrarules[key] = obj for i, attr in ipairs(
{'in', 'out', 'src', 'dest', 'dnat', 'ipset', 'ipsec', 'service'}
) do
params[attr] = (options.src or self)[attr]
end end
util.update(params, options.update)
extend(res, obj:trules()) if options.discard then params[options.discard] = nil end
extend(res, self:create(cls, params, label, options.index):trules())
end end
if self.dnat then 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