chore(lsm): some refactoring
All checks were successful
ci/woodpecker/push/docker Pipeline was successful
All checks were successful
ci/woodpecker/push/docker Pipeline was successful
This commit is contained in:
parent
e3aad2b5e4
commit
881f2defbe
9 changed files with 60 additions and 76 deletions
|
|
@ -14,13 +14,7 @@ lsm_error lsm_store_init(lsm_store **ptr) {
|
|||
return lsm_error_failed_alloc;
|
||||
}
|
||||
|
||||
lsm_error res = lsm_trie_init(&store->trie);
|
||||
|
||||
if (res != lsm_error_ok) {
|
||||
free(store);
|
||||
|
||||
return res;
|
||||
}
|
||||
LSM_RES2(lsm_trie_init(&store->trie), free(store));
|
||||
|
||||
pthread_mutex_init(&store->db.lock, NULL);
|
||||
pthread_mutex_init(&store->idx.lock, NULL);
|
||||
|
|
@ -35,7 +29,7 @@ uint64_t lsm_store_size(const lsm_store *store) {
|
|||
}
|
||||
|
||||
lsm_error lsm_store_open_read(lsm_entry_handle **out, lsm_store *store,
|
||||
lsm_str *key) {
|
||||
const lsm_str *key) {
|
||||
lsm_entry_wrapper *wrapper;
|
||||
|
||||
LSM_RES(lsm_trie_search((void **)&wrapper, store->trie, key));
|
||||
|
|
@ -54,13 +48,7 @@ lsm_error lsm_store_open_read(lsm_entry_handle **out, lsm_store *store,
|
|||
}
|
||||
|
||||
lsm_entry_handle *handle;
|
||||
lsm_error res = lsm_entry_handle_init(&handle);
|
||||
|
||||
if (res != lsm_error_ok) {
|
||||
pthread_rwlock_unlock(&wrapper->lock);
|
||||
|
||||
return res;
|
||||
}
|
||||
LSM_RES2(lsm_entry_handle_init(&handle), pthread_rwlock_unlock(&wrapper->lock));
|
||||
|
||||
handle->wrapper = wrapper;
|
||||
handle->store = store;
|
||||
|
|
@ -70,9 +58,8 @@ lsm_error lsm_store_open_read(lsm_entry_handle **out, lsm_store *store,
|
|||
}
|
||||
|
||||
lsm_error lsm_store_open_write(lsm_entry_handle **out, lsm_store *store,
|
||||
lsm_str *key) {
|
||||
const lsm_str *key) {
|
||||
lsm_entry_wrapper *wrapper;
|
||||
|
||||
LSM_RES(lsm_trie_search((void **)&wrapper, store->trie, key));
|
||||
|
||||
// Try to get a write lock on the entry's lock
|
||||
|
|
@ -90,13 +77,7 @@ lsm_error lsm_store_open_write(lsm_entry_handle **out, lsm_store *store,
|
|||
}
|
||||
|
||||
lsm_entry_handle *handle;
|
||||
lsm_error res = lsm_entry_handle_init(&handle);
|
||||
|
||||
if (res != lsm_error_ok) {
|
||||
pthread_rwlock_unlock(&wrapper->lock);
|
||||
|
||||
return res;
|
||||
}
|
||||
LSM_RES2(lsm_entry_handle_init(&handle), pthread_rwlock_unlock(&wrapper->lock));
|
||||
|
||||
handle->wrapper = wrapper;
|
||||
handle->store = store;
|
||||
|
|
@ -115,16 +96,11 @@ lsm_error lsm_store_insert(lsm_entry_handle **out, lsm_store *store,
|
|||
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);
|
||||
|
||||
lsm_error res = lsm_trie_insert(store->trie, key, wrapper);
|
||||
|
||||
// Check if entry isn't already present in advance
|
||||
if (res != lsm_error_ok) {
|
||||
lsm_entry_wrapper_free(wrapper);
|
||||
|
||||
return res;
|
||||
}
|
||||
LSM_RES2(lsm_trie_insert(store->trie, key, wrapper),
|
||||
lsm_entry_wrapper_free(wrapper));
|
||||
} else {
|
||||
pthread_rwlock_wrlock(&wrapper->lock);
|
||||
|
||||
|
|
@ -136,13 +112,14 @@ lsm_error lsm_store_insert(lsm_entry_handle **out, lsm_store *store,
|
|||
}
|
||||
|
||||
lsm_entry *entry;
|
||||
LSM_RES(lsm_entry_init(&entry));
|
||||
LSM_RES2(lsm_entry_init(&entry), pthread_rwlock_unlock(&wrapper->lock));
|
||||
|
||||
entry->key = key;
|
||||
wrapper->entry = entry;
|
||||
|
||||
lsm_entry_handle *handle;
|
||||
LSM_RES(lsm_entry_handle_init(&handle));
|
||||
LSM_RES2(lsm_entry_handle_init(&handle),
|
||||
pthread_rwlock_unlock(&wrapper->lock));
|
||||
|
||||
// No need to set the handle's file, as the entry doesn't have any data yet
|
||||
handle->wrapper = wrapper;
|
||||
|
|
@ -160,7 +137,7 @@ void lsm_entry_remove(lsm_entry_handle *handle) {
|
|||
handle->states |= lsm_entry_handle_state_removed;
|
||||
}
|
||||
|
||||
lsm_error lsm_entry_data_append(lsm_entry_handle *handle, lsm_str *data) {
|
||||
lsm_error lsm_entry_data_append(lsm_entry_handle *handle, const lsm_str *data) {
|
||||
if (lsm_str_len(data) == 0) {
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue