diff options
author | rsiddharth <s@ricketyspace.net> | 2020-02-03 22:55:25 -0500 |
---|---|---|
committer | rsiddharth <s@ricketyspace.net> | 2020-04-17 20:56:36 -0400 |
commit | 9edac00eacb582c091c76d96ba5ba73ac9101824 (patch) | |
tree | 3909e49f6c89528c7d889336f9f299e17676cb7f | |
parent | 701bf4e3d27395a44975d084de811fc0ab386f18 (diff) |
nserver/src/protocol.c: Refactor sscreate.
* nserver/src/protocol.c (ssinit): Remove function.
(sscreate): Refactor to use TSTree instead of Hashmap.
-rw-r--r-- | nserver/src/protocol.c | 46 |
1 files 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 <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: |