file mgr db unit tests and fixes

This commit is contained in:
Scott E. Graves 2024-12-09 10:33:46 -06:00
parent 2493a16828
commit 6da907910c
4 changed files with 60 additions and 48 deletions

View File

@ -43,7 +43,6 @@ public:
struct upload_entry final {
std::string api_path;
std::uint64_t date_time{};
std::string source_path;
};
@ -52,35 +51,35 @@ public:
[[nodiscard]] virtual auto add_upload(upload_entry entry) -> bool = 0;
[[nodiscard]] virtual auto
add_upload_active(upload_active_entry entry) -> bool = 0;
[[nodiscard]] virtual auto add_upload_active(upload_active_entry entry)
-> bool = 0;
virtual void clear() = 0;
[[nodiscard]] virtual auto
get_next_upload() const -> std::optional<upload_entry> = 0;
[[nodiscard]] virtual auto get_next_upload() const
-> std::optional<upload_entry> = 0;
[[nodiscard]] virtual auto
get_resume_list() const -> std::vector<resume_entry> = 0;
[[nodiscard]] virtual auto get_resume_list() const
-> std::vector<resume_entry> = 0;
[[nodiscard]] virtual auto get_upload(const std::string &api_path) const
-> std::optional<upload_entry> = 0;
[[nodiscard]] virtual auto
get_upload_active_list() const -> std::vector<upload_active_entry> = 0;
[[nodiscard]] virtual auto get_upload_active_list() const
-> std::vector<upload_active_entry> = 0;
[[nodiscard]] virtual auto
remove_resume(const std::string &api_path) -> bool = 0;
[[nodiscard]] virtual auto remove_resume(const std::string &api_path)
-> bool = 0;
[[nodiscard]] virtual auto
remove_upload(const std::string &api_path) -> bool = 0;
[[nodiscard]] virtual auto remove_upload(const std::string &api_path)
-> bool = 0;
[[nodiscard]] virtual auto
remove_upload_active(const std::string &api_path) -> bool = 0;
[[nodiscard]] virtual auto remove_upload_active(const std::string &api_path)
-> bool = 0;
[[nodiscard]] virtual auto
rename_resume(const std::string &from_api_path,
const std::string &to_api_path) -> bool = 0;
[[nodiscard]] virtual auto rename_resume(const std::string &from_api_path,
const std::string &to_api_path)
-> bool = 0;
};
} // namespace repertory

View File

