Diff

util/pubsub.lua @ 7695:56ce32cfd6d9

util.pubsub: Switch to use util.cache for item data
author Kim Alvefur <zash@zash.se>
date Sun, 16 Oct 2016 00:30:02 +0200
parent 6777:5de6b93d0190
child 7696:1c410b4f3a58
line wrap: on
line diff
--- a/util/pubsub.lua	Thu Oct 13 16:30:57 2016 +0200
+++ b/util/pubsub.lua	Sun Oct 16 00:30:02 2016 +0200
@@ -1,5 +1,5 @@
 local events = require "util.events";
-local t_remove = table.remove;
+local cache = require "util.cache";
 
 local service = {};
 local service_mt = { __index = service };
@@ -216,14 +216,13 @@
 		return false, "conflict";
 	end
 
-	self.data[node] = {};
 	self.nodes[node] = {
 		name = node;
 		subscribers = {};
 		config = setmetatable(options or {}, {__index=self.node_defaults});
 		affiliations = {};
 	};
-	setmetatable(self.nodes[node], { __index = { data = self.data[node] } }); -- COMPAT
+	self.data[node] = cache.new(self.nodes[node].config["pubsub#max_items"]);
 	self.events.fire_event("node-created", { node = node, actor = actor });
 	local ok, err = self:set_affiliation(node, true, actor, "owner");
 	if not ok then
@@ -250,25 +249,6 @@
 	return true;
 end
 
-local function remove_item_by_id(data, id)
-	if not data[id] then return end
-	data[id] = nil;
-	for i, _id in ipairs(data) do
-		if id == _id then
-			t_remove(data, i);
-			return i;
-		end
-	end
-end
-
-local function trim_items(data, max)
-	max = tonumber(max);
-	if not max or #data <= max then return end
-	repeat
-		data[t_remove(data, 1)] = nil;
-	until #data <= max
-end
-
 function service:publish(node, actor, id, item)
 	-- Access checking
 	if not self:may(node, actor, "publish") then
@@ -287,10 +267,10 @@
 		node_obj = self.nodes[node];
 	end
 	local node_data = self.data[node];
-	remove_item_by_id(node_data, id);
-	node_data[#node_data + 1] = id;
-	node_data[id] = item;
-	trim_items(node_data, node_obj.config["pubsub#max_items"]);
+	local ok = node_data:set(id, item);
+	if not ok then
+		return nil, "internal-server-error";
+	end
 	self.events.fire_event("item-published", { node = node, actor = actor, id = id, item = item });
 	self.config.broadcaster("items", node, node_obj.subscribers, item, actor);
 	return true;
@@ -303,11 +283,14 @@
 	end
 	--
 	local node_obj = self.nodes[node];
-	if (not node_obj) or (not self.data[node][id]) then
+	if (not node_obj) or (not self.data[node]:get(id)) then
 		return false, "item-not-found";
 	end
+	local ok = self.data[node]:set(id, nil);
+	if not ok then
+		return nil, "internal-server-error";
+	end
 	self.events.fire_event("item-retracted", { node = node, actor = actor, id = id });
-	remove_item_by_id(self.data[node], id);
 	if retract then
 		self.config.broadcaster("items", node, node_obj.subscribers, retract);
 	end
@@ -324,7 +307,7 @@
 	if not node_obj then
 		return false, "item-not-found";
 	end
-	self.data[node] = {}; -- Purge
+	self.data[node] = cache.new(node_obj.config["pubsub#max_items"]); -- Purge
 	self.events.fire_event("node-purged", { node = node, actor = actor });
 	if notify then
 		self.config.broadcaster("purge", node, node_obj.subscribers);
@@ -343,9 +326,13 @@
 		return false, "item-not-found";
 	end
 	if id then -- Restrict results to a single specific item
-		return true, { id, [id] = self.data[node][id] };
+		return true, { id, [id] = self.data[node]:get(id) };
 	else
-		return true, self.data[node];
+		local data = {}
+		for key, value in self.data[node]:items() do
+			data[key] = value;
+		end
+		return true, data;
 	end
 end
 
@@ -435,8 +422,11 @@
 	for k,v in pairs(new_config) do
 		node_obj.config[k] = v;
 	end
-	trim_items(self.data[node], node_obj.config["pubsub#max_items"]);
-
+	local new_data = cache.new(node_obj.config["pubsub#max_items"]);
+	for key, value in self.data[node]:items() do
+		new_data:set(key, value);
+	end
+	self.data[node] = new_data;
 	return true;
 end