Fray Engine
Loading...
Searching...
No Matches
fray::history::HistoryManager Class Reference

Accesses project undo/redo trees stored beside state in the project database. Each domain has a head; recording after undo creates a recoverable branch. The referenced state must outlive this manager. Database operations run on the calling thread under the state mutex. More...

#include <history_manager.hpp>

Public Member Functions

 HistoryManager (state::StateManager &state)
 Borrows the state store that owns the project database.
F_err record (const std::string &domain, std::uint64_t revision, const std::string &payload, bool &created)
 Records a new node under the domain head and advances the head transactionally. An existing domain/revision is ignored; recording after moving the head creates a branch. Takes the state mutex on the calling thread.
F_err head (const std::string &domain, std::optional< Node > &node)
 Finds the current node for a domain under the state mutex.
F_err find (const std::string &domain, std::uint64_t revision, std::optional< Node > &node)
 Finds a domain node by revision under the state mutex.
F_err latest (const std::string &domain, std::optional< Node > &node)
 Finds the highest revision in a domain across all branches under the state mutex.
F_err parent (const Node &node, std::optional< Node > &found)
 Finds a node’s parent under the state mutex.
F_err child (const Node &node, std::optional< Node > &found)
 Finds the most recently recorded child for redo under the state mutex.
F_err heads (std::vector< Node > &nodes)
 Reads all domain heads under the state mutex.
F_err moveHead (const std::string &domain, std::int64_t nodeId)
 Moves a domain head to an existing node without deleting branches. Runs on the calling thread under the state mutex.

Private Types

using Statement = std::unique_ptr<sqlite3_stmt, decltype(&sqlite3_finalize)>
 Owns an SQLite statement and finalizes it on destruction.

Static Private Member Functions

static Statement prepare (sqlite3 *db, const char *sql)
 Prepares an SQLite statement and logs failures.
static bool bindText (sqlite3_stmt *statement, int index, const std::string &value)
 Binds a copied UTF-8 string to a statement parameter.
static bool run (sqlite3 *db, const char *sql)
 Executes SQL and logs an SQLite failure.
static bool ensureTables (sqlite3 *db)
 Creates the history node and head tables if absent.
static Node readNode (sqlite3_stmt *statement)
 Reads a node from the current row in nodeColumns order.
static F_err selectNode (sqlite3 *db, const char *sql, const std::string &domain, std::optional< std::int64_t > number, std::optional< Node > &found)
 Ensures history tables and reads at most one node.

Private Attributes

state::StateManager & _state
 Non-owning reference to the state store and its database mutex.

Static Private Attributes

static constexpr const char * nodeColumns
 Column order expected by readNode, with null parents mapped to zero.

Detailed Description

Accesses project undo/redo trees stored beside state in the project database. Each domain has a head; recording after undo creates a recoverable branch. The referenced state must outlive this manager. Database operations run on the calling thread under the state mutex.

Definition at line 56 of file history_manager.hpp.

Member Typedef Documentation

◆ Statement

using fray::history::HistoryManager::Statement = std::unique_ptr<sqlite3_stmt, decltype(&sqlite3_finalize)>
private

Owns an SQLite statement and finalizes it on destruction.

Definition at line 62 of file history_manager.hpp.

Constructor & Destructor Documentation

◆ HistoryManager()

fray::history::HistoryManager::HistoryManager ( state::StateManager & state)
inlineexplicit

Borrows the state store that owns the project database.

Parameters
stateState store that must outlive this manager.

Definition at line 209 of file history_manager.hpp.

Member Function Documentation

◆ bindText()

bool fray::history::HistoryManager::bindText ( sqlite3_stmt * statement,
int index,
const std::string & value )
inlinestaticprivate

Binds a copied UTF-8 string to a statement parameter.

Parameters
statementPrepared statement.
indexOne-based SQLite parameter index.
valueText to copy into the binding.
Returns
True on success; false on an SQLite binding error.

Definition at line 95 of file history_manager.hpp.

◆ child()

F_err fray::history::HistoryManager::child ( const Node & node,
std::optional< Node > & found )
inline

Finds the most recently recorded child for redo under the state mutex.

Parameters
nodeParent node to query.
foundReceives the child, or an empty optional if absent.
Returns
errors::NONE on successful query; errors::FAILED_HISTORY on a closed store or SQL failure.

Definition at line 370 of file history_manager.hpp.

◆ ensureTables()

bool fray::history::HistoryManager::ensureTables ( sqlite3 * db)
inlinestaticprivate

Creates the history node and head tables if absent.

Parameters
dbOpen database connection, protected by its state mutex.
Returns
True if both tables exist or were created; false on SQL failure.

Definition at line 125 of file history_manager.hpp.

◆ find()

F_err fray::history::HistoryManager::find ( const std::string & domain,
std::uint64_t revision,
std::optional< Node > & node )
inline

Finds a domain node by revision under the state mutex.

Parameters
domainDomain to query.
revisionRevision to look up, converted to an SQLite signed integer.
nodeReceives the node, or an empty optional if not found.
Returns
errors::NONE on successful query; errors::FAILED_HISTORY on a closed store or SQL failure.

