Software / code / prosody
Comparison
plugins/mod_storage_memory.lua @ 9834:a657df70cc31
Merge 0.11->trunk
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Wed, 27 Feb 2019 10:29:10 +0100 |
| parent | 9783:4c91afc43639 |
| parent | 9832:96d9c121547b |
| child | 9839:9007ae90aeb1 |
comparison
equal
deleted
inserted
replaced
| 9831:43f81e85cec2 | 9834:a657df70cc31 |
|---|---|
| 74 a[i] = v; | 74 a[i] = v; |
| 75 a[key] = i; | 75 a[key] = i; |
| 76 return key; | 76 return key; |
| 77 end | 77 end |
| 78 | 78 |
| 79 local function archive_iter (a, start, stop, step, limit, when_start, when_end, match_with) | |
| 80 local item, when, with; | |
| 81 local count = 0; | |
| 82 coroutine.yield(true); -- Ready | |
| 83 for i = start, stop, step do | |
| 84 item = a[i]; | |
| 85 when, with = item.when, item.with; | |
| 86 if when >= when_start and when_end >= when and (not match_with or match_with == with) then | |
| 87 coroutine.yield(item.key, item.value(), when, with); | |
| 88 count = count + 1; | |
| 89 if limit and count >= limit then return end | |
| 90 end | |
| 91 end | |
| 92 end | |
| 93 | |
| 94 function archive_store:find(username, query) | 79 function archive_store:find(username, query) |
| 95 local a = self.store[username or NULL] or {}; | 80 local items = self.store[username or NULL]; |
| 96 local start, stop, step = 1, #a, 1; | 81 if not items then |
| 97 local qstart, qend, qwith = -math.huge, math.huge; | 82 return function () end, 0; |
| 98 local limit; | 83 end |
| 84 local count = #items; | |
| 85 local i = 0; | |
| 99 if query then | 86 if query then |
| 100 module:log("debug", "query included") | 87 items = array():append(items); |
| 88 if query.key then | |
| 89 items:filter(function (item) | |
| 90 return item.key == query.key; | |
| 91 end); | |
| 92 end | |
| 93 if query.with then | |
| 94 items:filter(function (item) | |
| 95 return item.with == query.with; | |
| 96 end); | |
| 97 end | |
| 98 if query.start then | |
| 99 items:filter(function (item) | |
| 100 return item.when >= query.start; | |
| 101 end); | |
| 102 end | |
| 103 if query["end"] then | |
| 104 items:filter(function (item) | |
| 105 return item.when <= query["end"]; | |
| 106 end); | |
| 107 end | |
| 108 count = #items; | |
| 101 if query.reverse then | 109 if query.reverse then |
| 102 start, stop, step = stop, start, -1; | 110 items:reverse(); |
| 103 if query.before then | 111 if query.before then |
| 104 start = a[query.before]; | 112 for j = 1, count do |
| 113 if (items[j].key or tostring(j)) == query.before then | |
| 114 i = j; | |
| 115 break; | |
| 116 end | |
| 117 end | |
| 105 end | 118 end |
| 106 elseif query.after then | 119 elseif query.after then |
| 107 start = a[query.after]; | 120 for j = 1, count do |
| 108 end | 121 if (items[j].key or tostring(j)) == query.after then |
| 109 limit = query.limit; | 122 i = j; |
| 110 qstart = query.start or qstart; | 123 break; |
| 111 qend = query["end"] or qend; | 124 end |
| 112 qwith = query.with; | 125 end |
| 113 end | 126 end |
| 114 if not start then return nil, "invalid-key"; end | 127 if query.limit and #items - i > query.limit then |
| 115 local iter = coroutine.wrap(archive_iter); | 128 items[i+query.limit+1] = nil; |
| 116 iter(a, start, stop, step, limit, qstart, qend, qwith); | 129 end |
| 117 return iter; | 130 end |
| 118 end | 131 return function () |
| 132 i = i + 1; | |
| 133 local item = items[i]; | |
| 134 if not item then return; end | |
| 135 return item.key, item.value(), item.when, item.with; | |
| 136 end, count; | |
| 137 end | |
| 138 | |
| 119 | 139 |
| 120 function archive_store:delete(username, query) | 140 function archive_store:delete(username, query) |
| 121 if not query or next(query) == nil then | 141 if not query or next(query) == nil then |
| 122 self.store[username or NULL] = nil; | 142 self.store[username or NULL] = nil; |
| 123 return true; | 143 return true; |