meta db unit tests and fixes
This commit is contained in:
parent
88736fc58a
commit
5f51a9384e
@ -26,7 +26,6 @@
|
||||
#include "utils/error_utils.hpp"
|
||||
#include "utils/path.hpp"
|
||||
#include "utils/string.hpp"
|
||||
#include <memory>
|
||||
|
||||
namespace {
|
||||
[[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,
|
||||
json &json_data) const -> api_error {
|
||||
{
|
||||
std::string value;
|
||||
auto res = perform_action(__FUNCTION__, [&]() -> rocksdb::Status {
|
||||
return db_->Get(rocksdb::ReadOptions(), default_family_, api_path,
|
||||
&value);
|
||||
});
|
||||
if (res != api_error::success) {
|
||||
return res;
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
try {
|
||||
{
|
||||
std::string value;
|
||||
auto res = perform_action(__FUNCTION__, [&]() -> rocksdb::Status {
|
||||
return db_->Get(rocksdb::ReadOptions(), default_family_, api_path,
|
||||
&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");
|
||||
}
|
||||
|
||||
{
|
||||
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;
|
||||
return api_error::error;
|
||||
}
|
||||
|
||||
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)
|
||||
-> api_error {
|
||||
auto directory =
|
||||
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>();
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
json_data.erase(META_DIRECTORY);
|
||||
json_data.erase(META_PINNED);
|
||||
json_data.erase(META_SIZE);
|
||||
json_data.erase(META_SOURCE);
|
||||
try {
|
||||
auto directory =
|
||||
utils::string::to_bool(json_data.at(META_DIRECTORY).get<std::string>());
|
||||
|
||||
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()) {
|
||||
return res;
|
||||
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] = "";
|
||||
}
|
||||
|
||||
if (not directory) {
|
||||
db_->Put(rocksdb::WriteOptions(), pinned_family_, api_path,
|
||||
utils::string::from_bool(pinned));
|
||||
auto pinned =
|
||||
utils::string::to_bool(json_data.at(META_PINNED).get<std::string>());
|
||||
|
||||
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()) {
|
||||
return res;
|
||||
}
|
||||
|
||||
res = db_->Put(rocksdb::WriteOptions(), size_family_, api_path,
|
||||
std::to_string(size));
|
||||
if (not directory) {
|
||||
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()) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
res = db_->Put(rocksdb::WriteOptions(), source_family_, api_path,
|
||||
source_path);
|
||||
if (not res.ok()) {
|
||||
return res;
|
||||
}
|
||||
return db_->Put(rocksdb::WriteOptions(), default_family_, api_path,
|
||||
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 db_->Put(rocksdb::WriteOptions(), default_family_, api_path,
|
||||
json_data.dump());
|
||||
});
|
||||
return api_error::error;
|
||||
}
|
||||
} // namespace repertory
|
||||
|
@ -306,33 +306,48 @@ auto sqlite_meta_db::update_item_meta(const std::string &api_path,
|
||||
api_meta_map meta) -> api_error {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
auto directory = utils::string::to_bool(meta[META_DIRECTORY]);
|
||||
auto pinned = utils::string::to_bool(meta[META_PINNED]);
|
||||
auto size =
|
||||
directory ? std::uint64_t(0U) : utils::string::to_uint64(meta[META_SIZE]);
|
||||
auto source_path = meta[META_SOURCE];
|
||||
try {
|
||||
auto directory = utils::string::to_bool(meta.at(META_DIRECTORY));
|
||||
|
||||
meta.erase(META_DIRECTORY);
|
||||
meta.erase(META_PINNED);
|
||||
meta.erase(META_SIZE);
|
||||
meta.erase(META_SOURCE);
|
||||
if (meta[META_PINNED].empty()) {
|
||||
meta[META_PINNED] = utils::string::from_bool(false);
|
||||
}
|
||||
if (meta[META_SIZE].empty()) {
|
||||
meta[META_SIZE] = "0";
|
||||
}
|
||||
|
||||
auto result = utils::db::sqlite::db_insert{*db_, table_name}
|
||||
.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(),
|
||||
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];
|
||||
|
||||
meta.erase(META_DIRECTORY);
|
||||
meta.erase(META_PINNED);
|
||||
meta.erase(META_SIZE);
|
||||
meta.erase(META_SOURCE);
|
||||
|
||||
auto result = utils::db::sqlite::db_insert{*db_, table_name}
|
||||
.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");
|
||||
return api_error::error;
|
||||
}
|
||||
|
||||
return api_error::success;
|
||||
return api_error::error;
|
||||
}
|
||||
} // namespace repertory
|
||||
|
@ -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) {
|
||||
auto test_file = create_test_file();
|
||||
auto test_source = create_test_file();
|
||||
EXPECT_EQ(api_error::success,
|
||||
this->meta_db->set_item_meta(test_file, META_SOURCE, test_source));
|
||||
EXPECT_EQ(
|
||||
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;
|
||||
EXPECT_EQ(api_error::success,
|
||||
|
Loading…
x
Reference in New Issue
Block a user