|
12 | 12 | // See the License for the specific language governing permissions and
|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 |
| -use std::{collections::BTreeMap, fmt, iter::once, result::Result as StdResult}; |
| 15 | +use std::{fmt, iter::once, result::Result as StdResult}; |
16 | 16 |
|
17 |
| -use async_trait::async_trait; |
18 | 17 | use matrix_sdk_common::deserialized_responses::SyncTimelineEvent;
|
19 |
| -use ruma::{OwnedRoomId, RoomId}; |
20 |
| -use tokio::sync::RwLock; |
21 | 18 |
|
22 |
| -use super::{ |
23 |
| - linked_chunk::{ |
24 |
| - Chunk, ChunkIdentifier, ItemPosition, LinkedChunk, LinkedChunkError, LinkedChunkIter, |
25 |
| - LinkedChunkIterBackward, |
26 |
| - }, |
27 |
| - Result, |
| 19 | +use super::linked_chunk::{ |
| 20 | + Chunk, ChunkIdentifier, ItemPosition, LinkedChunk, LinkedChunkError, LinkedChunkIter, |
| 21 | + LinkedChunkIterBackward, |
28 | 22 | };
|
29 | 23 |
|
30 |
| -/// A store that can be remember information about the event cache. |
31 |
| -/// |
32 |
| -/// It really acts as a cache, in the sense that clearing the backing data |
33 |
| -/// should not have any irremediable effect, other than providing a lesser user |
34 |
| -/// experience. |
35 |
| -#[async_trait] |
36 |
| -pub trait EventCacheStore: Send + Sync { |
37 |
| - /// Returns all the known events for the given room. |
38 |
| - async fn room_events(&self, room: &RoomId) -> Result<Vec<SyncTimelineEvent>>; |
39 |
| - |
40 |
| - /// Adds all the entries to the given room's timeline. |
41 |
| - async fn append_room_entries(&self, room: &RoomId, entries: Vec<TimelineEntry>) -> Result<()>; |
42 |
| - |
43 |
| - /// Returns whether the store knows about the given pagination token. |
44 |
| - async fn contains_gap(&self, room: &RoomId, pagination_token: &PaginationToken) |
45 |
| - -> Result<bool>; |
46 |
| - |
47 |
| - /// Replaces a given gap (identified by its pagination token) with the given |
48 |
| - /// entries. |
49 |
| - /// |
50 |
| - /// Note: if the gap hasn't been found, then nothing happens, and the events |
51 |
| - /// are lost. |
52 |
| - /// |
53 |
| - /// Returns whether the gap was found. |
54 |
| - async fn replace_gap( |
55 |
| - &self, |
56 |
| - room: &RoomId, |
57 |
| - gap_id: Option<&PaginationToken>, |
58 |
| - entries: Vec<TimelineEntry>, |
59 |
| - ) -> Result<bool>; |
60 |
| - |
61 |
| - /// Retrieve the oldest backpagination token for the given room. |
62 |
| - async fn oldest_backpagination_token(&self, room: &RoomId) -> Result<Option<PaginationToken>>; |
63 |
| - |
64 |
| - /// Clear all the information tied to a given room. |
65 |
| - /// |
66 |
| - /// This forgets the following: |
67 |
| - /// - events in the room |
68 |
| - /// - pagination tokens |
69 |
| - async fn clear_room(&self, room: &RoomId) -> Result<()>; |
70 |
| -} |
71 |
| - |
72 | 24 | /// A newtype wrapper for a pagination token returned by a /messages response.
|
73 | 25 | #[derive(Clone, Debug, PartialEq)]
|
74 | 26 | pub struct PaginationToken(pub String);
|
75 | 27 |
|
76 |
| -#[derive(Clone)] |
77 |
| -pub enum TimelineEntry { |
78 |
| - Event(SyncTimelineEvent), |
79 |
| - |
80 |
| - Gap { |
81 |
| - /// The token to use in the query, extracted from a previous "from" / |
82 |
| - /// "end" field of a `/messages` response. |
83 |
| - prev_token: PaginationToken, |
84 |
| - }, |
85 |
| -} |
86 |
| - |
87 |
| -/// All the information related to a room and stored in the event cache. |
88 |
| -#[derive(Default)] |
89 |
| -struct RoomInfo { |
90 |
| - /// All the timeline entries per room, in sync order. |
91 |
| - entries: Vec<TimelineEntry>, |
92 |
| -} |
93 |
| - |
94 |
| -impl RoomInfo { |
95 |
| - fn clear(&mut self) { |
96 |
| - self.entries.clear(); |
97 |
| - } |
98 |
| -} |
99 |
| - |
100 |
| -/// An [`EventCacheStore`] implementation that keeps all the information in |
101 |
| -/// memory. |
102 |
| -#[derive(Default)] |
103 |
| -pub(crate) struct MemoryStore { |
104 |
| - by_room: RwLock<BTreeMap<OwnedRoomId, RoomInfo>>, |
105 |
| -} |
106 |
| - |
107 |
| -impl MemoryStore { |
108 |
| - /// Create a new empty [`MemoryStore`]. |
109 |
| - pub fn new() -> Self { |
110 |
| - Default::default() |
111 |
| - } |
112 |
| -} |
113 |
| - |
114 |
| -#[async_trait] |
115 |
| -impl EventCacheStore for MemoryStore { |
116 |
| - async fn room_events(&self, room: &RoomId) -> Result<Vec<SyncTimelineEvent>> { |
117 |
| - Ok(self |
118 |
| - .by_room |
119 |
| - .read() |
120 |
| - .await |
121 |
| - .get(room) |
122 |
| - .map(|room_info| { |
123 |
| - room_info |
124 |
| - .entries |
125 |
| - .iter() |
126 |
| - .filter_map( |
127 |
| - |entry| if let TimelineEntry::Event(ev) = entry { Some(ev) } else { None }, |
128 |
| - ) |
129 |
| - .cloned() |
130 |
| - .collect() |
131 |
| - }) |
132 |
| - .unwrap_or_default()) |
133 |
| - } |
134 |
| - |
135 |
| - async fn append_room_entries(&self, room: &RoomId, entries: Vec<TimelineEntry>) -> Result<()> { |
136 |
| - self.by_room.write().await.entry(room.to_owned()).or_default().entries.extend(entries); |
137 |
| - Ok(()) |
138 |
| - } |
139 |
| - |
140 |
| - async fn clear_room(&self, room: &RoomId) -> Result<()> { |
141 |
| - // Clear the room, so as to avoid reallocations if the room is being reused. |
142 |
| - // XXX: do we also want an actual way to *remove* a room? (for left rooms) |
143 |
| - if let Some(room) = self.by_room.write().await.get_mut(room) { |
144 |
| - room.clear(); |
145 |
| - } |
146 |
| - |
147 |
| - Ok(()) |
148 |
| - } |
149 |
| - |
150 |
| - async fn oldest_backpagination_token(&self, room: &RoomId) -> Result<Option<PaginationToken>> { |
151 |
| - Ok(self.by_room.read().await.get(room).and_then(|room| { |
152 |
| - room.entries.iter().find_map(|entry| { |
153 |
| - if let TimelineEntry::Gap { prev_token: backpagination_token } = entry { |
154 |
| - Some(backpagination_token.clone()) |
155 |
| - } else { |
156 |
| - None |
157 |
| - } |
158 |
| - }) |
159 |
| - })) |
160 |
| - } |
161 |
| - |
162 |
| - async fn contains_gap(&self, room: &RoomId, needle: &PaginationToken) -> Result<bool> { |
163 |
| - let mut by_room_guard = self.by_room.write().await; |
164 |
| - let room = by_room_guard.entry(room.to_owned()).or_default(); |
165 |
| - |
166 |
| - Ok(room.entries.iter().any(|entry| { |
167 |
| - if let TimelineEntry::Gap { prev_token: existing } = entry { |
168 |
| - existing == needle |
169 |
| - } else { |
170 |
| - false |
171 |
| - } |
172 |
| - })) |
173 |
| - } |
174 |
| - |
175 |
| - async fn replace_gap( |
176 |
| - &self, |
177 |
| - room: &RoomId, |
178 |
| - token: Option<&PaginationToken>, |
179 |
| - entries: Vec<TimelineEntry>, |
180 |
| - ) -> Result<bool> { |
181 |
| - let mut by_room_guard = self.by_room.write().await; |
182 |
| - let room = by_room_guard.entry(room.to_owned()).or_default(); |
183 |
| - |
184 |
| - if let Some(token) = token { |
185 |
| - let gap_pos = room.entries.iter().enumerate().find_map(|(i, t)| { |
186 |
| - if let TimelineEntry::Gap { prev_token: existing } = t { |
187 |
| - if existing == token { |
188 |
| - return Some(i); |
189 |
| - } |
190 |
| - } |
191 |
| - None |
192 |
| - }); |
193 |
| - |
194 |
| - if let Some(pos) = gap_pos { |
195 |
| - room.entries.splice(pos..pos + 1, entries); |
196 |
| - Ok(true) |
197 |
| - } else { |
198 |
| - Ok(false) |
199 |
| - } |
200 |
| - } else { |
201 |
| - // We had no previous token: assume we can prepend the events. |
202 |
| - room.entries.splice(0..0, entries); |
203 |
| - Ok(true) |
204 |
| - } |
205 |
| - } |
206 |
| -} |
207 |
| - |
208 | 28 | #[derive(Debug)]
|
209 | 29 | pub struct Gap {
|
210 | 30 | /// The token to use in the query, extracted from a previous "from" /
|
|
0 commit comments