Skip to content

Commit c0104db

Browse files
Javier Gil Avilesdepink5
authored andcommitted
Refs #23586: First version of save & load from backend to SQL database
Signed-off-by: Javier Gil Aviles <javiergil@eprosima.com>
1 parent 0d33a4c commit c0104db

File tree

5 files changed

+280
-287
lines changed

5 files changed

+280
-287
lines changed

sustainml_cpp/include/sustainml_cpp/database/Database.hpp

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
#ifndef SUSTAINMLCPP_DATABASE_DATABASE_HPP
2020
#define SUSTAINMLCPP_DATABASE_DATABASE_HPP
2121

22-
#include <sqlite/sqlite3.h>
22+
#include <sqlite3.h>
2323
#include <string>
24+
#include <vector>
2425

2526
namespace sustainml {
2627
namespace database {
@@ -35,6 +36,20 @@ namespace database {
3536
class Database
3637
{
3738
public:
39+
40+
struct Row
41+
{
42+
long long problem_id{0};
43+
long long iteration_id{0};
44+
std::string user_input_json;
45+
std::string app_requirements_json;
46+
std::string ml_model_metadata_json;
47+
std::string ml_model_json;
48+
std::string hw_constraints_json;
49+
std::string hw_resources_json;
50+
std::string carbon_footprint_json;
51+
};
52+
3853
/**
3954
* @brief Construct with the database file path.
4055
*
@@ -76,32 +91,21 @@ class Database
7691
int create_schema();
7792

7893
/**
79-
* @brief Save a JSON payload into the database.
94+
* @brief Replace all rows in the database with the provided set of rows.
8095
*
81-
* Database must be open.
96+
* @param rows Vector of rows to insert/replace.
8297
* @return SQLite result code.
8398
*/
84-
int save(std::string& json_in);
99+
int replace_all_rows(const std::vector<Row>& rows);
85100

86101
/**
87-
* @brief Load JSON payload(s) from the database into json_out.
102+
* @brief Load all rows from the database into the provided vector.
88103
*
89-
* Database must be open.
104+
* @param out_rows Vector to fill with loaded rows.
90105
* @return SQLite result code.
91106
*/
92-
int load(std::string& json_out);
93-
94-
/**
95-
* @brief True if the database was created during initialize().
96-
*/
97-
bool created_on_open() const { return created_on_open_; }
107+
int read_all_rows(std::vector<Row>& out_rows);
98108

99-
/**
100-
* @brief Dump database contents to stdout (for debugging).
101-
*
102-
* @return SQLite result code.
103-
*/
104-
int dump_to_stdout();
105109

106110
/**
107111
* @brief Access the raw sqlite3* handle (or nullptr if closed).

sustainml_cpp/include/sustainml_cpp/orchestrator/OrchestratorNode.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
#include <condition_variable>
2323
#include <memory>
2424
#include <mutex>
25+
#include <vector>
2526

2627
#include <sustainml_cpp/core/Constants.hpp>
2728
#include <sustainml_cpp/types/types.hpp>
29+
#include <sustainml_cpp/database/Database.hpp>
2830

2931
#include <fastdds/dds/domain/DomainParticipantListener.hpp>
3032

@@ -306,6 +308,19 @@ class OrchestratorNode
306308

307309
std::unique_ptr<OrchestratorParticipantListener> participant_listener_;
308310

311+
// Persistence
312+
std::unique_ptr<sustainml::database::Database> database_;
313+
std::vector<types::TaskId> persisted_task_ids_;
314+
315+
/**
316+
* @brief Restores the orchestrator's internal tasks data from the database.
317+
*/
318+
void hydrate_from_db_();
319+
320+
/**
321+
* @brief Saves the orchestrator's internal tasks data to the database.
322+
*/
323+
void persist_to_db_();
309324
};
310325

311326
} // namespace orchestrator

0 commit comments

Comments
 (0)