Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions src/db_adapter/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,45 @@ cc_library(
":context_loader",
":data_mapper",
":data_types",
":db_wrapper",
":database_connection",
":database_wrapper",
"//commons:commons_lib",
"//commons/atoms:atoms_lib",
"//db_adapter/postgres:postgres_lib",
],
)

cc_library(
name = "data_mapper",
srcs = ["DataMapper.cc"],
hdrs = ["DataMapper.h"],
name = "data_types",
hdrs = ["DataTypes.h"],
includes = ["."],
deps = [
":data_types",
"//commons:commons_lib",
"//commons/atoms:atoms_lib",
],
)

cc_library(
name = "data_types",
hdrs = ["DataTypes.h"],
name = "data_mapper",
srcs = ["DataMapper.cc"],
hdrs = ["DataMapper.h"],
includes = ["."],
deps = [
":data_types",
"//commons:commons_lib",
"//commons/atoms:atoms_lib",
],
)

cc_library(
name = "db_wrapper",
hdrs = ["DBWrapper.h"],
name = "database_wrapper",
srcs = ["DatabaseWrapper.cc"],
hdrs = ["DatabaseWrapper.h"],
includes = ["."],
deps = [
":data_mapper",
":data_types",
":database_connection",
"//commons:commons_lib",
"//commons/atoms:atoms_lib",
],
Expand All @@ -58,3 +62,16 @@ cc_library(
"@nlohmann_json//:json",
],
)

cc_library(
name = "database_connection",
srcs = ["DatabaseConnection.cc"],
hdrs = ["DatabaseConnection.h"],
includes = ["."],
deps = [
":data_types",
"//commons:commons_lib",
"//commons/atoms:atoms_lib",
"//commons/processor:processor_lib",
],
)
44 changes: 44 additions & 0 deletions src/db_adapter/DatabaseConnection.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "DatabaseConnection.h"

using namespace db_adapter;

DatabaseConnection::DatabaseConnection(const string& id, const string& host, int port) : Processor(id) {
this->host = host;
this->port = port;
this->connected = false;
this->setup();
}

DatabaseConnection::~DatabaseConnection() {}

void DatabaseConnection::setup() {
if (!this->is_setup()) {
Processor::setup();
}
}

void DatabaseConnection::start() {
if (this->is_running() || this->is_finished()) return;

{
lock_guard<mutex> lock(this->connection_mutex);
this->connect();
this->connected = true;
}

Processor::start();
}

void DatabaseConnection::stop() {
if (!this->is_running()) return;

{
lock_guard<mutex> lock(this->connection_mutex);
this->disconnect();
this->connected = false;
}

Processor::stop();
}

bool DatabaseConnection::is_connected() const { return this->connected; }
36 changes: 36 additions & 0 deletions src/db_adapter/DatabaseConnection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <mutex>
#include <string>

#include "Processor.h"

using namespace std;
using namespace processor;

namespace db_adapter {

class DatabaseConnection : public Processor {
public:
DatabaseConnection(const string& id, const string& host, int port);
~DatabaseConnection() override;

virtual void setup() override;
virtual void start() override;
virtual void stop() override;

virtual void connect() = 0;
virtual void disconnect() = 0;

bool is_connected() const;

protected:
string host;
int port;

private:
bool connected;
mutex connection_mutex;
};

} // namespace db_adapter
22 changes: 22 additions & 0 deletions src/db_adapter/DatabaseWrapper.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "DatabaseWrapper.h"

namespace {
shared_ptr<Mapper> create_mapper(MAPPER_TYPE mapper_type) {
switch (mapper_type) {
case MAPPER_TYPE::SQL2METTA:
return make_shared<SQL2MettaMapper>();
case MAPPER_TYPE::SQL2ATOMS:
return make_shared<SQL2AtomsMapper>();
default:
throw invalid_argument("Unknown mapper type");
}
}
} // namespace

SQLWrapper::SQLWrapper(MAPPER_TYPE mapper_type)
: DatabaseWrapper(create_mapper(mapper_type), mapper_type) {}

DatabaseWrapper::DatabaseWrapper(shared_ptr<Mapper> mapper, MAPPER_TYPE mapper_type)
: mapper(move(mapper)), mapper_type(mapper_type) {}

unsigned int DatabaseWrapper::mapper_handle_trie_size() { return this->mapper->handle_trie_size(); }
43 changes: 9 additions & 34 deletions src/db_adapter/DBWrapper.h → src/db_adapter/DatabaseWrapper.h
Original file line number Diff line number Diff line change
@@ -1,44 +1,34 @@
#pragma once

#include <memory>
#include <mutex>
#include <stdexcept>
#include <string>
#include <vector>

#include "DataMapper.h"
#include "DataTypes.h"
#include "DatabaseConnection.h"

using namespace std;
using namespace db_adapter;
using namespace commons;

namespace db_adapter {

/**
* @class DatabaseWrapper
* @brief Generic interface for a database connection wrapper.
*
* @tparam ConnT The underlying connection object type (e.g., pqxx::connection).
*/
template <typename ConnT>
class DatabaseWrapper {
public:
explicit DatabaseWrapper(shared_ptr<Mapper> mapper, MAPPER_TYPE mapper_type)
: mapper(move(mapper)), mapper_type(mapper_type) {}
DatabaseWrapper(shared_ptr<Mapper> mapper, MAPPER_TYPE mapper_type);
virtual ~DatabaseWrapper() = default;

/**
* @brief Closes the connection.
*/
virtual void disconnect() = 0;

unsigned int mapper_handle_trie_size() { return mapper->handle_trie_size(); }
unsigned int mapper_handle_trie_size();

protected:
/**
* @brief Establishes connection to the database.
*/
virtual unique_ptr<ConnT> connect() = 0;

unique_ptr<ConnT> db_client;
unique_ptr<DatabaseConnection> db_client;
shared_ptr<Mapper> mapper;
MAPPER_TYPE mapper_type;
};
Expand All @@ -47,11 +37,9 @@ class DatabaseWrapper {
* @class SQLWrapper
* @brief Specialization of DatabaseWrapper for SQL-based databases.
*/
template <typename ConnT>
class SQLWrapper : public DatabaseWrapper<ConnT> {
class SQLWrapper : public DatabaseWrapper {
public:
explicit SQLWrapper(MAPPER_TYPE mapper_type)
: DatabaseWrapper<ConnT>(create_mapper(mapper_type), mapper_type) {}
explicit SQLWrapper(MAPPER_TYPE mapper_type);
virtual ~SQLWrapper() = default;

/**
Expand Down Expand Up @@ -81,18 +69,5 @@ class SQLWrapper : public DatabaseWrapper<ConnT> {
* @brief Executes a raw SQL query and maps the result.
*/
virtual void map_sql_query(const string& virtual_name, const string& raw_query) = 0;

private:
// Factory method for creating the specific mapper strategy
static shared_ptr<Mapper> create_mapper(MAPPER_TYPE mapper_type) {
switch (mapper_type) {
case MAPPER_TYPE::SQL2METTA:
return make_shared<SQL2MettaMapper>();
case MAPPER_TYPE::SQL2ATOMS:
return make_shared<SQL2AtomsMapper>();
default:
throw invalid_argument("Unknown mapper type");
}
}
};
} // namespace db_adapter
3 changes: 2 additions & 1 deletion src/db_adapter/postgres/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ cc_library(
deps = [
"//commons:commons_lib",
"//db_adapter:data_mapper",
"//db_adapter:db_wrapper",
"//db_adapter:database_connection",
"//db_adapter:database_wrapper",
],
)
Loading