diff --git a/repertory/librepertory/include/db/i_meta_db.hpp b/repertory/librepertory/include/db/i_meta_db.hpp new file mode 100644 index 00000000..005d098a --- /dev/null +++ b/repertory/librepertory/include/db/i_meta_db.hpp @@ -0,0 +1,76 @@ +/* + Copyright <2018-2024> + + 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_I_META_DB_HPP_ +#define REPERTORY_INCLUDE_DB_I_META_DB_HPP_ + +#include "types/repertory.hpp" + +namespace repertory { +class i_meta_db { + INTERFACE_SETUP(i_meta_db); + +public: + [[nodiscard]] virtual auto get_api_path(const std::string &source_path, + std::string &api_path) const + -> api_error = 0; + + [[nodiscard]] virtual auto get_api_path_list() const + -> std::vector = 0; + + [[nodiscard]] virtual auto get_item_meta(const std::string &api_path, + api_meta_map &meta) const + -> api_error = 0; + + [[nodiscard]] virtual auto get_item_meta(const std::string &api_path, + const std::string &key, + std::string &value) const + -> api_error = 0; + + [[nodiscard]] virtual auto get_pinned_files() const + -> std::vector = 0; + + [[nodiscard]] virtual auto get_total_item_count() const -> std::uint64_t = 0; + + [[nodiscard]] virtual auto get_total_size() const -> std::uint64_t = 0; + + virtual void remove_api_path(const std::string &api_path) = 0; + + [[nodiscard]] virtual auto remove_item_meta(const std::string &api_path, + const std::string &key) + -> api_error = 0; + + [[nodiscard]] virtual auto rename_item_meta(const std::string &from_api_path, + const std::string &to_api_path) + -> api_error = 0; + + [[nodiscard]] virtual auto set_item_meta(const std::string &api_path, + const std::string &key, + const std::string &value) + -> api_error = 0; + + [[nodiscard]] virtual auto set_item_meta(const std::string &api_path, + const api_meta_map &meta) + -> api_error = 0; +}; +} // namespace repertory + +#endif // REPERTORY_INCLUDE_DB_I_META_DB_HPP_ diff --git a/repertory/librepertory/include/db/meta_db.hpp b/repertory/librepertory/include/db/meta_db.hpp new file mode 100644 index 00000000..cdaa12aa --- /dev/null +++ b/repertory/librepertory/include/db/meta_db.hpp @@ -0,0 +1,35 @@ +/* + Copyright <2018-2024> + + 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_META_DB_HPP_ +#define REPERTORY_INCLUDE_DB_META_DB_HPP_ + +#include "db/i_meta_db.hpp" +#include "db/sqlite_meta_db.hpp" + +namespace repertory { +[[nodiscard]] inline auto create_meta_db(const app_config &cfg) + -> std::unique_ptr { + return std::make_unique(cfg); +} +} // namespace repertory + +#endif // REPERTORY_INCLUDE_DB_META_DB_HPP_ diff --git a/repertory/librepertory/include/providers/meta_db.hpp b/repertory/librepertory/include/db/sqlite_meta_db.hpp similarity index 61% rename from repertory/librepertory/include/providers/meta_db.hpp rename to repertory/librepertory/include/db/sqlite_meta_db.hpp index 5f6ce24c..87f3a8f7 100644 --- a/repertory/librepertory/include/providers/meta_db.hpp +++ b/repertory/librepertory/include/db/sqlite_meta_db.hpp @@ -19,24 +19,25 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#ifndef REPERTORY_INCLUDE_PROVIDERS_META_DB_HPP_ -#define REPERTORY_INCLUDE_PROVIDERS_META_DB_HPP_ +#ifndef REPERTORY_INCLUDE_DB_SQLITE_META_DB_HPP_ +#define REPERTORY_INCLUDE_DB_SQLITE_META_DB_HPP_ +#include "db/i_meta_db.hpp" #include "types/repertory.hpp" #include "utils/db/sqlite/db_common.hpp" namespace repertory { class app_config; -class meta_db final { +class sqlite_meta_db final : public i_meta_db { public: - meta_db(const app_config &cfg); - ~meta_db(); + sqlite_meta_db(const app_config &cfg); + ~sqlite_meta_db() override; - meta_db(const meta_db &) = delete; - meta_db(meta_db &&) = delete; - auto operator=(const meta_db &) -> meta_db & = delete; - auto operator=(meta_db &&) -> meta_db & = delete; + sqlite_meta_db(const sqlite_meta_db &) = delete; + sqlite_meta_db(sqlite_meta_db &&) = delete; + auto operator=(const sqlite_meta_db &) -> sqlite_meta_db & = delete; + auto operator=(sqlite_meta_db &&) -> sqlite_meta_db & = delete; private: utils::db::sqlite::db3_t db_; @@ -48,39 +49,47 @@ private: public: [[nodiscard]] auto get_api_path(const std::string &source_path, - std::string &api_path) -> api_error; + std::string &api_path) const + -> api_error override; - [[nodiscard]] auto get_api_path_list() -> std::vector; + [[nodiscard]] auto get_api_path_list() const + -> std::vector override; [[nodiscard]] auto get_item_meta(const std::string &api_path, - api_meta_map &meta) -> api_error; + api_meta_map &meta) const + -> api_error override; [[nodiscard]] auto get_item_meta(const std::string &api_path, const std::string &key, - std::string &value) const -> api_error; + std::string &value) const + -> api_error override; - [[nodiscard]] auto get_pinned_files() const -> std::vector; + [[nodiscard]] auto get_pinned_files() const + -> std::vector override; - [[nodiscard]] auto get_total_item_count() const -> std::uint64_t; + [[nodiscard]] auto get_total_item_count() const -> std::uint64_t override; - [[nodiscard]] auto get_total_size() const -> std::uint64_t; + [[nodiscard]] auto get_total_size() const -> std::uint64_t override; - void remove_api_path(const std::string &api_path); + void remove_api_path(const std::string &api_path) override; [[nodiscard]] auto remove_item_meta(const std::string &api_path, - const std::string &key) -> api_error; + const std::string &key) + -> api_error override; [[nodiscard]] auto rename_item_meta(const std::string &from_api_path, const std::string &to_api_path) - -> api_error; + -> api_error override; [[nodiscard]] auto set_item_meta(const std::string &api_path, const std::string &key, - const std::string &value) -> api_error; + const std::string &value) + -> api_error override; [[nodiscard]] auto set_item_meta(const std::string &api_path, - const api_meta_map &meta) -> api_error; + const api_meta_map &meta) + -> api_error override; }; } // namespace repertory -#endif // REPERTORY_INCLUDE_PROVIDERS_META_DB_HPP_ +#endif // REPERTORY_INCLUDE_DB_SQLITE_META_DB_HPP_ diff --git a/repertory/librepertory/include/providers/base_provider.hpp b/repertory/librepertory/include/providers/base_provider.hpp index 59ae4da2..6a00603b 100644 --- a/repertory/librepertory/include/providers/base_provider.hpp +++ b/repertory/librepertory/include/providers/base_provider.hpp @@ -22,8 +22,8 @@ #ifndef REPERTORY_INCLUDE_PROVIDERS_BASE_PROVIDER_HPP_ #define REPERTORY_INCLUDE_PROVIDERS_BASE_PROVIDER_HPP_ +#include "db/i_meta_db.hpp" #include "providers/i_provider.hpp" -#include "providers/meta_db.hpp" #include "types/repertory.hpp" namespace repertory { @@ -49,7 +49,7 @@ private: private: api_item_added_callback api_item_added_; - std::unique_ptr db3_; + std::unique_ptr db3_; i_file_manager *fm_{}; private: @@ -106,9 +106,9 @@ protected: return config_; } - [[nodiscard]] auto get_db() -> meta_db & { return *db3_; } + [[nodiscard]] auto get_db() -> i_meta_db & { return *db3_; } - [[nodiscard]] auto get_db() const -> const meta_db & { return *db3_; } + [[nodiscard]] auto get_db() const -> const i_meta_db & { return *db3_; } [[nodiscard]] virtual auto get_directory_items_impl(const std::string &api_path, diff --git a/repertory/librepertory/src/providers/meta_db.cpp b/repertory/librepertory/src/db/sqlite_meta_db.cpp similarity index 85% rename from repertory/librepertory/src/providers/meta_db.cpp rename to repertory/librepertory/src/db/sqlite_meta_db.cpp index 0458cfcb..8adeae60 100644 --- a/repertory/librepertory/src/providers/meta_db.cpp +++ b/repertory/librepertory/src/db/sqlite_meta_db.cpp @@ -19,7 +19,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "providers/meta_db.hpp" +#include "db/sqlite_meta_db.hpp" #include "app_config.hpp" #include "utils/db/sqlite/db_common.hpp" @@ -31,7 +31,7 @@ #include "utils/string.hpp" namespace repertory { -meta_db::meta_db(const app_config &cfg) { +sqlite_meta_db::sqlite_meta_db(const app_config &cfg) { REPERTORY_USES_FUNCTION_NAME(); const std::map sql_create_tables{ @@ -55,10 +55,10 @@ meta_db::meta_db(const app_config &cfg) { sql_create_tables); } -meta_db::~meta_db() { db_.reset(); } +sqlite_meta_db::~sqlite_meta_db() { db_.reset(); } -auto meta_db::get_api_path(const std::string &source_path, - std::string &api_path) -> api_error { +auto sqlite_meta_db::get_api_path(const std::string &source_path, + std::string &api_path) const -> api_error { auto result = utils::db::sqlite::db_select{*db_, table_name} .column("api_path") .where("source_path") @@ -76,7 +76,7 @@ auto meta_db::get_api_path(const std::string &source_path, return api_error::item_not_found; } -auto meta_db::get_api_path_list() -> std::vector { +auto sqlite_meta_db::get_api_path_list() const -> std::vector { std::vector ret{}; auto result = @@ -91,8 +91,8 @@ auto meta_db::get_api_path_list() -> std::vector { return ret; } -auto meta_db::get_item_meta(const std::string &api_path, api_meta_map &meta) - -> api_error { +auto sqlite_meta_db::get_item_meta(const std::string &api_path, + api_meta_map &meta) const -> api_error { REPERTORY_USES_FUNCTION_NAME(); auto result = utils::db::sqlite::db_select{*db_, table_name} @@ -131,8 +131,9 @@ auto meta_db::get_item_meta(const std::string &api_path, api_meta_map &meta) return api_error::error; } -auto meta_db::get_item_meta(const std::string &api_path, const std::string &key, - std::string &value) const -> api_error { +auto sqlite_meta_db::get_item_meta(const std::string &api_path, + const std::string &key, + std::string &value) const -> api_error { REPERTORY_USES_FUNCTION_NAME(); auto result = utils::db::sqlite::db_select{*db_, table_name} @@ -176,7 +177,7 @@ auto meta_db::get_item_meta(const std::string &api_path, const std::string &key, return api_error::error; } -auto meta_db::get_pinned_files() const -> std::vector { +auto sqlite_meta_db::get_pinned_files() const -> std::vector { REPERTORY_USES_FUNCTION_NAME(); std::vector ret{}; @@ -200,7 +201,7 @@ auto meta_db::get_pinned_files() const -> std::vector { return ret; } -auto meta_db::get_total_item_count() const -> std::uint64_t { +auto sqlite_meta_db::get_total_item_count() const -> std::uint64_t { REPERTORY_USES_FUNCTION_NAME(); std::uint64_t ret{}; @@ -223,7 +224,7 @@ auto meta_db::get_total_item_count() const -> std::uint64_t { return ret; } -auto meta_db::get_total_size() const -> std::uint64_t { +auto sqlite_meta_db::get_total_size() const -> std::uint64_t { REPERTORY_USES_FUNCTION_NAME(); try { @@ -245,7 +246,7 @@ auto meta_db::get_total_size() const -> std::uint64_t { return 0U; } -void meta_db::remove_api_path(const std::string &api_path) { +void sqlite_meta_db::remove_api_path(const std::string &api_path) { REPERTORY_USES_FUNCTION_NAME(); auto result = utils::db::sqlite::db_delete{*db_, table_name} @@ -258,8 +259,8 @@ void meta_db::remove_api_path(const std::string &api_path) { } } -auto meta_db::remove_item_meta(const std::string &api_path, - const std::string &key) -> api_error { +auto sqlite_meta_db::remove_item_meta(const std::string &api_path, + const std::string &key) -> api_error { api_meta_map meta{}; auto res = get_item_meta(api_path, meta); if (res != api_error::success) { @@ -270,8 +271,9 @@ auto meta_db::remove_item_meta(const std::string &api_path, return update_item_meta(api_path, meta); } -auto meta_db::rename_item_meta(const std::string &from_api_path, - const std::string &to_api_path) -> api_error { +auto sqlite_meta_db::rename_item_meta(const std::string &from_api_path, + const std::string &to_api_path) + -> api_error { api_meta_map meta{}; auto res = get_item_meta(from_api_path, meta); if (res != api_error::success) { @@ -282,13 +284,14 @@ auto meta_db::rename_item_meta(const std::string &from_api_path, return update_item_meta(to_api_path, meta); } -auto meta_db::set_item_meta(const std::string &api_path, const std::string &key, - const std::string &value) -> api_error { +auto sqlite_meta_db::set_item_meta(const std::string &api_path, + const std::string &key, + const std::string &value) -> api_error { return set_item_meta(api_path, {{key, value}}); } -auto meta_db::set_item_meta(const std::string &api_path, - const api_meta_map &meta) -> api_error { +auto sqlite_meta_db::set_item_meta(const std::string &api_path, + const api_meta_map &meta) -> api_error { api_meta_map existing_meta{}; if (get_item_meta(api_path, existing_meta) != api_error::success) { // TODO handle error @@ -301,8 +304,8 @@ auto meta_db::set_item_meta(const std::string &api_path, return update_item_meta(api_path, existing_meta); } -auto meta_db::update_item_meta(const std::string &api_path, api_meta_map meta) - -> api_error { +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]); diff --git a/repertory/librepertory/src/providers/base_provider.cpp b/repertory/librepertory/src/providers/base_provider.cpp index 5a46e393..fb691c2c 100644 --- a/repertory/librepertory/src/providers/base_provider.cpp +++ b/repertory/librepertory/src/providers/base_provider.cpp @@ -22,6 +22,7 @@ #include "providers/base_provider.hpp" #include "app_config.hpp" +#include "db/meta_db.hpp" #include "events/event_system.hpp" #include "events/events.hpp" #include "file_manager/i_file_manager.hpp" @@ -716,7 +717,7 @@ auto base_provider::start(api_item_added_callback api_item_added, api_item_added_ = api_item_added; fm_ = mgr; - db3_ = std::make_unique(config_); + db3_ = create_meta_db(config_); api_meta_map meta{}; if (get_item_meta("/", meta) == api_error::item_not_found) { diff --git a/repertory/repertory_test/include/fixtures/fuse_fixture.hpp b/repertory/repertory_test/include/fixtures/fuse_fixture.hpp index 60db866f..cde50e92 100644 --- a/repertory/repertory_test/include/fixtures/fuse_fixture.hpp +++ b/repertory/repertory_test/include/fixtures/fuse_fixture.hpp @@ -27,10 +27,11 @@ #include "app_config.hpp" #include "comm/curl/curl_comm.hpp" +#include "db/i_meta_db.hpp" +#include "db/meta_db.hpp" #include "drives/fuse/fuse_drive.hpp" #include "platform/platform.hpp" #include "providers/encrypt/encrypt_provider.hpp" -#include "providers/meta_db.hpp" #include "providers/s3/s3_provider.hpp" #include "providers/sia/sia_provider.hpp" #include "types/repertory.hpp" @@ -76,7 +77,7 @@ public: static provider_type current_provider; static std::vector drive_args; static std::vector drive_args2; - static std::unique_ptr meta; + static std::unique_ptr meta; static std::string mount_location; static std::string mount_location2; @@ -122,7 +123,7 @@ protected: }); } - meta = std::make_unique(*config); + meta = create_meta_db(*config); execute_mount(drive_args, mount_location); }; @@ -164,7 +165,7 @@ protected: }); } - meta = std::make_unique(*config); + meta = create_meta_db(*config); execute_mount(drive_args, mount_location); }; @@ -257,8 +258,8 @@ public: return file_path; } - static auto create_file_and_test(std::string &file_name, - mode_t perms) -> std::string { + static auto create_file_and_test(std::string &file_name, mode_t perms) + -> std::string { file_name += std::to_string(++idx); auto file_path = utils::path::combine(mount_location, {file_name}); @@ -276,7 +277,7 @@ public: EXPECT_TRUE(utils::file::file(file_path).exists()); EXPECT_FALSE(utils::file::directory(file_path).exists()); - struct stat64 unix_st {}; + struct stat64 unix_st{}; EXPECT_EQ(0, stat64(file_path.c_str(), &unix_st)); EXPECT_EQ(getgid(), unix_st.st_gid); EXPECT_EQ(getuid(), unix_st.st_uid); @@ -288,8 +289,8 @@ public: return create_file_and_test(file_name, ACCESSPERMS); } - static auto create_directory_and_test(std::string &dir_name, - mode_t perms) -> std::string { + static auto create_directory_and_test(std::string &dir_name, mode_t perms) + -> std::string { dir_name += std::to_string(++idx); auto dir_path = utils::path::combine(mount_location, {dir_name}); @@ -298,7 +299,7 @@ public: EXPECT_TRUE(utils::file::directory(dir_path).exists()); EXPECT_FALSE(utils::file::file(dir_path).exists()); - struct stat64 unix_st {}; + struct stat64 unix_st{}; EXPECT_EQ(0, stat64(dir_path.c_str(), &unix_st)); EXPECT_EQ(getgid(), unix_st.st_gid); EXPECT_EQ(getuid(), unix_st.st_uid); @@ -391,7 +392,7 @@ template std::vector fuse_test::drive_args2; template -std::unique_ptr fuse_test::meta{}; +std::unique_ptr fuse_test::meta{}; template std::string fuse_test::mount_location;