feat: added randomly generated URLs
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Jef Roosens 2022-11-21 12:03:16 +01:00
parent 94f7400169
commit 51fc2867a8
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
6 changed files with 190 additions and 87 deletions

View file

@ -1,14 +1,17 @@
#include "ternarytrie.h"
#include "ternarytrie_node.c"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <pthread.h>
#include "ternarytrie.h"
#include "ternarytrie_node.c"
typedef struct ttrie {
TernaryTrieNode *root;
size_t size;
char* file_path;
pthread_rwlock_t lock;
} TernaryTrie;
/**
@ -17,10 +20,11 @@ typedef struct ttrie {
* @return pointer to the empty TernaryTrie
*/
TernaryTrie *ternarytrie_init() {
TernaryTrie *node = calloc(1, sizeof(TernaryTrie));
node->root = ttnode_init();
TernaryTrie *trie = calloc(1, sizeof(TernaryTrie));
trie->root = ttnode_init();
pthread_rwlock_init(&trie->lock, NULL);
return node;
return trie;
}
/**
@ -36,44 +40,44 @@ void ternarytrie_free(TernaryTrie *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);
trie->file_path = my_strdup(file_path);
FILE* fp = fopen(file_path, "r");
FILE* fp = fopen(file_path, "r");
// TODO properly handle this
if (fp == NULL) {
return;
// 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++;
}
// We read in lines of at most 8192 characters (sounds like enough)
char buffer[8192];
int i, j;
// Split the buffer into two strings, the key and the payload
buffer[i] = '\0';
while (fgets(buffer, 8192, fp)) {
printf("%s", buffer);
// Find index of space character
i = 0;
j = i + 1;
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);
// Now remove the newline character
while (buffer[j] != '\n') {
j++;
}
fclose(fp);
buffer[j] = '\0';
ternarytrie_add_internal(trie, buffer, buffer + i + 1);
}
fclose(fp);
}
typedef struct searchresult {
@ -140,13 +144,19 @@ SearchResult ternarytrie_search_node(TernaryTrie *trie, const char *string) {
* @return true if the string is present in the trie, false otherwise
*/
char * ternarytrie_search(TernaryTrie *trie, const char *string) {
pthread_rwlock_rdlock(&trie->lock);
SearchResult res = ternarytrie_search_node(trie, string);
char* return_value = NULL;
if (res.child != NULL) {
return res.child->payload;
return_value = res.child->payload;
}
return NULL;
pthread_rwlock_unlock(&trie->lock);
return return_value;
}
/**
@ -240,28 +250,78 @@ bool ternarytrie_add_internal(TernaryTrie *trie, const char *string, const char
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;
}
bool ternarytrie_add_persistent(TernaryTrie *trie, const char *string, const char *payload) {
bool return_value = 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);
if (trie->file_path != NULL) {
// Easiest way to make sure we don't add duplicate entries
// We use an internal function that doesn't require a read lock, as we're
// already inside a write lock
if (ternarytrie_search_node(trie, string).child != NULL) {
return false;
}
return ternarytrie_add_internal(trie, string, payload);
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);
}
// This function *should* always return true. Otherwise, the function would've
// exited because the string was found in the trie.
return ternarytrie_add_internal(trie, string, payload);
}
bool ternarytrie_add(TernaryTrie *trie, const char *string, const char *payload) {
pthread_rwlock_wrlock(&trie->lock);
bool return_value = ternarytrie_add_persistent(trie, string, payload);
pthread_rwlock_unlock(&trie->lock);
return return_value;
}
char* ternarytrie_add_random(TernaryTrie *trie, const char *payload) {
pthread_rwlock_wrlock(&trie->lock);
// Generate random key
bool ok = false;
char *key = malloc(RANDOM_KEY_LENGTH + 1);
key[RANDOM_KEY_LENGTH] = '\0';
// We naively generate new keys until we find a key that isn't in the trie
// yet. With charset_len ** RANDOM_KEY_LENGTH sufficiently large, this isn't a
// problem, because the chances of collisions are extremely small.
while (!ok) {
for (int i = 0; i < RANDOM_KEY_LENGTH; i++) {
key[i] = charset[rand() % charset_len];
}
ok = ternarytrie_search_node(trie, key).child == NULL;
}
bool res = ternarytrie_add_persistent(trie, key, payload);
char *return_value;
if (res) {
return_value = key;
} else {
return_value = NULL;
free(key);
}
pthread_rwlock_unlock(&trie->lock);
return return_value;
}
@ -273,13 +333,18 @@ bool ternarytrie_add(TernaryTrie *trie, const char *string, const char *payload)
* @return true if the string was in the trie and thus removed, false otherwise
*/
bool ternarytrie_remove(TernaryTrie *trie, const char *string) {
pthread_rwlock_wrlock(&trie->lock);
bool return_value = false;
SearchResult res = ternarytrie_search_node(trie, string);
if (res.child == NULL) {
return false;
goto end;
}
trie->size--;
return_value = true;
if (res.parent != NULL) {
// We're removing a full leaf, so we calculate the offset of the character
@ -303,7 +368,7 @@ bool ternarytrie_remove(TernaryTrie *trie, const char *string) {
} else {
res.child->type = 0;
return true;
goto end;
}
ttnode_free(res.child);
@ -313,7 +378,10 @@ bool ternarytrie_remove(TernaryTrie *trie, const char *string) {
res.child->type = 0;
}
return true;
end:
pthread_rwlock_unlock(&trie->lock);
return return_value;
}
/**