musq 0.0.4

Musq is an asynchronous SQLite toolkit for Rust.
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
use std::{
    ffi::CString,
    ptr,
    sync::{
        Arc,
        atomic::{AtomicUsize, Ordering},
    },
    thread::{self, JoinHandle},
};

use either::Either;
use tokio::sync::{Mutex, oneshot};

use crate::{
    QueryResult, Row,
    error::{Error, Result},
    sqlite::{
        Arguments,
        connection::{
            ConnectionState, DbStatus, DbStatusKind, WalCheckpoint, WalCheckpointMode,
            establish::EstablishParams, execute,
        },
        ffi,
        statement::Statement,
    },
    transaction::{
        begin_ansi_transaction_sql, commit_ansi_transaction_sql, rollback_ansi_transaction_sql,
    },
};

// Each SQLite connection has a dedicated thread. It's possible to create a worker pool for this,
// but given typical application usage patterns for SQLite, the simplicity of a single-threaded
// worker is preferred.

/// Background worker thread driving a SQLite connection.
pub struct ConnectionWorker {
    /// Command channel to the worker thread.
    command_tx: flume::Sender<Command>,
    /// Mutex for locking access to the database.
    pub(crate) shared: Arc<WorkerSharedState>,
    /// Join handle for the worker thread.
    join_handle: Arc<Mutex<Option<JoinHandle<()>>>>,
}

// ConnectionWorker is safe to share between threads because:
// - command_tx is Sync (flume::Sender implements Sync)
// - shared is Arc<WorkerSharedState> which is Sync
// - join_handle is Arc<Mutex<>> which is Sync
unsafe impl Sync for ConnectionWorker {}

/// Shared state between async tasks and the worker thread.
pub struct WorkerSharedState {
    /// Cached statement size tracking.
    pub(crate) cached_statements_size: AtomicUsize,
    /// Mutex-protected connection state.
    pub(crate) conn: Mutex<ConnectionState>,
}

#[allow(dead_code)]
/// Commands sent to the worker thread.
enum Command {
    /// Prepare a statement and return it.
    Prepare {
        /// SQL text to prepare.
        query: Box<str>,
        /// Response channel.
        tx: oneshot::Sender<Result<Statement>>,
    },
    /// Execute a statement and stream results.
    Execute {
        /// SQL text to execute.
        query: Box<str>,
        /// Optional arguments to bind.
        arguments: Option<Arguments>,
        /// Result channel.
        tx: flume::Sender<Result<Either<QueryResult, Row>>>,
    },
    /// Begin a transaction.
    Begin {
        /// Response channel.
        tx: rendezvous_oneshot::Sender<Result<()>>,
    },
    /// Commit a transaction.
    Commit {
        /// Response channel.
        tx: rendezvous_oneshot::Sender<Result<()>>,
    },
    /// Roll back a transaction.
    Rollback {
        /// Optional response channel.
        tx: Option<rendezvous_oneshot::Sender<Result<()>>>,
    },
    /// Return a database status counter.
    DbStatus {
        /// Status counter kind.
        kind: DbStatusKind,
        /// Whether SQLite should reset the high-water mark after reading it.
        reset_highwater: bool,
        /// Response channel.
        tx: oneshot::Sender<Result<DbStatus>>,
    },
    /// Run a WAL checkpoint operation.
    WalCheckpoint {
        /// Optional attached database schema name.
        schema: Option<CString>,
        /// Checkpoint mode.
        mode: WalCheckpointMode,
        /// Response channel.
        tx: oneshot::Sender<Result<WalCheckpoint>>,
    },
    /// Return the parser stack depth limit.
    ParserDepthLimit {
        /// Response channel.
        tx: oneshot::Sender<Result<u32>>,
    },

    #[cfg(test)]
    /// Clear cached statements (tests only).
    ClearCache {
        /// Response channel.
        tx: oneshot::Sender<()>,
    },

