Changeset

7155:4a0825984e42

rostermanager: Use map stores when only one contact is changed
author Kim Alvefur <zash@zash.se>
date Tue, 09 Feb 2016 17:09:01 +0100
parents 7154:b3b92204802f
children 7156:18b7ffd08d56
files core/rostermanager.lua
diffstat 1 files changed, 20 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/core/rostermanager.lua	Tue Feb 09 17:01:04 2016 +0100
+++ b/core/rostermanager.lua	Tue Feb 09 17:09:01 2016 +0100
@@ -29,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;
@@ -44,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;
@@ -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,8 +143,13 @@
 			metadata.version = (metadata.version or 0) + 1;
 		end
 		if metadata.broken then return nil, "Not saving broken roster" end
-		local roster_store = require "core.storagemanager".open(host, "roster", "keyval");
-		return roster_store:set(username, 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;
@@ -160,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
 
@@ -184,7 +189,7 @@
 		end
 	end
 	if changed then
-		return save_roster(username, host, roster);
+		return save_roster(username, host, roster, jid);
 	end
 end
 
@@ -208,7 +213,7 @@
 		end
 	end
 	if changed then
-		return save_roster(username, host, roster);
+		return save_roster(username, host, roster, jid);
 	end
 end
 
@@ -241,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);
@@ -260,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);
@@ -275,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
@@ -291,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)
@@ -311,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
 
@@ -320,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