Comparison

plugins/mod_storage_sql.lua @ 13150:9e6ede86d35d

mod_storage_sql: Do not keep track of quota when no quota is set No point in doing this expensive O(n) query if the result is not used for anything. Will still cache the total item count if an explicit query for this is performed, then try to keep it updated with new items added. Will likely forget eventually tho.
author Kim Alvefur <zash@zash.se>
date Sun, 11 Jun 2023 20:05:32 +0200
parent 13149:0aaf67f70015
child 13151:7ebb3d6afcd1
comparison
equal deleted inserted replaced
13149:0aaf67f70015 13150:9e6ede86d35d
301 archive_store.__index = archive_store 301 archive_store.__index = archive_store
302 function archive_store:append(username, key, value, when, with) 302 function archive_store:append(username, key, value, when, with)
303 local user,store = username,self.store; 303 local user,store = username,self.store;
304 local cache_key = jid_join(username, host, store); 304 local cache_key = jid_join(username, host, store);
305 local item_count = archive_item_count_cache:get(cache_key); 305 local item_count = archive_item_count_cache:get(cache_key);
306 if not item_count then 306
307 item_count_cache_miss(); 307 if archive_item_limit then
308 local ok, ret = engine:transaction(function() 308 if not item_count then
309 local count_sql = [[ 309 item_count_cache_miss();
310 local ok, ret = engine:transaction(function()
311 local count_sql = [[
310 SELECT COUNT(*) FROM "prosodyarchive" 312 SELECT COUNT(*) FROM "prosodyarchive"
311 WHERE "host"=? AND "user"=? AND "store"=?; 313 WHERE "host"=? AND "user"=? AND "store"=?;
312 ]]; 314 ]];
313 local result = engine:select(count_sql, host, user, store); 315 local result = engine:select(count_sql, host, user, store);
314 if result then 316 if result then
315 for row in result do 317 for row in result do
316 item_count = row[1]; 318 item_count = row[1];
317 end 319 end
318 end 320 end
319 end); 321 end);
320 if not ok or not item_count then 322 if not ok or not item_count then
321 module:log("error", "Failed while checking quota for %s: %s", username, ret); 323 module:log("error", "Failed while checking quota for %s: %s", username, ret);
322 return nil, "Failure while checking quota"; 324 return nil, "Failure while checking quota";
323 end 325 end
324 archive_item_count_cache:set(cache_key, item_count); 326 archive_item_count_cache:set(cache_key, item_count);
325 else 327 else
326 item_count_cache_hit(); 328 item_count_cache_hit();
327 end 329 end
328 330
329 if archive_item_limit then
330 module:log("debug", "%s has %d items out of %d limit", username, item_count, archive_item_limit); 331 module:log("debug", "%s has %d items out of %d limit", username, item_count, archive_item_limit);
331 if item_count >= archive_item_limit then 332 if item_count >= archive_item_limit then
332 return nil, "quota-limit"; 333 return nil, "quota-limit";
333 end 334 end
334 end 335 end
346 ("host", "user", "store", "when", "with", "key", "type", "value") 347 ("host", "user", "store", "when", "with", "key", "type", "value")
347 VALUES (?,?,?,?,?,?,?,?); 348 VALUES (?,?,?,?,?,?,?,?);
348 ]]; 349 ]];
349 if key then 350 if key then
350 local result = engine:delete(delete_sql, host, user or "", store, key); 351 local result = engine:delete(delete_sql, host, user or "", store, key);
351 if result then 352 if result and item_count then
352 item_count = item_count - result:affected(); 353 item_count = item_count - result:affected();
353 end 354 end
354 else 355 else
355 key = uuid.generate(); 356 key = uuid.generate();
356 end 357 end
357 local t, encoded_value = assert(serialize(value)); 358 local t, encoded_value = assert(serialize(value));
358 engine:insert(insert_sql, host, user or "", store, when, with, key, t, encoded_value); 359 engine:insert(insert_sql, host, user or "", store, when, with, key, t, encoded_value);
359 archive_item_count_cache:set(cache_key, item_count+1); 360 if item_count then
361 archive_item_count_cache:set(cache_key, item_count+1);
362 end
360 return key; 363 return key;
361 end); 364 end);
362 if not ok then return ok, ret; end 365 if not ok then return ok, ret; end
363 return ret; -- the key 366 return ret; -- the key
364 end 367 end
640 end); 643 end);
641 if username == true then 644 if username == true then
642 archive_item_count_cache:clear(); 645 archive_item_count_cache:clear();
643 else 646 else
644 local cache_key = jid_join(username, host, self.store); 647 local cache_key = jid_join(username, host, self.store);
645 archive_item_count_cache:set(cache_key, nil); 648 if query.start == nil and query.with == nil and query["end"] == nil and query.key == nil and query.ids == nil and query.truncate == nil then
649 -- All items deleted, count should be zero.
650 archive_item_count_cache:set(cache_key, 0);
651 else
652 -- Not sure how many items left
653 archive_item_count_cache:set(cache_key, nil);
654 end
646 end 655 end
647 return ok and stmt:affected(), stmt; 656 return ok and stmt:affected(), stmt;
648 end 657 end
649 658
650 function archive_store:users() 659 function archive_store:users()