    /// Shut down the worker thread.
    Shutdown {
        /// Response channel.
        tx: oneshot::Sender<Result<()>>,
    },
}

impl ConnectionWorker {
    /// Spawn a worker thread and establish the SQLite connection.
    pub(crate) async fn establish(params: EstablishParams) -> Result<Self> {
        let (establish_tx, establish_rx) = oneshot::channel();

        let join_handle = thread::Builder::new()
            .name(params.thread_name.clone())
            .spawn(move || {
                let (command_tx, command_rx) = flume::bounded(params.command_channel_size);

                let conn = match params.establish() {
                    Ok(conn) => conn,
                    Err(e) => {
                        establish_tx.send(Err(e)).ok();
                        return;
                    }
                };

                let shared = Arc::new(WorkerSharedState {
                    cached_statements_size: AtomicUsize::new(0),
                    // note: this mutex is used to synchronize access to the
                    // database from both the worker thread and async tasks.
                    // tokio's mutex is fair so we do not need any additional
                    // configuration here.
                    conn: Mutex::new(conn),
                });
                let mut conn = match shared.conn.try_lock() {
                    Ok(lock) => lock,
                    Err(e) => {
                        establish_tx.send(Err(e.into())).ok();
                        return;
                    }
                };

                if establish_tx
                    .send(Ok((command_tx, Arc::clone(&shared))))
                    .is_err()
                {
                    return;
                }

                // If COMMIT or ROLLBACK is processed but not acknowledged, there would be another
                // ROLLBACK sent when the `Transaction` drops. We need to ignore it otherwise we
                // would rollback an already completed transaction.
                let mut ignore_next_start_rollback = false;

                for cmd in command_rx {
                    match cmd {
                        Command::Prepare { query, tx } => {
                            tx.send(prepare(&mut conn, &query).inspect(|_prepared| {
                                update_cached_statements_size(
                                    &conn,
                                    &shared.cached_statements_size,
                                );
                            }))
                            .ok();
                        }
                        Command::Execute {
                            query,
                            arguments,
                            tx,
                        } => {
                            let iter = match execute::iter(&mut conn, &query, arguments)
                            {
                                Ok(iter) => iter,
                                Err(e) => {
                                    tx.send(Err(e)).ok();
                                    continue;
                                }
                            };

                            for res in iter {
                                if tx.send(res).is_err() {
                                    break;
                                }
                            }

                            update_cached_statements_size(&conn, &shared.cached_statements_size);
                        }
                        Command::Begin { tx } => {
                            let depth = conn.transaction_depth;
                            let res =
                                conn.handle
                                    .exec(begin_ansi_transaction_sql(depth))
                                    .map(|_| {
                                        conn.transaction_depth += 1;
                                    });
                            let res_ok = res.is_ok();

                            if tx.blocking_send(res).is_err() && res_ok {
                                // The BEGIN was processed but not acknowledged. This means no
                                // `Transaction` was created and so there is no way to commit /
                                // rollback this transaction. We need to roll it back
                                // immediately otherwise it would remain started forever.
                                if let Err(error) = conn
                                    .handle
                                    .exec(rollback_ansi_transaction_sql(depth + 1))
                                    .map(|_| {
                                        conn.transaction_depth -= 1;
                                    })
                                {
                                    // The rollback failed. To prevent leaving the connection
                                    // in an inconsistent state we shutdown this worker which
                                    // causes any subsequent operation on the connection to fail.
                                    tracing::error!(%error, "failed to rollback cancelled transaction");
                                    break;
                                }
                            }
                        }
                        Command::Commit { tx } => {
                            let depth = conn.transaction_depth;

                            let res = if depth > 0 {
                                conn.handle
                                    .exec(commit_ansi_transaction_sql(depth))
                                    .map(|_| {
                                        conn.transaction_depth -= 1;
                                    })
                            } else {
                                Ok(())
                            };
                            let res_ok = res.is_ok();

                            if tx.blocking_send(res).is_err() && res_ok {
                                // The COMMIT was processed but not acknowledged. This means that
                                // the `Transaction` doesn't know it was committed and will try to
                                // rollback on drop. We need to ignore that rollback.
                                ignore_next_start_rollback = true;
                            }
                        }
                        Command::Rollback { tx } => {
                            if ignore_next_start_rollback && tx.is_none() {
                                ignore_next_start_rollback = false;
                                continue;
                            }

                            let depth = conn.transaction_depth;

                            let res = if depth > 0 {
                                let sql = if depth == 1 {
                                    rollback_ansi_transaction_sql(depth)
                                } else {
                                    format!(
                                        "{}; {}",
                                        rollback_ansi_transaction_sql(depth),
                                        commit_ansi_transaction_sql(depth)
                                    )
                                };

                                conn.handle.exec(sql).map(|_| {
                                    conn.transaction_depth -= 1;
                                })
                            } else {
                                Ok(())
                            };

                            let res_ok = res.is_ok();

                            if let Some(tx) = tx && tx.blocking_send(res).is_err() && res_ok {
                                // The ROLLBACK was processed but not acknowledged. This means
                                // that the `Transaction` doesn't know it was rolled back and
                                // will try to rollback again on drop. We need to ignore that
                                // rollback.
                                ignore_next_start_rollback = true;
                            }
                        }
                        Command::DbStatus {
                            kind,
                            reset_highwater,
                            tx,
                        } => {
                            tx.send(db_status(&conn, kind, reset_highwater)).ok();
                        }
                        Command::WalCheckpoint { schema, mode, tx } => {
                            tx.send(wal_checkpoint(&conn, schema.as_ref(), mode)).ok();
                        }
                        Command::ParserDepthLimit { tx } => {
                            tx.send(parser_depth_limit(&conn)).ok();
                        }

                        #[cfg(test)]
                        Command::ClearCache { tx } => {
                            conn.statements.clear();
                            update_cached_statements_size(&conn, &shared.cached_statements_size);
                            tx.send(()).ok();
                        }

                        Command::Shutdown { tx } => {
                            conn.statements.clear();
                            let res = conn.handle.close();

                            // drop the connection references before sending confirmation
                            // and ending the command loop
                            drop(conn);
                            drop(shared);
                            let _send_result = tx.send(res);
                            return;
                        }
                    }
                }
            })?;

        let (command_tx, shared) = establish_rx.await.map_err(|_| Error::WorkerCrashed)??;

        Ok(Self {
            command_tx,
            shared,
            join_handle: Arc::new(Mutex::new(Some(join_handle))),
        })
    }

