nserver

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

commit 9edac00eacb582c091c76d96ba5ba73ac9101824
parent 701bf4e3d27395a44975d084de811fc0ab386f18
Author: rsiddharth <s@ricketyspace.net>
Date:   Mon,  3 Feb 2020 22:55:25 -0500

nserver/src/protocol.c: Refactor sscreate.

* nserver/src/protocol.c (ssinit): Remove function.
(sscreate): Refactor to use TSTree instead of Hashmap.

Diffstat:
nserver/src/protocol.c | 46++++++++++++++++++++++++++++------------------
1 file changed, 28 insertions(+), 18 deletions(-)

diff --git a/nserver/src/protocol.c b/nserver/src/protocol.c @@ -1,36 +1,46 @@ #include <protocol.h> static Hashmap *hash; +static TSTree *tst; -int ssinit() +int sscreate(char *key) { - if (hash == NULL) { - hash = Hashmap_create(NULL, NULL); - check(hash != NULL, "unable to create hashmap"); - } + check(key != NULL || strlen(key) < 1, "key invalid"); - return 0; - error: - return -1; -} + // 1. Check if key is already in the tree. + Record *rec = (Record *) TSTree_search(tst, key, strlen(key)); -int sscreate(char *key) -{ - int rc = 0; + // If it's already there; there's nothing to do. + if (rec != NULL && rec-> deleted == 0) { + return 1; + } + // If it's already there and deleted, undelete it. + if (rec != NULL && rec->deleted == 1) { + rec->deleted = 0; - check(ssinit() == 0, "ssinit failed"); + return 2; + } - // 1. create bstring from 'key'. + // 2. Create bstring from 'key'. bstring k = bfromcstr(key); check(k != NULL, "key creation failed"); - // 2. allocate fresh Stats. + // 3. Allocate fresh Stats. Stats *st = Stats_create(); check(st != NULL, "stats creation failed"); - // 3. add to hashmap. - rc = Hashmap_set(hash, k, st); - check(rc == 0, "hashmap set failed"); + // 4. Create Record. + rec = (Record *) calloc(1, sizeof(Record)); + check_mem(rec); + + // 5. Initialize Record. + rec->key = k; + rec->st = st; + rec->deleted = 0; + + // 6. Add Record to tree. + tst = TSTree_insert(tst, key, strlen(key), rec); + check(tst != NULL, "tstree insert failed"); return 0; error: