|
Fray Engine
|
Owns a SQLite connection and a typed key/value state table. Database operations serialize on a recursive mutex; the attached FileManager is borrowed, not owned. More...
#include <state_manager.hpp>
Public Types | |
| using | Binary = std::vector<std::uint8_t> |
| Byte sequence stored as a SQLite blob. | |
Public Member Functions | |
| StateManager ()=default | |
| Construct a closed state manager. | |
| StateManager (const StateManager &)=delete | |
| Disallow copying the owned connection. | |
| StateManager & | operator= (const StateManager &)=delete |
| Disallow copy assignment of the owned connection. | |
| ~StateManager () | |
| Close the connection and detach from the file manager if still alive. The owner must stop concurrent use before destruction; close failures are logged. | |
| F_err | open (fray::files::FileManager &files, const std::string &table="Fray", const std::string &applicationId="", bool requireExistingTable=false) |
| Open and initialize a SQLite state table in the file manager's directory. Locks _mutex, creates the table in a transaction, and attaches to the borrowed file manager on success. | |
| const std::string & | projectName () const |
| Return the project name captured at open; its contents change on open or close. Does not acquire _mutex; synchronize with open and close. | |
| const std::string & | applicationId () const |
| Return the application identifier captured at open; its contents change on open or close. Does not acquire _mutex; synchronize with open and close. | |
| bool | isOpen () const |
| Check whether a database connection is open. Locks _mutex. | |
| template<typename Function> | |
| F_err | withDatabase (Function &&function) |
| Run a callback against the open database for supporting tables. Holds _mutex throughout the callback so close or rename cannot swap the connection; the pointer is borrowed for the call. | |
| std::recursive_mutex & | mutex () |
| Return the store mutex, held by the owner across close and reopen during file rename. | |
| F_err | close () |
| Close the database and detach the project. Locks _mutex; logs errors. | |
| F_err | set (const std::string &key, const std::string &value) |
| Save a string state value. The underlying write locks _mutex and logs successful writes. | |
| F_err | set (const std::string &key, const F_char *value) |
| Save a NUL-terminated string; null is rejected state value. The underlying write locks _mutex and logs successful writes. | |
| F_err | set (const std::string &key, double value) |
| Save a finite double state value. The underlying write locks _mutex and logs successful writes. | |
| F_err | set (const std::string &key, bool value) |
| Save a boolean state value. The underlying write locks _mutex and logs successful writes. | |
| F_err | set (const std::string &key, const Binary &value) |
| Save a binary blob state value. The underlying write locks _mutex and logs successful writes. | |
| template<typename T, std::enable_if_t< std::is_integral_v< T > &&!std::is_same_v< T, bool >, int > = 0> | |
| F_err | set (const std::string &key, T value) |
| Save an integral value as signed 64-bit state. The underlying write locks _mutex and logs successful writes. | |
| F_err | get (const std::string &key, std::string &value) |
| Read a string state value. The underlying read locks _mutex; value is unchanged on failure. | |
| F_err | get (const std::string &key, double &value) |
| Read a double state value. The underlying read locks _mutex; value is unchanged on failure. | |
| F_err | get (const std::string &key, bool &value) |
| Read a boolean state value. The underlying read locks _mutex; value is unchanged on failure. | |
| F_err | get (const std::string &key, Binary &value) |
| Read a binary blob state value. The underlying read locks _mutex; value is unchanged on failure. | |
| F_err | contains (const std::string &key, F_bool &exists) |
| Check whether a state key exists. Locks _mutex. | |
| F_err | get (const std::string &key, float &value) |
| Read a float state value. The underlying read locks _mutex; value is unchanged on failure. | |
| template<typename T, std::enable_if_t< std::is_integral_v< T > &&!std::is_same_v< T, bool >, int > = 0> | |
| F_err | get (const std::string &key, T &value) |
| Read an integer state value into a checked destination type. The underlying read locks _mutex; value is unchanged on failure. | |
| F_err | remove (const std::string &key) |
| Remove a state key. Locks _mutex. | |
| F_err | removeTable () |
| Drop the current state table without closing the store. Locks _mutex; a later write can recreate the table. | |
| F_err | renameTable (const std::string &name) |
| Rename the current state table to an encoded logical name. Locks _mutex and updates the active table name on success. | |
Private Types | |
| using | Value = std::variant<std::string, std::int64_t, double, bool, Binary> |
| Supported state value; variant order defines persistent type tags and must remain stable. | |
| using | Statement = std::unique_ptr<sqlite3_stmt, decltype(&sqlite3_finalize)> |
| SQLite statement owner that finalizes its statement on destruction. | |
Private Member Functions | |
| F_bool | projectAvailable () |
| Check that the borrowed file manager still exists. Logs on failure; callers accessing store state must hold _mutex. | |
| F_void | releaseProject () |
| Detach the live file manager and clear the associated project metadata. Call while holding _mutex, or during destruction after concurrent use has stopped. | |
| F_bool | createTable (const std::string &name) |
| Create a typed state table or validate an existing table's columns. Requires an open database and _mutex held; logs SQLite or schema failures. | |
| F_void | closeAfterOpenFailure () |
| Close the SQLite connection after initialization fails. Requires _mutex held; logs a close failure. | |
| F_void | rollback () |
| Roll back the current SQLite transaction and log any failure. Requires an open database and _mutex held. | |
| F_bool | validKey (const std::string &key) const |
| Validate a state key and log invalid input. | |
| F_bool | tableExists (const F_char *name, F_bool &exists) |
| Inspect whether an SQLite table exists, rejecting a matching view. Requires an open database and _mutex held. | |
| F_bool | hasExpectedColumns (const F_char *table, std::initializer_list< const F_char * > expected) |
| Check that a table has exactly the named columns. Requires an open database and _mutex held; column types are not checked. | |
| F_err | statementFailure (Statement &statement, const F_char *operation, F_err failure) |
| Classify and log a failed state statement after finalizing it. Requires an open database and _mutex held. | |
| F_err | write (const std::string &key, const Value &value, F_bool retry=true) |
| Insert or replace a typed value in the state table. Locks _mutex, may recreate a missing table once, and logs successful writes and failures. | |
| template<typename T> | |
| F_err | read (const std::string &key, T &output, int expectedType) |
| Read and validate a typed state value. Locks _mutex; output is changed only on a successful read. | |
| F_err | invalidStoredValue () |
| Log an invalid stored value. | |
Static Private Member Functions | |
| static std::string | tableName (const std::string &name) |
| Encode a logical table name as a safe SQLite identifier. | |
| static std::string | escaped (const std::string &text) |
| Escape bytes for diagnostic log output. | |
| static F_void | logSet (const std::string &key, const Value &value) |
| Log a successfully saved key and its formatted value. | |
Private Attributes | |
| std::recursive_mutex | _mutex |
| Serializes database and mutable store operations; recursive for nested calls. | |
| sqlite3 * | _db = nullptr |
| Owned SQLite connection, or null when closed. | |
| std::string | _table |
| Encoded name of the active state table. | |
| fray::files::FileManager * | _files = nullptr |
| Borrowed attached project file manager, valid only while its lifetime token is alive. | |
| std::weak_ptr< int > | _filesLifetime |
| Weak lifetime token guarding access to the borrowed file manager. | |
| std::string | _projectName |
| Project name captured when the store opened. | |
| std::string | _applicationId |
| Application identifier captured when the store opened. | |
Owns a SQLite connection and a typed key/value state table. Database operations serialize on a recursive mutex; the attached FileManager is borrowed, not owned.
Definition at line 32 of file state_manager.hpp.
| using fray::state::StateManager::Binary = std::vector<std::uint8_t> |
Byte sequence stored as a SQLite blob.
Definition at line 38 of file state_manager.hpp.
|
private |
SQLite statement owner that finalizes its statement on destruction.
Definition at line 48 of file state_manager.hpp.
|
private |
Supported state value; variant order defines persistent type tags and must remain stable.
Definition at line 44 of file state_manager.hpp.
|
default |
Construct a closed state manager.
|
delete |
Disallow copying the owned connection.
|
inline |
Close the connection and detach from the file manager if still alive. The owner must stop concurrent use before destruction; close failures are logged.
Definition at line 614 of file state_manager.hpp.
|
inline |
Return the application identifier captured at open; its contents change on open or close. Does not acquire _mutex; synchronize with open and close.
Definition at line 716 of file state_manager.hpp.
|
inline |
Close the database and detach the project. Locks _mutex; logs errors.
Definition at line 757 of file state_manager.hpp.
|
inlineprivate |
Close the SQLite connection after initialization fails. Requires _mutex held; logs a close failure.
Definition at line 159 of file state_manager.hpp.
Check whether a state key exists. Locks _mutex.
| key | Nonempty key without NUL bytes. |
| exists | Receives true if present or false if absent on success. |
Definition at line 908 of file state_manager.hpp.
|
inlineprivate |
Create a typed state table or validate an existing table's columns. Requires an open database and _mutex held; logs SQLite or schema failures.
| name | Encoded SQL table identifier. |
Definition at line 131 of file state_manager.hpp.
|
inlinestaticprivate |
Escape bytes for diagnostic log output.
| text | Bytes to escape. |
Definition at line 315 of file state_manager.hpp.
Read a binary blob state value. The underlying read locks _mutex; value is unchanged on failure.
| key | Nonempty key without NUL bytes. |
| value | Receives the value on success. |
Definition at line 897 of file state_manager.hpp.
|
inline |
Read a boolean state value. The underlying read locks _mutex; value is unchanged on failure.
| key | Nonempty key without NUL bytes. |
| value | Receives the value on success. |
Definition at line 887 of file state_manager.hpp.
|
inline |
Read a double state value. The underlying read locks _mutex; value is unchanged on failure.
| key | Nonempty key without NUL bytes. |
| value | Receives the value on success. |
Definition at line 877 of file state_manager.hpp.
|
inline |
Read a float state value. The underlying read locks _mutex; value is unchanged on failure.
| key | Nonempty key without NUL bytes. |
| value | Receives the value on success. |
Definition at line 947 of file state_manager.hpp.
|
inline |
Read a string state value. The underlying read locks _mutex; value is unchanged on failure.
| key | Nonempty key without NUL bytes. |
| value | Receives the value on success. |
Definition at line 867 of file state_manager.hpp.
|
inline |
Read an integer state value into a checked destination type. The underlying read locks _mutex; value is unchanged on failure.
| key | Nonempty key without NUL bytes. |
| value | Receives the converted value on success. |
Definition at line 973 of file state_manager.hpp.
|
inlineprivate |
Check that a table has exactly the named columns. Requires an open database and _mutex held; column types are not checked.
| table | Table to inspect. |
| expected | Exact expected column names. |
Definition at line 244 of file state_manager.hpp.
|
inlineprivate |
Log an invalid stored value.
Definition at line 588 of file state_manager.hpp.
|
inline |
Check whether a database connection is open. Locks _mutex.
Definition at line 722 of file state_manager.hpp.
|
inlinestaticprivate |
Log a successfully saved key and its formatted value.
| key | Saved state key. |
| value | Saved typed value. |
Definition at line 344 of file state_manager.hpp.
|
inline |
Return the store mutex, held by the owner across close and reopen during file rename.
Definition at line 750 of file state_manager.hpp.
|
inline |
Open and initialize a SQLite state table in the file manager's directory. Locks _mutex, creates the table in a transaction, and attaches to the borrowed file manager on success.
| files | Open file manager; it must remain alive while used. |
| table | Nonempty logical table name without NUL bytes. |
| applicationId | Application identifier retained as metadata. |
| requireExistingTable | Reject a database without this state table instead of creating one. |
Definition at line 634 of file state_manager.hpp.
|
delete |
Disallow copy assignment of the owned connection.
|
inlineprivate |
Check that the borrowed file manager still exists. Logs on failure; callers accessing store state must hold _mutex.
Definition at line 83 of file state_manager.hpp.
|
inline |
Return the project name captured at open; its contents change on open or close. Does not acquire _mutex; synchronize with open and close.
Definition at line 711 of file state_manager.hpp.
|
inlineprivate |
Read and validate a typed state value. Locks _mutex; output is changed only on a successful read.
| key | Nonempty key without NUL bytes. |
| output | Receives the decoded value on success. |
| expectedType | Persisted type tag expected for T (1 through 5). |
Definition at line 486 of file state_manager.hpp.
|
inlineprivate |
Detach the live file manager and clear the associated project metadata. Call while holding _mutex, or during destruction after concurrent use has stopped.
Definition at line 97 of file state_manager.hpp.
|
inline |
Remove a state key. Locks _mutex.
| key | Nonempty key without NUL bytes. |
Definition at line 1006 of file state_manager.hpp.
|
inline |
Drop the current state table without closing the store. Locks _mutex; a later write can recreate the table.
Definition at line 1050 of file state_manager.hpp.
|
inline |
Rename the current state table to an encoded logical name. Locks _mutex and updates the active table name on success.
| name | New nonempty logical name without NUL bytes. |
Definition at line 1083 of file state_manager.hpp.
|
inlineprivate |
Roll back the current SQLite transaction and log any failure. Requires an open database and _mutex held.
Definition at line 174 of file state_manager.hpp.
|
inline |
Save a boolean state value. The underlying write locks _mutex and logs successful writes.
| key | Nonempty key without NUL bytes. |
| value | Value to save. |
Definition at line 822 of file state_manager.hpp.
Save a binary blob state value. The underlying write locks _mutex and logs successful writes.
| key | Nonempty key without NUL bytes. |
| value | Value to save. |
Definition at line 832 of file state_manager.hpp.
Save a NUL-terminated string; null is rejected state value. The underlying write locks _mutex and logs successful writes.
| key | Nonempty key without NUL bytes. |
| value | Value to save. |
Definition at line 794 of file state_manager.hpp.
|
inline |
Save a string state value. The underlying write locks _mutex and logs successful writes.
| key | Nonempty key without NUL bytes. |
| value | Value to save. |
Definition at line 784 of file state_manager.hpp.
|
inline |
Save a finite double state value. The underlying write locks _mutex and logs successful writes.
| key | Nonempty key without NUL bytes. |
| value | Value to save. |
Definition at line 812 of file state_manager.hpp.
|
inline |
Save an integral value as signed 64-bit state. The underlying write locks _mutex and logs successful writes.
| key | Nonempty key without NUL bytes. |
| value | Integer to save; unsigned values must fit in int64_t. |
Definition at line 844 of file state_manager.hpp.
|
inlineprivate |
Classify and log a failed state statement after finalizing it. Requires an open database and _mutex held.
| statement | Statement to finalize before inspecting the table. |
| operation | Operation name used in the error message. |
| failure | Error to return when the table exists or inspection fails. |
Definition at line 291 of file state_manager.hpp.
|
inlineprivate |
Inspect whether an SQLite table exists, rejecting a matching view. Requires an open database and _mutex held.
| name | Table name to look up case-insensitively. |
| exists | Receives whether the table exists on success. |
Definition at line 203 of file state_manager.hpp.
|
inlinestaticprivate |
Encode a logical table name as a safe SQLite identifier.
| name | Logical table name, encoded byte by byte. |
Definition at line 112 of file state_manager.hpp.
|
inlineprivate |
Validate a state key and log invalid input.
| key | Key to check; must be nonempty and contain no NUL. |
Definition at line 185 of file state_manager.hpp.
|
inline |
Run a callback against the open database for supporting tables. Holds _mutex throughout the callback so close or rename cannot swap the connection; the pointer is borrowed for the call.
| function | Callable receiving sqlite3* and returning an F_err. |
Definition at line 735 of file state_manager.hpp.
|
inlineprivate |
Insert or replace a typed value in the state table. Locks _mutex, may recreate a missing table once, and logs successful writes and failures.
| key | Nonempty key without NUL bytes. |
| value | Value to persist; floating-point values must be finite. |
| retry | Whether a missing table may be recreated and the write retried. |
Definition at line 382 of file state_manager.hpp.
|
private |
Application identifier captured when the store opened.
Definition at line 76 of file state_manager.hpp.
|
private |
Owned SQLite connection, or null when closed.
Definition at line 56 of file state_manager.hpp.
|
private |
Borrowed attached project file manager, valid only while its lifetime token is alive.
Definition at line 64 of file state_manager.hpp.
|
private |
Weak lifetime token guarding access to the borrowed file manager.
Definition at line 68 of file state_manager.hpp.
|
mutableprivate |
Serializes database and mutable store operations; recursive for nested calls.
Definition at line 52 of file state_manager.hpp.
|
private |
Project name captured when the store opened.
Definition at line 72 of file state_manager.hpp.
|
private |
Encoded name of the active state table.
Definition at line 60 of file state_manager.hpp.