    #[allow(dead_code)]
    /// Returns whether the worker has been shut down.
    pub(crate) fn is_shutdown(&self) -> bool {
        // For now, just return false as checking would require async
        // This is only used in drop, so it's not critical
        false
    }

    #[allow(dead_code)]
    /// Prepare a SQL statement on the worker thread.
    pub(crate) async fn prepare(&self, query: &str) -> Result<Statement> {
        self.oneshot_cmd(|tx| Command::Prepare {
            query: query.into(),
            tx,
        })
        .await?
    }

    /// Execute a SQL statement and stream the results.
    ///
    /// We take an owned string here - we immediatley copy it into the command anyway.
    pub(crate) async fn execute(
        &self,
        query: String,
        args: Option<Arguments>,
        chan_size: usize,
    ) -> Result<flume::Receiver<Result<Either<QueryResult, Row>>>> {
        let (tx, rx) = flume::bounded(chan_size);

        self.command_tx
            .send_async(Command::Execute {
                query: query.into(),
                arguments: args,
                tx,
            })
            .await
            .map_err(|_| Error::WorkerCrashed)?;

        Ok(rx)
    }

    /// Begin a transaction on the worker thread.
    pub(crate) async fn begin(&self) -> Result<()> {
        self.oneshot_cmd_with_ack(|tx| Command::Begin { tx })
            .await?
    }

