extract common behavior
This commit is contained in:
		
							
								
								
									
										146
									
								
								include/providers/base_provider.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										146
									
								
								include/providers/base_provider.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,146 @@ | ||||
| /* | ||||
|   Copyright <2018-2023> <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 INCLUDE_PROVIDERS_BASE_PROVIDER_HPP_ | ||||
| #define INCLUDE_PROVIDERS_BASE_PROVIDER_HPP_ | ||||
|  | ||||
| #include "providers/i_provider.hpp" | ||||
| #include "types/repertory.hpp" | ||||
|  | ||||
| namespace repertory { | ||||
| class app_config; | ||||
| class i_file_manager; | ||||
| class i_http_comm; | ||||
|  | ||||
| class base_provider : public i_provider { | ||||
| public: | ||||
|   base_provider(app_config &config, i_http_comm &comm) | ||||
|       : config_(config), comm_(comm) {} | ||||
|  | ||||
| private: | ||||
|   app_config &config_; | ||||
|   i_http_comm &comm_; | ||||
|  | ||||
| private: | ||||
|   api_item_added_callback api_item_added_; | ||||
|   std::unique_ptr<rocksdb::DB> db_; | ||||
|   std::string DB_NAME = "meta_db"; | ||||
|   i_file_manager *fm_{}; | ||||
|  | ||||
| private: | ||||
|   void remove_deleted_files(); | ||||
|  | ||||
| protected: | ||||
|   [[nodiscard]] static auto create_api_file(std::string path, std::string key, | ||||
|                                             std::uint64_t size) -> api_file; | ||||
|  | ||||
|   [[nodiscard]] static auto create_api_file(std::string path, | ||||
|                                             std::uint64_t size, | ||||
|                                             api_meta_map &meta) -> api_file; | ||||
|  | ||||
|   [[nodiscard]] auto get_api_item_added() -> api_item_added_callback & { | ||||
|     return api_item_added_; | ||||
|   } | ||||
|  | ||||
|   [[nodiscard]] auto get_api_item_added() const | ||||
|       -> const api_item_added_callback & { | ||||
|     return api_item_added_; | ||||
|   } | ||||
|  | ||||
|   [[nodiscard]] auto get_comm() const -> i_http_comm & { return comm_; } | ||||
|  | ||||
|   [[nodiscard]] auto get_config() -> app_config & { return config_; } | ||||
|  | ||||
|   [[nodiscard]] auto get_config() const -> const app_config & { | ||||
|     return config_; | ||||
|   } | ||||
|  | ||||
|   [[nodiscard]] auto get_db() const -> rocksdb::DB * { return db_.get(); } | ||||
|  | ||||
|   [[nodiscard]] auto get_file_mgr() -> i_file_manager * { return fm_; } | ||||
|  | ||||
|   [[nodiscard]] auto get_file_mgr() const -> const i_file_manager * { | ||||
|     return fm_; | ||||
|   } | ||||
|  | ||||
| public: | ||||
|   [[nodiscard]] auto | ||||
|   create_directory_clone_source_meta(const std::string &source_api_path, | ||||
|                                      const std::string &api_path) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_api_path_from_source(const std::string &source_path, | ||||
|                                               std::string &api_path) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_filesystem_item(const std::string &api_path, | ||||
|                                          bool directory, | ||||
|                                          filesystem_item &fsi) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_filesystem_item_and_file(const std::string &api_path, | ||||
|                                                   api_file &f, | ||||
|                                                   filesystem_item &fsi) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto | ||||
|   get_filesystem_item_from_source_path(const std::string &source_path, | ||||
|                                        filesystem_item &fsi) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_item_meta(const std::string &api_path, | ||||
|                                    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 override; | ||||
|  | ||||
|   [[nodiscard]] auto get_pinned_files() const | ||||
|       -> std::vector<std::string> override; | ||||
|  | ||||
|   [[nodiscard]] auto get_total_item_count() const -> std::uint64_t override; | ||||
|  | ||||
|   [[nodiscard]] auto is_file_writeable(const std::string &api_path) const | ||||
|       -> bool override; | ||||
|  | ||||
|   [[nodiscard]] auto remove_item_meta(const std::string &api_path, | ||||
|                                       const std::string &key) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto set_item_meta(const std::string &api_path, | ||||
|                                    const std::string &key, | ||||
|                                    const std::string &value) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto set_item_meta(const std::string &api_path, | ||||
|                                    const api_meta_map &meta) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto start(api_item_added_callback api_item_added, | ||||
|                            i_file_manager *mgr) -> bool override; | ||||
|  | ||||
|   void stop() override; | ||||
| }; | ||||
| } // namespace repertory | ||||
|  | ||||
| #endif // INCLUDE_PROVIDERS_BASE_PROVIDER_HPP_ | ||||
| @@ -23,7 +23,7 @@ | ||||
| #define INCLUDE_PROVIDERS_S3_S3_PROVIDER_HPP_ | ||||
| #if defined(REPERTORY_ENABLE_S3) | ||||
|  | ||||
| #include "providers/i_provider.hpp" | ||||
| #include "providers/base_provider.hpp" | ||||
| #include "types/repertory.hpp" | ||||
|  | ||||
| namespace repertory { | ||||
| @@ -32,7 +32,7 @@ class i_file_manager; | ||||
| class i_http_comm; | ||||
| struct head_object_result; | ||||
|  | ||||
| class s3_provider final : public i_provider { | ||||
| class s3_provider final : public base_provider { | ||||
| public: | ||||
|   s3_provider(app_config &config, i_http_comm &comm); | ||||
|  | ||||
| @@ -44,25 +44,11 @@ public: | ||||
|   auto operator=(const s3_provider &) -> s3_provider & = delete; | ||||
|   auto operator=(s3_provider &&) -> s3_provider & = delete; | ||||
|  | ||||
| private: | ||||
|   app_config &config_; | ||||
|   i_http_comm &comm_; | ||||
|  | ||||
| private: | ||||
|   api_item_added_callback api_item_added_; | ||||
|   std::unique_ptr<rocksdb::DB> db_; | ||||
|   std::string DB_NAME = "meta_db"; | ||||
|   i_file_manager *fm_{}; | ||||
|  | ||||
| private: | ||||
|   [[nodiscard]] auto add_if_not_found(api_file &file, | ||||
|                                       const std::string &object_name) const | ||||
|       -> api_error; | ||||
|  | ||||
|   [[nodiscard]] static auto create_api_file(const std::string &path, | ||||
|                                             const std::string &key, | ||||
|                                             std::uint64_t size) -> api_file; | ||||
|  | ||||
|   [[nodiscard]] auto create_path_directories(const std::string &api_path, | ||||
|                                              const std::string &key) const | ||||
|       -> api_error; | ||||
| @@ -75,24 +61,13 @@ private: | ||||
|                   bool &is_encrypted, std::string &object_name, | ||||
|                   head_object_result &result) const -> api_error; | ||||
|  | ||||
|   void remove_deleted_files(); | ||||
|  | ||||
| public: | ||||
|   [[nodiscard]] auto create_directory(const std::string &api_path, | ||||
|                                       api_meta_map &meta) -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto | ||||
|   create_directory_clone_source_meta(const std::string &source_api_path, | ||||
|                                      const std::string &api_path) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto create_file(const std::string &api_path, | ||||
|                                  api_meta_map &meta) -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_api_path_from_source(const std::string &source_path, | ||||
|                                               std::string &api_path) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_directory_item_count(const std::string &api_path) const | ||||
|       -> std::uint64_t override; | ||||
|  | ||||
| @@ -110,37 +85,8 @@ public: | ||||
|                                    std::uint64_t &file_size) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_filesystem_item(const std::string &api_path, | ||||
|                                          bool directory, | ||||
|                                          filesystem_item &fsi) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_filesystem_item_and_file(const std::string &api_path, | ||||
|                                                   api_file &file, | ||||
|                                                   filesystem_item &fsi) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto | ||||
|   get_filesystem_item_from_source_path(const std::string &source_path, | ||||
|                                        filesystem_item &fsi) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_pinned_files() const | ||||
|       -> std::vector<std::string> override; | ||||
|  | ||||
|   [[nodiscard]] auto get_item_meta(const std::string &api_path, | ||||
|                                    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 override; | ||||
|  | ||||
|   [[nodiscard]] auto get_total_drive_space() const -> std::uint64_t override; | ||||
|  | ||||
|   [[nodiscard]] auto get_total_item_count() const -> std::uint64_t override; | ||||
|  | ||||
|   [[nodiscard]] auto get_provider_type() const -> provider_type override { | ||||
|     return provider_type::s3; | ||||
|   } | ||||
| @@ -155,9 +101,6 @@ public: | ||||
|   [[nodiscard]] auto is_file(const std::string &api_path, bool &exists) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto is_file_writeable(const std::string &api_path) const | ||||
|       -> bool override; | ||||
|  | ||||
|   [[nodiscard]] auto is_online() const -> bool override; | ||||
|  | ||||
|   [[nodiscard]] auto is_rename_supported() const -> bool override { | ||||
| @@ -176,23 +119,10 @@ public: | ||||
|   [[nodiscard]] auto remove_file(const std::string &api_path) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto remove_item_meta(const std::string &api_path, | ||||
|                                       const std::string &key) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto rename_file(const std::string &from_api_path, | ||||
|                                  const std::string &to_api_path) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto set_item_meta(const std::string &api_path, | ||||
|                                    const std::string &key, | ||||
|                                    const std::string &value) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto set_item_meta(const std::string &api_path, | ||||
|                                    const api_meta_map &meta) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto start(api_item_added_callback api_item_added, | ||||
|                            i_file_manager *mgr) -> bool override; | ||||
|  | ||||
|   | ||||
| @@ -22,7 +22,7 @@ | ||||
| #ifndef INCLUDE_PROVIDERS_SIA_SIA_PROVIDER_HPP_ | ||||
| #define INCLUDE_PROVIDERS_SIA_SIA_PROVIDER_HPP_ | ||||
|  | ||||
| #include "providers/i_provider.hpp" | ||||
| #include "providers/base_provider.hpp" | ||||
| #include "types/repertory.hpp" | ||||
|  | ||||
| namespace repertory { | ||||
| @@ -30,7 +30,7 @@ class app_config; | ||||
| class i_file_manager; | ||||
| class i_http_comm; | ||||
|  | ||||
| class sia_provider : public i_provider { | ||||
| class sia_provider : public base_provider { | ||||
| public: | ||||
|   sia_provider(app_config &config, i_http_comm &comm); | ||||
|  | ||||
| @@ -43,50 +43,22 @@ public: | ||||
|   auto operator=(sia_provider &&) -> sia_provider & = delete; | ||||
|  | ||||
| private: | ||||
|   app_config &config_; | ||||
|   i_http_comm &comm_; | ||||
|  | ||||
| private: | ||||
|   api_item_added_callback api_item_added_; | ||||
|   std::unique_ptr<rocksdb::DB> db_; | ||||
|   std::string DB_NAME = "meta_db"; | ||||
|   i_file_manager *fm_{}; | ||||
|  | ||||
| private: | ||||
|   [[nodiscard]] static auto create_api_file(std::string path, | ||||
|                                             std::uint64_t size) -> api_file; | ||||
|  | ||||
|   [[nodiscard]] static auto create_api_file(std::string path, | ||||
|                                             std::uint64_t size, | ||||
|                                             api_meta_map &meta) -> api_file; | ||||
|  | ||||
|   [[nodiscard]] auto get_object_info(const std::string &api_path, | ||||
|                                      json &object_info) const -> api_error; | ||||
|  | ||||
|   [[nodiscard]] auto get_object_list(const std::string &api_path, | ||||
|                                      nlohmann::json &object_list) const -> bool; | ||||
|  | ||||
|   void remove_deleted_files(); | ||||
|  | ||||
| public: | ||||
|   [[nodiscard]] auto create_directory(const std::string &api_path, | ||||
|                                       api_meta_map &meta) -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto | ||||
|   create_directory_clone_source_meta(const std::string &source_api_path, | ||||
|                                      const std::string &api_path) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_directory_item_count(const std::string &api_path) const | ||||
|       -> std::uint64_t override; | ||||
|  | ||||
|   [[nodiscard]] auto create_file(const std::string &api_path, | ||||
|                                  api_meta_map &meta) -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_api_path_from_source(const std::string &source_path, | ||||
|                                               std::string &api_path) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_directory_items(const std::string &api_path, | ||||
|                                          directory_item_list &list) const | ||||
|       -> api_error override; | ||||
| @@ -101,41 +73,12 @@ public: | ||||
|                                    std::uint64_t &file_size) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_filesystem_item(const std::string &api_path, | ||||
|                                          bool directory, | ||||
|                                          filesystem_item &fsi) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_filesystem_item_and_file(const std::string &api_path, | ||||
|                                                   api_file &f, | ||||
|                                                   filesystem_item &fsi) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto | ||||
|   get_filesystem_item_from_source_path(const std::string &source_path, | ||||
|                                        filesystem_item &fsi) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_item_meta(const std::string &api_path, | ||||
|                                    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 override; | ||||
|  | ||||
|   [[nodiscard]] auto get_pinned_files() const | ||||
|       -> std::vector<std::string> override; | ||||
|  | ||||
|   [[nodiscard]] auto get_provider_type() const -> provider_type override { | ||||
|     return provider_type::sia; | ||||
|   } | ||||
|  | ||||
|   [[nodiscard]] auto get_total_drive_space() const -> std::uint64_t override; | ||||
|  | ||||
|   [[nodiscard]] auto get_total_item_count() const -> std::uint64_t override; | ||||
|  | ||||
|   [[nodiscard]] auto get_used_drive_space() const -> std::uint64_t override; | ||||
|  | ||||
|   [[nodiscard]] auto is_direct_only() const -> bool override { return false; } | ||||
| @@ -146,9 +89,6 @@ public: | ||||
|   [[nodiscard]] auto is_file(const std::string &api_path, bool &exists) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto is_file_writeable(const std::string &api_path) const | ||||
|       -> bool override; | ||||
|  | ||||
|   [[nodiscard]] auto is_online() const -> bool override; | ||||
|  | ||||
|   [[nodiscard]] auto is_rename_supported() const -> bool override { | ||||
| @@ -167,23 +107,10 @@ public: | ||||
|   [[nodiscard]] auto remove_file(const std::string &api_path) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto remove_item_meta(const std::string &api_path, | ||||
|                                       const std::string &key) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto rename_file(const std::string &from_api_path, | ||||
|                                  const std::string &to_api_path) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto set_item_meta(const std::string &api_path, | ||||
|                                    const std::string &key, | ||||
|                                    const std::string &value) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto set_item_meta(const std::string &api_path, | ||||
|                                    const api_meta_map &meta) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto start(api_item_added_callback api_item_added, | ||||
|                            i_file_manager *mgr) -> bool override; | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user