fix(lsm): properly calculate db file size

feature/50-one-time-keys
Jef Roosens 2024-08-29 11:12:56 +02:00
parent 3952496378
commit 7c938d592e
Signed by: Jef Roosens
GPG Key ID: 02D4C0997E74717B
2 changed files with 15 additions and 4 deletions

View File

@ -19,6 +19,8 @@
} \ } \
} }
#define LSM_MAX(x, y) ((x) > (y) ? (x) : (y))
typedef enum lsm_error { typedef enum lsm_error {
lsm_error_ok = 0, lsm_error_ok = 0,
lsm_error_failed_alloc = 1, lsm_error_failed_alloc = 1,

View File

@ -163,14 +163,14 @@ lsm_error lsm_store_insert_from_db(lsm_store *store, uint64_t pos,
LSM_RES(lsm_fseek(store->db.f, pos)); LSM_RES(lsm_fseek(store->db.f, pos));
lsm_str *key; lsm_str *key;
LSM_RES(lsm_entry_read_str(&key, &store->db.size, store->db.f)); LSM_RES(lsm_entry_read_str(&key, NULL, store->db.f));
lsm_entry_handle *handle; lsm_entry_handle *handle;
LSM_RES(lsm_store_insert(&handle, store, key)); LSM_RES(lsm_store_insert(&handle, store, key));
LSM_RES(lsm_fread(&handle->wrapper->entry->data_len, &store->db.size, LSM_RES(lsm_fread(&handle->wrapper->entry->data_len, NULL, store->db.f,
store->db.f, sizeof(uint64_t), 1)); sizeof(uint64_t), 1));
LSM_RES(lsm_entry_read_attrs(&store->db.size, handle, store->db.f)); LSM_RES(lsm_entry_read_attrs(NULL, handle, store->db.f));
handle->wrapper->entry->idx_file_offset = idx_file_offset; handle->wrapper->entry->idx_file_offset = idx_file_offset;
@ -189,6 +189,8 @@ lsm_error lsm_store_load_db(lsm_store *store) {
LSM_RES(lsm_fread(&store->idx.block_count, &store->idx.size, store->idx.f, LSM_RES(lsm_fread(&store->idx.block_count, &store->idx.size, store->idx.f,
sizeof(uint64_t), 1)); sizeof(uint64_t), 1));
uint64_t db_file_size = 0;
for (uint64_t i = 0; i < store->idx.block_count; i++) { for (uint64_t i = 0; i < store->idx.block_count; i++) {
uint64_t idx_file_offset = store->idx.size; uint64_t idx_file_offset = store->idx.size;
@ -201,7 +203,14 @@ lsm_error lsm_store_load_db(lsm_store *store) {
} }
LSM_RES(lsm_store_insert_from_db(store, db_dim[0], idx_file_offset)); LSM_RES(lsm_store_insert_from_db(store, db_dim[0], idx_file_offset));
// The non-zeroed entry with the largest index determines the actual size of
// the file. This way, any zeroed entries at the end of the file can be
// overwritten.
db_file_size = LSM_MAX(db_file_size, db_dim[0] + db_dim[1]);
} }
store->db.size = db_file_size;
return lsm_error_ok; return lsm_error_ok;
} }