chore(lsm): format code

lsm
Jef Roosens 2023-11-08 08:47:24 +01:00
parent 38e9496717
commit 719a65beff
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
5 changed files with 35 additions and 20 deletions

View File

@ -17,7 +17,7 @@ int main() {
lsm_entry_handle *handle;
assert(lsm_store_insert(&handle, store, key) == lsm_error_ok);
lsm_str *attr;
lsm_str_init_copy(&attr, "some attribute value");
lsm_entry_attr_insert(handle, lsm_attr_type_content_type, attr);

View File

@ -52,7 +52,8 @@ lsm_error lsm_entry_attr_get(lsm_str **out, lsm_entry_handle *handle,
* @param entry entry to search for
* @param type type of attribute to return
*/
lsm_error lsm_entry_attr_get_num(uint64_t *out, lsm_entry_handle *handle, lsm_attr_type type);
lsm_error lsm_entry_attr_get_num(uint64_t *out, lsm_entry_handle *handle,
lsm_attr_type type);
/**
* Add a new attribute to the entry.
@ -72,7 +73,8 @@ lsm_error lsm_entry_attr_insert(lsm_entry_handle *handle, lsm_attr_type type,
* @param type type of attribute to add
* @param data data of attribute
*/
lsm_error lsm_entry_attr_insert_num(lsm_entry_handle *handle, lsm_attr_type type, uint64_t data);
lsm_error lsm_entry_attr_insert_num(lsm_entry_handle *handle,
lsm_attr_type type, uint64_t data);
/**
* Remove an atribute from the given entry, if present.
@ -178,7 +180,8 @@ lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle,
* @param data data to append
* @param len length of data array
*/
lsm_error lsm_entry_data_append_raw(lsm_store *store, lsm_entry_handle *handle, char *data, uint64_t len);
lsm_error lsm_entry_data_append_raw(lsm_store *store, lsm_entry_handle *handle,
char *data, uint64_t len);
/**
* Read a number of bytes from the entry's data field. The position from which

View File

@ -37,7 +37,8 @@ lsm_error lsm_store_load(lsm_store **ptr, lsm_str *data_path) {
// Try to open an existing db file or create a new one otherwise
// This shit is why I need to improve the str library
char db_file_path[lsm_str_len(data_path) + strlen(LSM_DB_FILE_NAME) + 2];
memcpy(db_file_path, lsm_str_ptr(data_path), lsm_str_len(data_path) * sizeof(char));
memcpy(db_file_path, lsm_str_ptr(data_path),
lsm_str_len(data_path) * sizeof(char));
sprintf(&db_file_path[lsm_str_len(data_path)], "/%s", LSM_DB_FILE_NAME);
FILE *db_file = fopen(db_file_path, "r+b");
@ -52,7 +53,8 @@ lsm_error lsm_store_load(lsm_store **ptr, lsm_str *data_path) {
// Same for idx file
char idx_file_path[lsm_str_len(data_path) + strlen(LSM_IDX_FILE_NAME) + 2];
memcpy(idx_file_path, lsm_str_ptr(data_path), lsm_str_len(data_path) * sizeof(char));
memcpy(idx_file_path, lsm_str_ptr(data_path),
lsm_str_len(data_path) * sizeof(char));
sprintf(&idx_file_path[lsm_str_len(data_path)], "/%s", LSM_IDX_FILE_NAME);
FILE *idx_file = fopen(idx_file_path, "r+b");
@ -200,7 +202,8 @@ lsm_error lsm_store_insert(lsm_entry_handle **out, lsm_store *store,
// If a key was previously removed from the trie, the wrapper will already be
// present in the trie
if (lsm_trie_search((void **)&wrapper, store->trie, key) == lsm_error_not_found) {
if (lsm_trie_search((void **)&wrapper, store->trie, key) ==
lsm_error_not_found) {
LSM_RES(lsm_entry_wrapper_init(&wrapper));
pthread_rwlock_wrlock(&wrapper->lock);
@ -222,7 +225,6 @@ lsm_error lsm_store_insert(lsm_entry_handle **out, lsm_store *store,
}
}
lsm_entry *entry;
LSM_RES(lsm_entry_init(&entry));

View File

@ -78,7 +78,8 @@ lsm_error lsm_entry_attr_get(lsm_str **out, lsm_entry_handle *handle,
return lsm_error_ok;
}
lsm_error lsm_entry_attr_get_num(uint64_t *out, lsm_entry_handle *handle, lsm_attr_type type) {
lsm_error lsm_entry_attr_get_num(uint64_t *out, lsm_entry_handle *handle,
lsm_attr_type type) {
lsm_str *s;
LSM_RES(lsm_entry_attr_get(&s, handle, type));
@ -167,9 +168,11 @@ lsm_error lsm_entry_attr_insert(lsm_entry_handle *handle, lsm_attr_type type,
return lsm_error_ok;
}
lsm_error lsm_entry_attr_insert_num(lsm_entry_handle *handle, lsm_attr_type type, uint64_t data) {
lsm_error lsm_entry_attr_insert_num(lsm_entry_handle *handle,
lsm_attr_type type, uint64_t data) {
lsm_str *s;
LSM_RES(lsm_str_init_copy_n(&s, (char *)&data, sizeof(uint64_t) / sizeof(char)));
LSM_RES(
lsm_str_init_copy_n(&s, (char *)&data, sizeof(uint64_t) / sizeof(char)));
return lsm_entry_attr_insert(handle, type, s);
}

View File

@ -35,16 +35,19 @@ lsm_error lsm_entry_write_db(uint64_t *size, FILE *db_file, lsm_entry *entry) {
for (uint64_t i = 0; i < entry->attrs.count; i++) {
// Write attribute type, length & value
LSM_RES(lsm_entry_write_uint64_t(db_file, entry->attrs.items[i].type));
LSM_RES(lsm_entry_write_uint64_t(db_file, lsm_str_len(entry->attrs.items[i].str)));
LSM_RES(lsm_entry_write_uint64_t(db_file,
lsm_str_len(entry->attrs.items[i].str)));
LSM_RES(lsm_entry_write_str(db_file, entry->attrs.items[i].str));
*size += 2 * sizeof(uint64_t) + lsm_str_len(entry->attrs.items[i].str) * sizeof(char);
*size += 2 * sizeof(uint64_t) +
lsm_str_len(entry->attrs.items[i].str) * sizeof(char);
}
return lsm_error_ok;
}
lsm_error lsm_entry_write_idx(uint64_t *size, FILE *idx_file, lsm_entry *entry, uint64_t offset, uint64_t len) {
lsm_error lsm_entry_write_idx(uint64_t *size, FILE *idx_file, lsm_entry *entry,
uint64_t offset, uint64_t len) {
LSM_RES(lsm_entry_write_uint64_t(idx_file, lsm_str_len(entry->key)));
LSM_RES(lsm_entry_write_str(idx_file, entry->key));
LSM_RES(lsm_entry_write_uint64_t(idx_file, offset));
@ -66,7 +69,8 @@ lsm_error lsm_entry_sync(lsm_store *store, lsm_entry_handle *handle) {
}
uint64_t entry_size;
lsm_error res = lsm_entry_write_db(&entry_size, store->db_file, handle->wrapper->entry);
lsm_error res =
lsm_entry_write_db(&entry_size, store->db_file, handle->wrapper->entry);
fflush(store->db_file);
// TODO fsync db file?
@ -76,7 +80,7 @@ lsm_error lsm_entry_sync(lsm_store *store, lsm_entry_handle *handle) {
return res;
}
uint64_t entry_index = store->db_file_size;
store->db_file_size += entry_size;
@ -92,7 +96,8 @@ lsm_error lsm_entry_sync(lsm_store *store, lsm_entry_handle *handle) {
return lsm_error_failed_io;
}
res = lsm_entry_write_idx(&entry_size, store->idx_file, handle->wrapper->entry, entry_index, entry_size);
res = lsm_entry_write_idx(&entry_size, store->idx_file,
handle->wrapper->entry, entry_index, entry_size);
if (res == lsm_error_ok) {
// Update the counter at the beginning of the file
@ -151,14 +156,15 @@ static lsm_error lsm_entry_read_attrs(lsm_entry_handle *handle, FILE *db_file) {
if (val_s == NULL) {
return lsm_error_failed_alloc;
}
uint64_t read = 0;
while (read < nums[1]) {
read += fread(&val_s[read], 1, nums[1] - read, db_file);
}
LSM_RES(lsm_str_init(&val, val_s));;
LSM_RES(lsm_str_init(&val, val_s));
;
lsm_entry_attr_insert(handle, nums[0], val);
}
@ -172,7 +178,8 @@ lsm_error lsm_store_load_db(lsm_store *store) {
lsm_entry_handle *handle;
// idx file starts with block count
size_t res = fread(&store->idx_file_block_count, sizeof(uint64_t), 1, store->idx_file);
size_t res =
fread(&store->idx_file_block_count, sizeof(uint64_t), 1, store->idx_file);
if (res == 0) {
return lsm_error_failed_io;