Skip to content

Commit

Permalink
Merge pull request #100 from thenetrunna/master
Browse files Browse the repository at this point in the history
shogdb: minor updates
  • Loading branch information
thenetrunna authored May 17, 2024
2 parents 0329310 + 3325afb commit 16b348f
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 38 deletions.
2 changes: 1 addition & 1 deletion makefiles/build.mk

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions shogdb/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.



Expand Down Expand Up @@ -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

6 changes: 3 additions & 3 deletions shogdb/examples/simple.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../include/cjson.h"
#include "../include/jansson.h"
#include "../src/client/client.h"

int main() {
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions shogdb/makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions shogdb/src/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
52 changes: 25 additions & 27 deletions shogdb/src/db/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions shogdb/src/db/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <netlibc/mem.h>
Expand All @@ -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) {
Expand Down

0 comments on commit 16b348f

Please sign in to comment.