summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2020-02-03 22:56:34 -0500
committerrsiddharth <s@ricketyspace.net>2020-04-17 20:56:36 -0400
commitc3cbd1174f15770656583b9e15664270c28512de (patch)
treef5114db00acfe29932e736d3e543f1422ef8d0d8
parent9edac00eacb582c091c76d96ba5ba73ac9101824 (diff)
nserver/src/protocol.c: Update ssample.
* nserver/src/protocol.c (ssample): Refactor to use TSTree. * nserver/tests/protocol_tests.c (test_ssample): Update test.
-rw-r--r--nserver/src/protocol.c21
-rw-r--r--nserver/tests/protocol_tests.c6
2 files changed, 13 insertions, 14 deletions
diff --git a/nserver/src/protocol.c b/nserver/src/protocol.c
index d08eeda..0872bdb 100644
--- a/nserver/src/protocol.c
+++ b/nserver/src/protocol.c
@@ -76,21 +76,20 @@ int ssdelete(char *key)
double sssample(char *key, double s)
{
- check(hash != NULL, "hash not initialized");
+ check(key != NULL || strlen(key) < 1, "key invalid");
+ check(tst != NULL, "tstree not initialized");
- // 1. create bstring from 'key'.
- bstring k = bfromcstr(key);
- check(k != NULL, "key creation failed");
+ // 1. Try to get Record for key.
+ Record *rec = (Record *) TSTree_search(tst, key, strlen(key));
- // 2. try to get Stats for key.
- Stats *st = (Stats *) Hashmap_get(hash, k);
- check(st != NULL, "stats not found for key");
+ check(rec != NULL, "record not found");
+ check(rec->st != NULL, "record's st invalid");
- // 3. sample!
- Stats_sample(st, s);
+ // 2. Sample!
+ Stats_sample(rec->st, s);
- // 4. get mean.
- double m = Stats_mean(st);
+ // 3. Get mean.
+ double m = Stats_mean(rec->st);
return m;
error:
diff --git a/nserver/tests/protocol_tests.c b/nserver/tests/protocol_tests.c
index 146fc08..cb04581 100644
--- a/nserver/tests/protocol_tests.c
+++ b/nserver/tests/protocol_tests.c
@@ -18,13 +18,13 @@ char *test_sssample()
{
double mean = 0;
- mean = sssample("crimson", 3);
+ mean = sssample("/crimson", 3);
mu_assert(mean == 3.0, "sssample failed 0");
- mean = sssample("crimson", 9);
+ mean = sssample("/crimson", 9);
mu_assert(mean == 6.0, "sssample failed 1");
- mean = sssample("crimson", 12);
+ mean = sssample("/crimson", 12);
mu_assert(mean == 8.0, "sssample failed 2");
mean = sssample("/vermilion", 20);