tenferro_runtime/runtime/
cache_owner.rs1use std::error::Error;
2use std::fmt;
3use std::sync::Arc;
4
5use super::identity::validate_identifier;
6use super::{IdentityError, IdentityKind};
7
8#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
21pub struct CacheOwnerId(Arc<str>);
22
23impl CacheOwnerId {
24 pub fn new(value: impl Into<Arc<str>>) -> Result<Self, IdentityError> {
31 validate_identifier(value.into(), IdentityKind::CacheOwner).map(Self)
32 }
33
34 pub fn as_str(&self) -> &str {
36 &self.0
37 }
38
39 pub(super) fn from_canonical_owner_id(value: Arc<str>) -> Self {
40 Self(value)
41 }
42}
43
44#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
54pub struct CacheStats {
55 pub entries: usize,
57 pub retained_bytes: usize,
59 pub hits: u64,
61 pub misses: u64,
63 pub evictions: u64,
65 pub clears: u64,
67}
68
69pub trait RuntimeCacheOwner: fmt::Debug + Send + Sync + 'static {
71 fn cache_stats(&self) -> Result<CacheStats, CacheOwnerError>;
77
78 fn clear_caches(&self) -> Result<(), CacheOwnerError>;
84}
85
86#[derive(Clone)]
88pub struct CacheOwnerError {
89 source: Arc<dyn Error + Send + Sync>,
90}
91
92impl CacheOwnerError {
93 pub fn new(source: Arc<dyn Error + Send + Sync>) -> Self {
95 Self { source }
96 }
97
98 pub fn source_arc(&self) -> &Arc<dyn Error + Send + Sync> {
100 &self.source
101 }
102}
103
104impl fmt::Debug for CacheOwnerError {
105 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
106 formatter
107 .debug_struct("CacheOwnerError")
108 .field("source", &self.source.to_string())
109 .finish()
110 }
111}
112
113impl fmt::Display for CacheOwnerError {
114 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
115 write!(formatter, "{}", self.source)
116 }
117}
118
119impl Error for CacheOwnerError {
120 fn source(&self) -> Option<&(dyn Error + 'static)> {
121 Some(self.source.as_ref())
122 }
123}
124
125#[derive(Clone, Debug)]
127pub struct CacheOwnerFailure {
128 pub owner: CacheOwnerId,
130 pub source: CacheOwnerError,
132}
133
134#[derive(Clone, Copy, Debug)]
135pub(super) enum FrozenCacheOwnerKind {
136 Engine,
137 Extension,
138}
139
140#[derive(Clone)]
141pub(super) struct FrozenCacheOwner {
142 pub(super) id: CacheOwnerId,
143 pub(super) kind: FrozenCacheOwnerKind,
144 pub(super) owner: Arc<dyn RuntimeCacheOwner>,
145}
146
147impl fmt::Debug for FrozenCacheOwner {
148 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
149 formatter
150 .debug_struct("FrozenCacheOwner")
151 .field("id", &self.id)
152 .field("kind", &self.kind)
153 .field("owner_strong_count", &Arc::strong_count(&self.owner))
154 .finish()
155 }
156}
157
158#[derive(Debug, thiserror::Error)]
160pub enum RuntimeStateError {
161 #[error("{lock} poisoned")]
163 Poisoned {
164 lock: &'static str,
166 },
167}
168
169#[derive(Debug, thiserror::Error)]
171pub enum RuntimeCacheError {
172 #[error("runtime cache operation failed")]
174 Aggregate {
175 runtime: Option<RuntimeStateError>,
177 owners: Box<[CacheOwnerFailure]>,
179 },
180}