matrix_sdk_base/event_cache/store/
traits.rs1use std::{fmt, sync::Arc};
16
17use async_trait::async_trait;
18use matrix_sdk_common::{
19 linked_chunk::{RawChunk, Update},
20 AsyncTraitDeps,
21};
22use ruma::{MxcUri, RoomId};
23
24use super::{
25 media::{IgnoreMediaRetentionPolicy, MediaRetentionPolicy},
26 EventCacheStoreError,
27};
28use crate::{
29 event_cache::{Event, Gap},
30 media::MediaRequestParameters,
31};
32
33pub const DEFAULT_CHUNK_CAPACITY: usize = 128;
37
38#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
41#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
42pub trait EventCacheStore: AsyncTraitDeps {
43 type Error: fmt::Debug + Into<EventCacheStoreError>;
45
46 async fn try_take_leased_lock(
48 &self,
49 lease_duration_ms: u32,
50 key: &str,
51 holder: &str,
52 ) -> Result<bool, Self::Error>;
53
54 async fn handle_linked_chunk_updates(
58 &self,
59 room_id: &RoomId,
60 updates: Vec<Update<Event, Gap>>,
61 ) -> Result<(), Self::Error>;
62
63 async fn remove_room(&self, room_id: &RoomId) -> Result<(), Self::Error> {
65 self.handle_linked_chunk_updates(room_id, vec![Update::Clear]).await
68 }
69
70 async fn reload_linked_chunk(
73 &self,
74 room_id: &RoomId,
75 ) -> Result<Vec<RawChunk<Event, Gap>>, Self::Error>;
76
77 async fn clear_all_rooms_chunks(&self) -> Result<(), Self::Error>;
82
83 async fn add_media_content(
91 &self,
92 request: &MediaRequestParameters,
93 content: Vec<u8>,
94 ignore_policy: IgnoreMediaRetentionPolicy,
95 ) -> Result<(), Self::Error>;
96
97 async fn replace_media_key(
117 &self,
118 from: &MediaRequestParameters,
119 to: &MediaRequestParameters,
120 ) -> Result<(), Self::Error>;
121
122 async fn get_media_content(
128 &self,
129 request: &MediaRequestParameters,
130 ) -> Result<Option<Vec<u8>>, Self::Error>;
131
132 async fn remove_media_content(
138 &self,
139 request: &MediaRequestParameters,
140 ) -> Result<(), Self::Error>;
141
142 async fn get_media_content_for_uri(&self, uri: &MxcUri)
157 -> Result<Option<Vec<u8>>, Self::Error>;
158
159 async fn remove_media_content_for_uri(&self, uri: &MxcUri) -> Result<(), Self::Error>;
169
170 async fn set_media_retention_policy(
177 &self,
178 policy: MediaRetentionPolicy,
179 ) -> Result<(), Self::Error>;
180
181 fn media_retention_policy(&self) -> MediaRetentionPolicy;
183
184 async fn set_ignore_media_retention_policy(
196 &self,
197 request: &MediaRequestParameters,
198 ignore_policy: IgnoreMediaRetentionPolicy,
199 ) -> Result<(), Self::Error>;
200
201 async fn clean_up_media_cache(&self) -> Result<(), Self::Error>;
205}
206
207#[repr(transparent)]
208struct EraseEventCacheStoreError<T>(T);
209
210#[cfg(not(tarpaulin_include))]
211impl<T: fmt::Debug> fmt::Debug for EraseEventCacheStoreError<T> {
212 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
213 self.0.fmt(f)
214 }
215}
216
217#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
218#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
219impl<T: EventCacheStore> EventCacheStore for EraseEventCacheStoreError<T> {
220 type Error = EventCacheStoreError;
221
222 async fn try_take_leased_lock(
223 &self,
224 lease_duration_ms: u32,
225 key: &str,
226 holder: &str,
227 ) -> Result<bool, Self::Error> {
228 self.0.try_take_leased_lock(lease_duration_ms, key, holder).await.map_err(Into::into)
229 }
230
231 async fn handle_linked_chunk_updates(
232 &self,
233 room_id: &RoomId,
234 updates: Vec<Update<Event, Gap>>,
235 ) -> Result<(), Self::Error> {
236 self.0.handle_linked_chunk_updates(room_id, updates).await.map_err(Into::into)
237 }
238
239 async fn reload_linked_chunk(
240 &self,
241 room_id: &RoomId,
242 ) -> Result<Vec<RawChunk<Event, Gap>>, Self::Error> {
243 self.0.reload_linked_chunk(room_id).await.map_err(Into::into)
244 }
245
246 async fn clear_all_rooms_chunks(&self) -> Result<(), Self::Error> {
247 self.0.clear_all_rooms_chunks().await.map_err(Into::into)
248 }
249
250 async fn add_media_content(
251 &self,
252 request: &MediaRequestParameters,
253 content: Vec<u8>,
254 ignore_policy: IgnoreMediaRetentionPolicy,
255 ) -> Result<(), Self::Error> {
256 self.0.add_media_content(request, content, ignore_policy).await.map_err(Into::into)
257 }
258
259 async fn replace_media_key(
260 &self,
261 from: &MediaRequestParameters,
262 to: &MediaRequestParameters,
263 ) -> Result<(), Self::Error> {
264 self.0.replace_media_key(from, to).await.map_err(Into::into)
265 }
266
267 async fn get_media_content(
268 &self,
269 request: &MediaRequestParameters,
270 ) -> Result<Option<Vec<u8>>, Self::Error> {
271 self.0.get_media_content(request).await.map_err(Into::into)
272 }
273
274 async fn remove_media_content(
275 &self,
276 request: &MediaRequestParameters,
277 ) -> Result<(), Self::Error> {
278 self.0.remove_media_content(request).await.map_err(Into::into)
279 }
280
281 async fn get_media_content_for_uri(
282 &self,
283 uri: &MxcUri,
284 ) -> Result<Option<Vec<u8>>, Self::Error> {
285 self.0.get_media_content_for_uri(uri).await.map_err(Into::into)
286 }
287
288 async fn remove_media_content_for_uri(&self, uri: &MxcUri) -> Result<(), Self::Error> {
289 self.0.remove_media_content_for_uri(uri).await.map_err(Into::into)
290 }
291
292 async fn set_media_retention_policy(
293 &self,
294 policy: MediaRetentionPolicy,
295 ) -> Result<(), Self::Error> {
296 self.0.set_media_retention_policy(policy).await.map_err(Into::into)
297 }
298
299 fn media_retention_policy(&self) -> MediaRetentionPolicy {
300 self.0.media_retention_policy()
301 }
302
303 async fn set_ignore_media_retention_policy(
304 &self,
305 request: &MediaRequestParameters,
306 ignore_policy: IgnoreMediaRetentionPolicy,
307 ) -> Result<(), Self::Error> {
308 self.0.set_ignore_media_retention_policy(request, ignore_policy).await.map_err(Into::into)
309 }
310
311 async fn clean_up_media_cache(&self) -> Result<(), Self::Error> {
312 self.0.clean_up_media_cache().await.map_err(Into::into)
313 }
314}
315
316pub type DynEventCacheStore = dyn EventCacheStore<Error = EventCacheStoreError>;
318
319pub trait IntoEventCacheStore {
325 #[doc(hidden)]
326 fn into_event_cache_store(self) -> Arc<DynEventCacheStore>;
327}
328
329impl<T> IntoEventCacheStore for T
330where
331 T: EventCacheStore + Sized + 'static,
332{
333 fn into_event_cache_store(self) -> Arc<DynEventCacheStore> {
334 Arc::new(EraseEventCacheStoreError(self))
335 }
336}
337
338impl<T> IntoEventCacheStore for Arc<T>
341where
342 T: EventCacheStore + 'static,
343{
344 fn into_event_cache_store(self) -> Arc<DynEventCacheStore> {
345 let ptr: *const T = Arc::into_raw(self);
346 let ptr_erased = ptr as *const EraseEventCacheStoreError<T>;
347 unsafe { Arc::from_raw(ptr_erased) }
350 }
351}