|
| 1 | +#include "sqlite_pool.h" |
| 2 | +#include <iostream> |
| 3 | +#include <cassert> |
| 4 | +#include <thread> |
| 5 | + |
| 6 | +namespace services { |
| 7 | + |
| 8 | +SQLiteConnectionPool::SQLiteConnectionPool(const std::string& db_path, size_t pool_size) |
| 9 | + : db_path_(db_path), pool_size_(pool_size) {} |
| 10 | + |
| 11 | +SQLiteConnectionPool::~SQLiteConnectionPool() { |
| 12 | + shutdown(); |
| 13 | +} |
| 14 | + |
| 15 | +bool SQLiteConnectionPool::initialize() { |
| 16 | + std::lock_guard<std::mutex> lk(mtx_); |
| 17 | + if (initialized_) return true; |
| 18 | + |
| 19 | + conns_.resize(pool_size_, nullptr); |
| 20 | + in_use_.assign(pool_size_, false); |
| 21 | + |
| 22 | + for (size_t i = 0; i < pool_size_; ++i) { |
| 23 | + sqlite3* db = nullptr; |
| 24 | + int rc = sqlite3_open(db_path_.c_str(), &db); |
| 25 | + if (rc != SQLITE_OK) { |
| 26 | + std::cerr << "[sqlite_pool] failed to open db: " << sqlite3_errstr(rc) << std::endl; |
| 27 | + // close previously opened |
| 28 | + for (auto c : conns_) if (c) sqlite3_close(c); |
| 29 | + return false; |
| 30 | + } |
| 31 | + |
| 32 | + // Enable WAL mode |
| 33 | + char* errmsg = nullptr; |
| 34 | + rc = sqlite3_exec(db, "PRAGMA journal_mode=WAL;", nullptr, nullptr, &errmsg); |
| 35 | + if (rc != SQLITE_OK) { |
| 36 | + std::cerr << "[sqlite_pool] failed to set WAL mode: " << (errmsg ? errmsg : "") << std::endl; |
| 37 | + if (errmsg) sqlite3_free(errmsg); |
| 38 | + sqlite3_close(db); |
| 39 | + for (auto c : conns_) if (c) sqlite3_close(c); |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + conns_[i] = db; |
| 44 | + } |
| 45 | + |
| 46 | + initialized_ = true; |
| 47 | + std::cout << "[sqlite_pool] initialized with " << pool_size_ << " connections (WAL enabled)" << std::endl; |
| 48 | + return true; |
| 49 | +} |
| 50 | + |
| 51 | +void SQLiteConnectionPool::shutdown() { |
| 52 | + std::lock_guard<std::mutex> lk(mtx_); |
| 53 | + for (auto c : conns_) { |
| 54 | + if (c) sqlite3_close(c); |
| 55 | + } |
| 56 | + conns_.clear(); |
| 57 | + in_use_.clear(); |
| 58 | + initialized_ = false; |
| 59 | +} |
| 60 | + |
| 61 | +sqlite3* SQLiteConnectionPool::acquire() { |
| 62 | + std::unique_lock<std::mutex> lk(mtx_); |
| 63 | + cv_.wait(lk, [this]() { |
| 64 | + for (bool b : in_use_) if (!b) return true; return false; |
| 65 | + }); |
| 66 | + for (size_t i = 0; i < conns_.size(); ++i) { |
| 67 | + if (!in_use_[i]) { |
| 68 | + in_use_[i] = true; |
| 69 | + return conns_[i]; |
| 70 | + } |
| 71 | + } |
| 72 | + return nullptr; // should not reach |
| 73 | +} |
| 74 | + |
| 75 | +void SQLiteConnectionPool::release(sqlite3* conn) { |
| 76 | + std::lock_guard<std::mutex> lk(mtx_); |
| 77 | + for (size_t i = 0; i < conns_.size(); ++i) { |
| 78 | + if (conns_[i] == conn) { |
| 79 | + in_use_[i] = false; |
| 80 | + cv_.notify_one(); |
| 81 | + return; |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// SQLiteConnHandle |
| 87 | +SQLiteConnHandle::SQLiteConnHandle(SQLiteConnectionPool& pool, sqlite3* conn) |
| 88 | + : pool_(pool), conn_(conn) {} |
| 89 | + |
| 90 | +SQLiteConnHandle::~SQLiteConnHandle() { |
| 91 | + if (conn_) pool_.release(conn_); |
| 92 | +} |
| 93 | + |
| 94 | +sqlite3* SQLiteConnHandle::get() { return conn_; } |
| 95 | + |
| 96 | +} // namespace services |
0 commit comments