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,80 +116,91 @@ 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();
std::string value;
auto res = perform_action(__FUNCTION__, [&]() -> rocksdb::Status { try {
return db_->Get(rocksdb::ReadOptions(), default_family_, api_path, {
&value); std::string value;
}); auto res = perform_action(__FUNCTION__, [&]() -> rocksdb::Status {
if (res != api_error::success) { return db_->Get(rocksdb::ReadOptions(), default_family_, api_path,
return res; &value);
});
if (res != api_error::success) {
return res;
}
if (value.empty()) {
return api_error::item_not_found;
}
json_data = json::parse(value);
} }
if (value.empty()) { {
return api_error::item_not_found; std::string value;
auto res = perform_action(__FUNCTION__, [&]() -> rocksdb::Status {
return db_->Get(rocksdb::ReadOptions(), directory_family_, api_path,
&value);
});
if (res != api_error::success) {
return res;
}
json_data[META_DIRECTORY] = value;
} }
json_data = json::parse(value); {
std::string value;
auto res = perform_action(__FUNCTION__, [&]() -> rocksdb::Status {
return db_->Get(rocksdb::ReadOptions(), keys_family_, api_path, &value);
});
if (res != api_error::success) {
return res;
}
json_data[META_KEY] = value;
}
{
std::string value;
auto res = perform_action(__FUNCTION__, [&]() -> rocksdb::Status {
return db_->Get(rocksdb::ReadOptions(), pinned_family_, api_path,
&value);
});
if (res != api_error::success) {
return res;
}
json_data[META_PINNED] = value;
}
{
std::string value;
auto res = perform_action(__FUNCTION__, [&]() -> rocksdb::Status {
return db_->Get(rocksdb::ReadOptions(), size_family_, api_path, &value);
});
if (res != api_error::success) {
return res;
}
json_data[META_SIZE] = value;
}
{
std::string value;
auto res = perform_action(__FUNCTION__, [&]() -> rocksdb::Status {
return db_->Get(rocksdb::ReadOptions(), source_family_, api_path,
&value);
});
if (res != api_error::success) {
return res;
}
json_data[META_SOURCE] = value;
}
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;
std::string value;
auto res = perform_action(__FUNCTION__, [&]() -> rocksdb::Status {
return db_->Get(rocksdb::ReadOptions(), directory_family_, api_path,
&value);
});
if (res != api_error::success) {
return res;
}
json_data[META_DIRECTORY] = value;
}
{
std::string value;
auto res = perform_action(__FUNCTION__, [&]() -> rocksdb::Status {
return db_->Get(rocksdb::ReadOptions(), keys_family_, api_path, &value);
});
if (res != api_error::success) {
return res;
}
json_data[META_KEY] = value;
}
{
std::string value;
auto res = perform_action(__FUNCTION__, [&]() -> rocksdb::Status {
return db_->Get(rocksdb::ReadOptions(), pinned_family_, api_path, &value);
});
if (res != api_error::success) {
return res;
}
json_data[META_PINNED] = value;
}
{
std::string value;
auto res = perform_action(__FUNCTION__, [&]() -> rocksdb::Status {
return db_->Get(rocksdb::ReadOptions(), size_family_, api_path, &value);
});
if (res != api_error::success) {
return res;
}
json_data[META_SIZE] = value;
}
{
std::string value;
auto res = perform_action(__FUNCTION__, [&]() -> rocksdb::Status {
return db_->Get(rocksdb::ReadOptions(), source_family_, api_path, &value);
});
if (res != api_error::success) {
return res;
}
json_data[META_SOURCE] = value;
}
return api_error::success;
} }
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,50 +356,71 @@ 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 {
auto directory = REPERTORY_USES_FUNCTION_NAME();
utils::string::to_bool(json_data[META_DIRECTORY].get<std::string>());
auto pinned =
utils::string::to_bool(json_data[META_PINNED].get<std::string>());
auto size =
directory
? std::uint64_t(0U)
: utils::string::to_uint64(json_data[META_SIZE].get<std::string>());
auto source_path = json_data[META_SOURCE].get<std::string>();
json_data.erase(META_DIRECTORY); try {
json_data.erase(META_PINNED); auto directory =
json_data.erase(META_SIZE); utils::string::to_bool(json_data.at(META_DIRECTORY).get<std::string>());
json_data.erase(META_SOURCE);
return perform_action(__FUNCTION__, [&]() -> rocksdb::Status { if (not json_data.contains(META_PINNED)) {
auto res = db_->Put(rocksdb::WriteOptions(), directory_family_, api_path, json_data[META_PINNED] = utils::string::from_bool(false);
utils::string::from_bool(directory)); }
if (not res.ok()) { if (not json_data.contains(META_SIZE)) {
return res; json_data[META_SIZE] = "0";
}
if (not json_data.contains(META_SOURCE)) {
json_data[META_SOURCE] = "";
} }
if (not directory) { auto pinned =
db_->Put(rocksdb::WriteOptions(), pinned_family_, api_path, utils::string::to_bool(json_data.at(META_PINNED).get<std::string>());
utils::string::from_bool(pinned));
auto size = directory ? std::uint64_t(0U)
: utils::string::to_uint64(
json_data.at(META_SIZE).get<std::string>());
auto source_path = json_data.at(META_SOURCE).get<std::string>();
json_data.erase(META_DIRECTORY);
json_data.erase(META_PINNED);
json_data.erase(META_SIZE);
json_data.erase(META_SOURCE);
return perform_action(__FUNCTION__, [&]() -> rocksdb::Status {
auto res = db_->Put(rocksdb::WriteOptions(), directory_family_, api_path,
utils::string::from_bool(directory));
if (not res.ok()) { if (not res.ok()) {
return res; return res;
} }
res = db_->Put(rocksdb::WriteOptions(), size_family_, api_path, if (not directory) {
std::to_string(size)); db_->Put(rocksdb::WriteOptions(), pinned_family_, api_path,
utils::string::from_bool(pinned));
if (not res.ok()) {
return res;
}
res = db_->Put(rocksdb::WriteOptions(), size_family_, api_path,
std::to_string(size));
if (not res.ok()) {
return res;
}
}
res = db_->Put(rocksdb::WriteOptions(), source_family_, api_path,
source_path);
if (not res.ok()) { if (not res.ok()) {
return res; return res;
} }
}
res = db_->Put(rocksdb::WriteOptions(), source_family_, api_path, return db_->Put(rocksdb::WriteOptions(), default_family_, api_path,
source_path); json_data.dump());
if (not res.ok()) { });
return res; } catch (const std::exception &e) {
} utils::error::raise_api_path_error(function_name, api_path, e,
"failed to update item meta");
}
return db_->Put(rocksdb::WriteOptions(), default_family_, api_path, return api_error::error;
json_data.dump());
});
} }
} // namespace repertory } // namespace repertory

