feat(lsm): require changes to be committed before writing to persistent

storage
This commit is contained in:
Jef Roosens 2024-08-27 17:00:12 +02:00
parent d64fec048f
commit 3dce25239b
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
7 changed files with 67 additions and 20 deletions

View file

@ -157,7 +157,8 @@ lsm_error lsm_entry_data_open_read(lsm_entry_handle *handle);
lsm_error lsm_entry_data_open_write(lsm_entry_handle *handle);
/**
* Remove the entry's data file if present and close its handle.
* Remove the entry's data file if present and close its handle. Any uncommitted
* changes will be reverted.
*
* @param handle handle to the entry
*/

View file

@ -58,31 +58,42 @@ lsm_error lsm_entry_handle_init(lsm_entry_handle **out) {
return lsm_error_ok;
}
void lsm_entry_close(lsm_entry_handle *handle) {
if (handle->f != NULL) {
fclose(handle->f);
}
bool state_new = handle->states & lsm_entry_handle_state_new;
bool state_removed = handle->states & lsm_entry_handle_state_removed;
/* bool state_updated = handle->states & lsm_entry_handle_state_updated; */
lsm_error lsm_entry_commit(lsm_entry_handle *handle) {
uint8_t state_new = handle->states & lsm_entry_handle_state_new;
uint8_t state_removed = handle->states & lsm_entry_handle_state_removed;
// Clean new entry
if (state_new && !state_removed) {
lsm_entry_disk_insert(handle);
}
// New entry that was removed before being written to disk; only its data file
// needs to be removed if present
else if (state_new && state_removed) {
lsm_entry_data_remove(handle);
lsm_entry_free(handle->wrapper->entry);
handle->wrapper->entry = NULL;
LSM_RES(lsm_entry_disk_insert(handle));
}
// Previously stored entry that needs to be removed; should be removed from db
// file as well
else if (state_removed && !state_new) {
lsm_entry_disk_remove(handle);
LSM_RES(lsm_entry_disk_remove(handle));
lsm_entry_free(handle->wrapper->entry);
handle->wrapper->entry = NULL;
}
// Reset states after committing current changes
handle->states = 0;
return lsm_error_ok;
}
void lsm_entry_close(lsm_entry_handle *handle) {
if (handle->f != NULL) {
fclose(handle->f);
handle->f = NULL;
}
uint8_t state_new = handle->states & lsm_entry_handle_state_new;
/* bool state_updated = handle->states & lsm_entry_handle_state_updated; */
// New entries create a wrapper in the trie that should be removed if not
// committed
if (state_new) {
lsm_entry_data_remove(handle);
lsm_entry_free(handle->wrapper->entry);
handle->wrapper->entry = NULL;
@ -328,7 +339,6 @@ lsm_error lsm_entry_data_open_read(lsm_entry_handle *handle) {
return lsm_error_ok;
}
lsm_error lsm_entry_data_remove(lsm_entry_handle *handle) {
const lsm_entry *entry = handle->wrapper->entry;