lander/lsm/src/store/lsm_handle_read.c

80 lines
2.0 KiB
C

#include <stdlib.h>
#include "lsm/store_internal.h"
lsm_error lsm_read_handle_init(lsm_read_handle **out) {
lsm_read_handle *handle = calloc(1, sizeof(lsm_read_handle));
if (handle == NULL) {
return lsm_error_failed_alloc;
}
*out = handle;
return lsm_error_ok;
}
bool lsm_read_attr_present(lsm_read_handle *handle, uint8_t type) {
return lsm_entry_attr_present(handle->wrapper->entry, type);
}
lsm_error lsm_read_attr_get(const lsm_str **out, const lsm_read_handle *handle,
uint8_t type) {
return lsm_entry_attr_get(out, handle->wrapper->entry, type);
}
lsm_error lsm_read_attr_get_uint64_t(uint64_t *out,
const lsm_read_handle *handle,
uint8_t type) {
return lsm_entry_attr_get_uint64_t(out, handle->wrapper->entry, type);
}
lsm_error lsm_read_attr_get_uint8_t(uint8_t *out, const lsm_read_handle *handle,
uint8_t type) {
return lsm_entry_attr_get_uint8_t(out, handle->wrapper->entry, type);
}
uint64_t lsm_read_data_len(const lsm_read_handle *handle) {
return handle->wrapper->entry->data_len;
}
lsm_error lsm_read_data_read(uint64_t *out, char *buf, lsm_read_handle *handle,
uint64_t len) {
const lsm_entry *entry = handle->wrapper->entry;
if (entry->data_len == 0) {
*out = 0;
return lsm_error_ok;
}
// Entries don't open their file unless needed
if (handle->data.f == NULL) {
LSM_RES(lsm_entry_data_open(&handle->data.f, handle->store,
handle->wrapper->entry, "rb"));
}
uint64_t read;
read = fread(buf, sizeof(char), len, handle->data.f);
if ((read == 0) && (ferror(handle->data.f) != 0)) {
return lsm_error_failed_io;
}
handle->data.pos += read;
*out = read;
return lsm_error_ok;
}
void lsm_read_close(lsm_read_handle *handle) {
if (handle->data.f != NULL) {
fclose(handle->data.f);
handle->data.f = NULL;
}
pthread_rwlock_unlock(&handle->wrapper->lock);
free(handle);
}