mod_storage_mongodb/mod_storage_mongodb.lua
changeset 1593 3e4d15ae2133
parent 1325 b21236b6b8d8
--- a/mod_storage_mongodb/mod_storage_mongodb.lua	Tue Jan 20 11:02:14 2015 +0000
+++ b/mod_storage_mongodb/mod_storage_mongodb.lua	Sun Jan 25 13:04:02 2015 +0100
@@ -1,5 +1,8 @@
 local next = next;
 local setmetatable = setmetatable;
+local set = require"util.set";
+local it = require"util.iterators";
+local array = require"util.array";
 
 local params = assert ( module:get_option("mongodb") , "mongodb configuration not found" );
 
@@ -46,9 +49,79 @@
 	end;
 end
 
+local roster_store = {};
+roster_store.__index = roster_store;
+
+function roster_store:get(username)
+	local host = module.host or "_global";
+	local store = self.store;
+
+	-- The database name can't have a period in it (hence it can't be a host/ip)
+	local namespace = params.dbname .. "." .. host;
+	local v = { _id = { store = store ; username = username } };
+
+	local cursor , err = conn:query ( namespace , v );
+	if not cursor then return nil , err end;
+
+	local r , err = cursor:next ( );
+	if not r then return nil , err end;
+	local roster = {
+		[false] = {
+			version = r.version;
+			pending = setmetatable(set.new( r.pending )._items, nil);
+		};
+	};
+	local items = r.items;
+	for i = 1, #items do
+		local item = items[i];
+		roster[item.jid] = {
+			subscription = item.subscription;
+			groups = set.new( item.groups )._items;
+			ask = item.ask;
+			name = item.name;
+		}
+	end
+	return roster;
+end
+
+function roster_store:set(username, data)
+	local host = module.host or "_global";
+	local store = self.store;
+
+	-- The database name can't have a period in it (hence it can't be a host/ip)
+	local namespace = params.dbname .. "." .. host;
+	local v = { _id = { store = store ; username = username } };
+
+	if data == nil or next(data) == nil then -- delete data
+		return conn:remove ( namespace , v );
+	end
+
+	for k,v in pairs(data[false]) do
+		v[k]=v;
+	end
+	v.pending = array(it.keys(v.pending or data.pending));
+
+	local items  = {}
+	for jid, item in pairs(data) do
+		if jid and jid ~=  "pending" then
+			table.insert(items, {
+				jid = jid;
+				subscription = item.subscription;
+				groups = array(it.keys( item.groups ));
+				name = item.name;
+				ask = item.ask;
+			});
+		end
+	end
+	v.items = items;
+
+	return conn:insert ( namespace , v );
+end
+
 local driver = {};
 
 function driver:open(store, typ)
+	typ = typ or "keyval";
 	if not conn then
 		conn = assert ( mongo.Connection.New ( true ) );
 		assert ( conn:connect ( params.server ) );
@@ -57,8 +130,12 @@
 		end
 	end
 
-	if not typ then -- default key-value store
+	if typ == "keyval" then -- default key-value store
+		if store == "roster" then
+			return setmetatable({ store = store }, roster_store);
+		end
 		return setmetatable({ store = store }, keyval_store);
+		-- TODO archives?
 	end;
 	return nil, "unsupported-store";
 end