Comparison

util/pubsub.lua @ 8502:6c2c2fc4b8dd

util.pubsub: Support a 'nodestore' for persisting nodes (same API as kv stores)
author Matthew Wild <mwild1@gmail.com>
date Fri, 02 Feb 2018 19:31:03 +0000
parent 8501:8d9e2c2095dd
child 8694:059183e5571e
comparison
equal deleted inserted replaced
8501:8d9e2c2095dd 8502:6c2c2fc4b8dd
15 ["persist_items"] = false; 15 ["persist_items"] = false;
16 ["max_items"] = 20; 16 ["max_items"] = 20;
17 }; 17 };
18 local default_node_config_mt = { __index = default_node_config }; 18 local default_node_config_mt = { __index = default_node_config };
19 19
20 -- Storage helper functions
21
22 local function load_node_from_store(nodestore, node_name)
23 local node = nodestore:get(node_name);
24 node.config = setmetatable(node.config or {}, default_node_config_mt);
25 return node;
26 end
27
28 local function save_node_to_store(nodestore, node)
29 return nodestore:set(node.name, {
30 name = node.name;
31 config = node.config;
32 subscribers = node.subscribers;
33 affiliations = node.affiliations;
34 });
35 end
36
37 -- Create and return a new service object
20 local function new(config) 38 local function new(config)
21 config = config or {}; 39 config = config or {};
22 return setmetatable({ 40
41 local service = setmetatable({
23 config = setmetatable(config, default_config_mt); 42 config = setmetatable(config, default_config_mt);
24 node_defaults = setmetatable(config.node_defaults or {}, default_node_config_mt); 43 node_defaults = setmetatable(config.node_defaults or {}, default_node_config_mt);
25 affiliations = {}; 44 affiliations = {};
26 subscriptions = {}; 45 subscriptions = {};
27 nodes = {}; 46 nodes = {};
28 data = {}; 47 data = {};
29 events = events.new(); 48 events = events.new();
30 }, service_mt); 49 }, service_mt);
50
51 -- Load nodes from storage, if we have a store and it supports iterating over stored items
52 if config.nodestore and config.nodestore.users then
53 for node_name in config.nodestore:users() do
54 service.nodes[node_name] = load_node_from_store(config.nodestore, node_name);
55 service.data[node_name] = config.itemstore(service.nodes[node_name].config, node_name);
56 end
57 end
58
59 return service;
31 end 60 end
32 61
33 --- Service methods 62 --- Service methods
34 63
35 local service = {}; 64 local service = {};
217 name = node; 246 name = node;
218 subscribers = {}; 247 subscribers = {};
219 config = setmetatable(options or {}, {__index=self.node_defaults}); 248 config = setmetatable(options or {}, {__index=self.node_defaults});
220 affiliations = {}; 249 affiliations = {};
221 }; 250 };
251
252 if self.config.nodestore then
253 local ok, err = save_node_to_store(self.config.nodestore, self.nodes[node]);
254 if not ok then
255 self.nodes[node] = nil;
256 return ok, err;
257 end
258 end
259
222 self.data[node] = self.config.itemstore(self.nodes[node].config, node); 260 self.data[node] = self.config.itemstore(self.nodes[node].config, node);
223 self.events.fire_event("node-created", { node = node, actor = actor }); 261 self.events.fire_event("node-created", { node = node, actor = actor });
224 local ok, err = self:set_affiliation(node, true, actor, "owner"); 262 local ok, err = self:set_affiliation(node, true, actor, "owner");
225 if not ok then 263 if not ok then
226 self.nodes[node] = nil; 264 self.nodes[node] = nil;
227 self.data[node] = nil; 265 self.data[node] = nil;
228 end 266 return ok, err;
267 end
268
229 return ok, err; 269 return ok, err;
230 end 270 end
231 271
232 function service:delete(node, actor) 272 function service:delete(node, actor)
233 -- Access checking 273 -- Access checking