    /// Commit the current transaction on the worker thread.
    pub(crate) async fn commit(&self) -> Result<()> {
        self.oneshot_cmd_with_ack(|tx| Command::Commit { tx })
            .await?
    }

    /// Roll back the current transaction on the worker thread.
    pub(crate) async fn rollback(&self) -> Result<()> {
        self.oneshot_cmd_with_ack(|tx| Command::Rollback { tx: Some(tx) })
            .await?
    }

    /// Start an asynchronous rollback without awaiting acknowledgement.
    pub(crate) fn start_rollback(&self) -> Result<()> {
        self.command_tx
            .send(Command::Rollback { tx: None })
            .map_err(|_| Error::WorkerCrashed)
    }

    /// Return a database status counter from the worker thread.
    pub(crate) async fn db_status(
        &self,
        kind: DbStatusKind,
        reset_highwater: bool,
    ) -> Result<DbStatus> {
        self.oneshot_cmd(|tx| Command::DbStatus {
            kind,
            reset_highwater,
            tx,
        })
        .await?
    }

    /// Run a WAL checkpoint operation on the worker thread.
    pub(crate) async fn wal_checkpoint(
        &self,
        schema: Option<CString>,
        mode: WalCheckpointMode,
    ) -> Result<WalCheckpoint> {
        self.oneshot_cmd(|tx| Command::WalCheckpoint { schema, mode, tx })
            .await?
    }

    /// Return the parser stack depth limit from the worker thread.
    pub(crate) async fn parser_depth_limit(&self) -> Result<u32> {
        self.oneshot_cmd(|tx| Command::ParserDepthLimit { tx })
            .await?
    }

    #[allow(dead_code)]
    /// Send a oneshot command and await the response.
    async fn oneshot_cmd<F, T>(&self, command: F) -> Result<T>
    where
        F: FnOnce(oneshot::Sender<T>) -> Command,
    {
        let (tx, rx) = oneshot::channel();

        self.command_tx
            .send_async(command(tx))
            .await
            .map_err(|_| Error::WorkerCrashed)?;

        rx.await.map_err(|_| Error::WorkerCrashed)
    }

    /// Send a oneshot command requiring acknowledgement before returning.
    async fn oneshot_cmd_with_ack<F, T>(&self, command: F) -> Result<T>
    where
        F: FnOnce(rendezvous_oneshot::Sender<T>) -> Command,
    {
        let (tx, rx) = rendezvous_oneshot::channel();

        self.command_tx
            .send_async(command(tx))
            .await
            .map_err(|_| Error::WorkerCrashed)?;

        rx.recv().await.map_err(|_| Error::WorkerCrashed)
    }

    #[cfg(test)]
    /// Clear cached statements in tests.
    pub(crate) async fn clear_cache(&self) -> Result<()> {
        self.oneshot_cmd(|tx| Command::ClearCache { tx }).await
    }

    /// Send a command to the worker to shut down the processing thread.
    ///
    /// A `WorkerCrashed` error may be returned if the thread has already stopped.
    pub(crate) async fn shutdown(&self) -> Result<()> {
        let join_handle = self.join_handle.lock().await.take();
        let (tx, rx) = oneshot::channel();

        let send_res = self
            .command_tx
            .send(Command::Shutdown { tx })
            .map_err(|_| Error::WorkerCrashed);

        if let Err(e) = send_res {
            if let Some(handle) = join_handle {
                let _join_result = handle.join();
            }
            return Err(e);
        }

        // wait for the response
        let res = rx.await.map_err(|_| Error::WorkerCrashed)?;
        res?;

        if let Some(handle) = join_handle {
            handle.join().map_err(|_| Error::WorkerCrashed)?;
        }

        Ok(())
    }
}

/// Prepare a SQL statement, using the cache when possible.
fn prepare(conn: &mut ConnectionState, query: &str) -> Result<Statement> {
    // prepare statement object (or checkout from cache)
    let statement = conn.statements.get(query)?;

    while let Some(_statement) = statement.prepare_next(&conn.handle)? {
        // prepare all statements in the compound query
    }

    Ok(Statement {
        sql: query.to_string(),
    })
}

