diff --git a/lsm/src/store/lsm_store_disk_read.c b/lsm/src/store/lsm_store_disk_read.c index 17b91d7..8ce53c1 100644 --- a/lsm/src/store/lsm_store_disk_read.c +++ b/lsm/src/store/lsm_store_disk_read.c @@ -142,6 +142,7 @@ lsm_error lsm_store_load_db(lsm_store *store) { uint64_t db_dim[2]; lsm_str *key; lsm_entry_handle *handle; + bool valid_entry; rewind(store->idx_file); @@ -152,24 +153,43 @@ lsm_error lsm_store_load_db(lsm_store *store) { for (uint64_t i = 0; i < store->idx_file_block_count; i++) { uint64_t idx_file_offset = store->idx_file_size; - LSM_RES(lsm_entry_read_str(&key, &store->idx_file_size, store->idx_file)); - LSM_RES(lsm_fread(&db_dim, &store->idx_file_size, store->idx_file, - sizeof(uint64_t), 2)); - LSM_RES(lsm_store_insert(&handle, store, key)); + LSM_RES(lsm_fread(&valid_entry, &store->idx_file_size, store->idx_file, + sizeof(bool), 1)); - // Read attributes from database file - if (fseek(store->db_file, db_dim[0], SEEK_SET) != 0) { - return lsm_error_failed_io; + if (valid_entry) { + LSM_RES(lsm_entry_read_str(&key, &store->idx_file_size, store->idx_file)); + LSM_RES(lsm_fread(&db_dim, &store->idx_file_size, store->idx_file, + sizeof(uint64_t), 2)); + LSM_RES(lsm_store_insert(&handle, store, key)); + + // Read attributes from database file + if (fseek(store->db_file, db_dim[0], SEEK_SET) != 0) { + return lsm_error_failed_io; + } + + LSM_RES(lsm_fread(&handle->wrapper->entry->data_len, NULL, store->db_file, + sizeof(uint64_t), 1)); + LSM_RES(lsm_entry_read_attrs(NULL, handle, store->db_file)); + + handle->wrapper->entry->idx_file_offset = idx_file_offset; + lsm_entry_close(handle); + + store->db_file_size += db_dim[1]; } + // Simply skip the invalid entry + else { + uint64_t key_len; + LSM_RES(lsm_fread(&key_len, &store->idx_file_size, store->idx_file, + sizeof(uint64_t), 1)); - LSM_RES(lsm_fread(&handle->wrapper->entry->data_len, NULL, store->db_file, - sizeof(uint64_t), 1)); - LSM_RES(lsm_entry_read_attrs(NULL, handle, store->db_file)); + uint64_t remaining = key_len + 2 * sizeof(uint64_t); - handle->wrapper->entry->idx_file_offset = idx_file_offset; - lsm_entry_close(handle); + if (fseek(store->idx_file, remaining, SEEK_CUR) != 0) { + return lsm_error_failed_io; + } - store->db_file_size += db_dim[1]; + store->idx_file_size += remaining; + } } return lsm_error_ok; diff --git a/lsm/src/store/lsm_store_disk_write.c b/lsm/src/store/lsm_store_disk_write.c index 3482f53..3c9293d 100644 --- a/lsm/src/store/lsm_store_disk_write.c +++ b/lsm/src/store/lsm_store_disk_write.c @@ -65,6 +65,9 @@ lsm_error lsm_write_idx_entry(uint64_t *size, FILE *idx_file, lsm_entry *entry, LSM_RES(lsm_fseek(idx_file, pos)); + bool valid_entry_marker = true; + LSM_RES(lsm_fwrite(size, idx_file, sizeof(bool), 1, &valid_entry_marker)); + LSM_RES(lsm_write_str(size, idx_file, entry->key)); LSM_RES(lsm_fwrite(size, idx_file, sizeof(uint64_t), 1, &offset)); LSM_RES(lsm_fwrite(size, idx_file, sizeof(uint64_t), 1, &len));