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,"
" created INTEGER"
")");
_tf_ssb_db_exec(db,
"CREATE TABLE IF NOT EXISTS blob_wants ("
" id TEXT PRIMARY KEY"
")");
_tf_ssb_db_exec(db,"DROP TABLE IF EXISTS blob_wants");
_tf_ssb_db_exec(db,
"CREATE TABLE IF NOT EXISTS properties ("
" 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_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_convert_timestamp_to_real = false;
@ -301,31 +305,32 @@ bool tf_ssb_db_store_message(tf_ssb_t* ssb, JSContext* context, const char* id,
printf("Previous message doesn't exist.\n");
}
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";
if (sqlite3_prepare(db, query, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_bind_int64(statement, 1, last_row_id) == SQLITE_OK &&
sqlite3_bind_int(statement, 2, k_blob_id_len - 1) == SQLITE_OK)
{
int r = SQLITE_OK;
while ((r = sqlite3_step(statement)) == SQLITE_ROW)
{
tf_ssb_notify_blob_want_added(ssb, (const char*)sqlite3_column_text(statement, 0));
}
if (r != SQLITE_DONE)
{
printf("%s\n", sqlite3_errmsg(db));
}
}
sqlite3_finalize(statement);
}
else
{
printf("prepare failed: %s\n", sqlite3_errmsg(db));
}
}
if (last_row_id != -1)
{
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_bind_int64(statement, 1, last_row_id) == SQLITE_OK &&
sqlite3_bind_int(statement, 2, k_blob_id_len - 1) == SQLITE_OK)
{
int r = SQLITE_OK;
while ((r = sqlite3_step(statement)) == SQLITE_ROW)
{
tf_ssb_notify_blob_want_added(ssb, (const char*)sqlite3_column_text(statement, 0));
}
if (r != SQLITE_DONE)
{
printf("%s\n", sqlite3_errmsg(db));
}
}
sqlite3_finalize(statement);
}
else
{
printf("prepare failed: %s\n", sqlite3_errmsg(db));
}
}
JS_FreeValue(context, previousval);
JS_FreeCString(context, author);
@ -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);
}
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)
{
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;
case SQLITE_READ:
result = (
strcmp(arg0, "blob_wants") == 0 ||
strcmp(arg0, "blob_wants_view") == 0 ||
strcmp(arg0, "json_each") == 0 ||
strcmp(arg0, "json_tree") == 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);
sqlite3* db = tf_ssb_get_db(ssb);
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)
{