From 9c3af74e1b23f192b3f1d4694d1c82f5e7c9fb6c Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 15 Nov 2022 22:49:36 +0100 Subject: [PATCH] Add very simple add-only persistence model --- landerctl | 13 +++++++ src/main.cpp | 3 ++ tries/include/ternarytrie.h | 2 ++ tries/src/ternarytrie.c | 72 ++++++++++++++++++++++++++++++++++++- 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100755 landerctl diff --git a/landerctl b/landerctl new file mode 100755 index 0000000..518b019 --- /dev/null +++ b/landerctl @@ -0,0 +1,13 @@ +#!/usr/bin/env sh + +API_KEY=test +URL=http://localhost:18080 + +if [ "$1" = add ]; then + curl \ + -XPOST \ + -d "$3" \ + -H "X-Api-Key: $API_KEY" \ + "$URL/$2" + echo "$URL/$2" +fi diff --git a/src/main.cpp b/src/main.cpp index d96ddc5..b076fe4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,6 +15,9 @@ int main() { TernaryTrie *trie = ternarytrie_init(); + std::string file_path = "lander.data"; + ternarytrie_populate(trie, file_path.c_str()); + crow::SimpleApp app; CROW_ROUTE(app, "/") diff --git a/tries/include/ternarytrie.h b/tries/include/ternarytrie.h index 7d7a308..ffed4f5 100644 --- a/tries/include/ternarytrie.h +++ b/tries/include/ternarytrie.h @@ -24,6 +24,8 @@ typedef struct ttrie TernaryTrie; */ TernaryTrie* ternarytrie_init(); +void ternarytrie_populate(TernaryTrie* trie, const char* file_path); + /** * De-allocate a trie by freeing the memory occupied by this trie. * diff --git a/tries/src/ternarytrie.c b/tries/src/ternarytrie.c index 59963bd..fe95468 100644 --- a/tries/src/ternarytrie.c +++ b/tries/src/ternarytrie.c @@ -3,10 +3,12 @@ #include #include #include +#include typedef struct ttrie { TernaryTrieNode *root; size_t size; + char* file_path; } TernaryTrie; /** @@ -31,6 +33,49 @@ void ternarytrie_free(TernaryTrie *trie) { free(trie); } +bool ternarytrie_add_internal(TernaryTrie *trie, const char *string, const char *payload); + +void ternarytrie_populate(TernaryTrie *trie, const char *file_path) { + trie->file_path = my_strdup(file_path); + + FILE* fp = fopen(file_path, "r"); + + // TODO properly handle this + if (fp == NULL) { + return; + } + + // We read in lines of at most 8192 characters (sounds like enough) + char buffer[8192]; + int i, j; + + while (fgets(buffer, 8192, fp)) { + printf("%s", buffer); + // Find index of space character + i = 0; + + while (buffer[i] != ' ') { + i++; + } + + // Split the buffer into two strings, the key and the payload + buffer[i] = '\0'; + + j = i + 1; + + // Now remove the newline character + while (buffer[j] != '\n') { + j++; + } + + buffer[j] = '\0'; + + ternarytrie_add_internal(trie, buffer, buffer + i + 1); + } + + fclose(fp); +} + typedef struct searchresult { TernaryTrieNode *parent; TernaryTrieNode *child; @@ -112,7 +157,7 @@ char * ternarytrie_search(TernaryTrie *trie, const char *string) { * @return true if the string wasn't present in the trie and thus added, false * otherwise */ -bool ternarytrie_add(TernaryTrie *trie, const char *string, const char *payload) { +bool ternarytrie_add_internal(TernaryTrie *trie, const char *string, const char *payload) { // Edge case for empty string if (string[0] == DELIMITER) { if (trie->root->type == 0) { @@ -195,6 +240,31 @@ bool ternarytrie_add(TernaryTrie *trie, const char *string, const char *payload) return true; } +bool ternarytrie_add(TernaryTrie *trie, const char *string, const char *payload) { + if (trie->file_path != NULL) { + // Easiest way to make sure we don't add duplicate entries + if (ternarytrie_search(trie, string) != NULL) { + return false; + } + + FILE *fp = fopen(trie->file_path, "a"); + + if (fp == NULL) { + return false; + } + + fputs(string, fp); + fputs(" ", fp); + fputs(payload, fp); + fputs("\n", fp); + + fclose(fp); + } + + return ternarytrie_add_internal(trie, string, payload); +} + + /** * Remove the given string from a TernaryTrie. *