feat(lsm): implement basic remove

lsm
Jef Roosens 2023-11-12 12:48:36 +01:00
parent 51e4a203e9
commit b40389bbe2
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
6 changed files with 65 additions and 8 deletions

View File

@ -20,18 +20,13 @@ int main() {
lsm_str *attr;
lsm_str_init_copy(&attr, "some attribute value");
lsm_entry_attr_insert(handle, lsm_attr_type_content_type, attr);
lsm_entry_attr_insert(handle, 1, attr);
lsm_str *data;
lsm_str_init_copy(&data, "hello");
for (int i = 0; i < 50; i++) {
lsm_entry_data_append(store, handle, data);
}
if (lsm_entry_sync(store, handle) != lsm_error_ok) {
printf("godver");
return 1;
lsm_entry_data_append(handle, data);
}
lsm_entry_close(handle);
@ -50,4 +45,12 @@ int main() {
total += read;
}
printf("\n%lu", total);
lsm_entry_close(handle);
assert(lsm_store_open_write(&handle, store, key) == lsm_error_ok);
lsm_entry_remove(handle);
lsm_entry_close(handle);
assert(lsm_store_open_read(&handle, store, key) == lsm_error_not_found);
}

View File

@ -176,6 +176,13 @@ void lsm_entry_close(lsm_entry_handle *handle);
lsm_error lsm_store_insert(lsm_entry_handle **out, lsm_store *store,
lsm_str *key);
/**
* Mark the entry as removed.
*
* @param handle handle to entry to remove
*/
void lsm_entry_remove(lsm_entry_handle *handle);
/**
* Append new data to the given entry, which is expected to be in the store.
*

View File

@ -40,6 +40,13 @@ typedef struct lsm_entry {
*/
lsm_error lsm_entry_init(lsm_entry **ptr);
/**
* Deallocate an existing entry
*
* @param entry pointer to entry
*/
void lsm_entry_free(lsm_entry *entry);
/**
* Deallocate an existing lsm_entry object.
*

View File

@ -158,6 +158,10 @@ lsm_error lsm_store_insert(lsm_entry_handle **out, lsm_store *store,
return lsm_error_ok;
}
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) {
if (lsm_str_len(data) == 0) {
return lsm_error_ok;

View File

@ -124,3 +124,28 @@ lsm_error lsm_entry_disk_insert(lsm_entry_handle *handle) {
return res;
}
// Marking an entry as removed in the idx file is simply setting the length of
// its entry to zero
lsm_error lsm_entry_disk_remove(lsm_entry_handle *handle) {
lsm_store *store = handle->store;
lsm_entry *entry = handle->wrapper->entry;
pthread_mutex_lock(&store->idx.lock);
lsm_error res =
lsm_fseek(store->idx.f, entry->idx_file_offset + sizeof(uint64_t));
if (res != lsm_error_ok) {
pthread_mutex_unlock(&store->idx.lock);
return res;
}
uint64_t val = 0;
res = lsm_fwrite(NULL, store->idx.f, sizeof(uint64_t), 1, &val);
pthread_mutex_unlock(&store->idx.lock);
return res;
}

View File

@ -19,6 +19,14 @@ lsm_error lsm_entry_init(lsm_entry **ptr) {
return lsm_error_ok;
}
void lsm_entry_free(lsm_entry *entry) {
if (entry->attrs.count > 0) {
free(entry->attrs.items);
}
free(entry);
}
lsm_error lsm_entry_wrapper_init(lsm_entry_wrapper **ptr) {
lsm_entry_wrapper *wrap = calloc(1, sizeof(lsm_entry_wrapper));
@ -58,7 +66,10 @@ void lsm_entry_close(lsm_entry_handle *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); */
lsm_entry_disk_remove(handle);
lsm_entry_free(handle->wrapper->entry);
handle->wrapper->entry = NULL;
} else if (handle->states & lsm_entry_handle_state_updated) {
/* lsm_entry_disk_update(handle); */
}