From 2759cd40c2466e0b118c24b1e9965492d2f8b069 Mon Sep 17 00:00:00 2001 From: Netrunner Date: Fri, 10 May 2024 03:19:34 +0100 Subject: [PATCH 1/2] shogdb: minor updates --- makefiles/build.mk | 2 +- shogdb/README.txt | 12 ++++++--- shogdb/examples/simple.c | 6 ++--- shogdb/makefile | 2 +- shogdb/src/client/client.h | 4 +++ shogdb/src/db/db.c | 52 ++++++++++++++++++-------------------- shogdb/src/db/json.c | 4 +-- 7 files changed, 45 insertions(+), 37 deletions(-) diff --git a/makefiles/build.mk b/makefiles/build.mk index a8d649c..f3a1ce0 100644 --- a/makefiles/build.mk +++ b/makefiles/build.mk @@ -26,7 +26,7 @@ else ifeq (gcc, $(shell if [ "$$(cc --help 2>&1 | grep -o -m 1 'gcc')" = "gcc" ] CFLAGS += -Wno-format-overflow -Wno-format-truncation endif -LDFLAGS = $$(pkg-config --libs openssl) $$(pkg-config --cflags --libs uuid) -pthread -lm +LDFLAGS = $$(pkg-config --libs openssl) $$(pkg-config --cflags --libs uuid) -pthread -lm -ljansson # warning flags WARN_CFLAGS += -Werror -Wall -Wextra -Wformat -Wformat-security -Warray-bounds -Wconversion diff --git a/shogdb/README.txt b/shogdb/README.txt index be1217d..1725f55 100644 --- a/shogdb/README.txt +++ b/shogdb/README.txt @@ -2,7 +2,7 @@ SHOGDB - Shoggoth Database =============================================================================== -ShogDB is an in-memory, key-value database written in the C programming language, developed by ShogAI for use in the Shoggoth project. +ShogDB is an in-memory, key-value database written in the C programming language, developed by ShogAI. ShogDB is the database used by Shoggoth Nodes to store information like DHTs and pins. ShogDB also persists data on disk by periodically saving data to a file. ShogDB uses string keys, with values of different types like: @@ -39,7 +39,13 @@ Using the CLI Configuration ================================================ -ShogDB requires a TOML configuration file ./dbconfig.toml located in the current working directory, and stores data on disk in a ./save.sdb file. +ShogDB requires a TOML configuration file ./dbconfig.toml located in the current working directory + + +Persistence +================================================ + +ShogDB stores data on disk in a ./save.sdb file. @@ -78,5 +84,5 @@ The above command will build a binary into ./target/shogdb CONTRIBUTING =============================================================================== -Please follow the Shoggoth contribution guidelines at https://shoggoth.network/explorer/docs#contributing +Please follow the Shoggoth contribution guidelines at https://node.shog.ai/explorer/docs#contributing diff --git a/shogdb/examples/simple.c b/shogdb/examples/simple.c index af295bc..88991e6 100644 --- a/shogdb/examples/simple.c +++ b/shogdb/examples/simple.c @@ -1,4 +1,4 @@ -#include "../include/cjson.h" +#include "../include/jansson.h" #include "../src/client/client.h" int main() { @@ -28,10 +28,10 @@ int main() { UNWRAP(shogdb_set_json(db_ctx, "my_json", "[1, 2, 4]")); db_value_t *res_json = UNWRAP(shogdb_get(db_ctx, "my_json")); - printf("JSON VALUE: %s\n", cJSON_Print(res_json->value_json)); + printf("JSON VALUE: %s\n", json_dumps(res_json->value_json, JSON_INDENT(0))); // UNWRAP(shogdb_delete(db_ctx, "my_str")); - + char *res_all = UNWRAP(shogdb_print(db_ctx)); printf("ALL VALUES: %s\n", res_all); diff --git a/shogdb/makefile b/shogdb/makefile index 5120fed..edfa868 100644 --- a/shogdb/makefile +++ b/shogdb/makefile @@ -15,7 +15,7 @@ CFLAGS_SANITIZED += -fsanitize=address,undefined,leak LDFLAGS_SANITIZED += -lasan -lubsan endif -STATIC_LIBS = ../target/cjson.a ../target/tomlc.a ../target/sonic.a ../target/libnetlibc.a ./target/janssonpath.o +STATIC_LIBS = ../target/tomlc.a ../target/sonic.a ../target/libnetlibc.a ./target/janssonpath.o .PHONY: ./target/janssonpath.o diff --git a/shogdb/src/client/client.h b/shogdb/src/client/client.h index 92142e4..c8b2585 100644 --- a/shogdb/src/client/client.h +++ b/shogdb/src/client/client.h @@ -51,6 +51,10 @@ typedef struct { void *value_json; } db_value_t; +void *str_to_json(char *str); +char *json_to_str(void *json); +void free_json(void *json); + result_t shogdb_set_int(shogdb_ctx_t *ctx, char *key, s64 value); result_t shogdb_set_uint(shogdb_ctx_t *ctx, char *key, u64 value); result_t shogdb_set_float(shogdb_ctx_t *ctx, char *key, f64 value); diff --git a/shogdb/src/db/db.c b/shogdb/src/db/db.c index 3498cba..e1a120f 100644 --- a/shogdb/src/db/db.c +++ b/shogdb/src/db/db.c @@ -9,7 +9,7 @@ ****/ #include "db.h" -#include "../../include/cjson.h" +#include "../../include/jansson.h" #include "../../include/sonic.h" #include "../hashmap/hashmap.h" #include "dht.h" @@ -27,60 +27,59 @@ db_ctx_t *global_ctx = NULL; -// FIXME: serialize seems to not include the latest data from a DHT item's -// unreachable_count char *serialize_data(db_ctx_t *ctx) { - cJSON *save_json = cJSON_CreateArray(); + void *save_json = json_array(); hashmap_t *hashmap = global_ctx->hashmap; hashmap_t *s; for (s = hashmap; s != NULL; s = s->hh.next) { db_value_t *value = s->value; - cJSON *item_json = cJSON_CreateObject(); - cJSON_AddStringToObject(item_json, "key", s->key); - cJSON_AddStringToObject(item_json, "type", - value_type_to_str(value->value_type)); + void *item_json = json_object(); + + json_object_set_new(item_json, "key", json_string(s->key)); + json_object_set_new(item_json, "type", + json_string(value_type_to_str(value->value_type))); if (value->value_type == VALUE_STR) { - cJSON_AddStringToObject(item_json, "value", value->value_str); + json_object_set_new(item_json, "value", json_string(value->value_str)); } else if (value->value_type == VALUE_BOOL) { if (value->value_bool == true) { - cJSON_AddStringToObject(item_json, "value", "true"); + json_object_set_new(item_json, "value", json_string("true")); } else { - cJSON_AddStringToObject(item_json, "value", "false"); + json_object_set_new(item_json, "value", json_string("false")); } } else if (value->value_type == VALUE_INT) { char val[256]; sprintf(val, S64_FORMAT_SPECIFIER, value->value_int); - cJSON_AddStringToObject(item_json, "value", val); + json_object_set_new(item_json, "value", json_string(val)); } else if (value->value_type == VALUE_UINT) { char val[256]; sprintf(val, U64_FORMAT_SPECIFIER, value->value_uint); - cJSON_AddStringToObject(item_json, "value", val); + json_object_set_new(item_json, "value", json_string(val)); } else if (value->value_type == VALUE_FLOAT) { char val[256]; sprintf(val, "%lf", value->value_float); - cJSON_AddStringToObject(item_json, "value", val); + json_object_set_new(item_json, "value", json_string(val)); } else if (value->value_type == VALUE_JSON) { assert(value->value_json != NULL); char *val = json_to_str(value->value_json); - cJSON_AddStringToObject(item_json, "value", val); + json_object_set_new(item_json, "value", json_string(val)); free(val); } else { PANIC("unhandled value type"); } - cJSON_AddItemToArray(save_json, item_json); + json_array_append_new(save_json, item_json); } - char *db_data = cJSON_Print(save_json); - cJSON_Delete(save_json); + char *db_data = json_to_str(save_json); + free_json(save_json); return db_data; } @@ -562,18 +561,17 @@ result_t db_restore_data(db_ctx_t *ctx) { LOG(INFO, "restoring data from %s", ctx->config->save.path); char *data = UNWRAP(read_file_to_string(ctx->config->save.path)); - cJSON *data_json = cJSON_Parse(data); + void *data_json = str_to_json(data); free(data); - const cJSON *item_json = NULL; - cJSON_ArrayForEach(item_json, data_json) { - char *key = cJSON_GetObjectItemCaseSensitive(item_json, "key")->valuestring; - + void *item_json = NULL; + size_t index; + json_array_foreach(data_json, index, item_json) { + char *key = (char *)json_string_value(json_object_get(item_json, "key")); char *type_str = - cJSON_GetObjectItemCaseSensitive(item_json, "type")->valuestring; - + (char *)json_string_value(json_object_get(item_json, "type")); char *value_str = - cJSON_GetObjectItemCaseSensitive(item_json, "value")->valuestring; + (char *)json_string_value(json_object_get(item_json, "value")); value_type_t value_type = str_to_value_type(type_str); @@ -622,7 +620,7 @@ result_t db_restore_data(db_ctx_t *ctx) { } } - cJSON_Delete(data_json); + free_json(data_json); return OK_INT(0); } diff --git a/shogdb/src/db/json.c b/shogdb/src/db/json.c index 299c48d..95aef42 100644 --- a/shogdb/src/db/json.c +++ b/shogdb/src/db/json.c @@ -8,11 +8,11 @@ * ****/ +#include "json.h" #include "../../include/jansson.h" #include "../../include/janssonpath.h" #include "../../include/sonic.h" #include "db.h" -#include "json.h" #include "pins.h" #include @@ -26,7 +26,7 @@ void *filter_json(void *json, char *filter) { } void add_item_to_array(void *json, void *item) { - json_array_append(json, item); + json_array_append_new(json, item); } void json_append_route(sonic_server_request_t *req) { From 3325afbf1e1f921d78a266af0a5aeeb9d74d81c9 Mon Sep 17 00:00:00 2001 From: net Date: Fri, 17 May 2024 14:05:19 +0100 Subject: [PATCH 2/2] shogdb: update makefile --- shogdb/makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shogdb/makefile b/shogdb/makefile index edfa868..fce5f0f 100644 --- a/shogdb/makefile +++ b/shogdb/makefile @@ -7,7 +7,7 @@ CC = gcc endif CFLAGS += -g -std=c11 -D_GNU_SOURCE -I ../netlibc/include $$(pkg-config --cflags openssl) -LDFLAGS += -pthread $$(pkg-config --libs openssl) -ljansson +LDFLAGS += -pthread $$(pkg-config --libs openssl) -ljansson $$(pkg-config --cflags --libs uuid) # sanitizers (only on linux) ifeq ($(shell uname), Linux)