refactor(lsm): abstract determining entry data path
parent
b40389bbe2
commit
c8728f2371
|
@ -10,6 +10,7 @@
|
|||
|
||||
#define LSM_DB_FILE_NAME "lsm.db"
|
||||
#define LSM_IDX_FILE_NAME "lsm.idx"
|
||||
#define LSM_DATA_FILE_SUFFIX ".data"
|
||||
|
||||
typedef struct lsm_attr {
|
||||
uint8_t type;
|
||||
|
@ -129,4 +130,15 @@ lsm_error lsm_entry_disk_remove(lsm_entry_handle *handle);
|
|||
*/
|
||||
lsm_error lsm_entry_disk_update(lsm_entry_handle *handle);
|
||||
|
||||
/**
|
||||
* Return the length of the path to this entry's data file
|
||||
*/
|
||||
uint64_t lsm_entry_data_path_len(lsm_entry_handle *handle);
|
||||
|
||||
/**
|
||||
* Fill in the entry's data file path in the provided buffer. Use
|
||||
* `lsm_entry_data_path_len` to allocate an appropriately-sized buffer
|
||||
*/
|
||||
void lsm_entry_data_path(char *buf, lsm_entry_handle *handle);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -174,11 +174,10 @@ lsm_error lsm_entry_data_append(lsm_entry_handle *handle, lsm_str *data) {
|
|||
|
||||
// Entries don't open their file unless needed
|
||||
if (handle->f == NULL) {
|
||||
char path[handle->store->data_path->len + entry->key->len + 2];
|
||||
sprintf(path, "%s/%s", lsm_str_ptr(handle->store->data_path),
|
||||
lsm_str_ptr(entry->key));
|
||||
char data_path[lsm_entry_data_path_len(handle) + 1];
|
||||
lsm_entry_data_path(data_path, handle);
|
||||
|
||||
FILE *f = fopen(path, "ab");
|
||||
FILE *f = fopen(data_path, "ab");
|
||||
|
||||
if (f == NULL) {
|
||||
return lsm_error_failed_io;
|
||||
|
@ -213,11 +212,10 @@ lsm_error lsm_entry_data_read(uint64_t *out, char *buf,
|
|||
|
||||
// Entries don't open their file unless needed
|
||||
if (handle->f == NULL) {
|
||||
char path[handle->store->data_path->len + entry->key->len + 2];
|
||||
sprintf(path, "%s/%s", lsm_str_ptr(handle->store->data_path),
|
||||
lsm_str_ptr(entry->key));
|
||||
char data_path[lsm_entry_data_path_len(handle) + 1];
|
||||
lsm_entry_data_path(data_path, handle);
|
||||
|
||||
FILE *f = fopen(path, "rb");
|
||||
FILE *f = fopen(data_path, "rb");
|
||||
|
||||
if (f == NULL) {
|
||||
return lsm_error_failed_io;
|
||||
|
|
|
@ -227,3 +227,25 @@ lsm_error lsm_entry_attr_insert_uint8_t(lsm_entry_handle *handle, uint8_t type,
|
|||
uint64_t lsm_entry_data_len(lsm_entry_handle *handle) {
|
||||
return handle->wrapper->entry->data_len;
|
||||
}
|
||||
|
||||
uint64_t lsm_entry_data_path_len(lsm_entry_handle *handle) {
|
||||
// [data path]/[entry key][data file suffix]
|
||||
return lsm_str_len(handle->store->data_path) +
|
||||
lsm_str_len(handle->wrapper->entry->key) +
|
||||
strlen(LSM_DATA_FILE_SUFFIX) + 1;
|
||||
}
|
||||
void lsm_entry_data_path(char *buf, lsm_entry_handle *handle) {
|
||||
lsm_str *data_path = handle->store->data_path;
|
||||
lsm_str *key = handle->wrapper->entry->key;
|
||||
|
||||
memcpy(buf, lsm_str_ptr(data_path), lsm_str_len(data_path));
|
||||
|
||||
uint64_t index = lsm_str_len(data_path);
|
||||
buf[index] = '/';
|
||||
|
||||
index += 1;
|
||||
memcpy(&buf[index], lsm_str_ptr(key), lsm_str_len(key));
|
||||
|
||||
index += lsm_str_len(key);
|
||||
strcpy(&buf[index], LSM_DATA_FILE_SUFFIX);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue