fix(lsm): write in-memory data to disk when switching to file
parent
f44c512099
commit
f19a8814f5
|
@ -108,8 +108,9 @@ lsm_error lsm_store_open_read(lsm_entry_handle **out, lsm_store *store,
|
|||
lsm_str *key);
|
||||
|
||||
/**
|
||||
* Open a write handle to the given entry. This entry must be properly closed
|
||||
* using `lsm_store_handle_close`.
|
||||
* Open a write handle to the given entry. This handle should only be used for
|
||||
* 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 store store to retrieve entry from
|
||||
|
|
|
@ -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),
|
||||
lsm_str_ptr(entry->key));
|
||||
|
||||
FILE *f = fopen(path, "wb");
|
||||
FILE *f = fopen(path, "ab");
|
||||
|
||||
if (f == NULL) {
|
||||
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_str *data) {
|
||||
if (lsm_str_len(data) == 0) {
|
||||
return lsm_error_ok;
|
||||
}
|
||||
|
||||
lsm_entry *entry = handle->wrapper->entry;
|
||||
|
||||
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));
|
||||
entry->data.value.ptr = buf;
|
||||
entry->data.len = new_len;
|
||||
}
|
||||
// Data will end up on disk
|
||||
else {
|
||||
|
@ -215,10 +218,19 @@ lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle,
|
|||
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;
|
||||
entry->data.on_disk = true;
|
||||
|
||||
// TODO free old buff, write original data to file
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue