diff --git a/repertory/librepertory/src/db/impl/rdb_file_db.cpp b/repertory/librepertory/src/db/impl/rdb_file_db.cpp index 0b926231..61a8aedc 100644 --- a/repertory/librepertory/src/db/impl/rdb_file_db.cpp +++ b/repertory/librepertory/src/db/impl/rdb_file_db.cpp @@ -91,14 +91,22 @@ auto rdb_file_db::add_directory(const std::string &api_path, const std::string &source_path) -> api_error { REPERTORY_USES_FUNCTION_NAME(); + std::string existing_source_path; + auto res = get_directory_source_path(api_path, existing_source_path); + if (res != api_error::success && res != api_error::directory_not_found) { + return res; + } + return perform_action( function_name, [&](rocksdb::Transaction *txn) -> rocksdb::Status { - auto res = remove_item(api_path, source_path, txn); - if (not res.ok() && not res.IsNotFound()) { - return res; + if (not existing_source_path.empty()) { + auto res = remove_item(api_path, existing_source_path, txn); + if (not res.ok() && not res.IsNotFound()) { + return res; + } } - res = txn->Put(directory_family_, api_path, source_path); + auto res = txn->Put(directory_family_, api_path, source_path); if (not res.ok()) { return res; } @@ -111,11 +119,19 @@ auto rdb_file_db::add_or_update_file(const i_file_db::file_data &data) -> api_error { REPERTORY_USES_FUNCTION_NAME(); + std::string existing_source_path; + auto res = get_file_source_path(data.api_path, existing_source_path); + if (res != api_error::success && res != api_error::item_not_found) { + return res; + } + return perform_action( function_name, [&](rocksdb::Transaction *txn) -> rocksdb::Status { - auto res = remove_item(data.api_path, data.source_path, txn); - if (not res.ok() && not res.IsNotFound()) { - return res; + if (not existing_source_path.empty()) { + auto res = remove_item(data.api_path, existing_source_path, txn); + if (not res.ok() && not res.IsNotFound()) { + return res; + } } json json_data = { @@ -124,7 +140,7 @@ auto rdb_file_db::add_or_update_file(const i_file_db::file_data &data) {"source_path", data.source_path}, }; - res = txn->Put(file_family_, data.api_path, json_data.dump()); + auto res = txn->Put(file_family_, data.api_path, json_data.dump()); if (not res.ok()) { return res; }