Keep the db connection open throughout the lifetime of OriginsDB

This commit is contained in:
King_DuckZ 2020-08-15 23:32:22 +01:00
parent f648e3a8a2
commit 0faa8fb18f
2 changed files with 19 additions and 15 deletions

View file

@ -113,16 +113,6 @@ namespace {
", FOREIGN KEY(item_id) REFERENCES items(id)"
")";
class Database : private std::unique_lock<std::mutex>, public SQLite::Database {
public:
Database(std::mutex& mtx, const std::string& path) :
std::unique_lock<std::mutex>(mtx),
SQLite::Database(path, SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE)
{
SQLite::Database::exec("PRAGMA foreign_keys = ON");
}
};
class ItemIdToTableId {
public:
explicit ItemIdToTableId (SQLite::Database& db, bool with_caching=true) :
@ -269,14 +259,16 @@ namespace {
} //unnamed namespace
OriginsDB::OriginsDB (std::string_view path) :
m_path(path.data(), path.size())
m_path(path.data(), path.size()),
m_db(std::make_unique<SQLite::Database>(m_path, SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE))
{
}
OriginsDB::~OriginsDB() noexcept = default;
void OriginsDB::update (const Items& items) {
Database db(m_db_mutex, m_path);
auto& db = *m_db;
std::unique_lock<std::mutex> lock(m_db_mutex);
//example:
//{
@ -339,7 +331,9 @@ void OriginsDB::update (const Icons& icons) {
// "icon":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAMAAADXqc3KAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAP1BMVEX/AP/XqpHDkHqudmL////ox6X/9LxJSUm3t7eSkpLb29tubm7Kt6//8+u+QzDTjG7tZEfyhVmPIhi4o5v/8OVQ35yOAAAAAXRSTlMAQObYZgAAAAFiS0dEBI9o2VEAAAAHdElNRQfkAg4MBBc1u27AAAAAmElEQVQoz7WP2w6DIBAFsSyedavLRf//W7uQVmk0afvQeSIzOQSc+wfDcPOXnigE/1MYxxBw0szTYAMwv2kikunuPcBEc+8XknkCVNXOfHhADOYYVYG9mE92V2y08iksQBbRp+/CDM3CSRulKNb9E5o4p/QK6/Fe0WyhpVI6bxORZK7SD+pELn0t7ZqTd25raT35mrZL/TUPifEKODGf6AcAAAAldEVYdGRhdGU6Y3JlYXRlADIwMjAtMDItMTRUMTI6MDQ6MjMrMDA6MDCsyg5gAAAAJXRFWHRkYXRlOm1vZGlmeQAyMDIwLTAyLTE0VDEyOjA0OjIzKzAwOjAw3Ze23AAAAABJRU5ErkJggg=="
//}
Database db(m_db_mutex, m_path);
std::unique_lock<std::mutex> lock(m_db_mutex);
auto& db = *m_db;
db.exec(std::string(g_create_icons_a).append("icons_staging") + g_create_icons_b);
db.exec(std::string(g_create_icons_a).append("icons") + g_create_icons_b);
db.exec("CREATE UNIQUE INDEX IF NOT EXISTS icons_item_id_idx ON icons(item_id)");
@ -380,7 +374,9 @@ void OriginsDB::update (const Shops& shops) {
// "items":[{"item_id":678,"amount":200,"price":31000}]
//}
Database db(m_db_mutex, m_path);
auto& db = *m_db;
std::unique_lock<std::mutex> lock(m_db_mutex);
db.exec(g_create_shops);
db.exec("CREATE UNIQUE INDEX IF NOT EXISTS shops_owner_idx ON shops(owner, creation_date)");
db.exec(g_create_shop_snapshots);
@ -441,7 +437,9 @@ void OriginsDB::update (const Shops& shops) {
}
void OriginsDB::update (const Creators& creat) {
Database db(m_db_mutex, m_path);
std::unique_lock<std::mutex> lock(m_db_mutex);
auto& db = *m_db;
db.exec(g_create_fame_list);
auto insert_creators = [](SQLite::Statement& query, const std::vector<oro::Creator>& creats, int type) {

View file

@ -20,6 +20,11 @@
#include <mutex>
#include <string_view>
#include <string>
#include <memory>
namespace SQLite {
class Database;
} //namespace SQLite
namespace oro {
@ -41,6 +46,7 @@ public:
private:
std::string m_path;
std::mutex m_db_mutex;
std::unique_ptr<SQLite::Database> m_db;
};
} //namespace oro