Software /
code /
prosody
Comparison
plugins/mod_storage_sql.lua @ 11971:0b350909da24
mod_storage_sql: Return cached total where it makes sense
This should skip the summary SQL query when not needed, ie when the
cached value can be used directly.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 30 Nov 2021 16:26:01 +0100 |
parent | 11970:f0e78fa8f24c |
child | 12574:18d33668c5fa |
child | 12592:d580e6a57cbb |
comparison
equal
deleted
inserted
replaced
11970:f0e78fa8f24c | 11971:0b350909da24 |
---|---|
426 query = query or {}; | 426 query = query or {}; |
427 local user,store = username,self.store; | 427 local user,store = username,self.store; |
428 local cache_key = jid_join(username, host, self.store); | 428 local cache_key = jid_join(username, host, self.store); |
429 local total = archive_item_count_cache:get(cache_key); | 429 local total = archive_item_count_cache:get(cache_key); |
430 (total and item_count_cache_hit or item_count_cache_miss)(); | 430 (total and item_count_cache_hit or item_count_cache_miss)(); |
431 if total ~= nil and query.limit == 0 and query.start == nil and query.with == nil and query["end"] == nil | 431 if query.start == nil and query.with == nil and query["end"] == nil and query.key == nil and query.ids == nil then |
432 and query.key == nil and query.ids == nil then | 432 -- the query is for the whole archive, so a cached 'total' should be a |
433 return noop, total; | 433 -- relatively accurate response if that's all that is requested |
434 if total ~= nil and query.limit == 0 then return noop, total; end | |
435 else | |
436 -- not usable, so refresh it later if needed | |
437 total = nil; | |
434 end | 438 end |
435 local ok, result, err = engine:transaction(function() | 439 local ok, result, err = engine:transaction(function() |
436 local sql_query = [[ | 440 local sql_query = [[ |
437 SELECT "key", "type", "value", "when", "with" | 441 SELECT "key", "type", "value", "when", "with" |
438 FROM "prosodyarchive" | 442 FROM "prosodyarchive" |
443 local where = { "\"host\" = ?", "\"user\" = ?", "\"store\" = ?", }; | 447 local where = { "\"host\" = ?", "\"user\" = ?", "\"store\" = ?", }; |
444 | 448 |
445 archive_where(query, args, where); | 449 archive_where(query, args, where); |
446 | 450 |
447 -- Total matching | 451 -- Total matching |
448 if query.total then | 452 if query.total and not total then |
453 | |
449 local stats = engine:select("SELECT COUNT(*) FROM \"prosodyarchive\" WHERE " | 454 local stats = engine:select("SELECT COUNT(*) FROM \"prosodyarchive\" WHERE " |
450 .. t_concat(where, " AND "), unpack(args)); | 455 .. t_concat(where, " AND "), unpack(args)); |
451 if stats then | 456 if stats then |
452 for row in stats do | 457 for row in stats do |
453 total = row[1]; | 458 total = row[1]; |