feat(lsm): pave the way for removals and updates
parent
ddc38452be
commit
418de748f0
|
@ -7,8 +7,6 @@
|
||||||
#include "lsm.h"
|
#include "lsm.h"
|
||||||
#include "lsm/str.h"
|
#include "lsm/str.h"
|
||||||
|
|
||||||
#define LSM_STORE_DISK_THRESHOLD 1024
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A handle referencing an entry inside a store. Read/write operations from/to
|
* A handle referencing an entry inside a store. Read/write operations from/to
|
||||||
* the entry go through this handle.
|
* the entry go through this handle.
|
||||||
|
|
|
@ -20,8 +20,7 @@ typedef struct lsm_attr {
|
||||||
* An entry inside an LSM store.
|
* An entry inside an LSM store.
|
||||||
*
|
*
|
||||||
* Each entry consists of the key it's stored behind, zero or more attributes
|
* 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
|
* (metadata) and a data file.
|
||||||
* in-memory, depending on the size.
|
|
||||||
*/
|
*/
|
||||||
typedef struct lsm_entry {
|
typedef struct lsm_entry {
|
||||||
lsm_str *key;
|
lsm_str *key;
|
||||||
|
@ -56,6 +55,12 @@ typedef struct lsm_entry_wrapper {
|
||||||
lsm_error lsm_entry_wrapper_init(lsm_entry_wrapper **ptr);
|
lsm_error lsm_entry_wrapper_init(lsm_entry_wrapper **ptr);
|
||||||
void lsm_entry_wrapper_free(lsm_entry_wrapper *wrapper);
|
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 {
|
struct lsm_entry_handle {
|
||||||
lsm_entry_wrapper *wrapper;
|
lsm_entry_wrapper *wrapper;
|
||||||
lsm_store *store;
|
lsm_store *store;
|
||||||
|
@ -63,8 +68,8 @@ struct lsm_entry_handle {
|
||||||
FILE *f;
|
FILE *f;
|
||||||
// Current position in the file pointer
|
// Current position in the file pointer
|
||||||
uint64_t pos;
|
uint64_t pos;
|
||||||
// Whether the entry's metadata has changed
|
// Required to determine in what way the database files need to be synced
|
||||||
bool dirty;
|
uint64_t states;
|
||||||
};
|
};
|
||||||
|
|
||||||
lsm_error lsm_entry_handle_init(lsm_entry_handle **out);
|
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);
|
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
|
* @param handle handle to the removed entry
|
||||||
*/
|
*/
|
||||||
lsm_error lsm_entry_disk_remove(lsm_entry_handle *handle);
|
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
|
#endif
|
||||||
|
|
|
@ -151,7 +151,7 @@ lsm_error lsm_store_insert(lsm_entry_handle **out, lsm_store *store,
|
||||||
handle->store = store;
|
handle->store = store;
|
||||||
|
|
||||||
// Newly inserted entries are always dirty
|
// Newly inserted entries are always dirty
|
||||||
handle->dirty = true;
|
handle->states |= lsm_entry_handle_state_new;
|
||||||
|
|
||||||
*out = handle;
|
*out = handle;
|
||||||
|
|
||||||
|
@ -192,7 +192,7 @@ lsm_error lsm_entry_data_append(lsm_entry_handle *handle, lsm_str *data) {
|
||||||
}
|
}
|
||||||
|
|
||||||
entry->data_len = new_len;
|
entry->data_len = new_len;
|
||||||
handle->dirty = true;
|
handle->states |= lsm_entry_handle_state_updated;
|
||||||
|
|
||||||
return lsm_error_ok;
|
return lsm_error_ok;
|
||||||
}
|
}
|
||||||
|
|
|
@ -173,9 +173,7 @@ lsm_error lsm_store_load_db(lsm_store *store) {
|
||||||
|
|
||||||
handle->wrapper->entry->idx_file_offset = idx_file_offset;
|
handle->wrapper->entry->idx_file_offset = idx_file_offset;
|
||||||
|
|
||||||
// We explicitely set the dirty flag here to prevent writing to the datase
|
handle->states = 0;
|
||||||
// when reading it in
|
|
||||||
handle->dirty = false;
|
|
||||||
lsm_entry_close(handle);
|
lsm_entry_close(handle);
|
||||||
|
|
||||||
store->db_file_size += db_dim[1];
|
store->db_file_size += db_dim[1];
|
||||||
|
|
|
@ -53,8 +53,14 @@ void lsm_entry_close(lsm_entry_handle *handle) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO handle errors here
|
// 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);
|
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);
|
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.count--;
|
||||||
entry->attrs.bitmap[type / 64] &= ~(((uint64_t)1) << (type % 64));
|
entry->attrs.bitmap[type / 64] &= ~(((uint64_t)1) << (type % 64));
|
||||||
|
|
||||||
handle->dirty = true;
|
handle->states |= lsm_entry_handle_state_updated;
|
||||||
|
|
||||||
return lsm_error_ok;
|
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.count++;
|
||||||
entry->attrs.bitmap[type / 64] |= ((uint64_t)1) << (type % 64);
|
entry->attrs.bitmap[type / 64] |= ((uint64_t)1) << (type % 64);
|
||||||
|
|
||||||
handle->dirty = true;
|
handle->states |= lsm_entry_handle_state_updated;
|
||||||
|
|
||||||
return lsm_error_ok;
|
return lsm_error_ok;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue