// Changes from v4: // - topics and nodes: every topic and node id in data, with its name (its // id in digits if it has none) and where that comes from. Kept by // Server/Storage/Names.pbi from the config. Only names on both servers // replicate. Names are shared between servers, ids aren't. // - messages: a view of data with the names, for queries by name. // - No foreign keys from data to them (Notes.md): an unknown id would fail // a whole replicated batch. // // Needs SQLite 3.38+ with its JSON functions (json_valid, ->>), see Storage.pbi. // The database is in WAL mode, see Storage.pbi and Writer.pbi. table archives { // UTC Unix Timestamp after which this archived message should be deleted. delete_by integer [not null] message_data text [not null] } table data { topic_id integer [not null] node_id integer [not null] message_id integer [not null] message_data text [not null] // 1 if message_data is well-formed JSON, 0 otherwise. // Can't be written, SQLite computes it on insert and stores it. is_json integer [not null, note: 'GENERATED ALWAYS AS (json_valid(message_data)) STORED'] indexes { (topic_id, node_id, message_id) [unique] message_id } } // One row per topic/node id that has messages. // AFTER INSERT ON data: INSERT ... ON CONFLICT DO UPDATE SET // max_message_id = MAX(max_message_id, excluded.max_message_id) // Only fires for rows actually inserted (not the ones INSERT OR IGNORE drops). table node_heads [note: 'WITHOUT ROWID'] { topic_id integer [not null] node_id integer [not null] max_message_id integer [not null] indexes { (topic_id, node_id) [pk] } } // Create it, but we'll implement this later. table attachements { topic_id integer [not null] node_id integer [not null] message_id integer [not null] attachement_data blob [not null] indexes { (topic_id, node_id) message_id } } // origin: "config" (named in [BakaShrot.Topics]), "id" (unnamed, name is // the id in digits). table topics { topic_id integer [pk] name text [not null, unique, note: 'COLLATE NOCASE'] origin text [not null] } // origin: "config" (named in [BakaShrot.Nodes]), "id" (unnamed), "peer" // (only in databases from the short-lived version that added peer nodes). table nodes { node_id integer [pk] name text [not null, unique, note: 'COLLATE NOCASE'] origin text [not null] } // View messages: // SELECT t.name AS topic, n.name AS node, d.message_id, d.message_data, // d.is_json, d.topic_id, d.node_id // FROM data d JOIN topics t ON t.topic_id = d.topic_id // JOIN nodes n ON n.node_id = d.node_id // WHERE topic = ... AND node = ... goes through the name indexes, then // data's (topic_id, node_id, message_id) one. // Note, ignore it // msg_type TEXT GENERATED ALWAYS AS (data ->> '$.messageData.MsgType') VIRTUAL