Comparison

plugins/mod_storage_memory.lua @ 9535:c1befd1c886d

mod_storage_memory: Adapt archive deletion code from mod_storage_internal
author Kim Alvefur <zash@zash.se>
date Sun, 21 Oct 2018 19:06:41 +0200
parent 9534:b301f7edf346
child 9609:1dfcea523200
comparison
equal deleted inserted replaced
9534:b301f7edf346 9535:c1befd1c886d
1 local serialize = require "util.serialization".serialize; 1 local serialize = require "util.serialization".serialize;
2 local array = require "util.array";
2 local envload = require "util.envload".envload; 3 local envload = require "util.envload".envload;
3 local st = require "util.stanza"; 4 local st = require "util.stanza";
4 local is_stanza = st.is_stanza or function (s) return getmetatable(s) == st.stanza_mt end 5 local is_stanza = st.is_stanza or function (s) return getmetatable(s) == st.stanza_mt end
5 6
6 local auto_purge_enabled = module:get_option_boolean("storage_memory_temporary", false); 7 local auto_purge_enabled = module:get_option_boolean("storage_memory_temporary", false);
111 function archive_store:delete(username, query) 112 function archive_store:delete(username, query)
112 if not query or next(query) == nil then 113 if not query or next(query) == nil then
113 self.store[username or NULL] = nil; 114 self.store[username or NULL] = nil;
114 return true; 115 return true;
115 end 116 end
116 local old = self.store[username or NULL]; 117 local items = self.store[username or NULL];
117 if not old then return true; end 118 if not items then
118 local qstart = query.start or -math.huge; 119 -- Store is empty
119 local qend = query["end"] or math.huge; 120 return 0;
120 local qwith = query.with; 121 end
121 local new = {}; 122 items = array(items);
122 self.store[username or NULL] = new; 123 local count_before = #items;
123 local t; 124 if query then
124 for i = 1, #old do 125 if query.key then
125 i = old[i]; 126 items:filter(function (item)
126 t = i.when; 127 return item.key ~= query.key;
127 if not(qstart >= t and qend <= t and (not qwith or i.with == qwith)) then 128 end);
128 self:append(username, i.key, i.value(), t, i.with); 129 end
129 end 130 if query.with then
130 end 131 items:filter(function (item)
131 if #new == 0 then 132 return item.with ~= query.with;
132 self.store[username or NULL] = nil; 133 end);
133 end 134 end
134 return true; 135 if query.start then
136 items:filter(function (item)
137 return item.when < query.start;
138 end);
139 end
140 if query["end"] then
141 items:filter(function (item)
142 return item.when > query["end"];
143 end);
144 end
145 if query.truncate and #items > query.truncate then
146 if query.reverse then
147 -- Before: { 1, 2, 3, 4, 5, }
148 -- After: { 1, 2, 3 }
149 for i = #items, query.truncate + 1, -1 do
150 items[i] = nil;
151 end
152 else
153 -- Before: { 1, 2, 3, 4, 5, }
154 -- After: { 3, 4, 5 }
155 local offset = #items - query.truncate;
156 for i = 1, #items do
157 items[i] = items[i+offset];
158 end
159 end
160 end
161 end
162 local count = count_before - #items;
163 if count == 0 then
164 return 0; -- No changes, skip write
165 end
166 setmetatable(items, nil);
167
168 do -- re-index by key
169 for k in pairs(items) do
170 if type(k) == "string" then
171 items[k] = nil;
172 end
173 end
174
175 for i = 1, #items do
176 items[ items[i].key ] = i;
177 end
178 end
179
180 return count;
135 end 181 end
136 182
137 archive_store.purge = _purge_store; 183 archive_store.purge = _purge_store;
138 184
139 local stores = { 185 local stores = {