Definition at line 314 of file history_manager.hpp.

◆ head()

F_err fray::history::HistoryManager::head ( const std::string & domain,
std::optional< Node > & node )
inline

Finds the current node for a domain under the state mutex.

Parameters
domainDomain to query.
nodeReceives the node, or an empty optional if none exists.
Returns
errors::NONE on successful query; errors::FAILED_HISTORY on a closed store or SQL failure.

Definition at line 294 of file history_manager.hpp.

◆ heads()

F_err fray::history::HistoryManager::heads ( std::vector< Node > & nodes)
inline

Reads all domain heads under the state mutex.

Parameters
nodesCleared then populated with current nodes; may be partial on SQL failure.
Returns
errors::NONE on success; errors::FAILED_HISTORY on a closed store or SQL failure.

Definition at line 388 of file history_manager.hpp.

◆ latest()

F_err fray::history::HistoryManager::latest ( const std::string & domain,
std::optional< Node > & node )
inline

Finds the highest revision in a domain across all branches under the state mutex.

Parameters
domainDomain to query.
nodeReceives the node, or an empty optional if none exists.
Returns
errors::NONE on successful query; errors::FAILED_HISTORY on a closed store or SQL failure.

Definition at line 333 of file history_manager.hpp.

◆ moveHead()

F_err fray::history::HistoryManager::moveHead ( const std::string & domain,
std::int64_t nodeId )
inline

Moves a domain head to an existing node without deleting branches. Runs on the calling thread under the state mutex.

Parameters
domainDomain whose head is moved.
nodeIdIdentifier of a node in that domain.
Returns
errors::NONE on success; errors::FAILED_HISTORY if the store is closed, the node is absent, or SQL fails.

Definition at line 414 of file history_manager.hpp.

◆ parent()

F_err fray::history::HistoryManager::parent ( const Node & node,
std::optional< Node > & found )
inline

Finds a node’s parent under the state mutex.

Parameters
nodeNode whose parent identifier is queried.
foundReceives the parent, or an empty optional if absent.
Returns
errors::NONE on successful query; errors::FAILED_HISTORY on a closed store or SQL failure.

Definition at line 352 of file history_manager.hpp.

◆ prepare()

Statement fray::history::HistoryManager::prepare ( sqlite3 * db,
const char * sql )
inlinestaticprivate

Prepares an SQLite statement and logs failures.

Parameters
dbOpen database connection, protected by its state mutex.
sqlSQL text to prepare.
Returns
Owning statement, or a null statement on error.

Definition at line 74 of file history_manager.hpp.

◆ readNode()

Node fray::history::HistoryManager::readNode ( sqlite3_stmt * statement)
inlinestaticprivate

Reads a node from the current row in nodeColumns order.

Parameters
statementStatement positioned on a node row.
Returns
Copied node data.

Definition at line 149 of file history_manager.hpp.

◆ record()

F_err fray::history::HistoryManager::record ( const std::string & domain,
std::uint64_t revision,
const std::string & payload,
bool & created )
inline

Records a new node under the domain head and advances the head transactionally. An existing domain/revision is ignored; recording after moving the head creates a branch. Takes the state mutex on the calling thread.

Parameters
domainNonempty owning domain.
revisionDomain revision, at most INT64_MAX.
payloadOpaque domain data to store.
createdSet true only when a new node commits; otherwise false.
Returns
errors::NONE on insertion or duplicate; errors::FAILED_HISTORY for invalid input, a closed store, or database failure.

Definition at line 224 of file history_manager.hpp.

◆ run()

bool fray::history::HistoryManager::run ( sqlite3 * db,
const char * sql )
inlinestaticprivate

Executes SQL and logs an SQLite failure.

Parameters
dbOpen database connection, protected by its state mutex.
sqlSQL text to execute.
Returns
True on success; false if execution fails.

Definition at line 110 of file history_manager.hpp.

◆ selectNode()

F_err fray::history::HistoryManager::selectNode ( sqlite3 * db,
const char * sql,
const std::string & domain,
std::optional< std::int64_t > number,
std::optional< Node > & found )
inlinestaticprivate

Ensures history tables and reads at most one node.

Parameters
dbOpen database connection, protected by its state mutex.
sqlQuery selecting columns in nodeColumns order.
domainDomain bound at parameter 1 when nonempty.
numberOptional integer bound at parameter 2.
foundCleared and populated when a row is found.
Returns
errors::NONE on success, including no row; errors::FAILED_HISTORY on SQL failure.

Definition at line 179 of file history_manager.hpp.

Member Data Documentation

◆ _state

state::StateManager& fray::history::HistoryManager::_state
private

Non-owning reference to the state store and its database mutex.

Definition at line 66 of file history_manager.hpp.

◆ nodeColumns

const char* fray::history::HistoryManager::nodeColumns
staticconstexprprivate
Initial value:
=
"node_id, domain, revision, COALESCE(parent_id, 0), sequence, payload"

Column order expected by readNode, with null parents mapped to zero.

Definition at line 167 of file history_manager.hpp.


The documentation for this class was generated from the following file: