| | 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, |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | pub trait BackingStorage: BackingStorageSealed { |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | fn invalidate(&self, reason_code: &str) -> Result<()>; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | pub trait BackingStorageSealed: 'static + Send + Sync { |
| | type ReadTransaction<'l>; |
| | fn next_free_task_id(&self) -> Result<TaskId>; |
| | fn next_session_id(&self) -> Result<SessionId>; |
| | fn uncompleted_operations(&self) -> Result<Vec<AnyOperation>>; |
| | #[allow(clippy::ptr_arg)] |
| | fn serialize(&self, task: TaskId, data: &Vec<CachedDataItem>) -> Result<SmallVec<[u8; 16]>>; |
| | fn save_snapshot<I>( |
| | &self, |
| | session_id: SessionId, |
| | operations: Vec<Arc<AnyOperation>>, |
| | task_cache_updates: Vec<ChunkedVec<(Arc<CachedTaskType>, TaskId)>>, |
| | snapshots: Vec<I>, |
| | ) -> Result<()> |
| | where |
| | I: Iterator< |
| | Item = ( |
| | TaskId, |
| | Option<SmallVec<[u8; 16]>>, |
| | Option<SmallVec<[u8; 16]>>, |
| | ), |
| | > + Send |
| | + Sync; |
| | fn start_read_transaction(&self) -> Option<Self::ReadTransaction<'_>>; |
| | |
| | |
| | |
| | unsafe fn forward_lookup_task_cache( |
| | &self, |
| | tx: Option<&Self::ReadTransaction<'_>>, |
| | key: &CachedTaskType, |
| | ) -> Result<Option<TaskId>>; |
| | |
| | |
| | |
| | unsafe fn reverse_lookup_task_cache( |
| | &self, |
| | tx: Option<&Self::ReadTransaction<'_>>, |
| | task_id: TaskId, |
| | ) -> Result<Option<Arc<CachedTaskType>>>; |
| | |
| | |
| | |
| | unsafe fn lookup_data( |
| | &self, |
| | tx: Option<&Self::ReadTransaction<'_>>, |
| | task_id: TaskId, |
| | category: TaskDataCategory, |
| | ) -> Result<Vec<CachedDataItem>>; |
| |
|
| | fn shutdown(&self) -> Result<()> { |
| | Ok(()) |
| | } |
| | } |
| |
|
| | impl<L, R> BackingStorage for Either<L, R> |
| | where |
| | L: BackingStorage, |
| | R: BackingStorage, |
| | { |
| | fn invalidate(&self, reason_code: &str) -> Result<()> { |
| | either::for_both!(self, this => this.invalidate(reason_code)) |
| | } |
| | } |
| |
|
| | impl<L, R> BackingStorageSealed for Either<L, R> |
| | where |
| | L: BackingStorageSealed, |
| | R: BackingStorageSealed, |
| | { |
| | type ReadTransaction<'l> = Either<L::ReadTransaction<'l>, R::ReadTransaction<'l>>; |
| |
|
| | fn next_free_task_id(&self) -> Result<TaskId> { |
| | either::for_both!(self, this => this.next_free_task_id()) |
| | } |
| |
|
| | fn next_session_id(&self) -> Result<SessionId> { |
| | either::for_both!(self, this => this.next_session_id()) |
| | } |
| |
|
| | fn uncompleted_operations(&self) -> Result<Vec<AnyOperation>> { |
| | either::for_both!(self, this => this.uncompleted_operations()) |
| | } |
| |
|
| | fn serialize(&self, task: TaskId, data: &Vec<CachedDataItem>) -> Result<SmallVec<[u8; 16]>> { |
| | either::for_both!(self, this => this.serialize(task, data)) |
| | } |
| |
|
| | fn save_snapshot<I>( |
| | &self, |
| | session_id: SessionId, |
| | operations: Vec<Arc<AnyOperation>>, |
| | task_cache_updates: Vec<ChunkedVec<(Arc<CachedTaskType>, TaskId)>>, |
| | snapshots: Vec<I>, |
| | ) -> Result<()> |
| | where |
| | I: Iterator< |
| | Item = ( |
| | TaskId, |
| | Option<SmallVec<[u8; 16]>>, |
| | Option<SmallVec<[u8; 16]>>, |
| | ), |
| | > + Send |
| | + Sync, |
| | { |
| | either::for_both!(self, this => this.save_snapshot( |
| | session_id, |
| | operations, |
| | task_cache_updates, |
| | snapshots, |
| | )) |
| | } |
| |
|
| | fn start_read_transaction(&self) -> Option<Self::ReadTransaction<'_>> { |
| | 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<Option<TaskId>> { |
| | 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<Option<Arc<CachedTaskType>>> { |
| | 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<Vec<CachedDataItem>> { |
| | 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) } |
| | } |
| | } |
| | } |
| | } |
| |
|
| | |
| | fn read_transaction_left_or_panic<L, R>(either: Either<L, R>) -> L { |
| | match either { |
| | Either::Left(l) => l, |
| | Either::Right(_) => panic!( |
| | "expected ReadTransaction of Either::Left containing {}, received Either::Right type \ |
| | of {}", |
| | type_name::<L>(), |
| | type_name::<R>(), |
| | ), |
| | } |
| | } |
| |
|
| | |
| | fn read_transaction_right_or_panic<L, R>(either: Either<L, R>) -> R { |
| | match either { |
| | Either::Left(_) => panic!( |
| | "expected ReadTransaction of Either::Right containing {}, received Either::Left type \ |
| | of {}", |
| | type_name::<R>(), |
| | type_name::<L>(), |
| | ), |
| | Either::Right(r) => r, |
| | } |
| | } |
| |
|