diff --git a/src/ssb.db.c b/src/ssb.db.c index 9943c139..5df41a86 100644 --- a/src/ssb.db.c +++ b/src/ssb.db.c @@ -259,10 +259,40 @@ void tf_ssb_db_init(tf_ssb_t* ssb) _tf_ssb_db_exec(db, "CREATE INDEX IF NOT EXISTS messages_refs_message_idx ON messages_refs (message)"); _tf_ssb_db_exec(db, "CREATE INDEX IF NOT EXISTS messages_refs_ref_idx ON messages_refs (ref)"); + + if (!_tf_ssb_db_has_rows(db, "PRAGMA table_list('blobs_refs')")) + { + _tf_ssb_db_exec(db, + "CREATE TABLE IF NOT EXISTS blobs_refs (" + " blob TEXT, " + " ref TEXT, " + " UNIQUE(blob, ref)" + ")"); + tf_printf("Populating blobs_refs...\n"); + _tf_ssb_db_exec(db, + "INSERT INTO blobs_refs(blob, ref) " + "SELECT blobs.id, j.value FROM blobs, json_tree(blobs.content) as j WHERE " + "json_valid(blobs.content) AND j.value LIKE '&%.sha256' " + "ON CONFLICT DO NOTHING"); + tf_printf("Done.\n"); + } + _tf_ssb_db_exec(db, "DROP TRIGGER IF EXISTS blobs_ai_refs"); + _tf_ssb_db_exec(db, + "CREATE TRIGGER IF NOT EXISTS blobs_ai_refs AFTER INSERT ON blobs BEGIN " + "INSERT INTO blobs_refs(blob, ref) " + "SELECT DISTINCT new.id, j.value FROM json_tree(new.content) as j WHERE " + "json_valid(new.content) AND j.value LIKE '&%.sha256' " + "ON CONFLICT DO NOTHING; END"); + _tf_ssb_db_exec(db, "DROP TRIGGER IF EXISTS blobs_ad_refs"); + _tf_ssb_db_exec(db, "CREATE TRIGGER IF NOT EXISTS blobs_ad_refs AFTER DELETE ON blobs BEGIN DELETE FROM blobs_refs WHERE blobs_refs.blob = old.id; END"); + _tf_ssb_db_exec(db, "CREATE INDEX IF NOT EXISTS blobs_refs_blob_idx ON blobs_refs (blob)"); + _tf_ssb_db_exec(db, "CREATE INDEX IF NOT EXISTS blobs_refs_ref_idx ON blobs_refs (ref)"); + _tf_ssb_db_exec(db, "DROP VIEW IF EXISTS blob_wants_view"); _tf_ssb_db_exec(db, "CREATE VIEW IF NOT EXISTS blob_wants_view (id, timestamp) AS " - " WITH wanted AS ( " + " WITH RECURSIVE " + " wanted1 AS ( " " SELECT messages_refs.ref AS id, messages.timestamp AS timestamp " " FROM messages_refs " " JOIN messages ON messages.id = messages_refs.message " @@ -271,6 +301,11 @@ void tf_ssb_db_init(tf_ssb_t* ssb) " FROM messages_refs " " JOIN messages ON messages.id = messages_refs.message " " WHERE messages.content ->> 'type' = 'about' " + " ), " + " wanted(id, timestamp) AS ( " + " SELECT wanted1.id AS id, wanted1.timestamp AS timestamp FROM wanted1 " + " UNION " + " SELECT br.ref AS id, wanted.timestamp AS timestamp FROM wanted JOIN blobs_refs br ON br.blob = wanted.id " " ) " " SELECT wanted.id, wanted.timestamp FROM wanted " " LEFT OUTER JOIN blobs ON wanted.id = blobs.id "