Skip to content

Commit

Permalink
feat(db): writing list values to database file
Browse files Browse the repository at this point in the history
  • Loading branch information
aloima committed Dec 13, 2024
1 parent 2d8eb15 commit 806c4d4
Showing 1 changed file with 94 additions and 47 deletions.
141 changes: 94 additions & 47 deletions src/database/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,75 +63,122 @@ void close_database_fd() {
close(fd);
}

static off64_t generate_value(char **data, struct KVPair *kv) {
off64_t len;
static void generate_name_key(char **data, off64_t *len, const string_t key) {
const uint8_t bit_count = log2(key.len) + 1;
const uint8_t byte_count = ceil((float) (bit_count - 6) / 8);
const uint8_t first = (byte_count << 6) | (key.len & 0b111111);
const uint32_t length_in_bytes = key.len >> 6;

*data = realloc(*data, (*len + key.len + byte_count + 1));

(*data)[*len] = first;
memcpy(*data + (*len += 1), &length_in_bytes, byte_count);
memcpy(*data + (*len += byte_count), key.value, key.len);
*len += key.len;
}

{
string_t key = kv->key;
static void generate_number_value(char **data, off64_t *len, const long *number) {
const uint32_t bit_count = log2(*number) + 1;
const uint32_t byte_count = (bit_count / 8) + 1;

const uint8_t bit_count = log2(key.len) + 1;
const uint8_t byte_count = ceil((float) (bit_count - 6) / 8);
const uint8_t first = (byte_count << 6) | (key.len & 0b111111);
const uint32_t length_in_bytes = key.len >> 6;
*data = realloc(*data, (*len + byte_count + 2));
(*data)[*len] = TELLY_NUM;
(*data)[*len += 1] = byte_count;
memcpy(*data + (*len += 1), number, byte_count);
*len += byte_count;
}

len = key.len + byte_count + 1;
*data = realloc(*data, len);
static void generate_string_value(char **data, off64_t *len, const string_t *string) {
const uint8_t bit_count = log2(string->len) + 1;
const uint8_t byte_count = ceil((float) (bit_count - 6) / 8);
const uint8_t first = (byte_count << 6) | (string->len & 0b111111);
const uint32_t length_in_bytes = string->len >> 6;

*data = realloc(*data, (*len + string->len + byte_count + 2));
(*data)[*len] = TELLY_STR;
(*data)[*len += 1] = first;
memcpy(*data + (*len += 1), &length_in_bytes, byte_count);
memcpy(*data + (*len += byte_count), string->value, string->len);
*len += string->len;
}

(*data)[0] = first;
memcpy(*data + 1, &length_in_bytes, byte_count);
memcpy(*data + byte_count + 1, key.value, key.len);
}
static void generate_boolean_value(char **data, off64_t *len, const bool *boolean) {
*data = realloc(*data, (*len + 2));
(*data)[*len] = TELLY_BOOL;
(*data)[*len += 1] = *boolean;

*len += 1;
}

static void generate_null_value(char **data, off64_t *len) {
*data = realloc(*data, (*len + 1));
(*data)[*len] = TELLY_NULL;
*len += 1;
}

static off64_t generate_value(char **data, struct KVPair *kv) {
off64_t len = 0;

generate_name_key(data, &len, kv->key);

switch (kv->type) {
case TELLY_NULL:
*data = realloc(*data, len + 1);
(*data)[len] = TELLY_NULL;

len += 1;
generate_null_value(data, &len);
break;

case TELLY_NUM: {
const long *number = kv->value;
const uint32_t bit_count = log2(*number) + 1;
const uint32_t byte_count = (bit_count / 8) + 1;
case TELLY_NUM:
generate_number_value(data, &len, kv->value);
break;

*data = realloc(*data, len + byte_count + 2);
(*data)[len] = TELLY_NUM;
(*data)[len += 1] = byte_count;
memcpy(*data + (len += 1), number, byte_count);
case TELLY_STR:
generate_string_value(data, &len, kv->value);
break;

len += byte_count;
case TELLY_BOOL:
generate_boolean_value(data, &len, kv->value);
break;
}

case TELLY_STR: {
const string_t *string = kv->value;
case TELLY_LIST: {
struct List *list = kv->value;

const uint8_t bit_count = log2(string->len) + 1;
const uint8_t byte_count = ceil((float) (bit_count - 6) / 8);
const uint8_t first = (byte_count << 6) | (string->len & 0b111111);
const uint32_t length_in_bytes = string->len >> 6;
*data = realloc(*data, (len + 5));
(*data)[len] = TELLY_LIST;
memcpy(*data + (len += 1), &list->size, 4);
len += 4;

*data = realloc(*data, len + string->len + byte_count + 2);
(*data)[len] = TELLY_STR;
(*data)[len += 1] = first;
memcpy(*data + (len += 1), &length_in_bytes, byte_count);
memcpy(*data + (len += byte_count), string->value, string->len);
struct ListNode *node = list->begin;

len += string->len;
break;
}
while (node) {
switch (node->type) {
case TELLY_NULL:
generate_null_value(data, &len);
break;

case TELLY_BOOL:
*data = realloc(*data, len + 2);
(*data)[len] = TELLY_BOOL;
(*data)[len += 1] = *((bool *) kv->value);
case TELLY_NUM:
generate_number_value(data, &len, node->value);
break;

case TELLY_STR:
generate_string_value(data, &len, node->value);
break;

case TELLY_BOOL:
generate_boolean_value(data, &len, node->value);
break;

default:
break;
}

node = node->next;
}

len += 1;
break;
}

default:
len = 0;
break;
}

return len;
Expand Down

0 comments on commit 806c4d4

Please sign in to comment.