-- +goose Up -- The smart cache. One row per (bot, extractor, video, profile). -- tg_file_id layer 1: resend instantly, valid for one bot token only -- storage_key layer 2: stored copy, used for re-upload and transcoding -- A row with status 'blocked' is a takedown: it has no file and stops the -- key from being downloaded or cached again. CREATE TABLE cache_entries ( bot_id BIGINT NOT NULL, extractor TEXT NOT NULL, video_id TEXT NOT NULL, profile TEXT NOT NULL, tg_file_id TEXT NOT NULL DEFAULT '', tg_file_unique_id TEXT NOT NULL DEFAULT '', storage_key TEXT NOT NULL DEFAULT '', size BIGINT NOT NULL DEFAULT 0, duration BIGINT NOT NULL DEFAULT 0, title TEXT NOT NULL DEFAULT '', uploader TEXT NOT NULL DEFAULT '', file_ext TEXT NOT NULL DEFAULT '', width INTEGER NOT NULL DEFAULT 0, height INTEGER NOT NULL DEFAULT 0, hits BIGINT NOT NULL DEFAULT 0, last_hit BIGINT NOT NULL DEFAULT 0, expires_at BIGINT NOT NULL DEFAULT 0, private BOOLEAN NOT NULL DEFAULT FALSE, status TEXT NOT NULL DEFAULT 'ready', created_at BIGINT NOT NULL, PRIMARY KEY (bot_id, extractor, video_id, profile) ); CREATE INDEX cache_expiry_idx ON cache_entries (status, expires_at); CREATE INDEX cache_lru_idx ON cache_entries (status, last_hit); -- Owner blocklist: kind is 'site' (a host) or 'key' (extractor and video). CREATE TABLE blocklist ( kind TEXT NOT NULL, value TEXT NOT NULL, reason TEXT NOT NULL DEFAULT '', created_by BIGINT NOT NULL DEFAULT 0, created_at BIGINT NOT NULL, PRIMARY KEY (kind, value) ); -- source_key: stored copy a derive job transcodes from. -- tg_file_id: file_id a cache hit resends. -- cacheable: whether the result may enter the cache. -- cache_hit: the job was served from the cache and never saw a worker. ALTER TABLE jobs ADD COLUMN source_key TEXT NOT NULL DEFAULT ''; ALTER TABLE jobs ADD COLUMN tg_file_id TEXT NOT NULL DEFAULT ''; ALTER TABLE jobs ADD COLUMN cacheable BOOLEAN NOT NULL DEFAULT FALSE; ALTER TABLE jobs ADD COLUMN cache_hit BOOLEAN NOT NULL DEFAULT FALSE; CREATE INDEX jobs_cache_key_idx ON jobs (cache_key, status); -- Each requester keeps their own caps, because several requesters with -- different limits can share one job. ALTER TABLE deliveries ADD COLUMN max_size BIGINT NOT NULL DEFAULT 0; ALTER TABLE deliveries ADD COLUMN max_duration BIGINT NOT NULL DEFAULT 0; -- Playlist items are requests that wait their turn under a parent. ALTER TABLE requests ADD COLUMN parent_id TEXT NOT NULL DEFAULT ''; ALTER TABLE requests ADD COLUMN profile TEXT NOT NULL DEFAULT ''; CREATE INDEX requests_waiting_idx ON requests (status, user_id, created_at); -- +goose Down DROP INDEX requests_waiting_idx; ALTER TABLE requests DROP COLUMN profile; ALTER TABLE requests DROP COLUMN parent_id; ALTER TABLE deliveries DROP COLUMN max_duration; ALTER TABLE deliveries DROP COLUMN max_size; DROP INDEX jobs_cache_key_idx; ALTER TABLE jobs DROP COLUMN cache_hit; ALTER TABLE jobs DROP COLUMN cacheable; ALTER TABLE jobs DROP COLUMN tg_file_id; ALTER TABLE jobs DROP COLUMN source_key; DROP TABLE blocklist; DROP TABLE cache_entries;