Changeset

7157:0890a428a99b

Merge 0.10->trunk
author Kim Alvefur <zash@zash.se>
date Wed, 10 Feb 2016 07:57:58 +0100
parents 7148:a461946fe865 (current diff) 7156:18b7ffd08d56 (diff)
children 7159:f11d6c9d1c1f
files core/storagemanager.lua
diffstat 3 files changed, 69 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/core/rostermanager.lua	Mon Feb 08 21:50:04 2016 +0000
+++ b/core/rostermanager.lua	Wed Feb 10 07:57:58 2016 +0100
@@ -18,7 +18,6 @@
 local hosts = hosts;
 local bare_sessions = prosody.bare_sessions;
 
-local datamanager = require "util.datamanager"
 local um_user_exists = require "core.usermanager".user_exists;
 local st = require "util.stanza";
 
@@ -30,7 +29,7 @@
 	if session.roster then
 		local old_item = session.roster[jid];
 		session.roster[jid] = item;
-		if save_roster(session.username, session.host) then
+		if save_roster(session.username, session.host, nil, jid) then
 			return true;
 		else
 			session.roster[jid] = old_item;
@@ -45,7 +44,7 @@
 	if session.roster then
 		local old_item = session.roster[jid];
 		session.roster[jid] = nil;
-		if save_roster(session.username, session.host) then
+		if save_roster(session.username, session.host, nil, jid) then
 			return true;
 		else
 			session.roster[jid] = old_item;
@@ -109,7 +108,8 @@
 	else -- Attempt to load roster for non-loaded user
 		log("debug", "load_roster: loading for offline user: %s@%s", username, host);
 	end
-	local data, err = datamanager.load(username, host, "roster");
+	local roster_store = require "core.storagemanager".open(host, "roster", "keyval");
+	local data, err = roster_store:get(username);
 	roster = data or {};
 	if user then user.roster = roster; end
 	roster_metadata(roster, err);
@@ -123,13 +123,13 @@
 	return roster, err;
 end
 
-function save_roster(username, host, roster)
+function save_roster(username, host, roster, jid)
 	if not um_user_exists(username, host) then
 		log("debug", "not saving roster for %s@%s: the user doesn't exist", username, host);
 		return nil;
 	end
 
-	log("debug", "save_roster: saving roster for %s@%s", username, host);
+	log("debug", "save_roster: saving roster for %s@%s, (%s)", username, host, jid or "all contacts");
 	if not roster then
 		roster = hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster;
 		--if not roster then
@@ -143,7 +143,13 @@
 			metadata.version = (metadata.version or 0) + 1;
 		end
 		if metadata.broken then return nil, "Not saving broken roster" end
-		return datamanager.store(username, host, "roster", roster);
+		if jid == nil then
+			local roster_store = require "core.storagemanager".open(host, "roster", "keyval");
+			return roster_store:set(username, roster);
+		else
+			local roster_store = require "core.storagemanager".open(host, "roster", "map");
+			return roster_store:set_keys(username, { [false] = metadata, [jid] = roster[jid] or roster_store.remove });
+		end
 	end
 	log("warn", "save_roster: user had no roster to save");
 	return nil;
@@ -159,7 +165,7 @@
 			item.subscription = "both";
 		end
 		item.ask = nil;
-		return save_roster(username, host, roster);
+		return save_roster(username, host, roster, jid);
 	end
 end
 
@@ -183,7 +189,7 @@
 		end
 	end
 	if changed then
-		return save_roster(username, host, roster);
+		return save_roster(username, host, roster, jid);
 	end
 end
 
@@ -207,7 +213,7 @@
 		end
 	end
 	if changed then
-		return save_roster(username, host, roster);
+		return save_roster(username, host, roster, jid);
 	end
 end
 
@@ -240,7 +246,7 @@
 		return; -- false
 	end
 	roster[false].pending[jid] = true;
-	return save_roster(username, host, roster);
+	return save_roster(username, host, roster, jid);
 end
 function is_contact_pending_out(username, host, jid)
 	local roster = load_roster(username, host);
@@ -259,7 +265,7 @@
 	end
 	item.ask = "subscribe";
 	log("debug", "set_contact_pending_out: saving roster; set %s@%s.roster[%q].ask=subscribe", username, host, jid);
-	return save_roster(username, host, roster);
+	return save_roster(username, host, roster, jid);
 end
 local function unsubscribe(username, host, jid)
 	local roster = load_roster(username, host);
