Software /
code /
prosody
Comparison
plugins/mod_storage_memory.lua @ 9832:96d9c121547b 0.11
mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
The :find method in storage_internal works and is easier to read and
understand. Future changes should be simpler to apply to both modules.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 27 Feb 2019 10:20:38 +0100 |
parent | 9609:1dfcea523200 |
child | 9834:a657df70cc31 |
child | 9838:40ed04014b97 |
comparison
equal
deleted
inserted
replaced
9828:8e68136cde08 | 9832:96d9c121547b |
---|---|
66 a[i] = v; | 66 a[i] = v; |
67 a[key] = i; | 67 a[key] = i; |
68 return key; | 68 return key; |
69 end | 69 end |
70 | 70 |
71 local function archive_iter (a, start, stop, step, limit, when_start, when_end, match_with) | |
72 local item, when, with; | |
73 local count = 0; | |
74 coroutine.yield(true); -- Ready | |
75 for i = start, stop, step do | |
76 item = a[i]; | |
77 when, with = item.when, item.with; | |
78 if when >= when_start and when_end >= when and (not match_with or match_with == with) then | |
79 coroutine.yield(item.key, item.value(), when, with); | |
80 count = count + 1; | |
81 if limit and count >= limit then return end | |
82 end | |
83 end | |
84 end | |
85 | |
86 function archive_store:find(username, query) | 71 function archive_store:find(username, query) |
87 local a = self.store[username or NULL] or {}; | 72 local items = self.store[username or NULL]; |
88 local start, stop, step = 1, #a, 1; | 73 if not items then |
89 local qstart, qend, qwith = -math.huge, math.huge; | 74 return function () end, 0; |
90 local limit; | 75 end |
76 local count = #items; | |
77 local i = 0; | |
91 if query then | 78 if query then |
92 module:log("debug", "query included") | 79 items = array():append(items); |
80 if query.key then | |
81 items:filter(function (item) | |
82 return item.key == query.key; | |
83 end); | |
84 end | |
85 if query.with then | |
86 items:filter(function (item) | |
87 return item.with == query.with; | |
88 end); | |
89 end | |
90 if query.start then | |
91 items:filter(function (item) | |
92 return item.when >= query.start; | |
93 end); | |
94 end | |
95 if query["end"] then | |
96 items:filter(function (item) | |
97 return item.when <= query["end"]; | |
98 end); | |
99 end | |
100 count = #items; | |
93 if query.reverse then | 101 if query.reverse then |
94 start, stop, step = stop, start, -1; | 102 items:reverse(); |
95 if query.before then | 103 if query.before then |
96 start = a[query.before]; | 104 for j = 1, count do |
105 if (items[j].key or tostring(j)) == query.before then | |
106 i = j; | |
107 break; | |
108 end | |
109 end | |
97 end | 110 end |
98 elseif query.after then | 111 elseif query.after then |
99 start = a[query.after]; | 112 for j = 1, count do |
100 end | 113 if (items[j].key or tostring(j)) == query.after then |
101 limit = query.limit; | 114 i = j; |
102 qstart = query.start or qstart; | 115 break; |
103 qend = query["end"] or qend; | 116 end |
104 qwith = query.with; | 117 end |
105 end | 118 end |
106 if not start then return nil, "invalid-key"; end | 119 if query.limit and #items - i > query.limit then |
107 local iter = coroutine.wrap(archive_iter); | 120 items[i+query.limit+1] = nil; |
108 iter(a, start, stop, step, limit, qstart, qend, qwith); | 121 end |
109 return iter; | 122 end |
110 end | 123 return function () |
124 i = i + 1; | |
125 local item = items[i]; | |
126 if not item then return; end | |
127 return item.key, item.value(), item.when, item.with; | |
128 end, count; | |
129 end | |
130 | |
111 | 131 |
112 function archive_store:delete(username, query) | 132 function archive_store:delete(username, query) |
113 if not query or next(query) == nil then | 133 if not query or next(query) == nil then |
114 self.store[username or NULL] = nil; | 134 self.store[username or NULL] = nil; |
115 return true; | 135 return true; |