@ -104,19 +104,21 @@ auto rdb_file_mgr_db::add_upload(upload_entry entry) -> bool {
REPERTORY_USES_FUNCTION_NAME();
return perform_action(function_name, [this, &entry]() -> rocksdb::Status {
auto data = json({
{"date_time", entry.date_time},
{"source_path", entry.source_path},
});
return db_->Put(rocksdb::WriteOptions{}, upload_family_,
utils::string::zero_pad(std::to_string(++id_), 19U) + '|' +
utils::string::zero_pad(std::to_string(++id_), 20U) + '|' +
entry.api_path,
data.dump());
entry.source_path);
});
}
auto rdb_file_mgr_db::add_upload_active(upload_active_entry entry) -> bool {}
auto rdb_file_mgr_db::add_upload_active(upload_active_entry entry) -> bool {
REPERTORY_USES_FUNCTION_NAME();
return perform_action(function_name, [this, &entry]() -> rocksdb::Status {
return db_->Put(rocksdb::WriteOptions{}, upload_active_family_,
entry.api_path, entry.source_path);
});
}
void rdb_file_mgr_db::clear() { create_or_open(true); }
@ -137,8 +139,7 @@ auto rdb_file_mgr_db::get_next_upload() const -> std::optional<upload_entry> {
auto data = json::parse(iter->value().ToString());
return upload_entry{
api_path,
data.at("date_time").get<std::uint64_t>(),
data.at("source_path").get<std::string>(),
iter->value().ToString(),
};
}
@ -177,8 +178,7 @@ auto rdb_file_mgr_db::get_upload(const std::string &api_path) const
auto data = json::parse(iter->value().ToString());
return upload_entry{
api_path,
data.at("date_time").get<std::uint64_t>(),
data.at("source_path").get<std::string>(),
iter->value().ToString(),
};
}
@ -186,7 +186,19 @@ 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> {}
-> std::vector<upload_active_entry> {
std::vector<upload_active_entry> ret;
auto iter = create_iterator(upload_active_family_);
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
ret.emplace_back(upload_active_entry{
iter->key().ToString(),
iter->value().ToString(),
});
}
return ret;
}
auto rdb_file_mgr_db::perform_action(std::string_view function_name,
std::function<rocksdb::Status()> action)
@ -203,7 +215,7 @@ 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);
return db_->Delete(rocksdb::WriteOptions{}, resume_family_, api_path);
});
}
@ -219,8 +231,8 @@ auto rdb_file_mgr_db::remove_upload(const std::string &api_path) -> bool {
continue;
}
return perform_action(function_name, []() -> rocksdb::Status {
return db_->Delete(rocksdb::WriteOptions{}, iter->key());
return perform_action(function_name, [this, &iter]() -> rocksdb::Status {
return db_->Delete(rocksdb::WriteOptions{}, upload_family_, iter->key());
});
}
@ -228,7 +240,14 @@ auto rdb_file_mgr_db::remove_upload(const std::string &api_path) -> bool {
}
auto rdb_file_mgr_db::remove_upload_active(const std::string &api_path)
-> bool {}
-> bool {
REPERTORY_USES_FUNCTION_NAME();
return perform_action(function_name, [this, &api_path]() -> rocksdb::Status {
return db_->Delete(rocksdb::WriteOptions{}, upload_active_family_,
api_path);
});
}
auto rdb_file_mgr_db::rename_resume(const std::string &from_api_path,
const std::string &to_api_path) -> bool {

View File

@ -56,7 +56,6 @@ const std::map<std::string, std::string> sql_create_tables{
"("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"api_path TEXT UNIQUE, "
"date_time INTEGER, "
"source_path TEXT"
");",
},
@ -99,7 +98,6 @@ auto sqlite_file_mgr_db::add_upload(upload_entry entry) -> bool {
return utils::db::sqlite::db_insert{*db_, upload_table}
.or_replace()
.column_value("api_path", entry.api_path)
.column_value("date_time", static_cast<std::int64_t>(entry.date_time))
.column_value("source_path", entry.source_path)
.go()
.ok();
@ -152,8 +150,6 @@ auto sqlite_file_mgr_db::get_next_upload() const
return upload_entry{
row->get_column("api_path").get_value<std::string>(),
static_cast<std::uint64_t>(
row->get_column("date_time").get_value<std::int64_t>()),
row->get_column("source_path").get_value<std::string>(),
};
}
@ -202,8 +198,6 @@ auto sqlite_file_mgr_db::get_upload(const std::string &api_path) const
return upload_entry{
row->get_column("api_path").get_value<std::string>(),
static_cast<std::uint64_t>(
row->get_column("date_time").get_value<std::int64_t>()),
row->get_column("source_path").get_value<std::string>(),
};
}

View File

@ -361,10 +361,11 @@ auto file_manager::open(const std::string &api_path, bool directory,
return open(api_path, directory, ofd, handle, file, nullptr);
}
auto file_manager::open(
const std::string &api_path, bool directory, const open_file_data &ofd,
std::uint64_t &handle, std::shared_ptr<i_open_file> &file,
std::shared_ptr<i_closeable_open_file> closeable_file) -> api_error {
auto file_manager::open(const std::string &api_path, bool directory,
const open_file_data &ofd, std::uint64_t &handle,
std::shared_ptr<i_open_file> &file,
std::shared_ptr<i_closeable_open_file> closeable_file)
-> api_error {
const auto create_and_add_handle =
[&](std::shared_ptr<i_closeable_open_file> cur_file) {
handle = get_next_handle();
@ -426,7 +427,6 @@ void file_manager::queue_upload(const std::string &api_path,
if (mgr_db_->add_upload(i_file_mgr_db::upload_entry{
api_path,
utils::time::get_time_now(),
source_path,
})) {
remove_resume(api_path, source_path);
@ -597,8 +597,8 @@ auto file_manager::rename_directory(const std::string &from_api_path,
}
auto file_manager::rename_file(const std::string &from_api_path,
const std::string &to_api_path,
bool overwrite) -> api_error {
const std::string &to_api_path, bool overwrite)
-> api_error {
if (not provider_.is_rename_supported()) {
return api_error::not_implemented;
}