Changeset

8309:5281c479955a

mod_storage_internal: Add support for archive key deduplication (like mod_storage_sql)
author Kim Alvefur <zash@zash.se>
date Mon, 09 Oct 2017 01:01:28 +0200
parents 8308:ab189d3f55d1
children 8310:1759491b53db
files plugins/mod_storage_internal.lua
diffstat 1 files changed, 21 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_storage_internal.lua	Mon Oct 09 00:58:45 2017 +0200
+++ b/plugins/mod_storage_internal.lua	Mon Oct 09 01:01:28 2017 +0200
@@ -44,17 +44,36 @@
 driver.archive = { __index = archive };
 
 function archive:append(username, key, value, when, with)
-	key = key or id();
 	when = when or now();
 	if not st.is_stanza(value) then
 		return nil, "unsupported-datatype";
 	end
 	value = st.preserialize(st.clone(value));
-	value.key = key;
 	value.when = when;
 	value.with = with;
 	value.attr.stamp = datetime.datetime(when);
 	value.attr.stamp_legacy = datetime.legacy(when);
+
+	if key then
+		local items, err = datamanager.list_load(username, host, self.store);
+		if not items and err then return items, err; end
+		if items then
+			items = array(items);
+			items:filter(function (item)
+				return item.key ~= key;
+			end);
+			value.key = key;
+			items:push(value);
+			local ok, err = datamanager.list_store(username, host, self.store, items);
+			if not ok then return ok, err; end
+			return key;
+		end
+	else
+		key = id();
+	end
+
+	value.key = key;
+
 	local ok, err = datamanager.list_append(username, host, self.store, value);
 	if not ok then return ok, err; end
 	return key;