diff options
author | rsiddharth <s@ricketyspace.net> | 2020-04-11 18:51:34 -0400 |
---|---|---|
committer | rsiddharth <s@ricketyspace.net> | 2020-04-17 20:56:37 -0400 |
commit | dd0d4c75b2a78f2445eaf1f13bbaa644e578026a (patch) | |
tree | b71272c67269be43ffedd5d926f72ef53f95d3e1 /nserver/src/protocol.c | |
parent | 9adf646a9857c4073aa4e076269a84ee521a13ed (diff) |
nserver: Add ssload.
* nserver/src/protocol.c (ssload): New function definition.
* nserver/src/protocol.h (ssload): New function declaration.
* nserver/tests/protocol_tests.c (test_ssload): New test.
Diffstat (limited to 'nserver/src/protocol.c')
-rw-r--r-- | nserver/src/protocol.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/nserver/src/protocol.c b/nserver/src/protocol.c index 61f4668..5fab817 100644 --- a/nserver/src/protocol.c +++ b/nserver/src/protocol.c @@ -231,3 +231,48 @@ int ssstore(char *key) error: return -1; } + +int ssload(char *from, char *to) +{ + check(from != NULL && strlen(from) > 0, "from invalid"); + check(to != NULL && strlen(to) > 0, "to invalid"); + check(tst != NULL, "tstree not initialized"); + + // 1. Check if 'to' key already exists. + Record *rec = (Record *) TSTree_search(tst, to, strlen(to)); + + // 2. if 'to' key exists return immediately with -2. + if (rec != NULL && rec->deleted == 0) { + return -2; + } + + // 3. read 'from' key from database. + char *st_str = db_load(from); + check(st_str != NULL, "db load failed"); + + // 4. construct stats from string. + Stats *st = Stats_unstringify(st_str); + check(st != NULL, "stats unstringify failed"); + + // 5. create Record if needed. + if (rec == NULL) { + rec = (Record *) calloc(1, sizeof(Record)); + check_mem(rec); + } + + // 6. get things ready for insertiion + bstring tk = bfromcstr(to); + check(tk != NULL, "key creation failed"); + + rec->key = tk; + rec->st = st; + rec->deleted = 0; + + // 7. insert Record into 'to' key in the TSTree. + tst = TSTree_insert(tst, to, strlen(to), rec); + check(tst != NULL, "tstree insert failed"); + + return 0; + error: + return -1; +} |