nserver

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

commit a89b692e370e65b0fb087a2f843faf9ce1d5a6ae
parent 64ab0153238cd3d06296cd31bd9bc09ba8545f33
Author: rsiddharth <s@ricketyspace.net>
Date:   Sat,  2 Nov 2019 11:05:54 -0400

nserver: protocol: Define ssdelete.

* nserver/src/protocol.c (ssdelete): New function.
* nserver/tests/protocol_tests.c (test_ssdelete): New test.
(all_tests): Add test_ssdelete.

Diffstat:
nserver/src/protocol.c | 27+++++++++++++++++++++++++++
nserver/tests/protocol_tests.c | 17+++++++++++++++++
2 files changed, 44 insertions(+), 0 deletions(-)

diff --git a/nserver/src/protocol.c b/nserver/src/protocol.c @@ -37,6 +37,33 @@ int sscreate(char *key) return -1; } +int ssdelete(char *key) +{ + check(hash != NULL, "hash not initialized"); + + // 1. create bstring from 'key'. + bstring k = bfromcstr(key); + check(k != NULL, "key creation failed"); + + // 2. check if key exists. + Stats *st = (Stats *) Hashmap_get(hash, k); + if (st == NULL) { + // key does not exists. + return 0; + } + + // 3. delete key. + st = (Stats *) Hashmap_delete(hash, k); + check(st != NULL, "hash key delete failed"); + + // 4. clean up the stats for this key. + free(st); + + return 0; + error: + return -1; +} + int sssample(char *key, double s) { check(hash != NULL, "hash not initialized"); diff --git a/nserver/tests/protocol_tests.c b/nserver/tests/protocol_tests.c @@ -58,6 +58,22 @@ char *test_ssmean() return NULL; } +char *test_ssdelete() +{ + int rc = 0; + + rc = ssdelete("crimson"); + mu_assert(rc == 0, "delete failed 0"); + + rc = ssdelete("/vermilion"); + mu_assert(rc == 0, "delete failed 1"); + + rc = ssdelete("/ruby"); + mu_assert(rc == 0, "delete failed 2"); + + return NULL; +} + char *all_tests() { mu_suite_start(); @@ -65,6 +81,7 @@ char *all_tests() mu_run_test(test_sscreate); mu_run_test(test_sssample); mu_run_test(test_ssmean); + mu_run_test(test_ssdelete); return NULL; }