From dab8e939d5770101fbc419404beb4935e2ee6c54 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 29 Sep 2024 03:44:10 +0000 Subject: [PATCH] Interpret db strng param as name or path in SqliteStore ctr --- .../algo/AlgoDefinitionServiceServer.cpp | 8 +++-- .../spark/persistence/SqliteStore.cpp | 33 ++++++++++++++----- src/lib_spark/spark/persistence/SqliteStore.h | 13 +++++--- .../tests/PersistenceServiceTests.cpp | 3 +- 4 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/app_sparkd/sparkd/algo/AlgoDefinitionServiceServer.cpp b/src/app_sparkd/sparkd/algo/AlgoDefinitionServiceServer.cpp index 349b08f46..6c27c8b1f 100644 --- a/src/app_sparkd/sparkd/algo/AlgoDefinitionServiceServer.cpp +++ b/src/app_sparkd/sparkd/algo/AlgoDefinitionServiceServer.cpp @@ -1,4 +1,5 @@ #include "AlgoDefinitionServiceServer.h" +#include "spark/spark_data_directory.h" namespace bts { namespace api { @@ -19,8 +20,11 @@ struct AlgoDefinitionServiceServer::PImpl { const AlgoDefinitionServiceBase::Algo_definitionsStatefulPublisher &publisher) : table_name("algo_definitions"), - store(new SqliteStore("algo_definition")), context(context), - publisher(publisher) {} + store(std::make_unique( + spark::SparkDataDirectory{} + .database_file("algo_definitions") + .string())), + context(context), publisher(publisher) {} }; AlgoDefinitionServiceServer::AlgoDefinitionServiceServer( diff --git a/src/lib_spark/spark/persistence/SqliteStore.cpp b/src/lib_spark/spark/persistence/SqliteStore.cpp index 863ac3ee8..a14c41f57 100644 --- a/src/lib_spark/spark/persistence/SqliteStore.cpp +++ b/src/lib_spark/spark/persistence/SqliteStore.cpp @@ -2,6 +2,7 @@ #include "spark/config/config.h" #include "spark/persistence/persistence_utils.h" +#include "spark/spark_data_directory.h" #include "spark/utils/Sqlite_utils.h" #include "spark/utils/bts_assert.h" #include "spark/utils/bts_chrono.h" @@ -20,9 +21,23 @@ namespace persistence { using bts::system::protobufs::SystemConfig; +static boost::filesystem::path get_db_path(const std::string &str) { + for (char c : str) { + switch (c) { + case '.': + case '/': + case '\\': + // Looks like 'str' is already a path + return {str}; + } + } + spark::SparkDataDirectory data_dir; + return data_dir.database_file(str); +} + struct SqliteStore::PImpl { - PImpl(std::string const &store_root, std::string const &db_name) - : db(db_name), db_path(persistence::storage_location(db, store_root)), + PImpl(const boost::filesystem::path &db_path) + : db(db_path.stem().string()), db_path(db_path), conn(nullptr, sqlite3_close), insert(nullptr, sqlite3_finalize), remove(nullptr, sqlite3_finalize), remove_all(nullptr, sqlite3_finalize), @@ -30,6 +45,9 @@ struct SqliteStore::PImpl { bts_assert(db.size()); } + PImpl(std::string const &store_root, std::string const &db_name) + : PImpl(boost::filesystem::path(store_root) / (db_name + ".sqlite")) {} + std::string const db; boost::filesystem::path db_path; sqlite3_ptr conn; @@ -41,11 +59,8 @@ struct SqliteStore::PImpl { }; SqliteStore::SqliteStore(std::string const &db) - : SqliteStore(config::get().data_path(), db) {} - -SqliteStore::SqliteStore(std::string const &root_path, std::string const &db) - : me(new PImpl(root_path, db)) { - LOG(persist, Info, "SqliteStore ctr. db: {}; root_path: {}", db, root_path); + : me(std::make_unique(get_db_path(db))) { + LOG(persist, Info, "SqliteStore ctr. db: {}; db_path: {}", db, me->db_path); init_db(); } @@ -125,8 +140,8 @@ SqliteStore::Data SqliteStore::retrieve_all() { return result; } -std::pair -SqliteStore::retrieve_one(std::string const &ns, std::string const &k) { +std::pair SqliteStore::retrieve_one( + std::string const &ns, std::string const &k) { auto query_str = fmt::format( "select ns,key,value from store where ns='{}' and key='{}'", ns, k); sqlite3_stmt_ptr query = sqlite::prepare_query(me->conn, query_str.c_str()); diff --git a/src/lib_spark/spark/persistence/SqliteStore.h b/src/lib_spark/spark/persistence/SqliteStore.h index dbc7e0836..4f0caf725 100644 --- a/src/lib_spark/spark/persistence/SqliteStore.h +++ b/src/lib_spark/spark/persistence/SqliteStore.h @@ -8,17 +8,18 @@ namespace persistence { class SqliteStore : public IStore { public: - SqliteStore(std::string const &storage_root, std::string const &db); SqliteStore(std::string const &db); ~SqliteStore(); + static SqliteStore from_path(const std::string &full_path); + typedef std::map> Data; Data retrieve_all() override; - std::pair - retrieve_one(std::string const &ns, std::string const &k) override; + std::pair retrieve_one( + std::string const &ns, std::string const &k) override; - void - remove(std::string const &ns, std::set const &keys) override; + void remove( + std::string const &ns, std::set const &keys) override; void remove_all(std::string const &ns) override; void insert( std::string const &ns, @@ -33,6 +34,8 @@ class SqliteStore : public IStore { static std::string storage_location(std::string const &db); private: + SqliteStore(); + struct PImpl; std::unique_ptr me; void init_db(); diff --git a/src/lib_spark/spark/persistence/tests/PersistenceServiceTests.cpp b/src/lib_spark/spark/persistence/tests/PersistenceServiceTests.cpp index fc0014d5d..4fd94d3e3 100644 --- a/src/lib_spark/spark/persistence/tests/PersistenceServiceTests.cpp +++ b/src/lib_spark/spark/persistence/tests/PersistenceServiceTests.cpp @@ -15,7 +15,8 @@ namespace persistence { TEST_CASE("PersistenceService Sqlite storage", "[persistence]") { utils::TempDir db_dir; - SqliteStore st(db_dir.name(), "test_db"); + SqliteStore st( + (boost::filesystem::path{db_dir.name()} / "test_db.sqlite").string()); SECTION("initial_db_is_empty") { REQUIRE(st.retrieve_all().empty()); }