azoth-sqlite 0.2.5

SQLite-backed projection store for Azoth
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use azoth_core::{
    error::{AzothError, Result},
    traits::ProjectionStore,
    types::EventId,
    ProjectionConfig,
};
use parking_lot::Mutex;
use rusqlite::{Connection, OpenFlags};
use std::path::Path;
use std::sync::Arc;

use crate::read_pool::SqliteReadPool;
use crate::schema;
use crate::txn::SimpleProjectionTxn;

/// SQLite-backed projection store
///
/// Uses separate connections for reads and writes. SQLite WAL mode allows
/// readers and writers to operate concurrently at the database level.
/// While each connection still needs mutex protection (rusqlite::Connection
/// is not Sync), using separate connections means reads don't block writes
/// and vice versa.
///
/// Optionally supports a read connection pool for higher concurrency.
pub struct SqliteProjectionStore {
    /// Write connection (for transactions, schema operations, and writes)
    write_conn: Arc<Mutex<Connection>>,
    /// Read connection (for read-only queries, separate from write connection)
    read_conn: Arc<Mutex<Connection>>,
    config: ProjectionConfig,
    /// Optional read pool for concurrent reads
    read_pool: Option<Arc<SqliteReadPool>>,
}

impl SqliteProjectionStore {
    /// Get the underlying write connection (for migrations and custom queries).
    ///
    /// **Prefer `write_conn()`** for new code -- this method exists for
    /// backwards compatibility.
    pub fn conn(&self) -> &Arc<Mutex<Connection>> {
        &self.write_conn
    }

    /// Get the write connection explicitly.
    ///
    /// Use this for migrations, DDL, projector event application, and any
    /// other operations that mutate the projection database. Reads should
    /// go through `read_pool()`, `query()`, or `query_async()` instead.
    pub fn write_conn(&self) -> &Arc<Mutex<Connection>> {
        &self.write_conn
    }

    /// Open a read-only connection to the same database
    fn open_read_connection(path: &Path, cfg: &ProjectionConfig) -> Result<Connection> {
        let conn = Connection::open_with_flags(
            path,
            OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX,
        )
        .map_err(|e| AzothError::Projection(e.to_string()))?;

        // cache_size affects read performance
        conn.pragma_update(None, "cache_size", cfg.cache_size)
            .map_err(|e| AzothError::Config(e.to_string()))?;

        Ok(conn)
    }

    /// Initialize schema if needed
    fn init_schema(conn: &Connection) -> Result<()> {
        // Create projection_meta table
        conn.execute(
            "CREATE TABLE IF NOT EXISTS projection_meta (
                id INTEGER PRIMARY KEY CHECK (id = 0),
                last_applied_event_id INTEGER NOT NULL DEFAULT -1,
                schema_version INTEGER NOT NULL,
                updated_at TEXT NOT NULL DEFAULT (datetime('now'))
            )",
            [],
        )
        .map_err(|e| AzothError::Projection(e.to_string()))?;

        // Insert default row if not exists (-1 means no events processed yet)
        // schema_version starts at 0 so that migrations starting from version 1 will be applied
        conn.execute(
            "INSERT OR IGNORE INTO projection_meta (id, last_applied_event_id, schema_version)
             VALUES (0, -1, 0)",
            [],
        )
        .map_err(|e| AzothError::Projection(e.to_string()))?;

