refactor(lsm): rename some variables

lsm
Jef Roosens 2023-11-10 16:23:27 +01:00
parent 418de748f0
commit a6887d4094
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 49 additions and 47 deletions

View File

@ -78,14 +78,18 @@ struct lsm_store {
lsm_trie *trie;
lsm_str *data_path;
FILE *db_file;
uint64_t db_file_size;
pthread_mutex_t db_lock;
struct {
FILE *f;
uint64_t size;
pthread_mutex_t lock;
} db;
FILE *idx_file;
uint64_t idx_file_block_count;
uint64_t idx_file_size;
pthread_mutex_t idx_lock;
struct {
FILE *f;
uint64_t size;
uint64_t block_count;
pthread_mutex_t lock;
} idx;
};
/**

View File

@ -22,8 +22,8 @@ lsm_error lsm_store_init(lsm_store **ptr) {
return res;
}
pthread_mutex_init(&store->db_lock, NULL);
pthread_mutex_init(&store->idx_lock, NULL);
pthread_mutex_init(&store->db.lock, NULL);
pthread_mutex_init(&store->idx.lock, NULL);
*ptr = store;

View File

@ -70,8 +70,8 @@ lsm_error lsm_store_load(lsm_store **ptr, lsm_str *data_path) {
}
store->data_path = data_path;
store->db_file = db_file;
store->idx_file = idx_file;
store->db.f = db_file;
store->idx.f = idx_file;
LSM_RES(lsm_store_load_db(store));
@ -144,53 +144,53 @@ lsm_error lsm_store_load_db(lsm_store *store) {
lsm_entry_handle *handle;
bool valid_entry;
rewind(store->idx_file);
rewind(store->idx.f);
// idx file starts with block count
LSM_RES(lsm_fread(&store->idx_file_block_count, &store->idx_file_size,
store->idx_file, sizeof(uint64_t), 1));
LSM_RES(lsm_fread(&store->idx.block_count, &store->idx.size, store->idx.f,
sizeof(uint64_t), 1));
for (uint64_t i = 0; i < store->idx_file_block_count; i++) {
uint64_t idx_file_offset = store->idx_file_size;
for (uint64_t i = 0; i < store->idx.block_count; i++) {
uint64_t idx_file_offset = store->idx.size;
LSM_RES(lsm_fread(&valid_entry, &store->idx_file_size, store->idx_file,
LSM_RES(lsm_fread(&valid_entry, &store->idx.size, store->idx.f,
sizeof(bool), 1));
if (valid_entry) {
LSM_RES(lsm_entry_read_str(&key, &store->idx_file_size, store->idx_file));
LSM_RES(lsm_fread(&db_dim, &store->idx_file_size, store->idx_file,
LSM_RES(lsm_entry_read_str(&key, &store->idx.size, store->idx.f));
LSM_RES(lsm_fread(&db_dim, &store->idx.size, store->idx.f,
sizeof(uint64_t), 2));
LSM_RES(lsm_store_insert(&handle, store, key));
// Read attributes from database file
if (fseek(store->db_file, db_dim[0], SEEK_SET) != 0) {
if (fseek(store->db.f, db_dim[0], SEEK_SET) != 0) {
return lsm_error_failed_io;
}
LSM_RES(lsm_fread(&handle->wrapper->entry->data_len, NULL, store->db_file,
LSM_RES(lsm_fread(&handle->wrapper->entry->data_len, NULL, store->db.f,
sizeof(uint64_t), 1));
LSM_RES(lsm_entry_read_attrs(NULL, handle, store->db_file));
LSM_RES(lsm_entry_read_attrs(NULL, handle, store->db.f));
handle->wrapper->entry->idx_file_offset = idx_file_offset;
handle->states = 0;
lsm_entry_close(handle);
store->db_file_size += db_dim[1];
store->db.size += db_dim[1];
}
// Simply skip the invalid entry
else {
uint64_t key_len;
LSM_RES(lsm_fread(&key_len, &store->idx_file_size, store->idx_file,
LSM_RES(lsm_fread(&key_len, &store->idx.size, store->idx.f,
sizeof(uint64_t), 1));
uint64_t remaining = key_len + 2 * sizeof(uint64_t);
if (fseek(store->idx_file, remaining, SEEK_CUR) != 0) {
if (fseek(store->idx.f, remaining, SEEK_CUR) != 0) {
return lsm_error_failed_io;
}
store->idx_file_size += remaining;
store->idx.size += remaining;
}
}

View File

@ -78,55 +78,53 @@ lsm_error lsm_write_idx_entry(uint64_t *size, FILE *idx_file, lsm_entry *entry,
lsm_error lsm_entry_disk_insert(lsm_entry_handle *handle) {
lsm_store *store = handle->store;
pthread_mutex_lock(&store->db_lock);
pthread_mutex_lock(&store->db.lock);
uint64_t db_entry_index = store->db_file_size;
uint64_t db_entry_index = store->db.size;
uint64_t db_entry_size;
lsm_error res =
lsm_write_db_entry(&db_entry_size, store->db_file, handle->wrapper->entry,
store->db_file_size);
fflush(store->db_file);
lsm_error res = lsm_write_db_entry(&db_entry_size, store->db.f,
handle->wrapper->entry, store->db.size);
fflush(store->db.f);
pthread_mutex_unlock(&store->db_lock);
pthread_mutex_unlock(&store->db.lock);
if (res != lsm_error_ok) {
return res;
}
// Append entry to index file
pthread_mutex_lock(&store->idx_lock);
pthread_mutex_lock(&store->idx.lock);
uint64_t idx_entry_index = store->idx_file_size;
uint64_t idx_entry_index = store->idx.size;
uint64_t idx_entry_size;
res = lsm_write_idx_entry(&idx_entry_size, store->idx_file,
handle->wrapper->entry, db_entry_index,
db_entry_size, store->idx_file_size);
res =
lsm_write_idx_entry(&idx_entry_size, store->idx.f, handle->wrapper->entry,
db_entry_index, db_entry_size, store->idx.size);
if (res == lsm_error_ok) {
// Update the counter at the beginning of the file
rewind(store->idx_file);
rewind(store->idx.f);
uint64_t new_block_count = store->idx_file_block_count + 1;
uint64_t new_block_count = store->idx.block_count + 1;
res = lsm_fwrite(NULL, store->idx_file, sizeof(uint64_t), 1,
&new_block_count);
res = lsm_fwrite(NULL, store->idx.f, sizeof(uint64_t), 1, &new_block_count);
if (res == lsm_error_ok) {
// Only if we successfully updated the on-disk counter do we make the code
// aware that the files' sizes have increased. This way, if a write to the
// counter fails, the code will simply reuse the already written content.
store->idx_file_size += idx_entry_size;
store->idx_file_block_count = new_block_count;
store->db_file_size += db_entry_size;
store->idx.size += idx_entry_size;
store->idx.block_count = new_block_count;
store->db.size += db_entry_size;
handle->wrapper->entry->idx_file_offset = idx_entry_index;
}
}
fflush(store->idx_file);
pthread_mutex_unlock(&store->idx_lock);
fflush(store->idx.f);
pthread_mutex_unlock(&store->idx.lock);
return res;
}