file db unit tests and fixes

This commit is contained in:
Scott E. Graves 2024-12-18 08:37:12 -06:00
parent 73d1d993d7
commit c944039759
2 changed files with 33 additions and 14 deletions

View File

@ -61,6 +61,10 @@ private:
std::function<rocksdb::Status(rocksdb::Transaction *txn)> 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)

View File

@ -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