sqlite_helper.rs

Overview

This file provides functionality for managing SQLite database operations related to archival storage of blockchain data structures such as blocks, accounts, messages, and transactions. It acts as a helper layer to facilitate concurrent insertion and management of these records into an SQLite database, using a background worker thread for asynchronous writes. The implementation includes database connection setup with optimized SQLite pragmas, database file rotation for archival, and transactional batch inserts for performance.

The file interacts closely with the domain-specific data structures: ArchBlock, ArchAccount, ArchMessage, ArchTransaction, and the DocumentsDb trait for storing various records in the database.


Key Structs and Types

SqliteHelperConfig

Configuration struct holding paths for data directory and database file.

SqliteHelperContext

Holds the configuration and a thread-safe SQLite connection wrapped in a mutex for synchronized access.

DBFiles

Internal struct managing paths to key database files for rotation and archival.

SqliteHelper

Primary struct managing the SQLite helper functionality.


Trait Implementation: DocumentsDb for SqliteHelper

SqliteHelper implements the DocumentsDb trait for asynchronous insertion of blockchain data.


Utility Functions


Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram

flowchart TD
A[SqliteHelper] -->|uses| B[SqliteHelperConfig]
A -->|manages| C[DBFiles]
A -->|holds| D["record_sender(Channel Sender)"]
A -->|holds| E["conn(Arc<Mutex<Connection>>)"]
D -->|sends DBStoredRecord| F[put_records_worker Thread]
F -->|calls| G["store_block()"]
F -->|calls| H["store_accounts()"]
F -->|calls| I["store_messages()"]
F -->|calls| J["store_transactions()"]
A -->|implements| K[DocumentsDb Trait]
K -->|calls| D
subgraph DB Rotation
A --> L["rotate_db_file()"]
L --> M["rename_with_suffixes()"]
L -->|creates| E
L -->|spawns| F
end

Tests


This file provides a robust, concurrent SQLite storage helper tailored for blockchain archival data, focusing on performance and safe multi-threaded operation, with mechanisms for database file lifecycle management. It serves as a critical persistence layer in the system's data storage architecture.