        Ok(())
    }

    /// Configure SQLite connection
    fn configure_connection(conn: &Connection, cfg: &ProjectionConfig) -> Result<()> {
        // Enable WAL mode
        if cfg.wal_mode {
            conn.pragma_update(None, "journal_mode", "WAL")
                .map_err(|e| AzothError::Config(e.to_string()))?;
        }

        // Set synchronous mode
        let sync_mode = match cfg.synchronous {
            azoth_core::config::SynchronousMode::Full => "FULL",
            azoth_core::config::SynchronousMode::Normal => "NORMAL",
            azoth_core::config::SynchronousMode::Off => "OFF",
        };
        conn.pragma_update(None, "synchronous", sync_mode)
            .map_err(|e| AzothError::Config(e.to_string()))?;

        // Enable foreign keys
        conn.pragma_update(None, "foreign_keys", "ON")
            .map_err(|e| AzothError::Config(e.to_string()))?;

        // Set cache size
        conn.pragma_update(None, "cache_size", cfg.cache_size)
            .map_err(|e| AzothError::Config(e.to_string()))?;

        Ok(())
    }

    /// Execute a read-only SQL query asynchronously
    ///
    /// This method runs the query on a separate thread to avoid blocking,
    /// making it safe to call from async contexts. Uses a read-only connection
    /// that doesn't block writes.
    ///
    /// # Example
    /// ```ignore
    /// let balance: i64 = store.query_async(|conn| {
    ///     conn.query_row("SELECT balance FROM accounts WHERE id = ?1", [account_id], |row| row.get(0))
    /// }).await?;
    /// ```
    pub async fn query_async<F, R>(&self, f: F) -> Result<R>
    where
        F: FnOnce(&Connection) -> Result<R> + Send + 'static,
        R: Send + 'static,
    {
        if let Some(pool) = &self.read_pool {
            let pool = Arc::clone(pool);
            return tokio::task::spawn_blocking(move || {
                let conn = pool.acquire_blocking()?;
                f(conn.connection())
            })
            .await
            .map_err(|e| AzothError::Projection(format!("Query task failed: {}", e)))?;
        }

        let conn = self.read_conn.clone();
        tokio::task::spawn_blocking(move || {
            let conn_guard = conn.lock();
            f(&conn_guard)
        })
        .await
        .map_err(|e| AzothError::Projection(format!("Query task failed: {}", e)))?
    }

    /// Execute a read-only SQL query synchronously
    ///
    /// This is a convenience method for non-async contexts.
    /// For async contexts, prefer `query_async`. Uses a read-only connection
    /// that doesn't block writes.
    ///
    /// # Example
    /// ```ignore
    /// let balance: i64 = store.query(|conn| {
    ///     conn.query_row("SELECT balance FROM accounts WHERE id = ?1", [account_id], |row| row.get(0))
    /// })?;
    /// ```
    pub fn query<F, R>(&self, f: F) -> Result<R>
    where
        F: FnOnce(&Connection) -> Result<R>,
    {
        if let Some(pool) = &self.read_pool {
            let conn = pool.acquire_blocking()?;
            return f(conn.connection());
        }

        let conn_guard = self.read_conn.lock();
        f(&conn_guard)
    }

    /// Execute arbitrary SQL statements (DDL/DML) asynchronously
    ///
    /// Useful for creating tables, indexes, or performing bulk updates.
    /// Uses the write connection.
    ///
    /// # Example
    /// ```ignore
    /// store.execute_async(|conn| {
    ///     conn.execute("CREATE TABLE IF NOT EXISTS balances (id INTEGER PRIMARY KEY, amount INTEGER)", [])?;
    ///     conn.execute("CREATE INDEX IF NOT EXISTS idx_amount ON balances(amount)", [])?;
    ///     Ok(())
    /// }).await?;
    /// ```
    pub async fn execute_async<F>(&self, f: F) -> Result<()>
    where
        F: FnOnce(&Connection) -> Result<()> + Send + 'static,
    {
        let conn = self.write_conn.clone();
        tokio::task::spawn_blocking(move || {
            let conn_guard = conn.lock();
            f(&conn_guard)
        })
        .await
        .map_err(|e| AzothError::Projection(format!("Execute task failed: {}", e)))?
    }

    /// Execute arbitrary SQL statements (DDL/DML) synchronously
    ///
    /// Uses the write connection.
    ///
    /// # Example
    /// ```ignore
    /// store.execute(|conn| {
    ///     conn.execute("CREATE TABLE IF NOT EXISTS balances (id INTEGER PRIMARY KEY, amount INTEGER)", [])?;
    ///     Ok(())
    /// })?;
    /// ```
    pub fn execute<F>(&self, f: F) -> Result<()>
    where
        F: FnOnce(&Connection) -> Result<()>,
    {
        let conn_guard = self.write_conn.lock();
        f(&conn_guard)
    }

    /// Execute a transaction with multiple SQL statements
    ///
    /// The closure receives a transaction object and can execute multiple
    /// statements atomically. If the closure returns an error, the transaction
    /// is rolled back. Uses the write connection.
    ///
    /// # Example
    /// ```ignore
    /// store.transaction(|tx| {
    ///     tx.execute("INSERT INTO accounts (id, balance) VALUES (?1, ?2)", params![1, 100])?;
    ///     tx.execute("INSERT INTO accounts (id, balance) VALUES (?1, ?2)", params![2, 200])?;
    ///     Ok(())
    /// })?;
    /// ```
    pub fn transaction<F>(&self, f: F) -> Result<()>
    where
        F: FnOnce(&rusqlite::Transaction) -> Result<()>,
    {
        let mut conn_guard = self.write_conn.lock();
        let tx = conn_guard
            .transaction()
            .map_err(|e| AzothError::Projection(e.to_string()))?;

        f(&tx)?;

        tx.commit()
            .map_err(|e| AzothError::Projection(e.to_string()))?;
        Ok(())
    }

    /// Execute a transaction asynchronously
    ///
    /// Uses the write connection.
    pub async fn transaction_async<F>(&self, f: F) -> Result<()>
    where
        F: FnOnce(&rusqlite::Transaction) -> Result<()> + Send + 'static,
    {
        let conn = self.write_conn.clone();
        tokio::task::spawn_blocking(move || {
            let mut conn_guard = conn.lock();
            let tx = conn_guard
                .transaction()
                .map_err(|e| AzothError::Projection(e.to_string()))?;

            f(&tx)?;

            tx.commit()
                .map_err(|e| AzothError::Projection(e.to_string()))?;
            Ok(())
        })
        .await
        .map_err(|e| AzothError::Projection(format!("Transaction task failed: {}", e)))?
    }

    /// Get reference to the read pool (if enabled)
    ///
    /// Returns None if read pooling was not enabled in config.
    pub fn read_pool(&self) -> Option<&Arc<SqliteReadPool>> {
        self.read_pool.as_ref()
    }

    /// Check if read pooling is enabled
    pub fn has_read_pool(&self) -> bool {
        self.read_pool.is_some()
    }

    /// Get the database path
    pub fn db_path(&self) -> &Path {
        &self.config.path
    }
}

