use std::path::Path;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use happenstance_core::{
Authority, Checkpoint, CommitError, ProjectionId, ResetError, SendProjectionStore,
SequencePosition,
};
use rusqlite::types::Value;
use rusqlite::{Connection, OptionalExtension, TransactionBehavior, params_from_iter};
use tokio::runtime::{Handle, TryCurrentError};
use tokio::task::JoinError;
mod protection;
pub const SCHEMA_VERSION: u32 = 2;
const BASELINE_SCHEMA_VERSION: u32 = 1;
const PROTECTION_SCHEMA_VERSION: u32 = 2;
const MIGRATION_1: &str = "\
CREATE TABLE IF NOT EXISTS projection_checkpoint (
projection_id TEXT NOT NULL PRIMARY KEY,
position INTEGER NOT NULL,
authority TEXT NOT NULL
) WITHOUT ROWID;
CREATE TABLE IF NOT EXISTS projection_meta (
k TEXT NOT NULL PRIMARY KEY,
v INTEGER NOT NULL
) WITHOUT ROWID;";
#[cfg(feature = "conformance")]
const PROBE_TABLE: &str = "\
CREATE TABLE IF NOT EXISTS projection_probe (
k TEXT NOT NULL PRIMARY KEY,
v INTEGER NOT NULL
) WITHOUT ROWID;";
const SCHEMA_VERSION_KEY: &str = "schema_version";
const AUTHORITY_LIVE: &str = "live";
const AUTHORITY_REBUILDING: &str = "rebuilding";
fn mint_stamp() -> u64 {
static NEXT: AtomicU64 = AtomicU64::new(1);
NEXT.fetch_add(1, Ordering::Relaxed)
}
#[derive(Debug, Clone)]
pub struct SqliteProjectionStore {
connection: Arc<Mutex<Connection>>,
stamp: u64,
runtime: Option<Handle>,
}
impl SqliteProjectionStore {
#[must_use]
pub fn new(connection: Connection) -> Self {
Self {
connection: Arc::new(Mutex::new(connection)),
stamp: mint_stamp(),
runtime: Handle::try_current().ok(),
}
}
pub fn open(path: impl AsRef<Path>) -> Result<Self, SqliteProjectionStoreError> {
let mut connection = crate::connection::open_configured(path)?;
Self::migrate(&mut connection)?;
Ok(Self::new(connection))
}
pub fn open_in_memory() -> Result<Self, SqliteProjectionStoreError> {
let mut connection = Connection::open_in_memory()?;
crate::connection::configure(&connection)?;
Self::migrate(&mut connection)?;
Ok(Self::new(connection))
}
fn migrate(connection: &mut Connection) -> Result<(), SqliteProjectionStoreError> {
let transaction = connection.transaction_with_behavior(TransactionBehavior::Immediate)?;
transaction.execute_batch(MIGRATION_1)?;
transaction.execute_batch(protection::PROTECTION_TABLE)?;
#[cfg(feature = "conformance")]
transaction.execute_batch(PROBE_TABLE)?;
transaction.execute(
"INSERT OR IGNORE INTO projection_meta (k, v) VALUES (?, ?)",
rusqlite::params![SCHEMA_VERSION_KEY, BASELINE_SCHEMA_VERSION],
)?;
let found: u32 = transaction.query_row(
"SELECT v FROM projection_meta WHERE k = ?",
[SCHEMA_VERSION_KEY],
|row| row.get(0),
)?;
if found > SCHEMA_VERSION {
return Err(SqliteProjectionStoreError::UnsupportedSchemaVersion {
found,
supported: SCHEMA_VERSION,
});
}
transaction.commit()?;
Ok(())
}
fn handle(&self) -> Arc<Mutex<Connection>> {
Arc::clone(&self.connection)
}
fn runtime(&self) -> Result<Handle, SqliteProjectionStoreError> {
match Handle::try_current() {
Ok(current) => Ok(current),
Err(none) => self.runtime.clone().ok_or_else(|| none.into()),
}
}
async fn in_blocking_task<T, F>(&self, work: F) -> Result<T, SqliteProjectionStoreError>
where
F: FnOnce(&mut Connection) -> Result<T, SqliteProjectionStoreError> + Send + 'static,
T: Send + 'static,
{
let connection = self.handle();
let runtime = self.runtime()?;
let joined = runtime
.spawn_blocking(move || {
let mut guard = connection
.lock()
.map_err(|_| SqliteProjectionStoreError::ConnectionPoisoned)?;
work(&mut guard)
})
.await;
match joined {
Ok(outcome) => outcome,
Err(join) => Err(SqliteProjectionStoreError::from(join)),
}
}
}
#[derive(Debug, Clone)]
pub struct PendingStatement {
sql: Box<str>,
params: Box<[Value]>,
}
impl PendingStatement {
pub fn sql(&self) -> &str {
&self.sql
}
pub fn params(&self) -> &[Value] {
&self.params
}
}
#[derive(Debug, Clone)]
pub struct SqliteBatch {
statements: Vec<PendingStatement>,
stamp: u64,
}
impl SqliteBatch {
fn stamped(stamp: u64) -> Self {
Self {
statements: Vec::new(),
stamp,
}
}
pub fn push(&mut self, sql: &'static str, params: impl IntoIterator<Item = Value>) {
self.push_raw_sql(sql, params);
}
pub fn push_raw_sql(
&mut self,
sql: impl Into<String>,
params: impl IntoIterator<Item = Value>,
) {
self.statements.push(PendingStatement {
sql: sql.into().into_boxed_str(),
params: params.into_iter().collect(),
});
}
pub fn statements(&self) -> &[PendingStatement] {
&self.statements
}
pub fn len(&self) -> usize {
self.statements.len()
}
pub fn is_empty(&self) -> bool {
self.statements.is_empty()
}
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum SqliteProjectionStoreError {
#[error("SQLite failed: {0}")]
Sqlite(#[from] rusqlite::Error),
#[error("the SQLite connection mutex was poisoned by a panicking thread")]
ConnectionPoisoned,
#[error("the blocking SQLite task did not complete: {0}")]
Worker(#[from] JoinError),
#[error("no tokio runtime is available to run the blocking SQLite work: {0}")]
NoRuntime(#[from] TryCurrentError),
#[error("stored checkpoint position {0} is not a valid sequence position")]
InvalidPosition(i64),
#[error("stored checkpoint authority {0:?} is not one this build recognises")]
InvalidAuthority(String),
#[error(
"this build understands projection schema version {supported}, and the \
database is at version {found}"
)]
UnsupportedSchemaVersion {
found: u32,
supported: u32,
},
#[error("the batch was begun on a different store instance")]
ForeignBatch,
}
#[derive(Debug)]
enum ResetOutcome {
Reset,
Refused,
}
#[derive(Debug)]
enum CommitOutcome {
Committed,
Regressed {
current: SequencePosition,
attempted: SequencePosition,
},
}
impl SendProjectionStore for SqliteProjectionStore {
type Error = SqliteProjectionStoreError;
type Batch = SqliteBatch;
async fn begin(&self) -> Result<Self::Batch, Self::Error> {
Ok(SqliteBatch::stamped(self.stamp))
}
async fn checkpoint(&self, id: &ProjectionId) -> Result<Checkpoint, Self::Error> {
let id = id.clone();
self.in_blocking_task(move |connection| read_checkpoint(connection, &id))
.await
}
async fn commit(
&self,
batch: Self::Batch,
id: &ProjectionId,
position: SequencePosition,
authority: Authority,
) -> Result<(), CommitError<Self::Error>> {
if batch.stamp != self.stamp {
return Err(CommitError::ForeignBatch);
}
let id = id.clone();
let outcome = self
.in_blocking_task(move |connection| {
commit_locked(connection, &batch, &id, position, authority)
})
.await
.map_err(CommitError::Store)?;
match outcome {
CommitOutcome::Committed => Ok(()),
CommitOutcome::Regressed { current, attempted } => {
Err(CommitError::CheckpointRegression { current, attempted })
}
}
}
async fn reset(
&self,
batch: Self::Batch,
id: &ProjectionId,
) -> Result<(), ResetError<Self::Error>> {
if batch.stamp != self.stamp {
return Err(ResetError::ForeignBatch);
}
let id = id.clone();
let outcome = self
.in_blocking_task(move |connection| reset_locked(connection, &batch, &id))
.await
.map_err(ResetError::Store)?;
match outcome {
ResetOutcome::Reset => Ok(()),
ResetOutcome::Refused => Err(ResetError::Refused),
}
}
async fn rollback(&self, batch: Self::Batch) -> Result<(), Self::Error> {
if batch.stamp != self.stamp {
return Err(SqliteProjectionStoreError::ForeignBatch);
}
drop(batch);
Ok(())
}
}
#[cfg(feature = "conformance")]
impl happenstance_core::ProjectionProbe for SqliteProjectionStore {
const READS_THROUGH_BATCH: bool = false;
async fn probe_write(
&self,
batch: &mut Self::Batch,
key: &str,
value: u64,
) -> Result<(), Self::Error> {
batch.push(
"INSERT INTO projection_probe (k, v) VALUES (?, ?) \
ON CONFLICT(k) DO UPDATE SET v = excluded.v",
[
Value::Text(key.to_owned()),
Value::Integer(value.cast_signed()),
],
);
Ok(())
}
async fn probe_delete_all(&self, batch: &mut Self::Batch) -> Result<(), Self::Error> {
batch.push("DELETE FROM projection_probe", core::iter::empty());
Ok(())
}
async fn probe_read(&self, key: &str) -> Result<Option<u64>, Self::Error> {
let key = key.to_owned();
self.in_blocking_task(move |connection| {
let stored: Option<i64> = connection
.query_row(
"SELECT v FROM projection_probe WHERE k = ?",
[&key],
|row| row.get(0),
)
.optional()?;
Ok(stored.map(i64::cast_unsigned))
})
.await
}
async fn probe_read_through(
&self,
_batch: &mut Self::Batch,
_key: &str,
) -> Result<Option<u64>, Self::Error> {
unimplemented!(
"SqliteBatch buffers its statements, so `READS_THROUGH_BATCH` is \
`false` and this is never called: there is no open transaction to \
read through, and answering from committed state is what PS-12 \
forbids"
)
}
}
fn read_checkpoint(
connection: &Connection,
id: &ProjectionId,
) -> Result<Checkpoint, SqliteProjectionStoreError> {
let row: Option<(i64, String)> = connection
.query_row(
"SELECT position, authority FROM projection_checkpoint WHERE projection_id = ?",
[id.as_str()],
|row| Ok((row.get(0)?, row.get(1)?)),
)
.optional()?;
let Some((stored_position, stored_authority)) = row else {
return Ok(Checkpoint::NeverRun);
};
let through = position_from_row(stored_position)?;
match stored_authority.as_str() {
AUTHORITY_LIVE => Ok(Checkpoint::Live { through }),
AUTHORITY_REBUILDING => Ok(Checkpoint::Rebuilding { through }),
_ => Err(SqliteProjectionStoreError::InvalidAuthority(
stored_authority,
)),
}
}
fn read_position(
connection: &Connection,
id: &ProjectionId,
) -> Result<Option<SequencePosition>, SqliteProjectionStoreError> {
let stored: Option<i64> = connection
.query_row(
"SELECT position FROM projection_checkpoint WHERE projection_id = ?",
[id.as_str()],
|row| row.get(0),
)
.optional()?;
stored.map(position_from_row).transpose()
}
fn commit_locked(
connection: &mut Connection,
batch: &SqliteBatch,
id: &ProjectionId,
position: SequencePosition,
authority: Authority,
) -> Result<CommitOutcome, SqliteProjectionStoreError> {
let transaction = connection.transaction_with_behavior(TransactionBehavior::Immediate)?;
if let Some(current) = read_position(&transaction, id)?
&& position < current
{
return Ok(CommitOutcome::Regressed {
current,
attempted: position,
});
}
apply(&transaction, batch)?;
transaction.execute(
"INSERT INTO projection_checkpoint (projection_id, position, authority) VALUES (?, ?, ?) \
ON CONFLICT(projection_id) DO UPDATE SET \
position = excluded.position, authority = excluded.authority",
rusqlite::params![id.as_str(), as_i64(position), stored_authority(authority)],
)?;
transaction.commit()?;
Ok(CommitOutcome::Committed)
}
fn reset_locked(
connection: &mut Connection,
batch: &SqliteBatch,
id: &ProjectionId,
) -> Result<ResetOutcome, SqliteProjectionStoreError> {
let transaction = connection.transaction_with_behavior(TransactionBehavior::Immediate)?;
if protection::is_protected(&transaction, id)? {
transaction.rollback()?;
return Ok(ResetOutcome::Refused);
}
apply(&transaction, batch)?;
transaction.execute(
"DELETE FROM projection_checkpoint WHERE projection_id = ?",
[id.as_str()],
)?;
transaction.commit()?;
Ok(ResetOutcome::Reset)
}
fn apply(
transaction: &rusqlite::Transaction<'_>,
batch: &SqliteBatch,
) -> Result<(), SqliteProjectionStoreError> {
for statement in batch.statements() {
transaction.execute(statement.sql(), params_from_iter(statement.params()))?;
}
Ok(())
}
fn as_i64(position: SequencePosition) -> i64 {
i64::try_from(position.get()).unwrap_or(i64::MAX)
}
fn position_from_row(stored: i64) -> Result<SequencePosition, SqliteProjectionStoreError> {
u64::try_from(stored)
.ok()
.and_then(SequencePosition::new)
.ok_or(SqliteProjectionStoreError::InvalidPosition(stored))
}
fn stored_authority(authority: Authority) -> &'static str {
match authority {
Authority::Live => AUTHORITY_LIVE,
_ => AUTHORITY_REBUILDING,
}
}