diff --git a/repertory/librepertory/include/utils/db/rocksdb/utils.hpp b/repertory/librepertory/include/utils/db/rocksdb/utils.hpp new file mode 100644 index 00000000..e2ff2dbd --- /dev/null +++ b/repertory/librepertory/include/utils/db/rocksdb/utils.hpp @@ -0,0 +1,28 @@ +/* + 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. +*/ +namespace repertory::utils::db::rocksdb { +[[nodiscard]] auto +create_rocksdb(const repertory::app_config &cfg, const std::string &name, + const std::vector &families, + std::vector &handles, bool clear) + -> std::unique_ptr; +} // namespace repertory::utils::db::rocksdb diff --git a/repertory/librepertory/src/db/impl/rdb_file_mgr_db.cpp b/repertory/librepertory/src/db/impl/rdb_file_mgr_db.cpp index 165bf910..971f012e 100644 --- a/repertory/librepertory/src/db/impl/rdb_file_mgr_db.cpp +++ b/repertory/librepertory/src/db/impl/rdb_file_mgr_db.cpp @@ -24,46 +24,12 @@ #include "app_config.hpp" #include "types/startup_exception.hpp" #include "utils/config.hpp" +#include "utils/db/rocksdb/utils.hpp" #include "utils/error_utils.hpp" #include "utils/file.hpp" #include "utils/path.hpp" #include "utils/string.hpp" -namespace { -[[nodiscard]] auto -create_rocksdb(const repertory::app_config &cfg, const std::string &name, - const std::vector &families, - std::vector &handles, bool clear) - -> std::unique_ptr { - REPERTORY_USES_FUNCTION_NAME(); - - auto path = repertory::utils::path::combine(cfg.get_data_directory(), {name}); - if (clear && - not repertory::utils::file::directory{path}.remove_recursively()) { - repertory::utils::error::raise_error(function_name, - "failed to remove meta db|" + path); - } - - rocksdb::Options options{}; - options.create_if_missing = true; - options.create_missing_column_families = true; - options.db_log_dir = cfg.get_log_directory(); - options.keep_log_file_num = 10; - - rocksdb::TransactionDBOptions tx_options{}; - - rocksdb::TransactionDB *ptr{}; - auto status = rocksdb::TransactionDB::Open(options, tx_options, path, - families, &handles, &ptr); - if (not status.ok()) { - repertory::utils::error::raise_error(function_name, status.ToString()); - throw repertory::startup_exception(status.ToString()); - } - - return std::unique_ptr(ptr); -} -} // namespace - namespace repertory { rdb_file_mgr_db::rdb_file_mgr_db(const app_config &cfg) : cfg_(cfg) { create_or_open(false); @@ -81,7 +47,8 @@ void rdb_file_mgr_db::create_or_open(bool clear) { families.emplace_back("upload", rocksdb::ColumnFamilyOptions()); auto handles = std::vector(); - db_ = create_rocksdb(cfg_, "mgr", families, handles, clear); + db_ = utils::db::rocksdb::create_rocksdb(cfg_, "file_mgr", families, handles, + clear); std::size_t idx{}; resume_family_ = handles[idx++]; diff --git a/repertory/librepertory/src/db/impl/rdb_meta_db.cpp b/repertory/librepertory/src/db/impl/rdb_meta_db.cpp index a3b2565c..d1097295 100644 --- a/repertory/librepertory/src/db/impl/rdb_meta_db.cpp +++ b/repertory/librepertory/src/db/impl/rdb_meta_db.cpp @@ -23,46 +23,12 @@ #include "app_config.hpp" #include "types/startup_exception.hpp" +#include "utils/db/rocksdb/utils.hpp" #include "utils/error_utils.hpp" #include "utils/file.hpp" #include "utils/path.hpp" #include "utils/string.hpp" -namespace { -[[nodiscard]] auto -create_rocksdb(const repertory::app_config &cfg, const std::string &name, - const std::vector &families, - std::vector &handles, bool clear) - -> std::unique_ptr { - REPERTORY_USES_FUNCTION_NAME(); - - auto path = repertory::utils::path::combine(cfg.get_data_directory(), {name}); - if (clear && - not repertory::utils::file::directory{path}.remove_recursively()) { - repertory::utils::error::raise_error(function_name, - "failed to remove meta db|" + path); - } - - rocksdb::Options options{}; - options.create_if_missing = true; - options.create_missing_column_families = true; - options.db_log_dir = cfg.get_log_directory(); - options.keep_log_file_num = 10; - - rocksdb::TransactionDBOptions tx_options{}; - - rocksdb::TransactionDB *ptr{}; - auto status = rocksdb::TransactionDB::Open(options, tx_options, path, - families, &handles, &ptr); - if (not status.ok()) { - repertory::utils::error::raise_error(function_name, status.ToString()); - throw repertory::startup_exception(status.ToString()); - } - - return std::unique_ptr(ptr); -} -} // namespace - namespace repertory { rdb_meta_db::rdb_meta_db(const app_config &cfg) : cfg_(cfg) { create_or_open(false); @@ -81,7 +47,8 @@ void rdb_meta_db::create_or_open(bool clear) { families.emplace_back("source", rocksdb::ColumnFamilyOptions()); auto handles = std::vector(); - db_ = create_rocksdb(cfg_, "provider_meta", families, handles, clear); + db_ = utils::db::rocksdb::create_rocksdb(cfg_, "provider_meta", families, + handles, clear); std::size_t idx{}; default_family_ = handles[idx++]; diff --git a/repertory/librepertory/src/utils/db/rocksdb/utils.cpp b/repertory/librepertory/src/utils/db/rocksdb/utils.cpp new file mode 100644 index 00000000..e41e02f0 --- /dev/null +++ b/repertory/librepertory/src/utils/db/rocksdb/utils.cpp @@ -0,0 +1,55 @@ +/* + 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. +*/ +#include "utils/db/rocksdb/utils.hpp" + +namespace repertory::utils::db::rocksdb { +auto create_rocksdb( + const repertory::app_config &cfg, const std::string &name, + const std::vector &families, + std::vector &handles, bool clear) + -> std::unique_ptr { + REPERTORY_USES_FUNCTION_NAME(); + + auto path = repertory::utils::path::combine(cfg.get_data_directory(), {name}); + if (clear && + not repertory::utils::file::directory{path}.remove_recursively()) { + repertory::utils::error::raise_error( + function_name, "failed to remove " + name + " db|" + path); + } + + rocksdb::Options options{}; + options.create_if_missing = true; + options.create_missing_column_families = true; + options.db_log_dir = cfg.get_log_directory(); + options.keep_log_file_num = 10; + + rocksdb::TransactionDB *ptr{}; + auto status = rocksdb::TransactionDB::Open( + options, rocksdb::TransactionDBOptions{}, path, families, &handles, &ptr); + if (not status.ok()) { + repertory::utils::error::raise_error(function_name, status.ToString()); + throw repertory::startup_exception(status.ToString()); + } + + return std::unique_ptr(ptr); +} +} // namespace repertory::utils::db::rocksdb