Minor cleanup while pondering syncing faster.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4322 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2023-06-14 21:59:04 +00:00
parent 486212f22a
commit ef5afc1e23
2 changed files with 47 additions and 41 deletions

View File

@ -681,7 +681,9 @@ void tf_ssb_connection_remove_new_message_request(tf_ssb_connection_t* connectio
void tf_ssb_connection_remove_request(tf_ssb_connection_t* connection, int32_t request_number) void tf_ssb_connection_remove_request(tf_ssb_connection_t* connection, int32_t request_number)
{ {
tf_ssb_request_t* request = bsearch(&request_number, connection->requests, connection->requests_count, sizeof(tf_ssb_request_t), _request_compare); tf_ssb_request_t* request = connection->requests_count ?
bsearch(&request_number, connection->requests, connection->requests_count, sizeof(tf_ssb_request_t), _request_compare) :
NULL;
if (request) if (request)
{ {
if (request->cleanup) if (request->cleanup)

View File

@ -252,14 +252,26 @@ static bool _tf_ssb_db_previous_message_exists(sqlite3* db, const char* author,
bool tf_ssb_db_store_message(tf_ssb_t* ssb, JSContext* context, const char* id, JSValue val, const char* signature, bool sequence_before_author) bool tf_ssb_db_store_message(tf_ssb_t* ssb, JSContext* context, const char* id, JSValue val, const char* signature, bool sequence_before_author)
{ {
bool stored = false; bool stored = false;
JSValue previousval = JS_GetPropertyStr(context, val, "previous"); JSValue previousval = JS_GetPropertyStr(context, val, "previous");
const char* previous = JS_IsNull(previousval) ? NULL : JS_ToCString(context, previousval); const char* previous = JS_IsNull(previousval) ? NULL : JS_ToCString(context, previousval);
JS_FreeValue(context, previousval);
JSValue authorval = JS_GetPropertyStr(context, val, "author"); JSValue authorval = JS_GetPropertyStr(context, val, "author");
const char* author = JS_ToCString(context, authorval); const char* author = JS_ToCString(context, authorval);
JS_FreeValue(context, authorval);
int64_t sequence = -1; int64_t sequence = -1;
JSValue sequenceval = JS_GetPropertyStr(context, val, "sequence"); JSValue sequenceval = JS_GetPropertyStr(context, val, "sequence");
JS_ToInt64(context, &sequence, sequenceval); JS_ToInt64(context, &sequence, sequenceval);
JS_FreeValue(context, sequenceval); JS_FreeValue(context, sequenceval);
sqlite3* db = tf_ssb_get_db(ssb);
sqlite3_stmt* statement;
int64_t last_row_id = -1;
if (_tf_ssb_db_previous_message_exists(db, author, sequence, previous))
{
double timestamp = -1.0; double timestamp = -1.0;
JSValue timestampval = JS_GetPropertyStr(context, val, "timestamp"); JSValue timestampval = JS_GetPropertyStr(context, val, "timestamp");
JS_ToFloat64(context, &timestamp, timestampval); JS_ToFloat64(context, &timestamp, timestampval);
@ -271,12 +283,6 @@ bool tf_ssb_db_store_message(tf_ssb_t* ssb, JSContext* context, const char* id,
const char* contentstr = JS_ToCStringLen(context, &content_len, content); const char* contentstr = JS_ToCStringLen(context, &content_len, content);
JS_FreeValue(context, contentval); JS_FreeValue(context, contentval);
sqlite3* db = tf_ssb_get_db(ssb);
sqlite3_stmt* statement;
int64_t last_row_id = -1;
if (_tf_ssb_db_previous_message_exists(db, author, sequence, previous))
{
const char* query = "INSERT INTO messages (id, previous, author, sequence, timestamp, content, hash, signature, sequence_before_author) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT DO NOTHING"; const char* query = "INSERT INTO messages (id, previous, author, sequence, timestamp, content, hash, signature, sequence_before_author) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT DO NOTHING";
if (sqlite3_prepare(db, query, -1, &statement, NULL) == SQLITE_OK) if (sqlite3_prepare(db, query, -1, &statement, NULL) == SQLITE_OK)
{ {
@ -311,6 +317,9 @@ bool tf_ssb_db_store_message(tf_ssb_t* ssb, JSContext* context, const char* id,
{ {
tf_printf("prepare failed: %s\n", sqlite3_errmsg(db)); tf_printf("prepare failed: %s\n", sqlite3_errmsg(db));
} }
JS_FreeCString(context, contentstr);
JS_FreeValue(context, content);
} }
else else
{ {
@ -343,13 +352,8 @@ bool tf_ssb_db_store_message(tf_ssb_t* ssb, JSContext* context, const char* id,
} }
} }
JS_FreeValue(context, previousval);
JS_FreeCString(context, author); JS_FreeCString(context, author);
JS_FreeValue(context, authorval);
JS_FreeCString(context, previous); JS_FreeCString(context, previous);
JS_FreeCString(context, contentstr);
JS_FreeValue(context, content);
return stored; return stored;
} }