fix(lsm): work when first creating db

lsm
Jef Roosens 2023-11-08 09:05:38 +01:00
parent 719a65beff
commit e10c43dfd6
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 20 additions and 2 deletions

View File

@ -44,11 +44,20 @@ lsm_error lsm_store_load(lsm_store **ptr, lsm_str *data_path) {
FILE *db_file = fopen(db_file_path, "r+b");
if (db_file == NULL) {
// Create the file first, then reopen it in extended read
db_file = fopen(db_file_path, "wb");
if (db_file == NULL) {
return lsm_error_failed_io;
}
fclose(db_file);
FILE *db_file = fopen(db_file_path, "r+b");
if (db_file == NULL) {
return lsm_error_failed_io;
}
}
// Same for idx file
@ -60,6 +69,7 @@ lsm_error lsm_store_load(lsm_store **ptr, lsm_str *data_path) {
FILE *idx_file = fopen(idx_file_path, "r+b");
if (idx_file == NULL) {
// Create the file first
idx_file = fopen(idx_file_path, "wb");
if (idx_file == NULL) {
@ -75,6 +85,14 @@ lsm_error lsm_store_load(lsm_store **ptr, lsm_str *data_path) {
}
fflush(idx_file);
fclose(idx_file);
// If opening it in extended read mode still fails now, there's a problem
FILE *idx_file = fopen(idx_file_path, "r+b");
if (idx_file == NULL) {
return lsm_error_failed_io;
}
}
store->data_path = data_path;

View File

@ -73,8 +73,6 @@ lsm_error lsm_entry_sync(lsm_store *store, lsm_entry_handle *handle) {
lsm_entry_write_db(&entry_size, store->db_file, handle->wrapper->entry);
fflush(store->db_file);
// TODO fsync db file?
if (res != lsm_error_ok) {
pthread_mutex_unlock(&store->db_lock);
@ -177,6 +175,8 @@ lsm_error lsm_store_load_db(lsm_store *store) {
lsm_str *key;
lsm_entry_handle *handle;
rewind(store->idx_file);
// idx file starts with block count
size_t res =
fread(&store->idx_file_block_count, sizeof(uint64_t), 1, store->idx_file);