matrix_sdk_base/event_cache/store/
memory_store.rs

1// Copyright 2024 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::{
16    collections::HashMap,
17    num::NonZeroUsize,
18    sync::{Arc, RwLock as StdRwLock},
19};
20
21use async_trait::async_trait;
22use matrix_sdk_common::{
23    linked_chunk::{
24        relational::RelationalLinkedChunk, ChunkIdentifier, ChunkIdentifierGenerator, Position,
25        RawChunk, Update,
26    },
27    ring_buffer::RingBuffer,
28    store_locks::memory_store_helper::try_take_leased_lock,
29};
30use ruma::{
31    events::relation::RelationType,
32    time::{Instant, SystemTime},
33    EventId, MxcUri, OwnedEventId, OwnedMxcUri, RoomId,
34};
35use tracing::error;
36
37use super::{
38    compute_filters_string, extract_event_relation,
39    media::{EventCacheStoreMedia, IgnoreMediaRetentionPolicy, MediaRetentionPolicy, MediaService},
40    EventCacheStore, EventCacheStoreError, Result,
41};
42use crate::{
43    event_cache::{Event, Gap},
44    media::{MediaRequestParameters, UniqueKey as _},
45};
46
47/// In-memory, non-persistent implementation of the `EventCacheStore`.
48///
49/// Default if no other is configured at startup.
50#[derive(Debug, Clone)]
51pub struct MemoryStore {
52    inner: Arc<StdRwLock<MemoryStoreInner>>,
53    media_service: MediaService,
54}
55
56#[derive(Debug)]
57struct MemoryStoreInner {
58    media: RingBuffer<MediaContent>,
59    leases: HashMap<String, (String, Instant)>,
60    events: RelationalLinkedChunk<OwnedEventId, Event, Gap>,
61    media_retention_policy: Option<MediaRetentionPolicy>,
62    last_media_cleanup_time: SystemTime,
63}
64
65/// A media content in the `MemoryStore`.
66#[derive(Debug)]
67struct MediaContent {
68    /// The URI of the content.
69    uri: OwnedMxcUri,
70
71    /// The unique key of the content.
72    key: String,
73
74    /// The bytes of the content.
75    data: Vec<u8>,
76
77    /// Whether we should ignore the [`MediaRetentionPolicy`] for this content.
78    ignore_policy: bool,
79
80    /// The time of the last access of the content.
81    last_access: SystemTime,
82}
83
84const NUMBER_OF_MEDIAS: NonZeroUsize = NonZeroUsize::new(20).unwrap();
85
86impl Default for MemoryStore {
87    fn default() -> Self {
88        // Given that the store is empty, we won't need to clean it up right away.
89        let last_media_cleanup_time = SystemTime::now();
90        let media_service = MediaService::new();
91        media_service.restore(None, Some(last_media_cleanup_time));
92
93        Self {
94            inner: Arc::new(StdRwLock::new(MemoryStoreInner {
95                media: RingBuffer::new(NUMBER_OF_MEDIAS),
96                leases: Default::default(),
97                events: RelationalLinkedChunk::new(),
98                media_retention_policy: None,
99                last_media_cleanup_time,
100            })),
101            media_service,
102        }
103    }
104}
105
106impl MemoryStore {
107    /// Create a new empty MemoryStore
108    pub fn new() -> Self {
109        Self::default()
110    }
111}
112
113#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
114#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
115impl EventCacheStore for MemoryStore {
116    type Error = EventCacheStoreError;
117
118    async fn try_take_leased_lock(
119        &self,
120        lease_duration_ms: u32,
121        key: &str,
122        holder: &str,
123    ) -> Result<bool, Self::Error> {
124        let mut inner = self.inner.write().unwrap();
125
126        Ok(try_take_leased_lock(&mut inner.leases, lease_duration_ms, key, holder))
127    }
128
129    async fn handle_linked_chunk_updates(
130        &self,
131        room_id: &RoomId,
132        updates: Vec<Update<Event, Gap>>,
133    ) -> Result<(), Self::Error> {
134        let mut inner = self.inner.write().unwrap();
135        inner.events.apply_updates(room_id, updates);
136
137        Ok(())
138    }
139
140    async fn load_all_chunks(
141        &self,
142        room_id: &RoomId,
143    ) -> Result<Vec<RawChunk<Event, Gap>>, Self::Error> {
144        let inner = self.inner.read().unwrap();
145        inner
146            .events
147            .load_all_chunks(room_id)
148            .map_err(|err| EventCacheStoreError::InvalidData { details: err })
149    }
150
151    async fn load_last_chunk(
152        &self,
153        room_id: &RoomId,
154    ) -> Result<(Option<RawChunk<Event, Gap>>, ChunkIdentifierGenerator), Self::Error> {
155        let inner = self.inner.read().unwrap();
156        inner
157            .events
158            .load_last_chunk(room_id)
159            .map_err(|err| EventCacheStoreError::InvalidData { details: err })
160    }
161
162    async fn load_previous_chunk(
163        &self,
164        room_id: &RoomId,
165        before_chunk_identifier: ChunkIdentifier,
166    ) -> Result<Option<RawChunk<Event, Gap>>, Self::Error> {
167        let inner = self.inner.read().unwrap();
168        inner
169            .events
170            .load_previous_chunk(room_id, before_chunk_identifier)
171            .map_err(|err| EventCacheStoreError::InvalidData { details: err })
172    }
173
174    async fn clear_all_rooms_chunks(&self) -> Result<(), Self::Error> {
175        self.inner.write().unwrap().events.clear();
176        Ok(())
177    }
178
179    async fn filter_duplicated_events(
180        &self,
181        room_id: &RoomId,
182        mut events: Vec<OwnedEventId>,
183    ) -> Result<Vec<(OwnedEventId, Position)>, Self::Error> {
184        // Collect all duplicated events.
185        let inner = self.inner.read().unwrap();
186
187        let mut duplicated_events = Vec::new();
188
189        for (event, position) in inner.events.unordered_room_items(room_id) {
190            // If `events` is empty, we can short-circuit.
191            if events.is_empty() {
192                break;
193            }
194
195            if let Some(known_event_id) = event.event_id() {
196                // This event is a duplicate!
197                if let Some(index) =
198                    events.iter().position(|new_event_id| &known_event_id == new_event_id)
199                {
200                    duplicated_events.push((events.remove(index), position));
201                }
202            }
203        }
204
205        Ok(duplicated_events)
206    }
207
208    async fn find_event(
209        &self,
210        room_id: &RoomId,
211        event_id: &EventId,
212    ) -> Result<Option<Event>, Self::Error> {
213        let inner = self.inner.read().unwrap();
214
215        let event = inner.events.items().find_map(|(event, this_room_id)| {
216            (room_id == this_room_id && event.event_id()? == event_id).then_some(event.clone())
217        });
218
219        Ok(event)
220    }
221
222    async fn find_event_relations(
223        &self,
224        room_id: &RoomId,
225        event_id: &EventId,
226        filters: Option<&[RelationType]>,
227    ) -> Result<Vec<Event>, Self::Error> {
228        let inner = self.inner.read().unwrap();
229
230        let filters = compute_filters_string(filters);
231
232        let related_events = inner
233            .events
234            .items()
235            .filter_map(|(event, this_room_id)| {
236                // Must be in the same room.
237                if room_id != this_room_id {
238                    return None;
239                }
240
241                // Must have a relation.
242                let (related_to, rel_type) = extract_event_relation(event.raw())?;
243
244                // Must relate to the target item.
245                if related_to != event_id {
246                    return None;
247                }
248
249                // Must not be filtered out.
250                if let Some(filters) = &filters {
251                    filters.contains(&rel_type).then_some(event.clone())
252                } else {
253                    Some(event.clone())
254                }
255            })
256            .collect();
257
258        Ok(related_events)
259    }
260
261    async fn save_event(&self, room_id: &RoomId, event: Event) -> Result<(), Self::Error> {
262        if event.event_id().is_none() {
263            error!(%room_id, "Trying to save an event with no ID");
264            return Ok(());
265        }
266        self.inner.write().unwrap().events.save_item(room_id.to_owned(), event);
267        Ok(())
268    }
269
270    async fn add_media_content(
271        &self,
272        request: &MediaRequestParameters,
273        data: Vec<u8>,
274        ignore_policy: IgnoreMediaRetentionPolicy,
275    ) -> Result<()> {
276        self.media_service.add_media_content(self, request, data, ignore_policy).await
277    }
278
279    async fn replace_media_key(
280        &self,
281        from: &MediaRequestParameters,
282        to: &MediaRequestParameters,
283    ) -> Result<(), Self::Error> {
284        let expected_key = from.unique_key();
285
286        let mut inner = self.inner.write().unwrap();
287
288        if let Some(media_content) =
289            inner.media.iter_mut().find(|media_content| media_content.key == expected_key)
290        {
291            media_content.uri = to.uri().to_owned();
292            media_content.key = to.unique_key();
293        }
294
295        Ok(())
296    }
297
298    async fn get_media_content(&self, request: &MediaRequestParameters) -> Result<Option<Vec<u8>>> {
299        self.media_service.get_media_content(self, request).await
300    }
301
302    async fn remove_media_content(&self, request: &MediaRequestParameters) -> Result<()> {
303        let expected_key = request.unique_key();
304
305        let mut inner = self.inner.write().unwrap();
306
307        let Some(index) =
308            inner.media.iter().position(|media_content| media_content.key == expected_key)
309        else {
310            return Ok(());
311        };
312
313        inner.media.remove(index);
314
315        Ok(())
316    }
317
318    async fn get_media_content_for_uri(
319        &self,
320        uri: &MxcUri,
321    ) -> Result<Option<Vec<u8>>, Self::Error> {
322        self.media_service.get_media_content_for_uri(self, uri).await
323    }
324
325    async fn remove_media_content_for_uri(&self, uri: &MxcUri) -> Result<()> {
326        let mut inner = self.inner.write().unwrap();
327
328        let positions = inner
329            .media
330            .iter()
331            .enumerate()
332            .filter_map(|(position, media_content)| (media_content.uri == uri).then_some(position))
333            .collect::<Vec<_>>();
334
335        // Iterate in reverse-order so that positions stay valid after first removals.
336        for position in positions.into_iter().rev() {
337            inner.media.remove(position);
338        }
339
340        Ok(())
341    }
342
343    async fn set_media_retention_policy(
344        &self,
345        policy: MediaRetentionPolicy,
346    ) -> Result<(), Self::Error> {
347        self.media_service.set_media_retention_policy(self, policy).await
348    }
349
350    fn media_retention_policy(&self) -> MediaRetentionPolicy {
351        self.media_service.media_retention_policy()
352    }
353
354    async fn set_ignore_media_retention_policy(
355        &self,
356        request: &MediaRequestParameters,
357        ignore_policy: IgnoreMediaRetentionPolicy,
358    ) -> Result<(), Self::Error> {
359        self.media_service.set_ignore_media_retention_policy(self, request, ignore_policy).await
360    }
361
362    async fn clean_up_media_cache(&self) -> Result<(), Self::Error> {
363        self.media_service.clean_up_media_cache(self).await
364    }
365}
366
367#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
368#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
369impl EventCacheStoreMedia for MemoryStore {
370    type Error = EventCacheStoreError;
371
372    async fn media_retention_policy_inner(
373        &self,
374    ) -> Result<Option<MediaRetentionPolicy>, Self::Error> {
375        Ok(self.inner.read().unwrap().media_retention_policy)
376    }
377
378    async fn set_media_retention_policy_inner(
379        &self,
380        policy: MediaRetentionPolicy,
381    ) -> Result<(), Self::Error> {
382        self.inner.write().unwrap().media_retention_policy = Some(policy);
383        Ok(())
384    }
385
386    async fn add_media_content_inner(
387        &self,
388        request: &MediaRequestParameters,
389        data: Vec<u8>,
390        last_access: SystemTime,
391        policy: MediaRetentionPolicy,
392        ignore_policy: IgnoreMediaRetentionPolicy,
393    ) -> Result<(), Self::Error> {
394        // Avoid duplication. Let's try to remove it first.
395        self.remove_media_content(request).await?;
396
397        let ignore_policy = ignore_policy.is_yes();
398
399        if !ignore_policy && policy.exceeds_max_file_size(data.len() as u64) {
400            // Do not store it.
401            return Ok(());
402        };
403
404        // Now, let's add it.
405        let mut inner = self.inner.write().unwrap();
406        inner.media.push(MediaContent {
407            uri: request.uri().to_owned(),
408            key: request.unique_key(),
409            data,
410            ignore_policy,
411            last_access,
412        });
413
414        Ok(())
415    }
416
417    async fn set_ignore_media_retention_policy_inner(
418        &self,
419        request: &MediaRequestParameters,
420        ignore_policy: IgnoreMediaRetentionPolicy,
421    ) -> Result<(), Self::Error> {
422        let mut inner = self.inner.write().unwrap();
423        let expected_key = request.unique_key();
424
425        if let Some(media_content) = inner.media.iter_mut().find(|media| media.key == expected_key)
426        {
427            media_content.ignore_policy = ignore_policy.is_yes();
428        }
429
430        Ok(())
431    }
432
433    async fn get_media_content_inner(
434        &self,
435        request: &MediaRequestParameters,
436        current_time: SystemTime,
437    ) -> Result<Option<Vec<u8>>, Self::Error> {
438        let mut inner = self.inner.write().unwrap();
439        let expected_key = request.unique_key();
440
441        // First get the content out of the buffer, we are going to put it back at the
442        // end.
443        let Some(index) = inner.media.iter().position(|media| media.key == expected_key) else {
444            return Ok(None);
445        };
446        let Some(mut content) = inner.media.remove(index) else {
447            return Ok(None);
448        };
449
450        // Clone the data.
451        let data = content.data.clone();
452
453        // Update the last access time.
454        content.last_access = current_time;
455
456        // Put it back in the buffer.
457        inner.media.push(content);
458
459        Ok(Some(data))
460    }
461
462    async fn get_media_content_for_uri_inner(
463        &self,
464        expected_uri: &MxcUri,
465        current_time: SystemTime,
466    ) -> Result<Option<Vec<u8>>, Self::Error> {
467        let mut inner = self.inner.write().unwrap();
468
469        // First get the content out of the buffer, we are going to put it back at the
470        // end.
471        let Some(index) = inner.media.iter().position(|media| media.uri == expected_uri) else {
472            return Ok(None);
473        };
474        let Some(mut content) = inner.media.remove(index) else {
475            return Ok(None);
476        };
477
478        // Clone the data.
479        let data = content.data.clone();
480
481        // Update the last access time.
482        content.last_access = current_time;
483
484        // Put it back in the buffer.
485        inner.media.push(content);
486
487        Ok(Some(data))
488    }
489
490    async fn clean_up_media_cache_inner(
491        &self,
492        policy: MediaRetentionPolicy,
493        current_time: SystemTime,
494    ) -> Result<(), Self::Error> {
495        if !policy.has_limitations() {
496            // We can safely skip all the checks.
497            return Ok(());
498        }
499
500        let mut inner = self.inner.write().unwrap();
501
502        // First, check media content that exceed the max filesize.
503        if policy.computed_max_file_size().is_some() {
504            inner.media.retain(|content| {
505                content.ignore_policy || !policy.exceeds_max_file_size(content.data.len() as u64)
506            });
507        }
508
509        // Then, clean up expired media content.
510        if policy.last_access_expiry.is_some() {
511            inner.media.retain(|content| {
512                content.ignore_policy
513                    || !policy.has_content_expired(current_time, content.last_access)
514            });
515        }
516
517        // Finally, if the cache size is too big, remove old items until it fits.
518        if let Some(max_cache_size) = policy.max_cache_size {
519            // Reverse the iterator because in case the cache size is overflowing, we want
520            // to count the number of old items to remove. Items are sorted by last access
521            // and old items are at the start.
522            let (_, items_to_remove) = inner.media.iter().enumerate().rev().fold(
523                (0u64, Vec::with_capacity(NUMBER_OF_MEDIAS.into())),
524                |(mut cache_size, mut items_to_remove), (index, content)| {
525                    if content.ignore_policy {
526                        // Do not count it.
527                        return (cache_size, items_to_remove);
528                    }
529
530                    let remove_item = if items_to_remove.is_empty() {
531                        // We have not reached the max cache size yet.
532                        if let Some(sum) = cache_size.checked_add(content.data.len() as u64) {
533                            cache_size = sum;
534                            // Start removing items if we have exceeded the max cache size.
535                            cache_size > max_cache_size
536                        } else {
537                            // The cache size is overflowing, remove the remaining items, since the
538                            // max cache size cannot be bigger than
539                            // usize::MAX.
540                            true
541                        }
542                    } else {
543                        // We have reached the max cache size already, just remove it.
544                        true
545                    };
546
547                    if remove_item {
548                        items_to_remove.push(index);
549                    }
550
551                    (cache_size, items_to_remove)
552                },
553            );
554
555            // The indexes are already in reverse order so we can just iterate in that order
556            // to remove them starting by the end.
557            for index in items_to_remove {
558                inner.media.remove(index);
559            }
560        }
561
562        inner.last_media_cleanup_time = current_time;
563
564        Ok(())
565    }
566
567    async fn last_media_cleanup_time_inner(&self) -> Result<Option<SystemTime>, Self::Error> {
568        Ok(Some(self.inner.read().unwrap().last_media_cleanup_time))
569    }
570}
571
572#[cfg(test)]
573mod tests {
574    use super::{MemoryStore, Result};
575    use crate::event_cache_store_media_integration_tests;
576
577    async fn get_event_cache_store() -> Result<MemoryStore> {
578        Ok(MemoryStore::new())
579    }
580
581    event_cache_store_integration_tests!();
582    event_cache_store_integration_tests_time!();
583    event_cache_store_media_integration_tests!(with_media_size_tests);
584}