Changeset

1603:1fbec16996f5

mod_storage_gdbm: Add support for 'limit' query parameter
author Kim Alvefur <zash@zash.se>
date Sat, 07 Feb 2015 22:22:46 +0100
parents 1602:3912a53fd4db
children 1604:53052a610c67
files mod_storage_gdbm/mod_storage_gdbm.lua
diffstat 1 files changed, 4 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mod_storage_gdbm/mod_storage_gdbm.lua	Sat Feb 07 22:22:15 2015 +0100
+++ b/mod_storage_gdbm/mod_storage_gdbm.lua	Sat Feb 07 22:22:46 2015 +0100
@@ -80,20 +80,23 @@
 	local r = query.reverse;
 	local d = r and -1 or 1;
 	local s = meta[ifelse(r, query.before, query.after)];
+	local limit = query.limit;
 	if s then
 		s = s + d;
 	else
 		s = ifelse(r, #meta, 1)
 	end
 	local e = ifelse(r, 1, #meta);
+	local c = 0;
 	return function ()
+		if limit and c >= limit then return end
 		local item, value;
 		for i = s, e, d do
 			item = meta[i];
 			if (not query.with or item.with == query.with)
 			and (not query.start or item.when >= query.start)
 			and (not query["end"] or item.when >= query["end"]) then
-				s = i + d;
+				s = i + d; c = c + 1;
 				value = self:get(item.key);
 				return item.key, (deserialize[item.type] or id)(value), item.when, item.with;
 			end