This repository has been archived on 2026-04-20. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
short-link-backend/includes/database_service.hpp
2024-12-29 17:05:55 +01:00

36 lines
1.1 KiB
C++

#ifndef DATABASE_SERVICE_HPP
#define DATABASE_SERVICE_HPP
#include <string>
#include <functional>
#include <mutex>
#include <memory>
#include <vector>
#include <soci/soci.h>
#include <soci/sqlite3/soci-sqlite3.h>
class DatabaseService {
public:
static DatabaseService& getInstance(const std::string& db_path, std::size_t pool_size);
void shortenURL(const std::string& longURL, std::function<void(std::error_code, std::string)> callback);
void getLongURL(const std::string& shortCode, std::function<void(std::error_code, std::string)> callback);
private:
DatabaseService(const std::string& db_path, std::size_t pool_size = 4);
~DatabaseService();
DatabaseService(DatabaseService const&);
void operator=(DatabaseService const&);
std::shared_ptr<soci::session> getConnection();
std::string generateShortUUID();
std::string db_path_;
std::size_t pool_size_;
std::vector<std::shared_ptr<soci::session>> connection_pool_;
std::size_t current_index_ = 0; // Index used for round-robin selection
static DatabaseService* INSTANCE;
static std::mutex singleton_mutex;
};
#endif