nserver

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

commit 09339f341bd467f3f44458bd130c301fe7ccce4a
parent 3052fc1c4621357b91ae61b09b8e4aafa41b4f47
Author: rsiddharth <s@ricketyspace.net>
Date:   Mon, 17 Feb 2020 19:51:10 -0500

nserver: Refactor sslist.

* nserver/src/protocol.c (sslist): Refactor sslist to use TSTree.
* nserver/tests/protocol_tests.c (test_sslist): Update test.

Diffstat:
nserver/src/protocol.c | 51+++++++++++++++++++++++++--------------------------
nserver/tests/protocol_tests.c | 8++++----
2 files changed, 29 insertions(+), 30 deletions(-)

diff --git a/nserver/src/protocol.c b/nserver/src/protocol.c @@ -161,43 +161,42 @@ char *ssdump(char *key) return NULL; } -char *sslist() +// meant to be used by sslist. +void traverse_tree(void *value, void *data) { - DArray *ks = NULL; + Record *rec = (Record *) value; + bstring bstr = (bstring) data; - check(hash != NULL, "hash not initiliazed"); + check(rec != NULL, "Record is NULL"); + check(bstr != NULL, "bstr is NULL"); + check(rec->key != NULL, "Record's key is NULL"); + check(blength(rec->key) > 0, "Record's key is an empty string"); - // 1. Get keys. - ks = Hashmap_keys(hash); - check(ks != NULL, "error getting keys"); + int rc = bconcat(bstr, rec->key); + check(rc == BSTR_OK, "bstr key concat failed"); - bstring ks_str = bfromcstr(""); - check(ks_str != NULL, "error creating keys_str"); + rc = bconchar(bstr, '\n'); + check(rc == BSTR_OK, "bstr newline concat failed"); - int i, rc; - bstring k = NULL; - for (i = 0; i < DArray_count(ks); ++i) { - k = (bstring) DArray_get(ks, i); - check(k != NULL, "k at %d", i); + error: + return; +} - rc = bconcat(ks_str, k); - check(rc == BSTR_OK, "bstr key concat failed"); - rc = bconchar(ks_str, '\n'); - check(rc == BSTR_OK, "bstr newline concat failed"); - } +char *sslist() +{ + check(tst != NULL, "tstree not initialized"); + + // 1. Create "accumulator" string. + bstring ks_str = bfromcstr(""); + check(ks_str != NULL, "error creating keys_str"); - // cleanup. - DArray_destroy(ks); + // 2. Accumulate keys into "accumulator" string. + TSTree_traverse(tst, traverse_tree, ks_str); + // 3. Return result. return bstr2cstr(ks_str, ' '); error: - - // cleanup - if (ks) { - DArray_destroy(ks); - } - return NULL; } diff --git a/nserver/tests/protocol_tests.c b/nserver/tests/protocol_tests.c @@ -102,9 +102,9 @@ char *test_sslist() { char *ks = sslist(); mu_assert(ks != NULL, "sslist failed"); - mu_assert(strlen(ks) == 19, "length check failed"); + mu_assert(strlen(ks) == 33, "length check failed"); - debug("KEYS:\n %s", ks); + debug("KEYS:\n%s", ks); return NULL; } @@ -133,8 +133,8 @@ char *all_tests() mu_run_test(test_sssample); mu_run_test(test_ssmean); mu_run_test(test_ssdump); - /*mu_run_test(test_sslist); - mu_run_test(test_ssdelete);*/ + mu_run_test(test_sslist); + /*mu_run_test(test_ssdelete);*/ return NULL; }