file mgr db unit tests and fixes

This commit is contained in:
Scott E. Graves 2024-12-08 11:39:17 -06:00
parent 7567e3289c
commit 9eb6a377fd
7 changed files with 188 additions and 9 deletions

View File

@ -0,0 +1,34 @@
/*
Copyright <2018-2024> <scott.e.graves@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef REPERTORY_INCLUDE_DB_FILE_MGR_DB_HPP_
#define REPERTORY_INCLUDE_DB_FILE_MGR_DB_HPP_
#include "db/i_file_mgr_db.hpp"
namespace repertory {
class app_config;
[[nodiscard]] auto
create_file_mgr_db(const app_config &cfg) -> std::unique_ptr<i_file_mgr_db>;
} // namespace repertory
#endif // REPERTORY_INCLUDE_DB_FILE_MGR_DB_HPP_

View File

@ -50,6 +50,13 @@ private:
private:
void create_or_open(bool clear);
[[nodiscard]] auto create_iterator(rocksdb::ColumnFamilyHandle *family) const
-> std::shared_ptr<rocksdb::Iterator>;
[[nodiscard]] static auto
perform_action(std::string_view function_name,
std::function<rocksdb::Status()> action) -> bool;
public:
[[nodiscard]] auto add_resume(resume_entry entry) -> bool override;

View File

@ -0,0 +1,34 @@
/*
Copyright <2018-2024> <scott.e.graves@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "db/file_mgr_db.hpp"
#include "app_config.hpp"
#include "db/i_file_mgr_db.hpp"
#include "db/rdb_file_mgr_db.hpp"
#include "db/sqlite_file_mgr_db.hpp"
namespace repertory {
auto create_file_mgr_db(const app_config &cfg)
-> std::unique_ptr<i_file_mgr_db> {
return std::make_unique<sqlite_file_mgr_db>(cfg);
}
} // namespace repertory

View File

@ -28,6 +28,7 @@
#include "utils/file.hpp"
#include "utils/path.hpp"
#include "utils/string.hpp"
#include <vector>
namespace {
[[nodiscard]] auto
@ -86,17 +87,53 @@ void rdb_file_mgr_db::create_or_open(bool clear) {
upload_family_ = handles[idx++];
}
auto rdb_file_mgr_db::add_resume(resume_entry entry) -> bool {}
auto rdb_file_mgr_db::add_resume(resume_entry entry) -> bool {
REPERTORY_USES_FUNCTION_NAME();
return perform_action(function_name, [this, &entry]() -> rocksdb::Status {
auto data = json({
{"chunk_size", entry.chunk_size},
{"read_state", utils::string::from_dynamic_bitset(entry.read_state)},
{"source_path", entry.source_path},
});
return db_->Put(rocksdb::WriteOptions{}, resume_family_, entry.api_path,
data.dump());
});
}
auto rdb_file_mgr_db::add_upload(upload_entry entry) -> bool {}
auto rdb_file_mgr_db::add_upload_active(upload_active_entry entry) -> bool {}
void rdb_file_mgr_db::clear() {}
void rdb_file_mgr_db::clear() { create_or_open(true); }
auto rdb_file_mgr_db::create_iterator(rocksdb::ColumnFamilyHandle *family) const
-> std::shared_ptr<rocksdb::Iterator> {
return std::shared_ptr<rocksdb::Iterator>(
db_->NewIterator(rocksdb::ReadOptions(), family));
}
auto rdb_file_mgr_db::get_next_upload() const -> std::optional<upload_entry> {}
auto rdb_file_mgr_db::get_resume_list() const -> std::vector<resume_entry> {}
auto rdb_file_mgr_db::get_resume_list() const -> std::vector<resume_entry> {
REPERTORY_USES_FUNCTION_NAME();
std::vector<resume_entry> ret;
auto iter = create_iterator(resume_family_);
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
auto data = json::parse(iter->value().ToString());
ret.emplace_back(resume_entry{
iter->key().ToString(),
data.at("chunk_size").get<std::uint64_t>(),
utils::string::to_dynamic_bitset(
data.at("read_state").get<std::string>()),
data.at("source_path").get<std::string>(),
});
}
return ret;
}
auto rdb_file_mgr_db::get_upload(const std::string &api_path) const
-> std::optional<upload_entry> {}
@ -104,7 +141,24 @@ auto rdb_file_mgr_db::get_upload(const std::string &api_path) const
auto rdb_file_mgr_db::get_upload_active_list() const
-> std::vector<upload_active_entry> {}
auto rdb_file_mgr_db::remove_resume(const std::string &api_path) -> bool {}
auto rdb_file_mgr_db::perform_action(std::string_view function_name,
std::function<rocksdb::Status()> action)
-> bool {
auto res = action();
if (not res.ok()) {
utils::error::raise_error(function_name, res.ToString());
}
return res.ok();
}
auto rdb_file_mgr_db::remove_resume(const std::string &api_path) -> bool {
REPERTORY_USES_FUNCTION_NAME();
return perform_action(function_name, [this, &api_path]() -> rocksdb::Status {
return db_->Delete(rocksdb::WriteOptions{}, api_path);
});
}
auto rdb_file_mgr_db::remove_upload(const std::string &api_path) -> bool {}
@ -112,5 +166,35 @@ auto rdb_file_mgr_db::remove_upload_active(const std::string &api_path)
-> bool {}
auto rdb_file_mgr_db::rename_resume(const std::string &from_api_path,
const std::string &to_api_path) -> bool {}
const std::string &to_api_path) -> bool {
REPERTORY_USES_FUNCTION_NAME();
std::string value;
auto res = perform_action(
function_name, [this, &from_api_path, &value]() -> rocksdb::Status {
return db_->Get(rocksdb::ReadOptions{}, from_api_path, &value);
});
if (not res) {
return false;
}
if (value.empty()) {
return false;
}
auto data = json::parse(value);
resume_entry entry{
to_api_path,
data.at("chunk_size").get<std::uint64_t>(),
utils::string::to_dynamic_bitset(
data.at("read_state").get<std::string>()),
data.at("source_path").get<std::string>(),
};
if (not remove_resume(from_api_path)) {
return false;
}
return add_resume(entry);
}
} // namespace repertory

View File

@ -22,7 +22,7 @@
#include "file_manager/file_manager.hpp"
#include "app_config.hpp"
#include "db/sqlite_file_mgr_db.hpp"
#include "db/file_mgr_db.hpp"
#include "file_manager/events.hpp"
#include "file_manager/open_file.hpp"
#include "file_manager/open_file_base.hpp"
@ -42,7 +42,7 @@
namespace repertory {
file_manager::file_manager(app_config &config, i_provider &provider)
: config_(config), provider_(provider) {
mgr_db_ = std::make_unique<sqlite_file_mgr_db>(config_);
mgr_db_ = create_file_mgr_db(config);
if (provider_.is_read_only()) {
return;

View File

@ -25,7 +25,7 @@
#include "test_common.hpp"
#include "app_config.hpp"
// #include "db/rdb_meta_db.hpp"
#include "db/rdb_file_mgr_db.hpp"
#include "db/sqlite_file_mgr_db.hpp"
#include "events/consumers/console_consumer.hpp"
#include "events/event_system.hpp"
@ -58,7 +58,7 @@ protected:
}
};
using file_mgr_db_types = ::testing::Types<sqlite_file_mgr_db>;
using file_mgr_db_types = ::testing::Types<sqlite_file_mgr_db, rdb_file_mgr_db>;
template <typename db_t>
std::unique_ptr<app_config> file_mgr_db_test<db_t>::config;

View File

@ -93,4 +93,24 @@ TYPED_TEST(file_mgr_db_test, can_replace_resume) {
EXPECT_TRUE(this->file_mgr_db->remove_resume("/test0"));
}
TYPED_TEST(file_mgr_db_test, can_rename_resume) {
this->file_mgr_db->clear();
EXPECT_TRUE(this->file_mgr_db->add_resume({
"/test0",
2ULL,
{},
"/src/test0",
}));
EXPECT_TRUE(this->file_mgr_db->rename_resume("/test0", "/test1"));
auto list = this->file_mgr_db->get_resume_list();
EXPECT_EQ(1U, list.size());
EXPECT_STREQ("/test1", list.at(0U).api_path.c_str());
EXPECT_EQ(2ULL, list.at(0U).chunk_size);
EXPECT_STREQ("/src/test0", list.at(0U).source_path.c_str());
EXPECT_TRUE(this->file_mgr_db->remove_resume("/test0"));
}
} // namespace repertory