use std::{any::type_name, sync::Arc}; use anyhow::Result; use either::Either; use smallvec::SmallVec; use turbo_tasks::{SessionId, TaskId, backend::CachedTaskType}; use crate::{ backend::{AnyOperation, TaskDataCategory}, data::CachedDataItem, utils::chunked_vec::ChunkedVec, }; /// Represents types accepted by [`TurboTasksBackend::new`]. Typically this is the value returned by /// [`default_backing_storage`] or [`noop_backing_storage`]. /// /// This trait is [sealed]. External crates are not allowed to implement it. /// /// [`default_backing_storage`]: crate::default_backing_storage /// [`noop_backing_storage`]: crate::noop_backing_storage /// [`TurboTasksBackend::new`]: crate::TurboTasksBackend::new /// [sealed]: https://predr.ag/blog/definitive-guide-to-sealed-traits-in-rust/ pub trait BackingStorage: BackingStorageSealed { /// Called when the database should be invalidated upon re-initialization. /// /// This typically means that we'll restart the process or `turbo-tasks` soon with a fresh /// database. If this happens, there's no point in writing anything else to disk, or flushing /// during [`KeyValueDatabase::shutdown`]. /// /// This can be implemented by calling [`invalidate_db`] with /// the database's non-versioned base path. /// /// [`KeyValueDatabase::shutdown`]: crate::database::key_value_database::KeyValueDatabase::shutdown /// [`invalidate_db`]: crate::database::db_invalidation::invalidate_db fn invalidate(&self, reason_code: &str) -> Result<()>; } /// Private methods used by [`BackingStorage`]. This trait is `pub` (because of the sealed-trait /// pattern), but should not be exported outside of the crate. /// /// [`BackingStorage`] is exported for documentation reasons and to expose the public /// [`BackingStorage::invalidate`] method. pub trait BackingStorageSealed: 'static + Send + Sync { type ReadTransaction<'l>; fn next_free_task_id(&self) -> Result; fn next_session_id(&self) -> Result; fn uncompleted_operations(&self) -> Result>; #[allow(clippy::ptr_arg)] fn serialize(&self, task: TaskId, data: &Vec) -> Result>; fn save_snapshot( &self, session_id: SessionId, operations: Vec>, task_cache_updates: Vec, TaskId)>>, snapshots: Vec, ) -> Result<()> where I: Iterator< Item = ( TaskId, Option>, Option>, ), > + Send + Sync; fn start_read_transaction(&self) -> Option>; /// # Safety /// /// `tx` must be a transaction from this BackingStorage instance. unsafe fn forward_lookup_task_cache( &self, tx: Option<&Self::ReadTransaction<'_>>, key: &CachedTaskType, ) -> Result>; /// # Safety /// /// `tx` must be a transaction from this BackingStorage instance. unsafe fn reverse_lookup_task_cache( &self, tx: Option<&Self::ReadTransaction<'_>>, task_id: TaskId, ) -> Result>>; /// # Safety /// /// `tx` must be a transaction from this BackingStorage instance. unsafe fn lookup_data( &self, tx: Option<&Self::ReadTransaction<'_>>, task_id: TaskId, category: TaskDataCategory, ) -> Result>; fn shutdown(&self) -> Result<()> { Ok(()) } } impl BackingStorage for Either where L: BackingStorage, R: BackingStorage, { fn invalidate(&self, reason_code: &str) -> Result<()> { either::for_both!(self, this => this.invalidate(reason_code)) } } impl BackingStorageSealed for Either where L: BackingStorageSealed, R: BackingStorageSealed, { type ReadTransaction<'l> = Either, R::ReadTransaction<'l>>; fn next_free_task_id(&self) -> Result { either::for_both!(self, this => this.next_free_task_id()) } fn next_session_id(&self) -> Result { either::for_both!(self, this => this.next_session_id()) } fn uncompleted_operations(&self) -> Result> { either::for_both!(self, this => this.uncompleted_operations()) } fn serialize(&self, task: TaskId, data: &Vec) -> Result> { either::for_both!(self, this => this.serialize(task, data)) } fn save_snapshot( &self, session_id: SessionId, operations: Vec>, task_cache_updates: Vec, TaskId)>>, snapshots: Vec, ) -> Result<()> where I: Iterator< Item = ( TaskId, Option>, Option>, ), > + Send + Sync, { either::for_both!(self, this => this.save_snapshot( session_id, operations, task_cache_updates, snapshots, )) } fn start_read_transaction(&self) -> Option> { Some(match self { Either::Left(this) => Either::Left(this.start_read_transaction()?), Either::Right(this) => Either::Right(this.start_read_transaction()?), }) } unsafe fn forward_lookup_task_cache( &self, tx: Option<&Self::ReadTransaction<'_>>, key: &CachedTaskType, ) -> Result> { match self { Either::Left(this) => { let tx = tx.map(|tx| read_transaction_left_or_panic(tx.as_ref())); unsafe { this.forward_lookup_task_cache(tx, key) } } Either::Right(this) => { let tx = tx.map(|tx| read_transaction_right_or_panic(tx.as_ref())); unsafe { this.forward_lookup_task_cache(tx, key) } } } } unsafe fn reverse_lookup_task_cache( &self, tx: Option<&Self::ReadTransaction<'_>>, task_id: TaskId, ) -> Result>> { match self { Either::Left(this) => { let tx = tx.map(|tx| read_transaction_left_or_panic(tx.as_ref())); unsafe { this.reverse_lookup_task_cache(tx, task_id) } } Either::Right(this) => { let tx = tx.map(|tx| read_transaction_right_or_panic(tx.as_ref())); unsafe { this.reverse_lookup_task_cache(tx, task_id) } } } } unsafe fn lookup_data( &self, tx: Option<&Self::ReadTransaction<'_>>, task_id: TaskId, category: TaskDataCategory, ) -> Result> { match self { Either::Left(this) => { let tx = tx.map(|tx| read_transaction_left_or_panic(tx.as_ref())); unsafe { this.lookup_data(tx, task_id, category) } } Either::Right(this) => { let tx = tx.map(|tx| read_transaction_right_or_panic(tx.as_ref())); unsafe { this.lookup_data(tx, task_id, category) } } } } } // similar to `Either::unwrap_left`, but does not require `R: Debug`. fn read_transaction_left_or_panic(either: Either) -> L { match either { Either::Left(l) => l, Either::Right(_) => panic!( "expected ReadTransaction of Either::Left containing {}, received Either::Right type \ of {}", type_name::(), type_name::(), ), } } // similar to `Either::unwrap_right`, but does not require `R: Debug`. fn read_transaction_right_or_panic(either: Either) -> R { match either { Either::Left(_) => panic!( "expected ReadTransaction of Either::Right containing {}, received Either::Left type \ of {}", type_name::(), type_name::(), ), Either::Right(r) => r, } }