mod_storage_internal, mod_storage_memory: Add support for query.before
authorMatthew Wild <mwild1@gmail.com>
Fri, 12 Jun 2020 16:55:35 +0100
changeset 10930 c55bd98a54f8
parent 10929 73e95ecec733
child 10931 470602a8b633
mod_storage_internal, mod_storage_memory: Add support for query.before Previously returned all results.
plugins/mod_storage_internal.lua
plugins/mod_storage_memory.lua
--- a/plugins/mod_storage_internal.lua	Fri Jun 12 16:54:38 2020 +0100
+++ b/plugins/mod_storage_internal.lua	Fri Jun 12 16:55:35 2020 +0100
@@ -135,7 +135,7 @@
 		return function () end;
 	end
 	local count = nil;
-	local i = 0;
+	local i, last_key = 0;
 	if query then
 		items = array(items);
 		if query.key then
@@ -178,6 +178,8 @@
 					return nil, "item-not-found";
 				end
 			end
+		elseif query.before then
+			last_key = query.before;
 		elseif query.after then
 			local found = false;
 			for j = 1, #items do
@@ -198,7 +200,9 @@
 	return function ()
 		i = i + 1;
 		local item = items[i];
-		if not item then return; end
+		if not item or (last_key and item.key == last_key) then
+			return;
+		end
 		local key = item.key or tostring(i);
 		local when = item.when or datetime.parse(item.attr.stamp);
 		local with = item.with;
--- a/plugins/mod_storage_memory.lua	Fri Jun 12 16:54:38 2020 +0100
+++ b/plugins/mod_storage_memory.lua	Fri Jun 12 16:55:35 2020 +0100
@@ -101,7 +101,7 @@
 		return function () end;
 	end
 	local count = nil;
-	local i = 0;
+	local i, last_key = 0;
 	if query then
 		items = array():append(items);
 		if query.key then
@@ -142,6 +142,8 @@
 					return nil, "item-not-found";
 				end
 			end
+		elseif query.before then
+			last_key = query.before;
 		elseif query.after then
 			local found = false;
 			for j = 1, #items do
@@ -162,7 +164,7 @@
 	return function ()
 		i = i + 1;
 		local item = items[i];
-		if not item then return; end
+		if not item or (last_key and item.key == last_key) then return; end
 		return item.key, item.value(), item.when, item.with;
 	end, count;
 end