comparison mod_storage_gdbm/mod_storage_gdbm.lua @ 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 3912a53fd4db
children 53052a610c67
comparison
equal deleted inserted replaced
1602:3912a53fd4db 1603:1fbec16996f5
78 function archive:find(username, query) 78 function archive:find(username, query)
79 local meta = self:get(username); 79 local meta = self:get(username);
80 local r = query.reverse; 80 local r = query.reverse;
81 local d = r and -1 or 1; 81 local d = r and -1 or 1;
82 local s = meta[ifelse(r, query.before, query.after)]; 82 local s = meta[ifelse(r, query.before, query.after)];
83 local limit = query.limit;
83 if s then 84 if s then
84 s = s + d; 85 s = s + d;
85 else 86 else
86 s = ifelse(r, #meta, 1) 87 s = ifelse(r, #meta, 1)
87 end 88 end
88 local e = ifelse(r, 1, #meta); 89 local e = ifelse(r, 1, #meta);
90 local c = 0;
89 return function () 91 return function ()
92 if limit and c >= limit then return end
90 local item, value; 93 local item, value;
91 for i = s, e, d do 94 for i = s, e, d do
92 item = meta[i]; 95 item = meta[i];
93 if (not query.with or item.with == query.with) 96 if (not query.with or item.with == query.with)
94 and (not query.start or item.when >= query.start) 97 and (not query.start or item.when >= query.start)
95 and (not query["end"] or item.when >= query["end"]) then 98 and (not query["end"] or item.when >= query["end"]) then
96 s = i + d; 99 s = i + d; c = c + 1;
97 value = self:get(item.key); 100 value = self:get(item.key);
98 return item.key, (deserialize[item.type] or id)(value), item.when, item.with; 101 return item.key, (deserialize[item.type] or id)(value), item.when, item.with;
99 end 102 end
100 end 103 end
101 end 104 end