From d556df13854bf21479a743be8f91e4ce21e25279 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Petr=20=C5=A0pa=C4=8Dek?= Date: Wed, 14 Oct 2020 17:22:51 +0200 Subject: [PATCH] daf: adapt to changes in map result handling --- modules/daf/daf.lua | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/modules/daf/daf.lua b/modules/daf/daf.lua index 6cdf5dfec..a658fb401 100644 --- a/modules/daf/daf.lua +++ b/modules/daf/daf.lua @@ -191,6 +191,7 @@ function M.del(id) return true end end + return nil end -- @function Find a rule @@ -200,6 +201,7 @@ function M.get(id) return r end end + return nil end -- @function Enable/disable a rule @@ -210,6 +212,7 @@ function M.toggle(id, val) return true end end + return nil end -- @function Enable/disable a rule @@ -221,14 +224,10 @@ function M.enable(id) end local function consensus(op, ...) - local ret = false local results = map(string.format(op, ...)) - for idx, r in ipairs(results) do - if idx == 1 then - -- non-empty table, init to true - ret = true - end - ret = ret and r + local ret = results.n > 0 -- init to true for non-empty results + for idx=1, results.n do + ret = ret and results[idx] end return ret end @@ -270,8 +269,9 @@ local function api(h, stream) if query then local ok, r = pcall(M.add, query) if not ok then return 500, string.format('"%s"', r:match('/([^/]+)$')) end - -- Dispatch to all other workers - consensus('daf.add "%s"', query) + -- Dispatch to all other workers: + -- we ignore return values except error() because they are not serializable + consensus('daf.add "%s" and true', query) return rule_info(r) end return 400 @@ -298,11 +298,15 @@ end local function getmatches() local update = {} - for _, rules in ipairs(map 'daf.rules') do - for _, r in ipairs(rules) do - local id = tostring(r.rule.id) - -- Must have string keys for JSON object and not an array - update[id] = (update[id] or 0) + r.rule.count + -- Must have string keys for JSON object and not an array + local inst_counters = map('ret = {} ' + .. 'for _, rule in ipairs(daf.rules) do ' + .. 'ret[tostring(rule.rule.id)] = rule.rule.count ' + .. 'end ' + .. 'return ret') + for inst_idx=1, inst_counters.n do + for r_id, r_cnt in pairs(inst_counters[inst_idx]) do + update[r_id] = (update[r_id] or 0) + r_cnt end end return update -- 2.47.2