feat(lsm): pave the way for removals and updates

lsm
Jef Roosens 2023-11-10 16:10:20 +01:00
parent ddc38452be
commit 418de748f0
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
5 changed files with 30 additions and 16 deletions

View File

@ -7,8 +7,6 @@
#include "lsm.h"
#include "lsm/str.h"
#define LSM_STORE_DISK_THRESHOLD 1024
/**
* A handle referencing an entry inside a store. Read/write operations from/to
* the entry go through this handle.

View File

@ -19,9 +19,8 @@ typedef struct lsm_attr {
/**
* An entry inside an LSM store.
*
* Each entry consists of the key it's stored behind, zero or more attributes
* (metadata) and a data field. The data field can be stored on disk or
* in-memory, depending on the size.
* Each entry consists of the key it's stored behind, zero or more attributes
* (metadata) and a data file.
*/
typedef struct lsm_entry {
lsm_str *key;
@ -56,6 +55,12 @@ typedef struct lsm_entry_wrapper {
lsm_error lsm_entry_wrapper_init(lsm_entry_wrapper **ptr);
void lsm_entry_wrapper_free(lsm_entry_wrapper *wrapper);
typedef enum lsm_entry_handle_state : uint8_t {
lsm_entry_handle_state_new = 1 << 0,
lsm_entry_handle_state_updated = 1 << 1,
lsm_entry_handle_state_removed = 1 << 2,
} lsm_entry_handle_state;
struct lsm_entry_handle {
lsm_entry_wrapper *wrapper;
lsm_store *store;
@ -63,8 +68,8 @@ struct lsm_entry_handle {
FILE *f;
// Current position in the file pointer
uint64_t pos;
// Whether the entry's metadata has changed
bool dirty;
// Required to determine in what way the database files need to be synced
uint64_t states;
};
lsm_error lsm_entry_handle_init(lsm_entry_handle **out);
@ -100,10 +105,17 @@ lsm_error lsm_store_load_db(lsm_store *store);
lsm_error lsm_entry_disk_insert(lsm_entry_handle *handle);
/**
* Remove an entry from the database
* Remove an entry from the database.
*
* @param handle handle to the removed entry
*/
lsm_error lsm_entry_disk_remove(lsm_entry_handle *handle);
/**
* Update an existing entry already in the store.
*
* @param handle to updated entry
*/
lsm_error lsm_entry_disk_update(lsm_entry_handle *handle);
#endif

View File

@ -151,7 +151,7 @@ lsm_error lsm_store_insert(lsm_entry_handle **out, lsm_store *store,
handle->store = store;
// Newly inserted entries are always dirty
handle->dirty = true;
handle->states |= lsm_entry_handle_state_new;
*out = handle;
@ -192,7 +192,7 @@ lsm_error lsm_entry_data_append(lsm_entry_handle *handle, lsm_str *data) {
}
entry->data_len = new_len;
handle->dirty = true;
handle->states |= lsm_entry_handle_state_updated;
return lsm_error_ok;
}

View File

@ -173,9 +173,7 @@ lsm_error lsm_store_load_db(lsm_store *store) {
handle->wrapper->entry->idx_file_offset = idx_file_offset;
// We explicitely set the dirty flag here to prevent writing to the datase
// when reading it in
handle->dirty = false;
handle->states = 0;
lsm_entry_close(handle);
store->db_file_size += db_dim[1];

View File

@ -53,8 +53,14 @@ void lsm_entry_close(lsm_entry_handle *handle) {
}
// TODO handle errors here
if (handle->dirty) {
if ((handle->states & lsm_entry_handle_state_new) &&
!(handle->states & lsm_entry_handle_state_removed)) {
lsm_entry_disk_insert(handle);
} else if ((handle->states & lsm_entry_handle_state_removed) &&
!(handle->states & lsm_entry_handle_state_new)) {
/* lsm_entry_disk_remove(handle); */
} else if (handle->states & lsm_entry_handle_state_updated) {
/* lsm_entry_disk_update(handle); */
}
pthread_rwlock_unlock(&handle->wrapper->lock);
@ -157,7 +163,7 @@ lsm_error lsm_entry_attr_remove(lsm_str **out, lsm_entry_handle *handle,
entry->attrs.count--;
entry->attrs.bitmap[type / 64] &= ~(((uint64_t)1) << (type % 64));
handle->dirty = true;
handle->states |= lsm_entry_handle_state_updated;
return lsm_error_ok;
}
@ -184,7 +190,7 @@ lsm_error lsm_entry_attr_insert(lsm_entry_handle *handle, uint8_t type,
entry->attrs.count++;
entry->attrs.bitmap[type / 64] |= ((uint64_t)1) << (type % 64);
handle->dirty = true;
handle->states |= lsm_entry_handle_state_updated;
return lsm_error_ok;
}