/// Update the cached statement size metric.
fn update_cached_statements_size(conn: &ConnectionState, size: &AtomicUsize) {
    size.store(conn.statements.len(), Ordering::Release);
}

/// Return a database status counter for the active connection.
fn db_status(
    conn: &ConnectionState,
    kind: DbStatusKind,
    reset_highwater: bool,
) -> Result<DbStatus> {
    let (current, highwater) =
        ffi::db_status64(conn.handle.as_ptr(), kind.as_sqlite_code(), reset_highwater)?;
    Ok(DbStatus { current, highwater })
}

/// Run a WAL checkpoint operation for the active connection.
fn wal_checkpoint(
    conn: &ConnectionState,
    schema: Option<&CString>,
    mode: WalCheckpointMode,
) -> Result<WalCheckpoint> {
    let schema_ptr = schema.map_or(ptr::null(), |schema| schema.as_ptr());
    let (log_frames, checkpointed_frames) =
        ffi::wal_checkpoint_v2(conn.handle.as_ptr(), schema_ptr, mode.as_sqlite_code())?;
    Ok(WalCheckpoint {
        log_frames: frames_to_option(log_frames),
        checkpointed_frames: frames_to_option(checkpointed_frames),
    })
}

/// Return the parser stack depth limit for the active connection.
fn parser_depth_limit(conn: &ConnectionState) -> Result<u32> {
    let limit = ffi::limit(
        conn.handle.as_ptr(),
        libsqlite3_sys::SQLITE_LIMIT_PARSER_DEPTH,
        -1,
    );
    u32::try_from(limit).map_err(|_| {
        Error::Protocol(format!(
            "SQLite returned invalid parser depth limit {limit}"
        ))
    })
}

/// Convert SQLite frame counts where `-1` means unavailable.
fn frames_to_option(frames: i32) -> Option<i32> {
    if frames < 0 { None } else { Some(frames) }
}

// A oneshot channel where send completes only after the receiver receives the value.
/// Rendezvous-style oneshot channels with acknowledgement.
mod rendezvous_oneshot {
    use std::result::Result as StdResult;

    use super::oneshot;

    /// Error returned when a rendezvous channel is canceled.
    #[derive(Debug)]
    pub struct Canceled;

    /// Create a sender/receiver pair.
    pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
        let (inner_tx, inner_rx) = oneshot::channel();
        (Sender { inner: inner_tx }, Receiver { inner: inner_rx })
    }

    /// Sender half for rendezvous delivery.
    pub struct Sender<T> {
        /// Inner channel used for delivery.
        inner: oneshot::Sender<(T, oneshot::Sender<()>)>,
    }

    impl<T> Sender<T> {
        /// Send a value and await acknowledgement.
        pub async fn send(self, value: T) -> StdResult<(), Canceled> {
            let (ack_tx, ack_rx) = oneshot::channel();
            self.inner.send((value, ack_tx)).map_err(|_| Canceled)?;
            ack_rx.await.map_err(|_| Canceled)?;
            Ok(())
        }

        /// Send a value and block until acknowledged.
        pub fn blocking_send(self, value: T) -> StdResult<(), Canceled> {
            futures_executor::block_on(self.send(value))
        }
    }

    /// Receiver half for rendezvous delivery.
    pub struct Receiver<T> {
        /// Inner channel used for delivery.
        inner: oneshot::Receiver<(T, oneshot::Sender<()>)>,
    }

    impl<T> Receiver<T> {
        /// Receive a value and acknowledge receipt.
        pub async fn recv(self) -> StdResult<T, Canceled> {
            let (value, ack_tx) = self.inner.await.map_err(|_| Canceled)?;
            ack_tx.send(()).map_err(|_| Canceled)?;
            Ok(value)
        }
    }
}