Skip to content

Commit

Permalink
refactor(db): add read_string_from_file method
Browse files Browse the repository at this point in the history
  • Loading branch information
aloima committed Nov 5, 2024
1 parent 16fdc95 commit a294e91
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 50 deletions.
2 changes: 2 additions & 0 deletions headers/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ void close_logs();
bool is_integer(const char *value);
uint32_t get_digit_count(int64_t number);

void read_string_from_file(const int fd, string_t *string, const bool unallocated, const bool terminator);

uint64_t hash(char *key);
56 changes: 6 additions & 50 deletions src/database/get.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,7 @@ void get_all_keys(const off_t from) {
off_t start_at, end_at;

// Key reading
{
const uint8_t byte_count = first >> 6;
key.len = 0;
read(fd, &key.len, byte_count);

key.len = (key.len << 6) | (first & 0b111111);
key.value = malloc(key.len + 1),

read(fd, key.value, key.len);
key.value[key.len] = '\0';
}
read_string_from_file(fd, &key, true, true);

// Value reading
{
Expand Down Expand Up @@ -261,20 +251,10 @@ struct KVPair *get_data(const char *key) {
read(fd, &type, 1);
if (type == 0x17) break;

{
uint8_t first;
read(fd, &first, 1);

const uint8_t byte_count = first >> 6;
name.len = 0;
read(fd, &name.len, byte_count);
name.len = (name.len << 6) | (first & 0b111111);

name.value = realloc(name.value, name.len + 1);
read(fd, name.value, name.len);
name.value[name.len] = '\0';
}
// Element key reading
read_string_from_file(fd, &name, false, true);

// Element value reading
switch (type) {
case TELLY_NULL:
add_fv_to_hashtable(table, name, NULL, TELLY_NULL);
Expand All @@ -294,19 +274,7 @@ struct KVPair *get_data(const char *key) {

case TELLY_STR: {
string_t *string = malloc(sizeof(string_t));

uint8_t first;
read(fd, &first, 1);

const uint8_t byte_count = first >> 6;
string->len = 0;

read(fd, &string->len, byte_count);
string->len = (string->len << 6) | (first & 0b111111);

string->value = malloc(string->len);
read(fd, string->value, string->len);

read_string_from_file(fd, string, true, false);
add_fv_to_hashtable(table, name, string, TELLY_STR);
break;
}
Expand Down Expand Up @@ -353,19 +321,7 @@ struct KVPair *get_data(const char *key) {

case TELLY_STR: {
string_t *string = malloc(sizeof(string_t));

uint8_t first;
read(fd, &first, 1);

const uint8_t byte_count = first >> 6;
string->len = 0;

read(fd, &string->len, byte_count);
string->len = (string->len << 6) | (first & 0b111111);

string->value = malloc(string->len);
read(fd, string->value, string->len);

read_string_from_file(fd, string, true, false);
node = create_listnode(string, TELLY_STR);
break;
}
Expand Down
22 changes: 22 additions & 0 deletions src/utils/string.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
#include "../../headers/utils.h"

#include <stdint.h>
#include <stdlib.h>
#include <ctype.h>

#include <unistd.h>

void to_uppercase(char *in, char *out) {
while (*in != '\0') *(out++) = toupper(*(in++));
*out = '\0';
}

void read_string_from_file(const int fd, string_t *string, const bool unallocated, const bool terminator) {
uint8_t first;
read(fd, &first, 1);

const uint8_t byte_count = first >> 6;
string->len = 0;
read(fd, &string->len, byte_count);
string->len = (string->len << 6) | (first & 0b111111);

const uint32_t size = terminator ? (string->len + 1) : string->len;

if (unallocated) string->value = malloc(size);
else string->value = realloc(string->value, size);

read(fd, string->value, string->len);
if (terminator) string->value[string->len] = '\0';
}

0 comments on commit a294e91

Please sign in to comment.