impl ProjectionStore for SqliteProjectionStore {
    type Txn<'a> = SimpleProjectionTxn<'a>;

    fn open(cfg: ProjectionConfig) -> Result<Self> {
        // Create parent directory if needed
        if let Some(parent) = cfg.path.parent() {
            std::fs::create_dir_all(parent)?;
        }

        // Open write connection
        let write_conn = Connection::open_with_flags(
            &cfg.path,
            OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_CREATE,
        )
        .map_err(|e| AzothError::Projection(e.to_string()))?;

        // Configure write connection
        Self::configure_connection(&write_conn, &cfg)?;

        // Initialize schema
        Self::init_schema(&write_conn)?;

        // Open separate read connection for concurrent reads
        let read_conn = Self::open_read_connection(&cfg.path, &cfg)?;

        // Initialize read pool if enabled
        let read_pool = if cfg.read_pool.enabled {
            Some(Arc::new(SqliteReadPool::new(
                &cfg.path,
                cfg.read_pool.clone(),
            )?))
        } else {
            None
        };

        Ok(Self {
            write_conn: Arc::new(Mutex::new(write_conn)),
            read_conn: Arc::new(Mutex::new(read_conn)),
            config: cfg,
            read_pool,
        })
    }

    fn close(&self) -> Result<()> {
        // SQLite connections close automatically on drop
        Ok(())
    }

    fn begin_txn(&self) -> Result<Self::Txn<'_>> {
        // Begin exclusive transaction using SimpleProjectionTxn (uses write connection)
        let guard = self.write_conn.lock();
        SimpleProjectionTxn::new(guard)
    }

    fn get_cursor(&self) -> Result<EventId> {
        // Use read connection for this read-only operation
        let conn = self.read_conn.lock();
        let cursor: i64 = conn
            .query_row(
                "SELECT last_applied_event_id FROM projection_meta WHERE id = 0",
                [],
                |row| row.get(0),
            )
            .map_err(|e| AzothError::Projection(e.to_string()))?;

        Ok(cursor as EventId)
    }

    fn migrate(&self, target_version: u32) -> Result<()> {
        let conn = self.write_conn.lock();
        schema::migrate(&conn, target_version)
    }

    fn backup_to(&self, path: &Path) -> Result<()> {
        // Checkpoint WAL to flush all changes to the main database file
        {
            let conn = self.write_conn.lock();
            // Execute checkpoint with full iteration of results
            let mut stmt = conn
                .prepare("PRAGMA wal_checkpoint(RESTART)")
                .map_err(|e| AzothError::Projection(e.to_string()))?;
            let mut rows = stmt
                .query([])
                .map_err(|e| AzothError::Projection(e.to_string()))?;
            // Consume all rows to ensure checkpoint completes
            while let Ok(Some(_)) = rows.next() {}
        }

        // Get source path
        let src_path = &self.config.path;

        // Copy database file (now includes all changes from WAL)
        std::fs::copy(src_path, path)?;

        Ok(())
    }

    fn restore_from(path: &Path, cfg: ProjectionConfig) -> Result<Self> {
        // Copy backup file to target location
        std::fs::copy(path, &cfg.path)?;

        // Open the restored database
        Self::open(cfg)
    }

    fn schema_version(&self) -> Result<u32> {
        // Use read connection for this read-only operation
        let conn = self.read_conn.lock();
        let version: i64 = conn
            .query_row(
                "SELECT schema_version FROM projection_meta WHERE id = 0",
                [],
                |row| row.get(0),
            )
            .map_err(|e| AzothError::Projection(e.to_string()))?;

        Ok(version as u32)
    }
}