chore(lsm): format code
parent
38e9496717
commit
719a65beff
|
@ -17,7 +17,7 @@ int main() {
|
||||||
|
|
||||||
lsm_entry_handle *handle;
|
lsm_entry_handle *handle;
|
||||||
assert(lsm_store_insert(&handle, store, key) == lsm_error_ok);
|
assert(lsm_store_insert(&handle, store, key) == lsm_error_ok);
|
||||||
|
|
||||||
lsm_str *attr;
|
lsm_str *attr;
|
||||||
lsm_str_init_copy(&attr, "some attribute value");
|
lsm_str_init_copy(&attr, "some attribute value");
|
||||||
lsm_entry_attr_insert(handle, lsm_attr_type_content_type, attr);
|
lsm_entry_attr_insert(handle, lsm_attr_type_content_type, attr);
|
||||||
|
|
|
@ -52,7 +52,8 @@ lsm_error lsm_entry_attr_get(lsm_str **out, lsm_entry_handle *handle,
|
||||||
* @param entry entry to search for
|
* @param entry entry to search for
|
||||||
* @param type type of attribute to return
|
* @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.
|
* 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 type type of attribute to add
|
||||||
* @param data data of attribute
|
* @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.
|
* 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 data data to append
|
||||||
* @param len length of data array
|
* @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
|
* Read a number of bytes from the entry's data field. The position from which
|
||||||
|
|
|
@ -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
|
// Try to open an existing db file or create a new one otherwise
|
||||||
// This shit is why I need to improve the str library
|
// 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];
|
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);
|
sprintf(&db_file_path[lsm_str_len(data_path)], "/%s", LSM_DB_FILE_NAME);
|
||||||
|
|
||||||
FILE *db_file = fopen(db_file_path, "r+b");
|
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
|
// Same for idx file
|
||||||
char idx_file_path[lsm_str_len(data_path) + strlen(LSM_IDX_FILE_NAME) + 2];
|
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);
|
sprintf(&idx_file_path[lsm_str_len(data_path)], "/%s", LSM_IDX_FILE_NAME);
|
||||||
|
|
||||||
FILE *idx_file = fopen(idx_file_path, "r+b");
|
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
|
// If a key was previously removed from the trie, the wrapper will already be
|
||||||
// present in the trie
|
// 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));
|
LSM_RES(lsm_entry_wrapper_init(&wrapper));
|
||||||
pthread_rwlock_wrlock(&wrapper->lock);
|
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_entry *entry;
|
||||||
LSM_RES(lsm_entry_init(&entry));
|
LSM_RES(lsm_entry_init(&entry));
|
||||||
|
|
||||||
|
|
|
@ -78,7 +78,8 @@ lsm_error lsm_entry_attr_get(lsm_str **out, lsm_entry_handle *handle,
|
||||||
return lsm_error_ok;
|
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_str *s;
|
||||||
|
|
||||||
LSM_RES(lsm_entry_attr_get(&s, handle, type));
|
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;
|
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_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);
|
return lsm_entry_attr_insert(handle, type, s);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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++) {
|
for (uint64_t i = 0; i < entry->attrs.count; i++) {
|
||||||
// Write attribute type, length & value
|
// 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, 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));
|
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;
|
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_uint64_t(idx_file, lsm_str_len(entry->key)));
|
||||||
LSM_RES(lsm_entry_write_str(idx_file, entry->key));
|
LSM_RES(lsm_entry_write_str(idx_file, entry->key));
|
||||||
LSM_RES(lsm_entry_write_uint64_t(idx_file, offset));
|
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;
|
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);
|
fflush(store->db_file);
|
||||||
|
|
||||||
// TODO fsync db file?
|
// TODO fsync db file?
|
||||||
|
@ -76,7 +80,7 @@ lsm_error lsm_entry_sync(lsm_store *store, lsm_entry_handle *handle) {
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t entry_index = store->db_file_size;
|
uint64_t entry_index = store->db_file_size;
|
||||||
store->db_file_size += entry_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;
|
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) {
|
if (res == lsm_error_ok) {
|
||||||
// Update the counter at the beginning of the file
|
// 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) {
|
if (val_s == NULL) {
|
||||||
return lsm_error_failed_alloc;
|
return lsm_error_failed_alloc;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t read = 0;
|
uint64_t read = 0;
|
||||||
|
|
||||||
while (read < nums[1]) {
|
while (read < nums[1]) {
|
||||||
read += fread(&val_s[read], 1, nums[1] - read, db_file);
|
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);
|
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;
|
lsm_entry_handle *handle;
|
||||||
|
|
||||||
// idx file starts with block count
|
// 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) {
|
if (res == 0) {
|
||||||
return lsm_error_failed_io;
|
return lsm_error_failed_io;
|
||||||
|
|
Loading…
Reference in New Issue