@@ -274,7 +280,7 @@
 	elseif item.subscription == "to" then
 		item.subscription = "none";
 	end
-	return save_roster(username, host, roster);
+	return save_roster(username, host, roster, jid);
 end
 local function subscribed(username, host, jid)
 	if is_contact_pending_in(username, host, jid) then
@@ -290,7 +296,7 @@
 			item.subscription = "both";
 		end
 		roster[false].pending[jid] = nil;
-		return save_roster(username, host, roster);
+		return save_roster(username, host, roster, jid);
 	end -- TODO else implement optional feature pre-approval (ask = subscribed)
 end
 local function unsubscribed(username, host, jid)
@@ -310,7 +316,7 @@
 			is_subscribed = true;
 		end
 	end
-	local success = (pending or is_subscribed) and save_roster(username, host, roster);
+	local success = (pending or is_subscribed) and save_roster(username, host, roster, jid);
 	return success, pending, subscribed;
 end
 
@@ -319,7 +325,7 @@
 	local item = roster[jid];
 	if item and (item.subscription == "none" or item.subscription == "from") then
 		item.ask = "subscribe";
-		return save_roster(username, host, roster);
+		return save_roster(username, host, roster, jid);
 	end
 end
 
--- a/core/storagemanager.lua	Mon Feb 08 21:50:04 2016 +0000
+++ b/core/storagemanager.lua	Wed Feb 10 07:57:58 2016 +0100
@@ -118,6 +118,23 @@
 			current[key] = data;
 			return self.keyval_store:set(username, current);
 		end;
+		set_keys = function (self, username, keydatas)
+			local current, err = self.keyval_store:get(username);
+			if current == nil then
+				if err then
+					return nil, err;
+				else
+					current = keydatas;
+				end
+			else
+				for k,v in pairs(keydatas) do
+					if v == self.remove then v = nil; end
+					current[k] = v;
+				end
+			end
+			return self.keyval_store:set(username, current);
+		end;
+		remove = {};
 	};
 }
 
--- a/plugins/mod_storage_sql.lua	Mon Feb 08 21:50:04 2016 +0000
+++ b/plugins/mod_storage_sql.lua	Wed Feb 10 07:57:58 2016 +0100
@@ -1,3 +1,5 @@
+
+-- luacheck: ignore 212/self
 
 local json = require "util.json";
 local sql = require "util.sql";
@@ -125,8 +127,10 @@
 
 --- Archive store API
 
+-- luacheck: ignore 512 431/user 431/store
 local map_store = {};
 map_store.__index = map_store;
+map_store.remove = {};
 function map_store:get(username, key)
 	local ok, result = engine:transaction(function()
 		if type(key) == "string" and key ~= "" then
@@ -134,23 +138,40 @@
 				return deserialize(row[1], row[2]);
 			end
 		else
-			error("TODO: non-string keys");
+			for row in engine:select("SELECT `type`, `value` FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", host, username or "", self.store, "") do
+				local data = deserialize(row[1], row[2]);
+				return data and data[key] or nil;
+			end
 		end
 	end);
 	if not ok then return nil, result; end
 	return result;
 end
 function map_store:set(username, key, data)
+	return self:set_keys(username, { [key] = data or self.remove });
+end
+function map_store:set_keys(username, keydatas)
 	local ok, result = engine:transaction(function()
-		if type(key) == "string" and key ~= "" then
-			engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?",
-				host, username or "", self.store, key);
-			if data ~= nil then
-				local t, value = assert(serialize(data));
-				engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, username or "", self.store, key, t, value);
+		for key, data in pairs(keydatas) do
+			if type(key) == "string" and key ~= "" then
+				engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?",
+					host, username or "", self.store, key);
+				if data ~= self.remove then
+					local t, value = assert(serialize(data));
+					engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, username or "", self.store, key, t, value);
+				end
+			else
+				local extradata = {};
+				for row in engine:select("SELECT `type`, `value` FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", host, username or "", self.store, "") do
+					extradata = deserialize(row[1], row[2]);
+					break;
+				end
+				engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?",
+					host, username or "", self.store, "");
+				extradata[key] = data;
+				local t, value = assert(serialize(extradata));
+				engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, username or "", self.store, "", t, value);
 			end
-		else
-			error("TODO: non-string keys");
 		end
 		return true;
 	end);