feat: use custom binary file format for data

This commit is contained in:
Jef Roosens 2022-12-07 23:55:46 +01:00
parent c99bc83015
commit 4b772d003b
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
4 changed files with 69 additions and 59 deletions

View file

@ -43,42 +43,45 @@ TrieExitCode trie_init(Trie **trie_ptr, const char *file_path) {
return FileError;
}
// We read in lines of at most 8192 characters (sounds like enough)
char buffer[8192];
EntryType type;
Entry *entry;
char *string;
int i, j;
uint64_t key_size, data_size;
char *key;
void *data;
TrieExitCode status;
size_t items_read;
while (fgets(buffer, 8192, fp)) {
i = 0;
// Move index in buffer until we encounter first space character
while (buffer[i] != ' ') {
i++;
while (!feof(fp)) {
items_read = fread(&key_size, sizeof(uint64_t), 1, fp);
if (items_read < 1) {
break;
}
// Split the buffer into two strings, the key and the payload
buffer[i] = '\0';
key = malloc(key_size + 1);
type = entry_type_from_char(buffer[i + 1]);
// Skip type character & its surrounding spaces
j = i + 3;
// Now remove the newline character
while (buffer[j] != '\n') {
j++;
items_read = fread(key, 1, key_size, fp);
if (items_read < key_size) {
break;
}
buffer[j] = '\0';
key[key_size] = '\0';
entry = entry_new(type, buffer + i + 3);
status = trie_add_no_lock(trie, buffer, entry);
items_read = fread(&data_size, sizeof(uint64_t), 1, fp);
if (items_read < 1) {
break;
}
data = malloc(data_size);
items_read = fread(data, 1, data_size, fp);
if (items_read < data_size) {
break;
}
status = trie_add_no_lock(trie, key, data);
if (status != Ok) {
trie_free(trie);
free(key);
free(data);
return status;
}
}
@ -275,7 +278,8 @@ TrieExitCode trie_add_no_lock(Trie *trie, const char *string, void *data) {
return Ok;
}
TrieExitCode trie_add(Trie *trie, const char *key, void *entry) {
TrieExitCode trie_add(Trie *trie, const char *key, void *data,
uint64_t data_len) {
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
@ -290,23 +294,24 @@ TrieExitCode trie_add(Trie *trie, const char *key, void *entry) {
return FileError;
}
/* fputs(key, fp); */
/* fputs(" ", fp); */
/* fputc(entry_type_to_char(entry->type), fp); */
/* fputs(" ", fp); */
/* fputs(entry->string, fp); */
/* fputs("\n", fp); */
// First we write the key, then the actual data
uint64_t key_len = (uint64_t)strlen(key);
fwrite(&key_len, 1, sizeof(uint64_t), fp);
fwrite(key, 1, key_len, fp);
fwrite(&data_len, 1, sizeof(uint64_t), fp);
fwrite(data, 1, data_len, fp);
fclose(fp);
}
// This function *should* always return Ok. Otherwise, the function would've
// exited because the string was found in the trie.
return trie_add_no_lock(trie, key, entry);
return trie_add_no_lock(trie, key, data);
}
TrieExitCode trie_add_random(Trie *trie, char **key_ptr, void *data,
bool secure) {
uint64_t data_len, bool secure) {
// Generate random key
bool ok = false;
int key_length = secure ? RANDOM_KEY_LENGTH_LONG : RANDOM_KEY_LENGTH_SHORT;
@ -324,7 +329,7 @@ TrieExitCode trie_add_random(Trie *trie, char **key_ptr, void *data,
ok = trie_search_node(trie, key).child == NULL;
}
TrieExitCode return_value = trie_add(trie, key, data);
TrieExitCode return_value = trie_add(trie, key, data, data_len);
if (return_value == Ok) {
*key_ptr = key;

View file

@ -23,23 +23,20 @@ char entry_type_to_char(EntryType et) {
}
}
Entry *entry_new(EntryType type, const char *string) {
Entry *entry = malloc(sizeof(Entry));
uint64_t entry_new(Entry **entry_ptr, EntryType type, const char *string) {
size_t str_len = strlen(string);
uint64_t entry_size = sizeof(EntryType) + str_len + 1;
Entry *entry = malloc(entry_size);
entry->type = type;
if (string != NULL) {
entry->string = strdup(string);
memcpy(entry->string, string, str_len + 1);
} else {
entry->string = NULL;
entry->string[0] = '\0';
}
return entry;
*entry_ptr = entry;
return entry_size;
}
void entry_free(Entry *entry) {
if (entry->string != NULL) {
free(entry->string);
}
free(entry);
}
void entry_free(Entry *entry) { free(entry); }