From 9edac00eacb582c091c76d96ba5ba73ac9101824 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Mon, 3 Feb 2020 22:55:25 -0500 Subject: nserver/src/protocol.c: Refactor sscreate. * nserver/src/protocol.c (ssinit): Remove function. (sscreate): Refactor to use TSTree instead of Hashmap. --- nserver/src/protocol.c | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/nserver/src/protocol.c b/nserver/src/protocol.c index 1a388c3..d08eeda 100644 --- a/nserver/src/protocol.c +++ b/nserver/src/protocol.c @@ -1,36 +1,46 @@ #include 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: -- cgit v1.2.3