#![doc(
html_logo_url = "https://raw.githubusercontent.com/ChaoticQubit/troupekit/main/assets/brand/logo-mark.svg",
html_favicon_url = "https://raw.githubusercontent.com/ChaoticQubit/troupekit/main/assets/brand/favicon.svg"
)]
mod queue;
mod schema;
mod stores;
mod vectors;
use rusqlite::config::DbConfig;
use rusqlite::{Connection, OpenFlags};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use troupekit_core::clock::{Clock, SystemClock};
use troupekit_core::error::CoreError;
#[cfg(any(feature = "sqlcipher", feature = "sqlcipher-system"))]
use troupekit_core::secret::Secret;
pub use troupekit_index::{HnswParams, IndexKind, Quantization};
pub use vectors::{OpenReport, SqliteVectors, VectorSettings};
pub const SCHEMA_VERSION: u32 = schema::STEPS.len() as u32;
const _: () = assert!(SCHEMA_VERSION > 0);
const BYTES_LIMIT_SQLITE: u64 = 1_000_000_000;
const STATE_BYTES_MAX_DEFAULT: u64 = 64 * 1024 * 1024;
const BLOB_BYTES_MAX_DEFAULT: u64 = 64 * 1024 * 1024;
const HISTORY_STEPS_MAX_DEFAULT: u64 = 100_000;
const HISTORY_BYTES_MAX_DEFAULT: u64 = 1024 * 1024 * 1024;
const BUSY_TIMEOUT_MS_DEFAULT: u32 = 5_000;
const OPEN_RETRY_PAUSE: Duration = Duration::from_millis(20);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum Durability {
#[default]
Full,
Normal,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Limits {
pub(crate) state_bytes_max: u64,
pub(crate) blob_bytes_max: u64,
pub(crate) history_steps_max: u64,
pub(crate) history_bytes_max: u64,
}
#[derive(Clone)]
pub struct SqliteStoreBuilder {
path: PathBuf,
durability: Durability,
clock: Arc<dyn Clock>,
limits: Limits,
busy_timeout_ms: u32,
secure_delete: bool,
#[cfg(any(feature = "sqlcipher", feature = "sqlcipher-system"))]
key: Option<Secret>,
}
impl std::fmt::Debug for SqliteStoreBuilder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SqliteStoreBuilder")
.field("path", &self.path)
.field("durability", &self.durability)
.field("limits", &self.limits)
.field("busy_timeout_ms", &self.busy_timeout_ms)
.field("secure_delete", &self.secure_delete)
.finish_non_exhaustive()
}
}
impl SqliteStoreBuilder {
pub fn durability(mut self, durability: Durability) -> Self {
self.durability = durability;
self
}
pub fn clock(mut self, clock: Arc<dyn Clock>) -> Self {
self.clock = clock;
self
}
pub fn max_state_bytes(mut self, bytes: u64) -> Self {
self.limits.state_bytes_max = bytes;
self
}
pub fn max_blob_bytes(mut self, bytes: u64) -> Self {
self.limits.blob_bytes_max = bytes;
self
}
pub fn max_history_steps(mut self, steps: u64) -> Self {
self.limits.history_steps_max = steps;
self
}
pub fn max_history_bytes(mut self, bytes: u64) -> Self {
self.limits.history_bytes_max = bytes;
self
}
pub fn busy_timeout_ms(mut self, ms: u32) -> Self {
self.busy_timeout_ms = ms;
self
}
pub fn secure_delete(mut self, on: bool) -> Self {
self.secure_delete = on;
self
}
#[cfg(any(feature = "sqlcipher", feature = "sqlcipher-system"))]
pub fn key(mut self, key: Secret) -> Self {
self.key = Some(key);
self
}
pub async fn open(self) -> Result<SqliteStore, CoreError> {
validate(&self.limits, self.busy_timeout_ms)?;
let settings = self.clone();
let conn = tokio::task::spawn_blocking(move || open_retrying(&settings))
.await
.unwrap_or_else(join_failed)?;
Ok(SqliteStore {
inner: Arc::new(Inner {
conn: Mutex::new(conn),
clock: self.clock,
limits: self.limits,
path: self.path,
secure_delete: self.secure_delete,
vectors: Mutex::new(HashMap::new()),
}),
})
}
}
fn validate(limits: &Limits, busy_timeout_ms: u32) -> Result<(), CoreError> {
if i32::try_from(busy_timeout_ms).is_err() {
return Err(CoreError::Store(format!(
"busy_timeout_ms must be 0 to {}, got {busy_timeout_ms}",
i32::MAX
)));
}
for (name, value) in [
("max_state_bytes", limits.state_bytes_max),
("max_blob_bytes", limits.blob_bytes_max),
] {
if value == 0 || value > BYTES_LIMIT_SQLITE {
return Err(CoreError::Store(format!(
"{name} must be 1 to {BYTES_LIMIT_SQLITE}, got {value}"
)));
}
}
if limits.history_steps_max == 0 {
return Err(CoreError::Store(
"max_history_steps must be at least 1".into(),
));
}
if limits.history_bytes_max == 0 {
return Err(CoreError::Store(
"max_history_bytes must be at least 1".into(),
));
}
Ok(())
}
fn create_private(path: &Path) -> Result<(), CoreError> {
if path.exists() {
return Ok(());
}
let mut options = std::fs::OpenOptions::new();
options.write(true).create_new(true);
#[cfg(unix)]
std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600);
options.open(path).map(drop).or_else(|e| match e.kind() {
std::io::ErrorKind::AlreadyExists => Ok(()),
_ => Err(CoreError::Store(format!("cannot create store: {e}"))),
})
}
fn open_retrying(settings: &SqliteStoreBuilder) -> Result<Connection, CoreError> {
let deadline = Instant::now() + Duration::from_millis(u64::from(settings.busy_timeout_ms));
let mut attempts: u32 = 0;
loop {
attempts += 1;
match open_connection(settings) {
Err(e) if is_busy(&e) && Instant::now() < deadline => {
assert!(attempts < 1_000_000, "the deadline bounds the retries");
std::thread::sleep(OPEN_RETRY_PAUSE);
}
other => return other,
}
}
}
fn is_busy(e: &CoreError) -> bool {
matches!(e, CoreError::Store(m) if m.ends_with("database is locked") || m.ends_with("database is busy"))
}
fn open_connection(settings: &SqliteStoreBuilder) -> Result<Connection, CoreError> {
create_private(&settings.path)?;
let flags = OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_NO_MUTEX;
let mut conn = Connection::open_with_flags(&settings.path, flags)
.map_err(|e| CoreError::Store(format!("cannot open store: {e}")))?;
conn.busy_timeout(Duration::from_millis(u64::from(settings.busy_timeout_ms)))
.map_err(db)?;
#[cfg(any(feature = "sqlcipher", feature = "sqlcipher-system"))]
if let Some(key) = &settings.key {
conn.pragma_update(None, "key", key.expose())
.map_err(|_| wrong_key())?;
}
conn.query_row("SELECT COUNT(*) FROM sqlite_schema", [], |row| {
row.get::<_, i64>(0)
})
.map_err(|e| match e.sqlite_error_code() {
Some(rusqlite::ErrorCode::NotADatabase) => wrong_key(),
_ => db(e),
})?;
{
let preflight = conn.transaction().map_err(db)?;
schema::check_header(schema::read_header(&preflight)?, SCHEMA_VERSION)?;
preflight.commit().map_err(db)?;
}
let mode: String = conn
.pragma_update_and_check(None, "journal_mode", "WAL", |row| row.get(0))
.map_err(db)?;
if !mode.eq_ignore_ascii_case("wal") {
return Err(CoreError::Store(format!(
"store cannot use WAL, got {mode}"
)));
}
let (synchronous, fullfsync) = match settings.durability {
Durability::Full => ("FULL", true),
Durability::Normal => ("NORMAL", false),
};
let checkpoint_fullfsync = true;
conn.pragma_update(None, "synchronous", synchronous)
.map_err(db)?;
conn.pragma_update(None, "fullfsync", fullfsync)
.map_err(db)?;
conn.pragma_update(None, "checkpoint_fullfsync", checkpoint_fullfsync)
.map_err(db)?;
conn.pragma_update(None, "trusted_schema", "OFF")
.map_err(db)?;
conn.pragma_update(None, "secure_delete", settings.secure_delete)
.map_err(db)?;
conn.set_db_config(DbConfig::SQLITE_DBCONFIG_DEFENSIVE, true)
.map_err(db)?;
let version = schema::migrate(&mut conn, schema::STEPS)?;
assert_eq!(version, SCHEMA_VERSION);
assert_eq!(schema::read_header(&conn)?.user_version, SCHEMA_VERSION);
Ok(conn)
}
fn wrong_key() -> CoreError {
CoreError::Store("cannot open store: wrong key or not a database".into())
}
pub(crate) fn db(e: rusqlite::Error) -> CoreError {
CoreError::Store(format!("sqlite: {e}"))
}
fn join_failed<T>(e: tokio::task::JoinError) -> Result<T, CoreError> {
if e.is_panic() {
std::panic::resume_unwind(e.into_panic());
}
Err(CoreError::Store(format!("store task did not finish: {e}")))
}
struct Inner {
conn: Mutex<Connection>,
clock: Arc<dyn Clock>,
limits: Limits,
path: PathBuf,
secure_delete: bool,
vectors: Mutex<HashMap<String, Arc<vectors::IndexState>>>,
}
#[derive(Clone)]
pub struct SqliteStore {
inner: Arc<Inner>,
}
impl std::fmt::Debug for SqliteStore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SqliteStore")
.field("path", &self.inner.path)
.field("limits", &self.inner.limits)
.finish_non_exhaustive()
}
}
impl SqliteStore {
pub fn builder(path: impl AsRef<Path>) -> SqliteStoreBuilder {
SqliteStoreBuilder {
path: path.as_ref().to_path_buf(),
durability: Durability::Full,
clock: Arc::new(SystemClock),
limits: Limits {
state_bytes_max: STATE_BYTES_MAX_DEFAULT,
blob_bytes_max: BLOB_BYTES_MAX_DEFAULT,
history_steps_max: HISTORY_STEPS_MAX_DEFAULT,
history_bytes_max: HISTORY_BYTES_MAX_DEFAULT,
},
busy_timeout_ms: BUSY_TIMEOUT_MS_DEFAULT,
secure_delete: true,
#[cfg(any(feature = "sqlcipher", feature = "sqlcipher-system"))]
key: None,
}
}
pub async fn open(path: impl AsRef<Path>) -> Result<SqliteStore, CoreError> {
Self::builder(path).open().await
}
pub(crate) fn limits(&self) -> Limits {
self.inner.limits
}
pub(crate) async fn call<T, F>(&self, f: F) -> Result<T, CoreError>
where
T: Send + 'static,
F: FnOnce(&mut Connection) -> Result<T, CoreError> + Send + 'static,
{
let inner = Arc::clone(&self.inner);
let scope = troupekit_trace::current();
tokio::task::spawn_blocking(move || match scope {
Some(scope) => scope.enter(|| run_locked(&inner, f)),
None => run_locked(&inner, f),
})
.await
.unwrap_or_else(|e| join_failed(e))
}
}
fn run_locked<T, F>(inner: &Inner, f: F) -> Result<T, CoreError>
where
F: FnOnce(&mut Connection) -> Result<T, CoreError>,
{
let mut conn = match inner.conn.lock() {
Ok(conn) => conn,
Err(poisoned) => {
let conn = poisoned.into_inner();
if !conn.is_autocommit() {
conn.execute_batch("ROLLBACK").map_err(db)?;
}
inner.conn.clear_poison();
conn
}
};
assert!(
conn.is_autocommit(),
"a store call starts outside a transaction"
);
let out = f(&mut conn);
assert!(conn.is_autocommit(), "a store call left a transaction open");
out
}
#[cfg(test)]
mod tests {
use super::*;
use rusqlite::TransactionBehavior;
async fn temp_store(
builder: impl FnOnce(SqliteStoreBuilder) -> SqliteStoreBuilder,
) -> (tempfile::TempDir, SqliteStore) {
let dir = tempfile::tempdir().unwrap();
let store = builder(SqliteStore::builder(dir.path().join("t.db")))
.open()
.await
.unwrap();
(dir, store)
}
async fn pragma(store: &SqliteStore, name: &'static str) -> i64 {
store
.call(move |conn| {
conn.pragma_query_value(None, name, |row| row.get(0))
.map_err(db)
})
.await
.unwrap()
}
#[tokio::test]
async fn defaults_are_the_safe_values() {
let builder = SqliteStore::builder("unused.db");
assert_eq!(builder.durability, Durability::Full);
assert_eq!(builder.limits.state_bytes_max, 64 * 1024 * 1024);
assert_eq!(builder.limits.blob_bytes_max, 64 * 1024 * 1024);
assert_eq!(builder.limits.history_steps_max, 100_000);
assert_eq!(builder.limits.history_bytes_max, 1024 * 1024 * 1024);
assert_eq!(builder.busy_timeout_ms, 5_000);
assert!(builder.secure_delete);
let before = SystemClock.now_ms();
assert!(builder.clock.now_ms() >= before);
let (_dir, store) = temp_store(|b| b).await;
assert_eq!(pragma(&store, "synchronous").await, 2);
assert_eq!(pragma(&store, "fullfsync").await, 1);
assert_eq!(pragma(&store, "checkpoint_fullfsync").await, 1);
assert_eq!(pragma(&store, "busy_timeout").await, 5_000);
assert_eq!(pragma(&store, "trusted_schema").await, 0);
assert_eq!(pragma(&store, "secure_delete").await, 1);
}
#[tokio::test]
async fn secure_delete_off_reaches_the_connection() {
let (_dir, store) = temp_store(|b| b.secure_delete(false)).await;
assert_eq!(pragma(&store, "secure_delete").await, 0);
}
#[tokio::test]
async fn durability_normal_sets_synchronous_normal() {
let (_dir, store) = temp_store(|b| b.durability(Durability::Normal)).await;
assert_eq!(pragma(&store, "synchronous").await, 1);
assert_eq!(pragma(&store, "fullfsync").await, 0);
assert_eq!(pragma(&store, "checkpoint_fullfsync").await, 1);
}
#[tokio::test]
async fn busy_timeout_setting_reaches_the_connection() {
let (_dir, store) = temp_store(|b| b.busy_timeout_ms(1234)).await;
assert_eq!(pragma(&store, "busy_timeout").await, 1234);
}
#[tokio::test]
async fn zero_cap_is_refused() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("t.db");
for builder in [
SqliteStore::builder(&path).max_state_bytes(0),
SqliteStore::builder(&path).max_blob_bytes(0),
SqliteStore::builder(&path).max_history_steps(0),
SqliteStore::builder(&path).max_history_bytes(0),
] {
let err = builder.open().await.unwrap_err();
assert!(matches!(err, CoreError::Store(_)), "{err:?}");
}
assert!(!path.exists());
}
#[tokio::test]
async fn busy_timeout_above_i32_is_refused_not_a_panic() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("t.db");
let err = SqliteStore::builder(&path)
.busy_timeout_ms(u32::MAX)
.open()
.await
.unwrap_err();
assert!(
matches!(&err, CoreError::Store(m) if m.contains("busy_timeout_ms")),
"{err:?}"
);
assert!(!path.exists());
let max = u32::try_from(i32::MAX).unwrap();
let (_dir, store) = temp_store(|b| b.busy_timeout_ms(max)).await;
assert_eq!(pragma(&store, "busy_timeout").await, i64::from(i32::MAX));
}
#[tokio::test]
async fn cap_above_sqlite_limit_is_refused() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("t.db");
for builder in [
SqliteStore::builder(&path).max_state_bytes(1_000_000_001),
SqliteStore::builder(&path).max_blob_bytes(1_000_000_001),
] {
let err = builder.open().await.unwrap_err();
assert!(
matches!(&err, CoreError::Store(m) if m.contains("1000000000")),
"{err:?}"
);
}
assert!(SqliteStore::builder(&path)
.max_blob_bytes(1_000_000_000)
.open()
.await
.is_ok());
}
#[tokio::test]
async fn panic_in_a_store_call_reaches_the_caller() {
let (_dir, store) = temp_store(|b| b).await;
let task = tokio::spawn(async move {
store
.call(|_conn| -> Result<(), CoreError> { panic!("boom in call") })
.await
});
let err = task.await.unwrap_err();
assert!(err.is_panic());
let payload = err.into_panic();
assert_eq!(payload.downcast_ref::<&str>(), Some(&"boom in call"));
}
#[tokio::test]
async fn panic_inside_a_transaction_rolls_it_back() {
let (_dir, store) = temp_store(|b| b).await;
let clone = store.clone();
let task = tokio::spawn(async move {
clone
.call(|conn| -> Result<(), CoreError> {
let tx = conn
.transaction_with_behavior(TransactionBehavior::Immediate)
.unwrap();
tx.execute(
"INSERT INTO blob_entry (path, bytes) VALUES ('p', x'01')",
[],
)
.unwrap();
panic!("boom in transaction")
})
.await
});
assert!(task.await.unwrap_err().is_panic());
let rows: i64 = store
.call(|conn| {
conn.query_row("SELECT COUNT(*) FROM blob_entry", [], |r| r.get(0))
.map_err(db)
})
.await
.unwrap();
assert_eq!(rows, 0);
}
#[tokio::test]
async fn store_stays_usable_after_a_panicked_call() {
let (_dir, store) = temp_store(|b| b).await;
let clone = store.clone();
let task = tokio::spawn(async move {
clone
.call(|_conn| -> Result<(), CoreError> { panic!("boom") })
.await
});
assert!(task.await.unwrap_err().is_panic());
assert!(store.inner.conn.is_poisoned());
let written = store
.call(|conn| {
conn.execute(
"INSERT INTO blob_entry (path, bytes) VALUES ('q', x'02')",
[],
)
.map_err(db)
})
.await
.unwrap();
assert_eq!(written, 1);
assert_eq!(
pragma(&store, "user_version").await,
i64::from(SCHEMA_VERSION)
);
}
}