From c944039759a59eb7d1bc4539475778c52eeb5402 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Wed, 18 Dec 2024 08:37:12 -0600 Subject: [PATCH] file db unit tests and fixes --- .../include/db/impl/rdb_file_db.hpp | 4 ++ .../librepertory/src/db/impl/rdb_file_db.cpp | 43 +++++++++++++------ 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/repertory/librepertory/include/db/impl/rdb_file_db.hpp b/repertory/librepertory/include/db/impl/rdb_file_db.hpp index efaecaf2..88ed5048 100644 --- a/repertory/librepertory/include/db/impl/rdb_file_db.hpp +++ b/repertory/librepertory/include/db/impl/rdb_file_db.hpp @@ -61,6 +61,10 @@ private: std::function action) -> api_error; + [[nodiscard]] auto remove_item(const std::string &api_path, + const std::string &source_path, + rocksdb::Transaction *txn) -> rocksdb::Status; + public: [[nodiscard]] auto add_directory(const std::string &api_path, const std::string &source_path) diff --git a/repertory/librepertory/src/db/impl/rdb_file_db.cpp b/repertory/librepertory/src/db/impl/rdb_file_db.cpp index c4db7714..0b926231 100644 --- a/repertory/librepertory/src/db/impl/rdb_file_db.cpp +++ b/repertory/librepertory/src/db/impl/rdb_file_db.cpp @@ -93,7 +93,12 @@ auto rdb_file_db::add_directory(const std::string &api_path, return perform_action( function_name, [&](rocksdb::Transaction *txn) -> rocksdb::Status { - auto res = txn->Put(directory_family_, api_path, source_path); + auto res = remove_item(api_path, source_path, txn); + if (not res.ok() && not res.IsNotFound()) { + return res; + } + + res = txn->Put(directory_family_, api_path, source_path); if (not res.ok()) { return res; } @@ -108,13 +113,18 @@ auto rdb_file_db::add_or_update_file(const i_file_db::file_data &data) 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; + } + json json_data = { {"file_size", data.file_size}, {"iv", data.iv_list}, {"source_path", data.source_path}, }; - auto res = txn->Put(file_family_, data.api_path, json_data.dump()); + res = txn->Put(file_family_, data.api_path, json_data.dump()); if (not res.ok()) { return res; } @@ -158,7 +168,6 @@ auto rdb_file_db::get_directory_api_path(const std::string &source_path, REPERTORY_USES_FUNCTION_NAME(); auto result = perform_action(function_name, [&]() -> rocksdb::Status { - std::string api_path; auto res = db_->Get(rocksdb::ReadOptions{}, default_family_, source_path, &api_path); if (not res.ok()) { @@ -365,17 +374,23 @@ auto rdb_file_db::remove_item(const std::string &api_path) -> api_error { return perform_action(function_name, [&](rocksdb::Transaction *txn) -> rocksdb::Status { - auto res = txn->Delete(default_family_, source_path); - if (not res.ok()) { - return res; - } - - res = txn->Delete(directory_family_, api_path); - if (not res.ok()) { - return res; - } - - return txn->Delete(file_family_, api_path); + return remove_item(api_path, source_path, txn); }); } + +auto rdb_file_db::remove_item(const std::string &api_path, + const std::string &source_path, + rocksdb::Transaction *txn) -> rocksdb::Status { + auto res = txn->Delete(default_family_, source_path); + if (not res.ok()) { + return res; + } + + res = txn->Delete(directory_family_, api_path); + if (not res.ok()) { + return res; + } + + return txn->Delete(file_family_, api_path); +} } // namespace repertory