From cfd5341a6bc532deeaea454cd8931a117ecf6ba3 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sun, 22 Aug 2021 19:41:27 +0000 Subject: [PATCH] Rename the DB things. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3656 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- src/ssb.c | 4 ++-- src/ssb.db.c | 14 +++++++------- src/ssb.db.h | 14 +++++++------- src/ssb.export.c | 5 +++-- src/ssb.h | 11 ----------- src/ssb.import.c | 3 ++- src/ssb.qjs.c | 13 +++++++------ src/ssb.rpc.c | 11 ++++++----- src/ssb.tests.c | 12 +++++++----- 9 files changed, 41 insertions(+), 46 deletions(-) diff --git a/src/ssb.c b/src/ssb.c index f2a12d27..a3835bc3 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -946,7 +946,7 @@ void tf_ssb_append_message(tf_ssb_t* ssb, JSValue message) char previous_id[crypto_hash_sha256_BYTES * 2]; int64_t previous_sequence = 0; - bool have_previous = tf_ssb_get_latest_message_by_author(ssb, author, &previous_sequence, previous_id, sizeof(previous_id)); + bool have_previous = tf_ssb_db_get_latest_message_by_author(ssb, author, &previous_sequence, previous_id, sizeof(previous_id)); JSContext* context = ssb->context; JSValue root = JS_NewObject(context); @@ -998,7 +998,7 @@ void tf_ssb_append_message(tf_ssb_t* ssb, JSValue message) char id[sodium_base64_ENCODED_LEN(crypto_hash_sha256_BYTES, sodium_base64_VARIANT_ORIGINAL) + 1]; tf_ssb_calculate_message_id(ssb->context, root, id, sizeof(id)); if (valid && - !tf_ssb_store_message(ssb, ssb->context, id, root, signature_base64)) { + !tf_ssb_db_store_message(ssb, ssb->context, id, root, signature_base64)) { printf("message not stored.\n"); } diff --git a/src/ssb.db.c b/src/ssb.db.c index 247a9aae..c874bdf8 100644 --- a/src/ssb.db.c +++ b/src/ssb.db.c @@ -55,7 +55,7 @@ void tf_ssb_db_init(tf_ssb_t* ssb) NULL, NULL, NULL); } -bool tf_ssb_store_message(tf_ssb_t* ssb, JSContext* context, const char* id, JSValue val, const char* signature) +bool tf_ssb_db_store_message(tf_ssb_t* ssb, JSContext* context, const char* id, JSValue val, const char* signature) { bool stored = false; JSValue previousval = JS_GetPropertyStr(context, val, "previous"); @@ -105,7 +105,7 @@ bool tf_ssb_store_message(tf_ssb_t* ssb, JSContext* context, const char* id, JSV return stored; } -bool tf_ssb_message_content_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_t* out_size) +bool tf_ssb_db_message_content_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_t* out_size) { bool result = false; sqlite3_stmt* statement; @@ -130,7 +130,7 @@ bool tf_ssb_message_content_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blo return result; } -bool tf_ssb_blob_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_t* out_size) +bool tf_ssb_db_blob_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_t* out_size) { bool result = false; sqlite3_stmt* statement; @@ -155,7 +155,7 @@ bool tf_ssb_blob_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_t* return result; } -bool tf_ssb_blob_store(tf_ssb_t* ssb, const uint8_t* blob, size_t size, char* out_id, size_t out_id_size) +bool tf_ssb_db_blob_store(tf_ssb_t* ssb, const uint8_t* blob, size_t size, char* out_id, size_t out_id_size) { bool result = false; sqlite3* db = tf_ssb_get_db(ssb); @@ -190,7 +190,7 @@ bool tf_ssb_blob_store(tf_ssb_t* ssb, const uint8_t* blob, size_t size, char* ou return result; } -bool tf_ssb_get_message_by_author_and_sequence(tf_ssb_t* ssb, const char* author, int64_t sequence, char* out_message_id, size_t out_message_id_size, int64_t* out_timestamp, char** out_content) +bool tf_ssb_db_get_message_by_author_and_sequence(tf_ssb_t* ssb, const char* author, int64_t sequence, char* out_message_id, size_t out_message_id_size, int64_t* out_timestamp, char** out_content) { bool found = false; sqlite3_stmt* statement; @@ -217,7 +217,7 @@ bool tf_ssb_get_message_by_author_and_sequence(tf_ssb_t* ssb, const char* author return found; } -bool tf_ssb_get_latest_message_by_author(tf_ssb_t* ssb, const char* author, int64_t* out_sequence, char* out_message_id, size_t out_message_id_size) +bool tf_ssb_db_get_latest_message_by_author(tf_ssb_t* ssb, const char* author, int64_t* out_sequence, char* out_message_id, size_t out_message_id_size) { bool found = false; sqlite3_stmt* statement; @@ -325,7 +325,7 @@ static int _tf_ssb_sqlite_authorizer(void* user_data, int action_code, const cha return SQLITE_DENY; } -void tf_ssb_visit_query(tf_ssb_t* ssb, const char* query, const JSValue binds, void (*callback)(JSValue row, void* user_data), void* user_data) +void tf_ssb_db_visit_query(tf_ssb_t* ssb, const char* query, const JSValue binds, void (*callback)(JSValue row, void* user_data), void* user_data) { sqlite3* db = tf_ssb_get_db(ssb); sqlite3_stmt* statement; diff --git a/src/ssb.db.h b/src/ssb.db.h index 8c062a70..895dd19b 100644 --- a/src/ssb.db.h +++ b/src/ssb.db.h @@ -6,11 +6,11 @@ typedef struct _tf_ssb_t tf_ssb_t; void tf_ssb_db_init(tf_ssb_t* ssb); -bool tf_ssb_store_message(tf_ssb_t* ssb, JSContext* context, const char* id, JSValue val, const char* signature); -bool tf_ssb_message_content_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_t* out_size); -bool tf_ssb_blob_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_t* out_size); -bool tf_ssb_blob_store(tf_ssb_t* ssb, const uint8_t* blob, size_t size, char* out_id, size_t out_id_size); +bool tf_ssb_db_store_message(tf_ssb_t* ssb, JSContext* context, const char* id, JSValue val, const char* signature); +bool tf_ssb_db_message_content_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_t* out_size); +bool tf_ssb_db_blob_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_t* out_size); +bool tf_ssb_db_blob_store(tf_ssb_t* ssb, const uint8_t* blob, size_t size, char* out_id, size_t out_id_size); -bool tf_ssb_get_message_by_author_and_sequence(tf_ssb_t* ssb, const char* author, int64_t sequence, char* out_message_id, size_t out_message_id_size, int64_t* out_timestamp, char** out_content); -bool tf_ssb_get_latest_message_by_author(tf_ssb_t* ssb, const char* author, int64_t* out_sequence, char* out_message_id, size_t out_message_id_size); -void tf_ssb_visit_query(tf_ssb_t* ssb, const char* query, const JSValue binds, void (*callback)(JSValue row, void* user_data), void* user_data); +bool tf_ssb_db_get_message_by_author_and_sequence(tf_ssb_t* ssb, const char* author, int64_t sequence, char* out_message_id, size_t out_message_id_size, int64_t* out_timestamp, char** out_content); +bool tf_ssb_db_get_latest_message_by_author(tf_ssb_t* ssb, const char* author, int64_t* out_sequence, char* out_message_id, size_t out_message_id_size); +void tf_ssb_db_visit_query(tf_ssb_t* ssb, const char* query, const JSValue binds, void (*callback)(JSValue row, void* user_data), void* user_data); diff --git a/src/ssb.export.c b/src/ssb.export.c index 24d64e41..dfcb8fe5 100644 --- a/src/ssb.export.c +++ b/src/ssb.export.c @@ -1,5 +1,6 @@ #include "ssb.export.h" +#include "ssb.db.h" #include "ssb.h" #include @@ -51,7 +52,7 @@ void tf_ssb_export(tf_ssb_t* ssb, const char* key) uint8_t* blob = NULL; size_t size = 0; - if (!tf_ssb_blob_get(ssb, app_blob_id, &blob, &size)) { + if (!tf_ssb_db_blob_get(ssb, app_blob_id, &blob, &size)) { printf("Did not find blob for %s: %s.\n", key, app_blob_id); return; } @@ -75,7 +76,7 @@ void tf_ssb_export(tf_ssb_t* ssb, const char* key) uint8_t* file_blob = NULL; size_t file_size = 0; - if (tf_ssb_blob_get(ssb, blob_id, &file_blob, &file_size)) { + if (tf_ssb_db_blob_get(ssb, blob_id, &file_blob, &file_size)) { snprintf(file_path, sizeof(file_path), "apps/%s/%s/%s", user, path, file_name); _write_file(file_path, file_blob, file_size); free(file_blob); diff --git a/src/ssb.h b/src/ssb.h index b5a1c2e9..ab545fbf 100644 --- a/src/ssb.h +++ b/src/ssb.h @@ -65,13 +65,6 @@ bool tf_ssb_whoami(tf_ssb_t* ssb, char* out_id, size_t out_id_size); void tf_ssb_set_broadcasts_changed_callback(tf_ssb_t* ssb, void (*callback)(tf_ssb_t* ssb, void* user_data), void* user_data); void tf_ssb_visit_broadcasts(tf_ssb_t* ssb, void (*callback)(const struct sockaddr_in* addr, const uint8_t* pub, void* user_data), void* user_data); -bool tf_ssb_get_message_by_author_and_sequence(tf_ssb_t* ssb, const char* author, int64_t sequence, char* out_message_id, size_t out_message_id_size, int64_t* out_timestamp, char** out_content); - -bool tf_ssb_blob_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_t* out_size); -bool tf_ssb_blob_store(tf_ssb_t* ssb, const uint8_t* blob, size_t size, char* out_id, size_t out_id_size); - -bool tf_ssb_message_content_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_t* out_size); - typedef void (tf_ssb_connections_changed_callback_t)(tf_ssb_t* ssb, tf_ssb_change_t change, tf_ssb_connection_t* connection, void* user_data); void tf_ssb_add_connections_changed_callback(tf_ssb_t* ssb, tf_ssb_connections_changed_callback_t callback, void* user_data); const char** tf_ssb_get_connection_ids(tf_ssb_t* ssb); @@ -85,8 +78,6 @@ void tf_ssb_send_createHistoryStream(tf_ssb_t* ssb, const char* id); void tf_ssb_send_close(tf_ssb_t* ssb); -void tf_ssb_visit_query(tf_ssb_t* ssb, const char* query, const JSValue binds, void (*callback)(JSValue row, void* user_data), void* user_data); - bool tf_ssb_id_str_to_bin(uint8_t* bin, const char* str); bool tf_ssb_id_bin_to_str(char* str, size_t str_size, const uint8_t* bin); @@ -97,8 +88,6 @@ void tf_ssb_register_rpc(tf_ssb_t* ssb, const char** name, tf_ssb_rpc_callback_t bool tf_ssb_verify_and_strip_signature(JSContext* context, JSValue val, char* out_signature, size_t out_signature_size); void tf_ssb_calculate_message_id(JSContext* context, JSValue message, char* out_id, size_t out_id_size); -bool tf_ssb_store_message(tf_ssb_t* ssb, JSContext* context, const char* id, JSValue val, const char* signature); -bool tf_ssb_get_latest_message_by_author(tf_ssb_t* ssb, const char* author, int64_t* out_sequence, char* out_message_id, size_t out_message_id_size); const char* tf_ssb_connection_get_host(tf_ssb_connection_t* connection); int tf_ssb_connection_get_port(tf_ssb_connection_t* connection); diff --git a/src/ssb.import.c b/src/ssb.import.c index 028d1ad2..1e34f617 100644 --- a/src/ssb.import.c +++ b/src/ssb.import.c @@ -1,5 +1,6 @@ #include "ssb.import.h" +#include "ssb.db.h" #include "ssb.h" #include @@ -33,7 +34,7 @@ static void _tf_ssb_import_file_read(uv_fs_t* req) tf_import_file_t* file = req->data; char id[k_id_base64_len]; if (req->result >= 0) { - if (tf_ssb_blob_store(file->ssb, (const uint8_t*)file->data, req->result, id, sizeof(id))) { + if (tf_ssb_db_blob_store(file->ssb, (const uint8_t*)file->data, req->result, id, sizeof(id))) { printf("Stored %s/%s as %s.\n", file->parent, file->name, id); if (strcasecmp(file->name + strlen(file->name) - strlen(".json"), ".json") == 0) { sqlite3_stmt* statement; diff --git a/src/ssb.qjs.c b/src/ssb.qjs.c index 7ac72e0c..b2fd49da 100644 --- a/src/ssb.qjs.c +++ b/src/ssb.qjs.c @@ -1,5 +1,6 @@ #include "ssb.qjs.h" +#include "ssb.db.h" #include "ssb.h" #include "task.h" @@ -32,7 +33,7 @@ static JSValue _tf_ssb_getMessage(JSContext* context, JSValueConst this_val, int JS_ToInt64(context, &sequence, argv[1]); int64_t timestamp = -1; char* contents = NULL; - if (tf_ssb_get_message_by_author_and_sequence(ssb, id, sequence, NULL, 0, ×tamp, &contents)) { + if (tf_ssb_db_get_message_by_author_and_sequence(ssb, id, sequence, NULL, 0, ×tamp, &contents)) { result = JS_NewObject(context); JS_SetPropertyStr(context, result, "timestamp", JS_NewInt64(context, timestamp)); JS_SetPropertyStr(context, result, "content", JS_NewString(context, contents)); @@ -51,7 +52,7 @@ static JSValue _tf_ssb_blobGet(JSContext* context, JSValueConst this_val, int ar const char* id = JS_ToCString(context, argv[0]); uint8_t* blob = NULL; size_t size = 0; - if (tf_ssb_blob_get(ssb, id, &blob, &size)) { + if (tf_ssb_db_blob_get(ssb, id, &blob, &size)) { result = JS_NewArrayBufferCopy(context, blob, size); free(blob); } @@ -69,12 +70,12 @@ static JSValue _tf_ssb_blobStore(JSContext* context, JSValueConst this_val, int char id[512]; if (JS_IsString(argv[0])) { const char* text = JS_ToCStringLen(context, &size, argv[0]); - if (tf_ssb_blob_store(ssb, (const uint8_t*)text, size, id, sizeof(id))) { + if (tf_ssb_db_blob_store(ssb, (const uint8_t*)text, size, id, sizeof(id))) { result = JS_NewString(context, id); } JS_FreeCString(context, text); } else if ((blob = tf_try_get_array_buffer(context, &size, argv[0])) != 0) { - if (tf_ssb_blob_store(ssb, blob, size, id, sizeof(id))) { + if (tf_ssb_db_blob_store(ssb, blob, size, id, sizeof(id))) { result = JS_NewString(context, id); } } @@ -90,7 +91,7 @@ static JSValue _tf_ssb_messageContentGet(JSContext* context, JSValueConst this_v const char* id = JS_ToCString(context, argv[0]); uint8_t* blob = NULL; size_t size = 0; - if (tf_ssb_message_content_get(ssb, id, &blob, &size)) { + if (tf_ssb_db_message_content_get(ssb, id, &blob, &size)) { result = JS_NewArrayBufferCopy(context, blob, size); free(blob); } @@ -154,7 +155,7 @@ static JSValue _tf_ssb_sqlStream(JSContext* context, JSValueConst this_val, int .context = context, .callback = argv[2], }; - tf_ssb_visit_query(ssb, query, argv[1], _tf_ssb_sqlStream_callback, &info); + tf_ssb_db_visit_query(ssb, query, argv[1], _tf_ssb_sqlStream_callback, &info); JS_FreeCString(context, query); } } diff --git a/src/ssb.rpc.c b/src/ssb.rpc.c index a89c404e..cfee6eee 100644 --- a/src/ssb.rpc.c +++ b/src/ssb.rpc.c @@ -1,5 +1,6 @@ #include "ssb.rpc.h" +#include "ssb.db.h" #include "ssb.h" #include "trace.h" @@ -75,7 +76,7 @@ static void _tf_ssb_rpc_blob_get(tf_ssb_connection_t* connection, uint8_t flags, uint8_t* blob = NULL; size_t blob_size = 0; - if (tf_ssb_blob_get(ssb, blob_id, &blob, &blob_size)) { + if (tf_ssb_db_blob_get(ssb, blob_id, &blob, &blob_size)) { static const size_t k_block_size = 64 * 1024; for (size_t offset = 0; offset < blob_size; offset += k_block_size) { size_t block_size = offset + k_block_size < blob_size ? k_block_size : (blob_size - offset); @@ -121,7 +122,7 @@ static void _tf_ssb_connection_on_rpc_blobs_get_response(tf_ssb_connection_t* co snprintf(id, sizeof(id), "&%s.sha256", hash64); if (strcmp(id, get->blob_id) == 0) { - if (tf_ssb_blob_store(ssb, get->data, get->size, id, sizeof(id))) { + if (tf_ssb_db_blob_store(ssb, get->data, get->size, id, sizeof(id))) { printf("stored blob %s\n", get->blob_id); } else { printf("failed to store %s\n", get->blob_id); @@ -187,7 +188,7 @@ static void _tf_ssb_connection_on_rpc_blobs_createWants_response(tf_ssb_connecti tf_ssb_rpc_send_blobs_get(connection, blob_id, size); } else if (size < 0) { size_t blob_size = 0; - if (tf_ssb_blob_get(ssb, blob_id, NULL, &blob_size)) { + if (tf_ssb_db_blob_get(ssb, blob_id, NULL, &blob_size)) { JSValue size_response = JS_NewObject(context); JS_SetPropertyStr(context, size_response, blob_id, JS_NewInt64(context, blob_size)); JSValue jsonval = JS_JSONStringify(context, size_response, JS_NULL, JS_NULL); @@ -367,7 +368,7 @@ static void _tf_ssb_connection_on_rpc_createHistoryStream_response(tf_ssb_connec char id[crypto_hash_sha256_BYTES * 2 + 1]; tf_ssb_calculate_message_id(context, val, id, sizeof(id)); if (tf_ssb_verify_and_strip_signature(context, val, signature, sizeof(signature))) { - tf_ssb_store_message(tf_ssb_connection_get_ssb(connection), context, id, val, signature); + tf_ssb_db_store_message(tf_ssb_connection_get_ssb(connection), context, id, val, signature); } else { printf("failed to verify message\n"); } @@ -387,7 +388,7 @@ void tf_ssb_rpc_send_createHistoryStream(tf_ssb_connection_t* connection, const JS_SetPropertyStr(context, obj, "keys", JS_FALSE); int64_t sequence = 0; tf_ssb_t* ssb = tf_ssb_connection_get_ssb(connection); - if (tf_ssb_get_latest_message_by_author(ssb, id, &sequence, NULL, 0)) { + if (tf_ssb_db_get_latest_message_by_author(ssb, id, &sequence, NULL, 0)) { JS_SetPropertyStr(context, obj, "seq", JS_NewInt64(context, sequence)); } JSValue argsval = JS_NewArray(context); diff --git a/src/ssb.tests.c b/src/ssb.tests.c index 3236eb52..f16d5652 100644 --- a/src/ssb.tests.c +++ b/src/ssb.tests.c @@ -1,5 +1,7 @@ #include "ssb.h" +#include "ssb.db.h" + #include #include #include @@ -57,7 +59,7 @@ static void _count_messages_callback(JSValue row, void* user_data) static int _ssb_test_count_messages(tf_ssb_t* ssb) { int count = 0; - tf_ssb_visit_query(ssb, "SELECT * FROM messages", JS_UNDEFINED, _count_messages_callback, &count); + tf_ssb_db_visit_query(ssb, "SELECT * FROM messages", JS_UNDEFINED, _count_messages_callback, &count); return count; } @@ -98,7 +100,7 @@ static void _tf_ssb_test_ssb() char blob_id[k_id_base64_len] = { 0 }; const char* k_blob = "Hello, blob!"; - b = tf_ssb_blob_store(ssb0, (const uint8_t*)k_blob, strlen(k_blob), blob_id, sizeof(blob_id)); + b = tf_ssb_db_blob_store(ssb0, (const uint8_t*)k_blob, strlen(k_blob), blob_id, sizeof(blob_id)); assert(b); tf_ssb_append_post(ssb0, "Hello, world!"); @@ -116,8 +118,8 @@ static void _tf_ssb_test_ssb() tf_ssb_append_message(ssb0, message); JS_FreeValue(context, message); - assert(tf_ssb_blob_get(ssb0, blob_id, NULL, NULL)); - assert(!tf_ssb_blob_get(ssb1, blob_id, NULL, NULL)); + assert(tf_ssb_db_blob_get(ssb0, blob_id, NULL, NULL)); + assert(!tf_ssb_db_blob_get(ssb1, blob_id, NULL, NULL)); tf_ssb_server_open(ssb0, 12347); uint8_t id0bin[k_id_bin_len]; @@ -135,7 +137,7 @@ static void _tf_ssb_test_ssb() } printf("waiting for blob\n"); - while (!tf_ssb_blob_get(ssb1, blob_id, NULL, NULL)) { + while (!tf_ssb_db_blob_get(ssb1, blob_id, NULL, NULL)) { uv_run(&loop, UV_RUN_ONCE); } printf("done\n");