nserver

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit dd0d4c75b2a78f2445eaf1f13bbaa644e578026a
parent 9adf646a9857c4073aa4e076269a84ee521a13ed
Author: rsiddharth <s@ricketyspace.net>
Date:   Sat, 11 Apr 2020 18:51:34 -0400

nserver: Add ssload.

* nserver/src/protocol.c (ssload): New function definition.
* nserver/src/protocol.h (ssload): New function declaration.
* nserver/tests/protocol_tests.c (test_ssload): New test.

Diffstat:
nserver/src/protocol.c | 45+++++++++++++++++++++++++++++++++++++++++++++
nserver/src/protocol.h | 1+
nserver/tests/protocol_tests.c | 12++++++++++++
3 files changed, 58 insertions(+), 0 deletions(-)

diff --git a/nserver/src/protocol.c b/nserver/src/protocol.c @@ -231,3 +231,48 @@ int ssstore(char *key) error: return -1; } + +int ssload(char *from, char *to) +{ + check(from != NULL && strlen(from) > 0, "from invalid"); + check(to != NULL && strlen(to) > 0, "to invalid"); + check(tst != NULL, "tstree not initialized"); + + // 1. Check if 'to' key already exists. + Record *rec = (Record *) TSTree_search(tst, to, strlen(to)); + + // 2. if 'to' key exists return immediately with -2. + if (rec != NULL && rec->deleted == 0) { + return -2; + } + + // 3. read 'from' key from database. + char *st_str = db_load(from); + check(st_str != NULL, "db load failed"); + + // 4. construct stats from string. + Stats *st = Stats_unstringify(st_str); + check(st != NULL, "stats unstringify failed"); + + // 5. create Record if needed. + if (rec == NULL) { + rec = (Record *) calloc(1, sizeof(Record)); + check_mem(rec); + } + + // 6. get things ready for insertiion + bstring tk = bfromcstr(to); + check(tk != NULL, "key creation failed"); + + rec->key = tk; + rec->st = st; + rec->deleted = 0; + + // 7. insert Record into 'to' key in the TSTree. + tst = TSTree_insert(tst, to, strlen(to), rec); + check(tst != NULL, "tstree insert failed"); + + return 0; + error: + return -1; +} diff --git a/nserver/src/protocol.h b/nserver/src/protocol.h @@ -23,6 +23,7 @@ double ssmean(char *key); char *ssdump(char *key); char *sslist(); int ssstore(char *key); +int ssload(char *from, char *to); #endif diff --git a/nserver/tests/protocol_tests.c b/nserver/tests/protocol_tests.c @@ -151,6 +151,17 @@ char *test_ssstore() return NULL; } +char *test_ssload() +{ + int rc = ssload("/crimson", "/crimson"); + mu_assert(rc == -2, "ssload failed 0"); + + rc = ssload("/crimson", "/amaranth"); + mu_assert(rc == 0, "ssload failed 1"); + + return NULL; +} + char *test_ssdelete() { int rc = 0; @@ -177,6 +188,7 @@ char *all_tests() mu_run_test(test_ssdump); mu_run_test(test_sslist); mu_run_test(test_ssstore); + mu_run_test(test_ssload); mu_run_test(test_ssdelete); return NULL;