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
1 change: 1 addition & 0 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ cc_shared_library(
"//atomdb:atomdb_lib",
"//attention_broker:attention_broker_lib",
"//commons:commons_lib",
"//db_adapter:db_adapter_lib",
"//distributed_algorithm_node:distributed_algorithm_node_lib",
"//hasher:hasher_lib",
"//metta:metta_lib",
Expand Down
Binary file added src/assets/libpqxx-7.10.5.tar.gz
Binary file not shown.
11 changes: 10 additions & 1 deletion src/commons/HandleTrie.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <iostream>
#include <stack>

#include "Logger.h"
#include "Utils.h"
#include "expression_hasher.h"

Expand All @@ -26,7 +27,10 @@ HandleTrie::TrieNode::~TrieNode() {
delete children[i];
}
delete[] children;
delete value;
if (value != NULL) {
delete value;
value = NULL;
}
}

string HandleTrie::TrieValue::to_string() { return ""; }
Expand Down Expand Up @@ -264,3 +268,8 @@ void HandleTrie::traverse(bool keep_root_locked,
root->trie_node_mutex.unlock();
}
}

bool HandleTrie::exists(const string& key) {
TrieNode* node = lookup_node(key);
return node != NULL ? true : false;
}
2 changes: 2 additions & 0 deletions src/commons/HandleTrie.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class HandleTrie {
*/
TrieValue* lookup(const string& key);

bool exists(const string& key);

/**
* Remove a key from this HandleTrie and its associated value.
*
Expand Down
46 changes: 46 additions & 0 deletions src/db_adapter/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
load("@rules_cc//cc:cc_library.bzl", "cc_library")

package(default_visibility = ["//visibility:public"])

cc_library(
name = "db_adapter_lib",
includes = ["."],
deps = [
":data_mapper",
":data_types",
":db_wrapper",
"//db_adapter/postgres:postgres_lib",
],
)

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

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

cc_library(
name = "db_wrapper",
hdrs = ["DBWrapper.h"],
includes = ["."],
deps = [
":data_types",
"//commons:commons_lib",
"//commons/atoms:atoms_lib",
],
)
98 changes: 98 additions & 0 deletions src/db_adapter/DBWrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#pragma once

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

#include "DataMapper.h"

using namespace std;
using namespace db_adapter;

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eddiebrissow @marcocapozzoli
Please make this class implement commons/processor/Processor.h. Basically it means you need to implement
virtual void setup();
virtual void start();
virtual void stop();

setup() Is supposed to make proper initialization without starting any threads, connections etc. It's OK if it's empty.
start() is supposed to start any resources like threads, connections, file openings etc
stop() is supposed to gracefully stop the use of any resource. Stopping/synching threads, closing connections, closing open files etc.

The idea is that relevant resources like threads, connections etc become Processors as well. You can connect them (by calling add_subprocessor()) so when you setup/start/stop Processors they are setup/started/stopped in the proper order to avoid crashing.
We already have DedicatedThread.h and ThreadPool.h in the Processor package. The former is for when you need to spawn a single thread. The later is for when you want a thread pool of workers.
You may need to add a PostgresDBConnection (which inherits from Processor as well) in your package and probably you'll pass this class as the template argument instead of connection type.

public:
explicit DatabaseWrapper(shared_ptr<Mapper> mapper, MAPPER_TYPE mapper_type)
: mapper(move(mapper)), mapper_type(mapper_type) {}
virtual ~DatabaseWrapper() = default;

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method will disappear when you implement Processor


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

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

unique_ptr<ConnT> db_client;
shared_ptr<Mapper> mapper;
MAPPER_TYPE mapper_type;
};

/**
* @class SQLWrapper
* @brief Specialization of DatabaseWrapper for SQL-based databases.
*/
template <typename ConnT>
class SQLWrapper : public DatabaseWrapper<ConnT> {
public:
explicit SQLWrapper(MAPPER_TYPE mapper_type)
: DatabaseWrapper<ConnT>(create_mapper(mapper_type), mapper_type) {}
virtual ~SQLWrapper() = default;

/**
* @brief Retrieves schema information for a specific table.
*/
virtual Table get_table(const string& name) = 0;

/**
* @brief Lists all tables in the database.
*/
virtual vector<Table> list_tables() = 0;

/**
* @brief Maps a table's data to the target format.
*
* @param table The table metadata.
* @param clauses Additional SQL WHERE clauses.
* @param skip_columns Columns to exclude from mapping.
* @param second_level Boolean flag for recursive/depth mapping logic.
*/
virtual void map_table(const Table& table,
const vector<string>& clauses,
const vector<string>& skip_columns,
bool second_level) = 0;

/**
* @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
Loading
Loading