Comparison

util/pubsub.lua @ 7696:1c410b4f3a58

util.pubsub: Factor item storage cache into a per service configurable option
author Kim Alvefur <zash@zash.se>
date Sun, 16 Oct 2016 00:36:05 +0200
parent 7695:56ce32cfd6d9
child 7697:bd854e762875
child 7703:74e755674e0f
comparison
equal deleted inserted replaced
7695:56ce32cfd6d9 7696:1c410b4f3a58
3 3
4 local service = {}; 4 local service = {};
5 local service_mt = { __index = service }; 5 local service_mt = { __index = service };
6 6
7 local default_config = { __index = { 7 local default_config = { __index = {
8 itemstore = function (config) return cache.new(tonumber(config["pubsub#max_items"])) end;
8 broadcaster = function () end; 9 broadcaster = function () end;
9 get_affiliation = function () end; 10 get_affiliation = function () end;
10 capabilities = {}; 11 capabilities = {};
11 } }; 12 } };
12 local default_node_config = { __index = { 13 local default_node_config = { __index = {
220 name = node; 221 name = node;
221 subscribers = {}; 222 subscribers = {};
222 config = setmetatable(options or {}, {__index=self.node_defaults}); 223 config = setmetatable(options or {}, {__index=self.node_defaults});
223 affiliations = {}; 224 affiliations = {};
224 }; 225 };
225 self.data[node] = cache.new(self.nodes[node].config["pubsub#max_items"]); 226 self.data[node] = self.config.itemstore(self.nodes[node].config);
226 self.events.fire_event("node-created", { node = node, actor = actor }); 227 self.events.fire_event("node-created", { node = node, actor = actor });
227 local ok, err = self:set_affiliation(node, true, actor, "owner"); 228 local ok, err = self:set_affiliation(node, true, actor, "owner");
228 if not ok then 229 if not ok then
229 self.nodes[node] = nil; 230 self.nodes[node] = nil;
230 self.data[node] = nil; 231 self.data[node] = nil;
305 -- 306 --
306 local node_obj = self.nodes[node]; 307 local node_obj = self.nodes[node];
307 if not node_obj then 308 if not node_obj then
308 return false, "item-not-found"; 309 return false, "item-not-found";
309 end 310 end
310 self.data[node] = cache.new(node_obj.config["pubsub#max_items"]); -- Purge 311 self.data[node] = self.config.itemstore(self.nodes[node].config);
311 self.events.fire_event("node-purged", { node = node, actor = actor }); 312 self.events.fire_event("node-purged", { node = node, actor = actor });
312 if notify then 313 if notify then
313 self.config.broadcaster("purge", node, node_obj.subscribers); 314 self.config.broadcaster("purge", node, node_obj.subscribers);
314 end 315 end
315 return true 316 return true
420 end 421 end
421 422
422 for k,v in pairs(new_config) do 423 for k,v in pairs(new_config) do
423 node_obj.config[k] = v; 424 node_obj.config[k] = v;
424 end 425 end
425 local new_data = cache.new(node_obj.config["pubsub#max_items"]); 426 local new_data = self.config.itemstore(self.nodes[node].config);
426 for key, value in self.data[node]:items() do 427 for key, value in self.data[node]:items() do
427 new_data:set(key, value); 428 new_data:set(key, value);
428 end 429 end
429 self.data[node] = new_data; 430 self.data[node] = new_data;
430 return true; 431 return true;