Comparison

plugins/mod_storage_internal.lua @ 9885:64e16d1e91f6

mod_storage_internal,_sql: Key item count cache on both username and store
author Kim Alvefur <zash@zash.se>
date Fri, 22 Mar 2019 18:02:27 +0100
parent 9884:9751c17f5281
child 9888:5669bb11ac7b
comparison
equal deleted inserted replaced
9884:9751c17f5281 9885:64e16d1e91f6
3 local array = require "util.array"; 3 local array = require "util.array";
4 local datetime = require "util.datetime"; 4 local datetime = require "util.datetime";
5 local st = require "util.stanza"; 5 local st = require "util.stanza";
6 local now = require "util.time".now; 6 local now = require "util.time".now;
7 local id = require "util.id".medium; 7 local id = require "util.id".medium;
8 local jid_join = require "util.jid".join;
8 9
9 local host = module.host; 10 local host = module.host;
10 11
11 local archive_item_limit = module:get_option_number("storage_archive_item_limit", 1000); 12 local archive_item_limit = module:get_option_number("storage_archive_item_limit", 1000);
12 local archive_item_count_cache = cache.new(module:get_option("storage_archive_item_limit_cache_size", 1000)); 13 local archive_item_count_cache = cache.new(module:get_option("storage_archive_item_limit_cache_size", 1000));
62 value.when = when; 63 value.when = when;
63 value.with = with; 64 value.with = with;
64 value.attr.stamp = datetime.datetime(when); 65 value.attr.stamp = datetime.datetime(when);
65 value.attr.stamp_legacy = datetime.legacy(when); 66 value.attr.stamp_legacy = datetime.legacy(when);
66 67
67 local item_count = archive_item_count_cache:get(username); 68 local cache_key = jid_join(username, host, self.store);
69 local item_count = archive_item_count_cache:get(cache_key);
68 70
69 if key then 71 if key then
70 local items, err = datamanager.list_load(username, host, self.store); 72 local items, err = datamanager.list_load(username, host, self.store);
71 if not items and err then return items, err; end 73 if not items and err then return items, err; end
72 74
73 -- Check the quota 75 -- Check the quota
74 item_count = items and #items or 0; 76 item_count = items and #items or 0;
75 archive_item_count_cache:set(username, item_count); 77 archive_item_count_cache:set(cache_key, item_count);
76 if item_count >= archive_item_limit then 78 if item_count >= archive_item_limit then
77 module:log("debug", "%s reached or over quota, not adding to store", username); 79 module:log("debug", "%s reached or over quota, not adding to store", username);
78 return nil, "quota-limit"; 80 return nil, "quota-limit";
79 end 81 end
80 82
87 89
88 value.key = key; 90 value.key = key;
89 items:push(value); 91 items:push(value);
90 local ok, err = datamanager.list_store(username, host, self.store, items); 92 local ok, err = datamanager.list_store(username, host, self.store, items);
91 if not ok then return ok, err; end 93 if not ok then return ok, err; end
92 archive_item_count_cache:set(username, #items); 94 archive_item_count_cache:set(cache_key, #items);
93 return key; 95 return key;
94 end 96 end
95 else 97 else
96 if not item_count then -- Item count not cached? 98 if not item_count then -- Item count not cached?
97 -- We need to load the list to get the number of items currently stored 99 -- We need to load the list to get the number of items currently stored
98 local items, err = datamanager.list_load(username, host, self.store); 100 local items, err = datamanager.list_load(username, host, self.store);
99 if not items and err then return items, err; end 101 if not items and err then return items, err; end
100 item_count = items and #items or 0; 102 item_count = items and #items or 0;
101 archive_item_count_cache:set(username, item_count); 103 archive_item_count_cache:set(cache_key, item_count);
102 end 104 end
103 if item_count >= archive_item_limit then 105 if item_count >= archive_item_limit then
104 module:log("debug", "%s reached or over quota, not adding to store", username); 106 module:log("debug", "%s reached or over quota, not adding to store", username);
105 return nil, "quota-limit"; 107 return nil, "quota-limit";
106 end 108 end
111 113
112 value.key = key; 114 value.key = key;
113 115
114 local ok, err = datamanager.list_append(username, host, self.store, value); 116 local ok, err = datamanager.list_append(username, host, self.store, value);
115 if not ok then return ok, err; end 117 if not ok then return ok, err; end
116 archive_item_count_cache:set(username, item_count+1); 118 archive_item_count_cache:set(cache_key, item_count+1);
117 return key; 119 return key;
118 end 120 end
119 121
120 function archive:find(username, query) 122 function archive:find(username, query)
121 local items, err = datamanager.list_load(username, host, self.store); 123 local items, err = datamanager.list_load(username, host, self.store);
193 if not items then return items, err; end 195 if not items then return items, err; end
194 return array(items):pluck("when"):map(datetime.date):unique(); 196 return array(items):pluck("when"):map(datetime.date):unique();
195 end 197 end
196 198
197 function archive:delete(username, query) 199 function archive:delete(username, query)
200 local cache_key = jid_join(username, host, self.store);
198 if not query or next(query) == nil then 201 if not query or next(query) == nil then
199 archive_item_count_cache:set(username, nil); 202 archive_item_count_cache:set(cache_key, nil);
200 return datamanager.list_store(username, host, self.store, nil); 203 return datamanager.list_store(username, host, self.store, nil);
201 end 204 end
202 local items, err = datamanager.list_load(username, host, self.store); 205 local items, err = datamanager.list_load(username, host, self.store);
203 if not items then 206 if not items then
204 if err then 207 if err then
205 return items, err; 208 return items, err;
206 end 209 end
207 archive_item_count_cache:set(username, 0); 210 archive_item_count_cache:set(cache_key, 0);
208 -- Store is empty 211 -- Store is empty
209 return 0; 212 return 0;
210 end 213 end
211 items = array(items); 214 items = array(items);
212 local count_before = #items; 215 local count_before = #items;
252 if count == 0 then 255 if count == 0 then
253 return 0; -- No changes, skip write 256 return 0; -- No changes, skip write
254 end 257 end
255 local ok, err = datamanager.list_store(username, host, self.store, items); 258 local ok, err = datamanager.list_store(username, host, self.store, items);
256 if not ok then return ok, err; end 259 if not ok then return ok, err; end
257 archive_item_count_cache:set(username, #items); 260 archive_item_count_cache:set(cache_key, #items);
258 return count; 261 return count;
259 end 262 end
260 263
261 module:provides("storage", driver); 264 module:provides("storage", driver);