View File

@ -306,33 +306,48 @@ 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]);
auto source_path = meta[META_SOURCE];
meta.erase(META_DIRECTORY); if (meta[META_PINNED].empty()) {
meta.erase(META_PINNED); meta[META_PINNED] = utils::string::from_bool(false);
meta.erase(META_SIZE); }
meta.erase(META_SOURCE); if (meta[META_SIZE].empty()) {
meta[META_SIZE] = "0";
}
auto result = utils::db::sqlite::db_insert{*db_, table_name} auto pinned = utils::string::to_bool(meta.at(META_PINNED));
.or_replace() auto size = directory ? std::uint64_t(0U)
.column_value("api_path", api_path) : utils::string::to_uint64(meta.at(META_SIZE));
.column_value("data", nlohmann::json(meta).dump()) auto source_path = meta[META_SOURCE];
.column_value("directory", directory ? 1 : 0)
.column_value("pinned", pinned ? 1 : 0) meta.erase(META_DIRECTORY);
.column_value("size", static_cast<std::int64_t>(size)) meta.erase(META_PINNED);
.column_value("source_path", source_path) meta.erase(META_SIZE);
.go(); meta.erase(META_SOURCE);
if (not result.ok()) {
utils::error::raise_api_path_error(function_name, api_path, auto result = utils::db::sqlite::db_insert{*db_, table_name}
result.get_error(), .or_replace()
.column_value("api_path", api_path)
.column_value("data", nlohmann::json(meta).dump())
.column_value("directory", directory ? 1 : 0)
.column_value("pinned", pinned ? 1 : 0)
.column_value("size", static_cast<std::int64_t>(size))
.column_value("source_path", source_path)
.go();
if (not result.ok()) {
utils::error::raise_api_path_error(function_name, api_path,
result.get_error(),
"failed to update item meta");
return api_error::error;
}
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"); "failed to update item meta");
return api_error::error;
} }
return api_error::success; 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,