summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2020-04-11 12:53:53 -0400
committerrsiddharth <s@ricketyspace.net>2020-04-17 20:56:37 -0400
commit51a3127d1de8903cfed7837029af223f7b58db07 (patch)
treeadc2881d192ec99572e2d48106eea51c3edcd499
parent49edbb003a0aef0fd864e5f2a12228807618a175 (diff)
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.
-rw-r--r--nserver/src/protocol.c24
-rw-r--r--nserver/src/protocol.h2
-rw-r--r--nserver/tests/protocol_tests.c13
3 files changed, 39 insertions, 0 deletions
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 <bstrlib.h>
#include <darray.h>
+#include <db.h>
#include <hashmap.h>
#include <stats.h>
#include <tstree.h>
@@ -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;