fix(lsm): account for empty entries when appending data
parent
f19a8814f5
commit
8b2117a66c
|
@ -195,7 +195,14 @@ lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle,
|
|||
|
||||
// Data is in memory and still fits -> keep it in memory
|
||||
if ((new_len <= LSM_STORE_DISK_THRESHOLD) && (!entry->data.on_disk)) {
|
||||
char *buf = realloc(entry->data.value.ptr, new_len * sizeof(char));
|
||||
char *buf;
|
||||
|
||||
// Entries with no data do not have an allocated buffer yet
|
||||
if (entry->data.len == 0) {
|
||||
buf = malloc(new_len * sizeof(char));
|
||||
} else {
|
||||
buf = realloc(entry->data.value.ptr, new_len * sizeof(char));
|
||||
}
|
||||
|
||||
if (buf == NULL) {
|
||||
return lsm_error_failed_alloc;
|
||||
|
@ -218,6 +225,11 @@ lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle,
|
|||
return lsm_error_failed_io;
|
||||
}
|
||||
|
||||
// If there was data present in memory already, we sync this to disk.
|
||||
// This check is required because it's possible that more than the
|
||||
// treshold is written to an empty entry immediately, meaning there's no
|
||||
// allocated memory buffer present.
|
||||
if (entry->data.len > 0) {
|
||||
size_t written = 0;
|
||||
|
||||
// Write original in-memory data to file
|
||||
|
@ -228,6 +240,7 @@ lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle,
|
|||
|
||||
free(entry->data.value.ptr);
|
||||
entry->data.value.ptr = NULL;
|
||||
}
|
||||
|
||||
handle->f = f;
|
||||
entry->data.on_disk = true;
|
||||
|
|
Loading…
Reference in New Issue