Skip to content

Commit

Permalink
Merge pull request #97 from thenetrunna/master
Browse files Browse the repository at this point in the history
implement shogdb client library and client CLI
  • Loading branch information
thenetrunna authored May 6, 2024
2 parents be6035f + 8123674 commit ee3dffd
Show file tree
Hide file tree
Showing 23 changed files with 650 additions and 217 deletions.
1 change: 1 addition & 0 deletions deploy/1/dbconfig.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ host = "127.0.0.1"
port = 4961

[save]
enabled = true
path = "./save.sdb"
interval = 2 # seconds
1 change: 1 addition & 0 deletions deploy/2/dbconfig.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ host = "127.0.0.1"
port = 6961

[save]
enabled = true
path = "./save.sdb"
interval = 2 # seconds
3 changes: 2 additions & 1 deletion deploy/3/dbconfig.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ host = "127.0.0.1"
port = 5961

[save]
enabled = true
path = "./save.sdb"
interval = 2 # seconds
interval = 2 # seconds
16 changes: 8 additions & 8 deletions makefiles/build.mk

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

2 changes: 1 addition & 1 deletion makefiles/package.mk

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

2 changes: 1 addition & 1 deletion makefiles/tools.mk

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

6 changes: 6 additions & 0 deletions netlibc/include/netlibc.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ typedef size_t usize;
#define S64_FORMAT_SPECIFIER "%ld"
#endif

#ifdef __APPLE__
#define F64_FORMAT_SPECIFIER "%f"
#else
#define F64_FORMAT_SPECIFIER "%f"
#endif

#define LOOP() \
for (;;) { \
}
Expand Down
37 changes: 26 additions & 11 deletions shogdb/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
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 (https://shoggoth.network).
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 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 int, uint, float, string, bool and JSON.
ShogDB uses string keys, with values of different types like:
* int
* uint
* float
* string
* bool
* JSON.

Applications can communicate with ShogDB via HTTP requests. ShogDB exposes the HTTP API at http://127.0.0.1:6961 by default.

Expand All @@ -17,7 +23,24 @@ ShogDB is still early in development and should be considered unstable and exper
USING SHOGDB
===============================================================================

TODO

Using the HTTP API
================================================

Using the C client library
================================================

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.



===============================================================================
Expand Down Expand Up @@ -50,14 +73,6 @@ $ make

The above command will build a binary into ./target/shogdb

ShogDB requires a TOML configuration file ./dbconfig.toml located in the current working directory, and stores data on disk in a ./save.sdb file.


===============================================================================
DOCUMENTATION
===============================================================================

TODO

===============================================================================
CONTRIBUTING
Expand Down
39 changes: 39 additions & 0 deletions shogdb/examples/simple.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "../include/cjson.h"
#include "../src/client/client.h"

int main() {
NETLIBC_INIT();

shogdb_ctx_t *db_ctx = new_shogdb("http://127.0.0.1:6961");

UNWRAP(shogdb_set_int(db_ctx, "my_int", 69));
db_value_t *res_int = UNWRAP(shogdb_get(db_ctx, "my_int"));
printf("INT VALUE: %lld\n", res_int->value_int);

UNWRAP(shogdb_set_uint(db_ctx, "my_uint", 68));
db_value_t *res_uint = UNWRAP(shogdb_get(db_ctx, "my_uint"));
printf("UINT VALUE: %lld\n", res_uint->value_uint);

UNWRAP(shogdb_set_float(db_ctx, "my_float", 68.993));
db_value_t *res_float = UNWRAP(shogdb_get(db_ctx, "my_float"));
printf("FLOAT VALUE: %f\n", res_float->value_float);

UNWRAP(shogdb_set_str(db_ctx, "my_str", "deez nutz"));
db_value_t *res_str = UNWRAP(shogdb_get(db_ctx, "my_str"));
printf("STR VALUE: %s\n", res_str->value_str);

UNWRAP(shogdb_set_bool(db_ctx, "my_bool", 1));
db_value_t *res_bool = UNWRAP(shogdb_get(db_ctx, "my_bool"));
printf("BOOL VALUE: %d\n", res_bool->value_bool);

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

// UNWRAP(shogdb_delete(db_ctx, "my_str"));

char *res_all = UNWRAP(shogdb_print(db_ctx));
printf("ALL VALUES: %s\n", res_all);

free_shogdb(db_ctx);
}
34 changes: 24 additions & 10 deletions shogdb/makefile

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

73 changes: 73 additions & 0 deletions shogdb/src/cli/cli.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <netlibc/log.h>

#include "../../include/cjson.h"
#include "../client/client.h"

int main(int argc, char **argv) {
NETLIBC_INIT();

if (argc < 2) {
PANIC("no argument supplied");
}

char *address = argv[1];

shogdb_ctx_t *db_ctx = new_shogdb(address);

char input[100];

do {
// Prompt the user for input
printf("> ");

// Read the input
fgets(input, sizeof(input), stdin);

// Remove the trailing newline character
input[strcspn(input, "\n")] = '\0';

if (strlen(input) < 3) {
printf("invalid command - too short `%s`\n", input);
continue;
}

// Check if the user wants to exit
if (strcmp(input, "exit") == 0) {
break;
} else if (strcmp(input, "help") == 0) {
printf("Commands:\n");
printf("help - show this screen\n");
printf("exit - exit the program\n");
printf("print - print all the keys and their values\n");
printf("address - show the server address\n");
printf("\n");
} else if (strcmp(input, "print") == 0) {
char *res_all = UNWRAP(shogdb_print(db_ctx));
printf("%s\n", res_all);
} else if (strcmp(input, "address") == 0) {
printf("%s\n", address);
} else if (strncmp(input, "GET ", 4) == 0) {
char *key = &input[4];

db_value_t *res = UNWRAP(shogdb_get(db_ctx, key));
char *res_str = shogdb_print_value(res);

printf("%s\n", res_str);
} else if (strncmp(input, "SET ", 4) == 0) {
// char *key = &input[4];

// db_value_t *res = UNWRAP(shogdb_get(db_ctx, key));
// char *res_str = shogdb_print_value(res);

printf("OK\n");
} else {
printf("invalid command `%s`\n", input);
}
} while (1);

printf("Exiting ...\n");

free_shogdb(db_ctx);

return 0;
}
Loading

0 comments on commit ee3dffd

Please sign in to comment.