Rename the DB things.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3656 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2021-08-22 19:41:27 +00:00
parent e922af4c55
commit cfd5341a6b
9 changed files with 41 additions and 46 deletions

View File

@ -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");
}

View File

@ -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;

View File

@ -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);

View File

@ -1,5 +1,6 @@
#include "ssb.export.h"
#include "ssb.db.h"
#include "ssb.h"
#include <stdlib.h>
@ -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);

View File

@ -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);

View File

@ -1,5 +1,6 @@
#include "ssb.import.h"
#include "ssb.db.h"
#include "ssb.h"
#include <stdlib.h>
@ -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;

View File

@ -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, &timestamp, &contents)) {
if (tf_ssb_db_get_message_by_author_and_sequence(ssb, id, sequence, NULL, 0, &timestamp, &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);
}
}

View File

@ -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);

View File

@ -1,5 +1,7 @@
#include "ssb.h"
#include "ssb.db.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
@ -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");