Add very simple add-only persistence model
ci/woodpecker/push/woodpecker Pipeline was successful Details

trie-skips
Jef Roosens 2022-11-15 22:49:36 +01:00
parent 919976073f
commit 9c3af74e1b
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 89 additions and 1 deletions

13
landerctl 100755
View File

@ -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

View File

@ -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, "/<string>")

View File

@ -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.
*

View File

@ -3,10 +3,12 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
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.
*