/* Image retention: image bytes are dropped once no request will send them
* again, or after imageRetentionDays; the row (hash, type, size) stays, so
* the journal still says an image was there. */
#include <stdio.h>
#include <stdlib.h>
#include "pic.h"
#include "platform.h"
#include "userdb.h"
#define DAY_MS 86400000LL
/* Images with bytes that nothing live holds (?1 = 1: also those unused
* since ?2 ms, unless a queued message holds them). An entry is live when
* some conversation's path reaches it at or after the kept point of that
* path's latest compaction (what PIC_CONTEXT_CTE sends); a queued message
* while it is still queued. */
static const char prunable_sql[] =
"WITH RECURSIVE p(sid, seq) AS (SELECT id, head_seq FROM pic_session WHERE head_seq IS NOT NULL"
" UNION ALL SELECT p.sid, x.parent_seq FROM pic_entry x JOIN p ON x.seq = p.seq WHERE x.parent_seq IS NOT NULL),"
" c(sid, cseq) AS (SELECT p.sid, max(p.seq) FROM p JOIN pic_entry x ON x.seq = p.seq WHERE x.kind = 'compaction'"
" GROUP BY p.sid),"
" k(sid, kept) AS (SELECT c.sid, coalesce(x.first_kept_seq, 0) FROM c JOIN pic_entry x ON x.seq = c.cseq),"
" live(seq) AS (SELECT DISTINCT p.seq FROM p LEFT JOIN k ON k.sid = p.sid WHERE p.seq >= coalesce(k.kept, 0)),"
" used(hash, ms, live, queued) AS ("
" SELECT a.image, coalesce(e.created_ms, 0), a.entry_seq IN (SELECT seq FROM live), 0"
" FROM pic_attachment a JOIN pic_entry e ON e.seq = a.entry_seq"
" UNION ALL SELECT a.image, q.created_ms, q.state = 'queued', q.state = 'queued'"
" FROM pic_attachment a JOIN pic_queued q ON q.id = a.queued_id)"
" SELECT i.hash, i.sz FROM pic_image i WHERE i.data IS NOT NULL AND"
" (NOT EXISTS (SELECT 1 FROM used u WHERE u.hash = i.hash AND u.live)"
" OR (?1 AND NOT EXISTS (SELECT 1 FROM used u WHERE u.hash = i.hash AND (u.queued OR u.ms >= ?2))))";
int pic_prune(pic_store *s, int dry_run, long long *count, long long *bytes) {
sqlite3_stmt *q = NULL, *drop = NULL;
long long now = (long long)(pic_platform_get()->wall_ns() / 1000000), days = 30;
char *setting = pic_setting("imageRetentionDays");
int step, rc = 0;
*count = *bytes = 0;
if (setting) {
char *end;
long long v = strtoll(setting, &end, 10);
if (end != setting && *end == '\0' && v >= 0) {
days = v;
}
free(setting);
}
if (pic_begin(s) != 0) {
return -1;
}
if (sqlite3_prepare_v2(s->db, prunable_sql, -1, &q, NULL) != SQLITE_OK) {
(void)snprintf(s->err, sizeof s->err, "sql: %s", sqlite3_errmsg(s->db));
pic_rollback(s);
return -1;
}
sqlite3_bind_int(q, 1, days > 0);
sqlite3_bind_int64(q, 2, now - days * DAY_MS);
while ((step = sqlite3_step(q)) == SQLITE_ROW) {
++*count;
*bytes += sqlite3_column_int64(q, 1);
}
sqlite3_finalize(q);
if (step == SQLITE_DONE && !dry_run && *count > 0) {
char *sql = sqlite3_mprintf("UPDATE pic_image SET data = NULL WHERE hash IN (SELECT hash FROM (%s))", prunable_sql);
if (!sql || sqlite3_prepare_v2(s->db, sql, -1, &drop, NULL) != SQLITE_OK) {
rc = -1;
} else {
sqlite3_bind_int(drop, 1, days > 0);
sqlite3_bind_int64(drop, 2, now - days * DAY_MS);
rc = sqlite3_step(drop) == SQLITE_DONE ? 0 : -1;
}
sqlite3_finalize(drop);
sqlite3_free(sql);
}
if (rc != 0 || step != SQLITE_DONE) {
(void)snprintf(s->err, sizeof s->err, "prune: %s", sqlite3_errmsg(s->db));
pic_rollback(s);
return -1;
}
if (dry_run) {
pic_rollback(s);
return 0;
}
pic_fault("in-prune"); /* bytes dropped, not yet committed */
{
char sql[128];
(void)snprintf(sql, sizeof sql, "INSERT OR REPLACE INTO pic_meta(key, value) VALUES('last_prune_ms', '%lld')", now);
if (pic_exec(s, sql) != 0 || pic_commit(s) != 0) {
pic_rollback(s);
return -1;
}
}
return 0;
}