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