Changeset

7697:bd854e762875

Merge 0.10->trunk
author Kim Alvefur <zash@zash.se>
date Sun, 16 Oct 2016 00:39:10 +0200
parents 7694:bffbea1187ca (current diff) 7696:1c410b4f3a58 (diff)
children 7704:5022e6181193
files util/pubsub.lua
diffstat 1 files changed, 24 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/util/pubsub.lua	Sat Oct 15 17:40:31 2016 +0200
+++ b/util/pubsub.lua	Sun Oct 16 00:39:10 2016 +0200
@@ -1,10 +1,11 @@
 local events = require "util.events";
-local t_remove = table.remove;
+local cache = require "util.cache";
 
 local service = {};
 local service_mt = { __index = service };
 
 local default_config = { __index = {
+	itemstore = function (config) return cache.new(tonumber(config["pubsub#max_items"])) end;
 	broadcaster = function () end;
 	get_affiliation = function () end;
 	capabilities = {};
@@ -216,14 +217,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] = self.config.itemstore(self.nodes[node].config);
 	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 +250,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 +268,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 +284,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 +308,7 @@
 	if not node_obj then
 		return false, "item-not-found";
 	end
-	self.data[node] = {}; -- Purge
+	self.data[node] = self.config.itemstore(self.nodes[node].config);
 	self.events.fire_event("node-purged", { node = node, actor = actor });
 	if notify then
 		self.config.broadcaster("purge", node, node_obj.subscribers);
@@ -343,9 +327,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 +423,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 = self.config.itemstore(self.nodes[node].config);
+	for key, value in self.data[node]:items() do
+		new_data:set(key, value);
+	end
+	self.data[node] = new_data;
 	return true;
 end