Add very simple add-only persistence model
ci/woodpecker/push/woodpecker Pipeline was successful
Details
ci/woodpecker/push/woodpecker Pipeline was successful
Details
parent
919976073f
commit
9c3af74e1b
|
@ -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
|
|
@ -15,6 +15,9 @@ int main() {
|
||||||
|
|
||||||
TernaryTrie *trie = ternarytrie_init();
|
TernaryTrie *trie = ternarytrie_init();
|
||||||
|
|
||||||
|
std::string file_path = "lander.data";
|
||||||
|
ternarytrie_populate(trie, file_path.c_str());
|
||||||
|
|
||||||
crow::SimpleApp app;
|
crow::SimpleApp app;
|
||||||
|
|
||||||
CROW_ROUTE(app, "/<string>")
|
CROW_ROUTE(app, "/<string>")
|
||||||
|
|
|
@ -24,6 +24,8 @@ typedef struct ttrie TernaryTrie;
|
||||||
*/
|
*/
|
||||||
TernaryTrie* ternarytrie_init();
|
TernaryTrie* ternarytrie_init();
|
||||||
|
|
||||||
|
void ternarytrie_populate(TernaryTrie* trie, const char* file_path);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* De-allocate a trie by freeing the memory occupied by this trie.
|
* De-allocate a trie by freeing the memory occupied by this trie.
|
||||||
*
|
*
|
||||||
|
|
|
@ -3,10 +3,12 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
typedef struct ttrie {
|
typedef struct ttrie {
|
||||||
TernaryTrieNode *root;
|
TernaryTrieNode *root;
|
||||||
size_t size;
|
size_t size;
|
||||||
|
char* file_path;
|
||||||
} TernaryTrie;
|
} TernaryTrie;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -31,6 +33,49 @@ void ternarytrie_free(TernaryTrie *trie) {
|
||||||
free(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 {
|
typedef struct searchresult {
|
||||||
TernaryTrieNode *parent;
|
TernaryTrieNode *parent;
|
||||||
TernaryTrieNode *child;
|
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
|
* @return true if the string wasn't present in the trie and thus added, false
|
||||||
* otherwise
|
* 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
|
// Edge case for empty string
|
||||||
if (string[0] == DELIMITER) {
|
if (string[0] == DELIMITER) {
|
||||||
if (trie->root->type == 0) {
|
if (trie->root->type == 0) {
|
||||||
|
@ -195,6 +240,31 @@ bool ternarytrie_add(TernaryTrie *trie, const char *string, const char *payload)
|
||||||
return true;
|
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.
|
* Remove the given string from a TernaryTrie.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue