Interpret db strng param as name or path in SqliteStore ctr
This commit is contained in:
1 parent
7fd501369e
commit
dab8e939d5
4 files changed
+40
-17
No files matched your search
@@ -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<SqliteStore>(
|
||||
spark::SparkDataDirectory{}
|
||||
.database_file("algo_definitions")
|
||||
.string())),
|
||||
context(context), publisher(publisher) {}
|
||||
};
|
||||
|
||||
AlgoDefinitionServiceServer::AlgoDefinitionServiceServer(
|
||||
|
||||
@@ -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<SystemConfig>().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<PImpl>(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<bool, std::string>
|
||||
SqliteStore::retrieve_one(std::string const &ns, std::string const &k) {
|
||||
std::pair<bool, std::string> 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());
|
||||
|
||||
@@ -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<std::string, std::map<std::string, std::string>> Data;
|
||||
Data retrieve_all() override;
|
||||
std::pair<bool, std::string>
|
||||
retrieve_one(std::string const &ns, std::string const &k) override;
|
||||
std::pair<bool, std::string> retrieve_one(
|
||||
std::string const &ns, std::string const &k) override;
|
||||
|
||||
void
|
||||
remove(std::string const &ns, std::set<std::string> const &keys) override;
|
||||
void remove(
|
||||
std::string const &ns, std::set<std::string> 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<PImpl> me;
|
||||
void init_db();
|
||||
|
||||
@@ -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()); }
|
||||
|
||||
|
||||
Reference in new issue
Block a user