From 51a3127d1de8903cfed7837029af223f7b58db07 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sat, 11 Apr 2020 12:53:53 -0400 Subject: nserver: Add ssstore. * nserver/src/protocol.c (ssstore): New function definition. * nserver/src/protocol.h (ssstore): New function declaration. * nserver/tests/protocol_tests.c (test_ssstore): New test. --- nserver/src/protocol.c | 24 ++++++++++++++++++++++++ nserver/src/protocol.h | 2 ++ nserver/tests/protocol_tests.c | 13 +++++++++++++ 3 files changed, 39 insertions(+) diff --git a/nserver/src/protocol.c b/nserver/src/protocol.c index 84e7323..9c145a6 100644 --- a/nserver/src/protocol.c +++ b/nserver/src/protocol.c @@ -207,3 +207,27 @@ char *sslist() return NULL; } +int ssstore(char *key) +{ + check(key != NULL && strlen(key) > 0, "key invalid"); + check(tst != NULL, "tstree not initialized"); + + // 1. create bstring from 'key'. + Record *rec = (Record *) TSTree_search(tst, key, strlen(key)); + + check(rec != NULL, "record not found"); + check(rec->st != NULL, "stats not found for key"); + check(rec->deleted != 1, "record was deleted"); + + // 2. stringify the stats. + char *st_str = Stats_stringify(rec->st); + check(st_str != NULL, "stats stringify failed"); + + // 3. store stats in db. + int rc = db_store(key, st_str); + check(rc == 0, "db store failed"); + + return 0; + error: + return -1; +} diff --git a/nserver/src/protocol.h b/nserver/src/protocol.h index eec38ab..5b12bca 100644 --- a/nserver/src/protocol.h +++ b/nserver/src/protocol.h @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -21,6 +22,7 @@ double sssample(char *key, double s); double ssmean(char *key); char *ssdump(char *key); char *sslist(); +int ssstore(char *key); #endif diff --git a/nserver/tests/protocol_tests.c b/nserver/tests/protocol_tests.c index aa66707..90e092d 100644 --- a/nserver/tests/protocol_tests.c +++ b/nserver/tests/protocol_tests.c @@ -139,6 +139,18 @@ char *test_sslist() return NULL; } +char *test_ssstore() +{ + + int rc = ssstore("/crimson"); + mu_assert(rc == 0, "store /crimson failed"); + + rc = ssstore("/nonexistent"); + mu_assert(rc == -1, "store /nonexistent failed"); + + return NULL; +} + char *test_ssdelete() { int rc = 0; @@ -164,6 +176,7 @@ char *all_tests() mu_run_test(test_ssmean); mu_run_test(test_ssdump); mu_run_test(test_sslist); + mu_run_test(test_ssstore); mu_run_test(test_ssdelete); return NULL; -- cgit v1.2.3