feat(lsm): add entry data reading

This commit is contained in:
Jef Roosens 2023-10-29 14:41:40 +01:00
parent 1461956d98
commit fc4187e6ce
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
4 changed files with 86 additions and 1 deletions

View file

@ -62,6 +62,7 @@ void lsm_entry_wrapper_free(lsm_entry_wrapper *wrapper);
struct lsm_entry_handle {
lsm_entry_wrapper *wrapper;
FILE *f;
uint64_t pos;
};
lsm_error lsm_entry_handle_init(lsm_entry_handle **out);

View file

@ -259,3 +259,34 @@ lsm_error lsm_entry_data_append(lsm_store *store, lsm_entry_handle *handle,
return lsm_error_ok;
}
lsm_error lsm_entry_data_read(uint64_t *out, char *buf,
lsm_entry_handle *handle, uint64_t len) {
lsm_entry *entry = handle->wrapper->entry;
if (entry->data.len == 0) {
*out = 0;
return lsm_error_ok;
}
uint64_t read;
if (entry->data.on_disk) {
read = fread(buf, sizeof(char), len, handle->f);
if ((read == 0) && (ferror(handle->f) != 0)) {
return lsm_error_failed_io;
}
} else {
read = (entry->data.len - handle->pos) < len
? (entry->data.len - handle->pos)
: len;
memcpy(buf, &entry->data.value.ptr[handle->pos], read * sizeof(char));
}
handle->pos += read;
*out = read;
return lsm_error_ok;
}