diff --git a/crates/shared/src/persistence/mod.rs b/crates/shared/src/persistence/mod.rs index 28cd855..d052053 100644 --- a/crates/shared/src/persistence/mod.rs +++ b/crates/shared/src/persistence/mod.rs @@ -10,12 +10,13 @@ pub mod sqlite; pub trait BuilderPersistence { /// Append a transaction in Vec to persistence mempool async fn append(&self, tx_data: Vec) -> Result<(), sqlx::Error>; - /// Load all the transactions whose `created_at` is before or equal to `timeout_after` - async fn load(&self, timeout_after: Instant) -> Result>, sqlx::Error>; + /// Load all the transactions whose `created_at` is before or equal to `before_instant` + async fn load(&self, before_instant: Instant) -> Result>, sqlx::Error>; /// Remove a transaction in Vec from the persistence mempool async fn remove(&self, tx: Vec) -> Result<(), sqlx::Error>; } +/// build sqlite database path, if not exist, will create one pub fn build_sqlite_path(path: &Path) -> anyhow::Result { let sub_dir = path.join("sqlite"); diff --git a/crates/shared/src/persistence/sqlite.rs b/crates/shared/src/persistence/sqlite.rs index a64d4a6..659eaa3 100644 --- a/crates/shared/src/persistence/sqlite.rs +++ b/crates/shared/src/persistence/sqlite.rs @@ -14,8 +14,7 @@ pub struct SqliteTxnDb { pool: SqlitePool, } impl SqliteTxnDb { - /// New a SqliteTxnDb by calling with the database_url - #[allow(dead_code)] + /// New a `SqliteTxnDb` by calling with `database_url` pub async fn new(database_url: String) -> Result { let pool = SqlitePool::connect(&database_url).await?; // it will handle the default CURRENT_TIMESTAMP automatically and assign to transaction's created_at @@ -33,8 +32,7 @@ impl SqliteTxnDb { Ok(Self { pool }) } - /// Clear all the data in this SqliteTxnDb database - #[allow(dead_code)] + /// Clear all the data in this `SqliteTxnDb` database pub async fn clear(&self) -> Result<(), sqlx::Error> { // Execute a SQL statement to delete all rows from the `transactions` table sqlx::query("DELETE FROM transactions") @@ -58,10 +56,10 @@ impl BuilderPersistence for SqliteTxnDb { Ok(()) } - async fn load(&self, timeout_after: Instant) -> Result>, sqlx::Error> { + async fn load(&self, before_instant: Instant) -> Result>, sqlx::Error> { // Convert Instant to SystemTime let now = SystemTime::now(); - let elapsed = timeout_after.elapsed(); + let elapsed = before_instant.elapsed(); let target_time = now - elapsed; // Convert SystemTime to a format SQLite understands (RFC 3339) @@ -137,8 +135,8 @@ mod test { db.append(tx).await.expect("In test_persistence_append_and_load_txn, there shouldn't be any error when doing append"); } - // Set timeout_after to the current time - let timeout_after = Instant::now(); + // Set before_instant to the current time + let before_instant = Instant::now(); // Simulate some delay tokio::time::sleep(std::time::Duration::from_secs(1)).await; @@ -146,8 +144,8 @@ mod test { // Append one more transaction db.append(vec![7, 8, 9]).await.expect("In test_persistence_append_and_load_txn, there shouldn't be any error when doing append"); - // Load transactions before timeout_after - let tx_data_list = db.load(timeout_after).await.expect( + // Load transactions before before_instant + let tx_data_list = db.load(before_instant).await.expect( "In test_persistence_append_and_load_txn, it should be able to load some transactions.", ); tracing::debug!("Transaction data before timeout: {:?}", tx_data_list);