diff --git a/lsm/include/lsm.h b/lsm/include/lsm.h index 0a1639f..078a57f 100644 --- a/lsm/include/lsm.h +++ b/lsm/include/lsm.h @@ -19,6 +19,8 @@ } \ } +#define LSM_MAX(x, y) ((x) > (y) ? (x) : (y)) + typedef enum lsm_error { lsm_error_ok = 0, lsm_error_failed_alloc = 1, diff --git a/lsm/src/store/lsm_store_disk_read.c b/lsm/src/store/lsm_store_disk_read.c index 721b4f3..b49e9bc 100644 --- a/lsm/src/store/lsm_store_disk_read.c +++ b/lsm/src/store/lsm_store_disk_read.c @@ -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_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_RES(lsm_store_insert(&handle, store, key)); - LSM_RES(lsm_fread(&handle->wrapper->entry->data_len, &store->db.size, - store->db.f, sizeof(uint64_t), 1)); - LSM_RES(lsm_entry_read_attrs(&store->db.size, handle, store->db.f)); + LSM_RES(lsm_fread(&handle->wrapper->entry->data_len, NULL, store->db.f, + sizeof(uint64_t), 1)); + LSM_RES(lsm_entry_read_attrs(NULL, handle, store->db.f)); 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, sizeof(uint64_t), 1)); + uint64_t db_file_size = 0; + for (uint64_t i = 0; i < store->idx.block_count; i++) { 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)); + + // 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; }