meta db unit tests and fixes

This commit is contained in:
Scott E. Graves 2024-12-04 12:08:19 -06:00
parent 88736fc58a
commit 5f51a9384e
3 changed files with 177 additions and 126 deletions

View File

@ -26,7 +26,6 @@
#include "utils/error_utils.hpp" #include "utils/error_utils.hpp"
#include "utils/path.hpp" #include "utils/path.hpp"
#include "utils/string.hpp" #include "utils/string.hpp"
#include <memory>
namespace { namespace {
[[nodiscard]] auto [[nodiscard]] auto
@ -117,6 +116,9 @@ auto rdb_meta_db::get_api_path_list() const -> std::vector<std::string> {
auto rdb_meta_db::get_item_meta_json(const std::string &api_path, auto rdb_meta_db::get_item_meta_json(const std::string &api_path,
json &json_data) const -> api_error { json &json_data) const -> api_error {
REPERTORY_USES_FUNCTION_NAME();
try {
{ {
std::string value; std::string value;
auto res = perform_action(__FUNCTION__, [&]() -> rocksdb::Status { auto res = perform_action(__FUNCTION__, [&]() -> rocksdb::Status {
@ -160,7 +162,8 @@ auto rdb_meta_db::get_item_meta_json(const std::string &api_path,
{ {
std::string value; std::string value;
auto res = perform_action(__FUNCTION__, [&]() -> rocksdb::Status { auto res = perform_action(__FUNCTION__, [&]() -> rocksdb::Status {
return db_->Get(rocksdb::ReadOptions(), pinned_family_, api_path, &value); return db_->Get(rocksdb::ReadOptions(), pinned_family_, api_path,
&value);
}); });
if (res != api_error::success) { if (res != api_error::success) {
return res; return res;
@ -182,7 +185,8 @@ auto rdb_meta_db::get_item_meta_json(const std::string &api_path,
{ {
std::string value; std::string value;
auto res = perform_action(__FUNCTION__, [&]() -> rocksdb::Status { auto res = perform_action(__FUNCTION__, [&]() -> rocksdb::Status {
return db_->Get(rocksdb::ReadOptions(), source_family_, api_path, &value); return db_->Get(rocksdb::ReadOptions(), source_family_, api_path,
&value);
}); });
if (res != api_error::success) { if (res != api_error::success) {
return res; return res;
@ -191,6 +195,12 @@ auto rdb_meta_db::get_item_meta_json(const std::string &api_path,
} }
return api_error::success; return api_error::success;
} catch (const std::exception &e) {
utils::error::raise_api_path_error(function_name, api_path, e,
"failed to get item meta");
}
return api_error::error;
} }
auto rdb_meta_db::get_item_meta(const std::string &api_path, auto rdb_meta_db::get_item_meta(const std::string &api_path,
@ -346,15 +356,30 @@ auto rdb_meta_db::set_item_meta(const std::string &api_path,
auto rdb_meta_db::update_item_meta(const std::string &api_path, json json_data) auto rdb_meta_db::update_item_meta(const std::string &api_path, json json_data)
-> api_error { -> api_error {
REPERTORY_USES_FUNCTION_NAME();
try {
auto directory = auto directory =
utils::string::to_bool(json_data[META_DIRECTORY].get<std::string>()); utils::string::to_bool(json_data.at(META_DIRECTORY).get<std::string>());
if (not json_data.contains(META_PINNED)) {
json_data[META_PINNED] = utils::string::from_bool(false);
}
if (not json_data.contains(META_SIZE)) {
json_data[META_SIZE] = "0";
}
if (not json_data.contains(META_SOURCE)) {
json_data[META_SOURCE] = "";
}
auto pinned = auto pinned =
utils::string::to_bool(json_data[META_PINNED].get<std::string>()); utils::string::to_bool(json_data.at(META_PINNED).get<std::string>());
auto size =
directory auto size = directory ? std::uint64_t(0U)
? std::uint64_t(0U) : utils::string::to_uint64(
: utils::string::to_uint64(json_data[META_SIZE].get<std::string>()); json_data.at(META_SIZE).get<std::string>());
auto source_path = json_data[META_SOURCE].get<std::string>();
auto source_path = json_data.at(META_SOURCE).get<std::string>();
json_data.erase(META_DIRECTORY); json_data.erase(META_DIRECTORY);
json_data.erase(META_PINNED); json_data.erase(META_PINNED);
@ -391,5 +416,11 @@ auto rdb_meta_db::update_item_meta(const std::string &api_path, json json_data)
return db_->Put(rocksdb::WriteOptions(), default_family_, api_path, return db_->Put(rocksdb::WriteOptions(), default_family_, api_path,
json_data.dump()); json_data.dump());
}); });
} catch (const std::exception &e) {
utils::error::raise_api_path_error(function_name, api_path, e,
"failed to update item meta");
}
return api_error::error;
} }
} // namespace repertory } // namespace repertory

View File

@ -306,10 +306,19 @@ auto sqlite_meta_db::update_item_meta(const std::string &api_path,
api_meta_map meta) -> api_error { api_meta_map meta) -> api_error {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
auto directory = utils::string::to_bool(meta[META_DIRECTORY]); try {
auto pinned = utils::string::to_bool(meta[META_PINNED]); auto directory = utils::string::to_bool(meta.at(META_DIRECTORY));
auto size =
directory ? std::uint64_t(0U) : utils::string::to_uint64(meta[META_SIZE]); if (meta[META_PINNED].empty()) {
meta[META_PINNED] = utils::string::from_bool(false);
}
if (meta[META_SIZE].empty()) {
meta[META_SIZE] = "0";
}
auto pinned = utils::string::to_bool(meta.at(META_PINNED));
auto size = directory ? std::uint64_t(0U)
: utils::string::to_uint64(meta.at(META_SIZE));
auto source_path = meta[META_SOURCE]; auto source_path = meta[META_SOURCE];
meta.erase(META_DIRECTORY); meta.erase(META_DIRECTORY);
@ -334,5 +343,11 @@ auto sqlite_meta_db::update_item_meta(const std::string &api_path,
} }
return api_error::success; return api_error::success;
} catch (const std::exception &e) {
utils::error::raise_api_path_error(function_name, api_path, e,
"failed to update item meta");
}
return api_error::error;
} }
} // namespace repertory } // namespace repertory

View File

@ -35,8 +35,13 @@ TYPED_TEST_CASE(meta_db_test, meta_db_types);
TYPED_TEST(meta_db_test, can_get_api_path_from_source_path) { TYPED_TEST(meta_db_test, can_get_api_path_from_source_path) {
auto test_file = create_test_file(); auto test_file = create_test_file();
auto test_source = create_test_file(); auto test_source = create_test_file();
EXPECT_EQ(api_error::success, EXPECT_EQ(
this->meta_db->set_item_meta(test_file, META_SOURCE, test_source)); api_error::success,
this->meta_db->set_item_meta(
test_file, {
{META_DIRECTORY, utils::string::from_bool(false)},
{META_SOURCE, test_source},
}));
std::string api_path; std::string api_path;
EXPECT_EQ(api_error::success, EXPECT_EQ(api_error::success,