Change blob_wants from a table to a view. We can discover the information pretty fast, so let's not store extra data.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4125 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2023-01-14 23:25:56 +00:00
parent 9248dfd97e
commit 0cbc1a650b
2 changed files with 36 additions and 43 deletions

View File

@ -79,10 +79,7 @@ void tf_ssb_db_init(tf_ssb_t* ssb)
" content BLOB," " content BLOB,"
" created INTEGER" " created INTEGER"
")"); ")");
_tf_ssb_db_exec(db, _tf_ssb_db_exec(db,"DROP TABLE IF EXISTS blob_wants");
"CREATE TABLE IF NOT EXISTS blob_wants ("
" id TEXT PRIMARY KEY"
")");
_tf_ssb_db_exec(db, _tf_ssb_db_exec(db,
"CREATE TABLE IF NOT EXISTS properties (" "CREATE TABLE IF NOT EXISTS properties ("
" id TEXT," " id TEXT,"
@ -165,6 +162,13 @@ 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_message_idx ON messages_refs (message)");
_tf_ssb_db_exec(db, "CREATE INDEX IF NOT EXISTS messages_refs_ref_idx ON messages_refs (ref)"); _tf_ssb_db_exec(db, "CREATE INDEX IF NOT EXISTS messages_refs_ref_idx ON messages_refs (ref)");
_tf_ssb_db_exec(db,
"CREATE VIEW IF NOT EXISTS blob_wants_view (id) AS "
" SELECT messages_refs.ref AS id "
" FROM messages_refs "
" LEFT OUTER JOIN blobs ON messages_refs.ref = blobs.id "
" WHERE messages_refs.ref LIKE '&____________________________________________.sha256' "
" AND blobs.id IS NULL");
bool need_add_sequence_before_author = true; bool need_add_sequence_before_author = true;
bool need_convert_timestamp_to_real = false; bool need_convert_timestamp_to_real = false;
@ -303,7 +307,7 @@ bool tf_ssb_db_store_message(tf_ssb_t* ssb, JSContext* context, const char* id,
if (last_row_id != -1) if (last_row_id != -1)
{ {
const char* query = "INSERT INTO blob_wants (id) SELECT DISTINCT json.value FROM messages, json_tree(messages.content) AS json LEFT OUTER JOIN blobs ON json.value = blobs.id WHERE messages.rowid = ?1 AND json.value LIKE '&%%.sha256' AND length(json.value) = ?2 AND blobs.content IS NULL ON CONFLICT DO NOTHING RETURNING id"; const char* query = "SELECT DISTINCT json.value FROM messages, json_tree(messages.content) AS json LEFT OUTER JOIN blobs ON json.value = blobs.id WHERE messages.rowid = ?1 AND json.value LIKE '&%%.sha256' AND length(json.value) = ?2 AND blobs.content IS NULL";
if (sqlite3_prepare(db, query, -1, &statement, NULL) == SQLITE_OK) if (sqlite3_prepare(db, query, -1, &statement, NULL) == SQLITE_OK)
{ {
if (sqlite3_bind_int64(statement, 1, last_row_id) == SQLITE_OK && if (sqlite3_bind_int64(statement, 1, last_row_id) == SQLITE_OK &&
@ -327,6 +331,7 @@ bool tf_ssb_db_store_message(tf_ssb_t* ssb, JSContext* context, const char* id,
} }
} }
JS_FreeValue(context, previousval); JS_FreeValue(context, previousval);
JS_FreeCString(context, author); JS_FreeCString(context, author);
JS_FreeValue(context, authorval); JS_FreeValue(context, authorval);
@ -455,18 +460,6 @@ bool tf_ssb_db_blob_store(tf_ssb_t* ssb, const uint8_t* blob, size_t size, char*
printf("blob stored %s %zd => %d\n", id, size, result); printf("blob stored %s %zd => %d\n", id, size, result);
} }
if (result)
{
if (sqlite3_prepare(db, "DELETE FROM blob_wants WHERE id = ?1", -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_bind_text(statement, 1, id, -1, NULL) == SQLITE_OK)
{
sqlite3_step(statement);
}
sqlite3_finalize(statement);
}
}
if (result && out_id) if (result && out_id)
{ {
snprintf(out_id, out_id_size, "%s", id); snprintf(out_id, out_id_size, "%s", id);
@ -641,7 +634,7 @@ static int _tf_ssb_sqlite_authorizer(void* user_data, int action_code, const cha
break; break;
case SQLITE_READ: case SQLITE_READ:
result = ( result = (
strcmp(arg0, "blob_wants") == 0 || strcmp(arg0, "blob_wants_view") == 0 ||
strcmp(arg0, "json_each") == 0 || strcmp(arg0, "json_each") == 0 ||
strcmp(arg0, "json_tree") == 0 || strcmp(arg0, "json_tree") == 0 ||
strcmp(arg0, "messages") == 0 || strcmp(arg0, "messages") == 0 ||

View File

@ -136,7 +136,7 @@ static void _tf_ssb_rpc_request_more_blobs(tf_ssb_connection_t* connection)
tf_ssb_t* ssb = tf_ssb_connection_get_ssb(connection); tf_ssb_t* ssb = tf_ssb_connection_get_ssb(connection);
sqlite3* db = tf_ssb_get_db(ssb); sqlite3* db = tf_ssb_get_db(ssb);
sqlite3_stmt* statement; sqlite3_stmt* statement;
if (sqlite3_prepare(db, "SELECT id FROM blob_wants WHERE id > ? ORDER BY id LIMIT 32", -1, &statement, NULL) == SQLITE_OK) if (sqlite3_prepare(db, "SELECT id FROM blob_wants_view WHERE id > ? ORDER BY id LIMIT 32", -1, &statement, NULL) == SQLITE_OK)
{ {
if (sqlite3_bind_text(statement, 1, blob_wants->last_id, -1, NULL) == SQLITE_OK) if (sqlite3_bind_text(statement, 1, blob_wants->last_id, -1, NULL) == SQLITE_OK)
{ {