Skip to content

Commit

Permalink
introduce QueryCacheHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc committed Oct 28, 2024
1 parent 5255dfb commit c56fc6b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
46 changes: 46 additions & 0 deletions crates/store/re_query/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,52 @@ impl QueryCacheKey {
}
}

/// A ref-counted, inner-mutable handle to a [`QueryCache`].
///
/// Cheap to clone.
///
/// It is possible to grab the lock behind this handle while _maintaining a static lifetime_, see:
/// * [`QueryCacheHandle::read_arc`]
/// * [`QueryCacheHandle::write_arc`]
#[derive(Clone)]
pub struct QueryCacheHandle(Arc<parking_lot::RwLock<QueryCache>>);

impl QueryCacheHandle {
#[inline]
pub fn new(cache: QueryCache) -> Self {
Self(Arc::new(parking_lot::RwLock::new(cache)))
}

#[inline]
pub fn into_inner(self) -> Arc<parking_lot::RwLock<QueryCache>> {
self.0
}
}

impl QueryCacheHandle {
#[inline]
pub fn read(&self) -> parking_lot::RwLockReadGuard<'_, QueryCache> {
self.0.read()
}

#[inline]
pub fn write(&self) -> parking_lot::RwLockWriteGuard<'_, QueryCache> {
self.0.write()
}

#[inline]
pub fn read_arc(&self) -> parking_lot::ArcRwLockReadGuard<parking_lot::RawRwLock, QueryCache> {
parking_lot::RwLock::read_arc(&self.0)
}

#[inline]
pub fn write_arc(
&self,
) -> parking_lot::ArcRwLockWriteGuard<parking_lot::RawRwLock, QueryCache> {
parking_lot::RwLock::write_arc(&self.0)
}
}

pub struct QueryCache {
/// Handle to the associated [`ChunkStoreHandle`].
pub(crate) store: ChunkStoreHandle,
Expand Down
2 changes: 1 addition & 1 deletion crates/store/re_query/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod range;
pub mod clamped_zip;
pub mod range_zip;

pub use self::cache::{QueryCache, QueryCacheKey};
pub use self::cache::{QueryCache, QueryCacheHandle, QueryCacheKey};
pub use self::cache_stats::{CacheStats, CachesStats};
pub use self::clamped_zip::*;
pub use self::latest_at::LatestAtResults;
Expand Down

0 comments on commit c56fc6b

Please sign in to comment.