fix(lsm): write in-memory data to disk when switching to file

lsm
Jef Roosens 2023-10-29 12:33:07 +01:00
parent f44c512099
commit f19a8814f5
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 21 additions and 6 deletions

View File

@ -108,8 +108,9 @@ lsm_error lsm_store_open_read(lsm_entry_handle **out, lsm_store *store,
lsm_str *key); lsm_str *key);
/** /**
* Open a write handle to the given entry. This entry must be properly closed * Open a write handle to the given entry. This handle should only be used for
* using `lsm_store_handle_close`. * writing; read operations on this handle are unsupported. This entry must be
* properly closed using `lsm_store_handle_close`.
* *
* @param out pointer to store handle pointer * @param out pointer to store handle pointer
* @param store store to retrieve entry from * @param store store to retrieve entry from

View File

@ -132,7 +132,7 @@ lsm_error lsm_store_open_write(lsm_entry_handle **out, lsm_store *store,
sprintf(path, "%s/%s", lsm_str_ptr(store->data_path), sprintf(path, "%s/%s", lsm_str_ptr(store->data_path),
lsm_str_ptr(entry->key)); lsm_str_ptr(entry->key));
FILE *f = fopen(path, "wb"); FILE *f = fopen(path, "ab");
if (f == NULL) { if (f == NULL) {
free(handle); free(handle);
@ -184,6 +184,10 @@ lsm_error lsm_store_insert(lsm_entry_handle **out, lsm_store *store,
lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle, lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle,
lsm_str *data) { lsm_str *data) {
if (lsm_str_len(data) == 0) {
return lsm_error_ok;
}
lsm_entry *entry = handle->wrapper->entry; lsm_entry *entry = handle->wrapper->entry;
uint64_t new_len = entry->data.len + lsm_str_len(data); uint64_t new_len = entry->data.len + lsm_str_len(data);
@ -199,7 +203,6 @@ lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle,
memcpy(&buf[entry->data.len], data_s, lsm_str_len(data)); memcpy(&buf[entry->data.len], data_s, lsm_str_len(data));
entry->data.value.ptr = buf; entry->data.value.ptr = buf;
entry->data.len = new_len;
} }
// Data will end up on disk // Data will end up on disk
else { else {
@ -215,10 +218,19 @@ lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle,
return lsm_error_failed_io; return lsm_error_failed_io;
} }
size_t written = 0;
// Write original in-memory data to file
while (written < entry->data.len) {
written += fwrite(&entry->data.value.ptr[written], sizeof(char),
entry->data.len - written, f);
}
free(entry->data.value.ptr);
entry->data.value.ptr = NULL;
handle->f = f; handle->f = f;
entry->data.on_disk = true; entry->data.on_disk = true;
// TODO free old buff, write original data to file
} }
size_t written = 0; size_t written = 0;
@ -230,5 +242,7 @@ lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle,
} }
} }
entry->data.len = new_len;
return lsm_error_ok; return lsm_error_ok;
} }