Skip to content

Commit a178f62

Browse files
committed
refactor: EventCacheStore::handle_linked_chunk_updates takes a Vec<Update>.
This patch updates `EventCacheStore::handle_linked_chunk_updates` to take a `Vec<Update<Item, Gap>>` instead of `&[Update<Item, Gap>]`. In fact, `linked_chunk::ObservableUpdates::take()` already returns a `Vec<Update<Item, Gap>>`; we can simply forward this `Vec` up to here without any further clones.
1 parent bfad283 commit a178f62

File tree

4 files changed

+33
-38
lines changed

4 files changed

+33
-38
lines changed

crates/matrix-sdk-base/src/event_cache/store/memory_store.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,12 @@ impl EventCacheStore for MemoryStore {
8585
async fn handle_linked_chunk_updates(
8686
&self,
8787
room_id: &RoomId,
88-
updates: &[Update<Event, Gap>],
88+
updates: Vec<Update<Event, Gap>>,
8989
) -> Result<(), Self::Error> {
9090
let mut inner = self.inner.write().unwrap();
91+
inner.events.apply_updates(room_id, updates);
9192

92-
Ok(inner.events.apply_updates(updates))
93+
Ok(())
9394
}
9495

9596
async fn add_media_content(
@@ -128,7 +129,7 @@ impl EventCacheStore for MemoryStore {
128129
async fn get_media_content(&self, request: &MediaRequestParameters) -> Result<Option<Vec<u8>>> {
129130
let expected_key = request.unique_key();
130131

131-
let inner = self.inner.write().unwrap();
132+
let inner = self.inner.read().unwrap();
132133

133134
Ok(inner.media.iter().find_map(|(_media_uri, media_key, media_content)| {
134135
(media_key == &expected_key).then(|| media_content.to_owned())

crates/matrix-sdk-base/src/event_cache/store/traits.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub trait EventCacheStore: AsyncTraitDeps {
4646
async fn handle_linked_chunk_updates(
4747
&self,
4848
room_id: &RoomId,
49-
updates: &[Update<Event, Gap>],
49+
updates: Vec<Update<Event, Gap>>,
5050
) -> Result<(), Self::Error>;
5151

5252
/// Add a media file's content in the media store.
@@ -146,7 +146,7 @@ impl<T: EventCacheStore> EventCacheStore for EraseEventCacheStoreError<T> {
146146
async fn handle_linked_chunk_updates(
147147
&self,
148148
room_id: &RoomId,
149-
updates: &[Update<Event, Gap>],
149+
updates: Vec<Update<Event, Gap>>,
150150
) -> Result<(), Self::Error> {
151151
self.0.handle_linked_chunk_updates(room_id, updates).await.map_err(Into::into)
152152
}

crates/matrix-sdk-common/src/linked_chunk/relational.rs

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,7 @@ impl<Item, Gap> RelationalLinkedChunk<Item, Gap> {
8181

8282
/// Apply [`Update`]s. That's the only way to write data inside this
8383
/// relational linked chunk.
84-
pub fn apply_updates(&mut self, room_id: &RoomId, updates: &[Update<Item, Gap>])
85-
where
86-
Item: Clone,
87-
Gap: Clone,
88-
{
84+
pub fn apply_updates(&mut self, room_id: &RoomId, updates: Vec<Update<Item, Gap>>) {
8985
for update in updates {
9086
match update {
9187
Update::NewItemsChunk { previous, new, next } => {
@@ -96,8 +92,8 @@ impl<Item, Gap> RelationalLinkedChunk<Item, Gap> {
9692
insert_chunk(&mut self.chunks, room_id, previous, new, next);
9793
self.items.push(ItemRow {
9894
room_id: room_id.to_owned(),
99-
position: Position::new(*new, 0),
100-
item: Either::Gap(gap.clone()),
95+
position: Position::new(new, 0),
96+
item: Either::Gap(gap),
10197
});
10298
}
10399

@@ -111,7 +107,7 @@ impl<Item, Gap> RelationalLinkedChunk<Item, Gap> {
111107
.filter_map(
112108
|(nth, ItemRow { room_id: room_id_candidate, position, .. })| {
113109
(room_id == room_id_candidate
114-
&& position.chunk_identifier() == *chunk_identifier)
110+
&& position.chunk_identifier() == chunk_identifier)
115111
.then_some(nth)
116112
},
117113
)
@@ -122,14 +118,12 @@ impl<Item, Gap> RelationalLinkedChunk<Item, Gap> {
122118
}
123119
}
124120

125-
Update::PushItems { at, items } => {
126-
let mut at = *at;
127-
121+
Update::PushItems { mut at, items } => {
128122
for item in items {
129123
self.items.push(ItemRow {
130124
room_id: room_id.to_owned(),
131125
position: at,
132-
item: Either::Item(item.clone()),
126+
item: Either::Item(item),
133127
});
134128
at.increment_index();
135129
}
@@ -147,7 +141,7 @@ impl<Item, Gap> RelationalLinkedChunk<Item, Gap> {
147141
}
148142

149143
// Find the item to remove.
150-
if position == at {
144+
if *position == at {
151145
debug_assert!(entry_to_remove.is_none(), "Found the same entry twice");
152146

153147
entry_to_remove = Some(nth);
@@ -191,55 +185,55 @@ impl<Item, Gap> RelationalLinkedChunk<Item, Gap> {
191185
fn insert_chunk(
192186
chunks: &mut Vec<ChunkRow>,
193187
room_id: &RoomId,
194-
previous: &Option<ChunkIdentifier>,
195-
new: &ChunkIdentifier,
196-
next: &Option<ChunkIdentifier>,
188+
previous: Option<ChunkIdentifier>,
189+
new: ChunkIdentifier,
190+
next: Option<ChunkIdentifier>,
197191
) {
198192
// Find the previous chunk, and update its next chunk.
199193
if let Some(previous) = previous {
200194
let entry_for_previous_chunk = chunks
201195
.iter_mut()
202196
.find(|ChunkRow { room_id: room_id_candidate, chunk, .. }| {
203-
room_id == room_id_candidate && chunk == previous
197+
room_id == room_id_candidate && *chunk == previous
204198
})
205199
.expect("Previous chunk should be present");
206200

207201
// Insert the chunk.
208-
entry_for_previous_chunk.next_chunk = Some(*new);
202+
entry_for_previous_chunk.next_chunk = Some(new);
209203
}
210204

211205
// Find the next chunk, and update its previous chunk.
212206
if let Some(next) = next {
213207
let entry_for_next_chunk = chunks
214208
.iter_mut()
215209
.find(|ChunkRow { room_id: room_id_candidate, chunk, .. }| {
216-
room_id == room_id_candidate && chunk == next
210+
room_id == room_id_candidate && *chunk == next
217211
})
218212
.expect("Next chunk should be present");
219213

220214
// Insert the chunk.
221-
entry_for_next_chunk.previous_chunk = Some(*new);
215+
entry_for_next_chunk.previous_chunk = Some(new);
222216
}
223217

224218
// Insert the chunk.
225219
chunks.push(ChunkRow {
226220
room_id: room_id.to_owned(),
227-
previous_chunk: *previous,
228-
chunk: *new,
229-
next_chunk: *next,
221+
previous_chunk: previous,
222+
chunk: new,
223+
next_chunk: next,
230224
});
231225
}
232226

233227
fn remove_chunk(
234228
chunks: &mut Vec<ChunkRow>,
235229
room_id: &RoomId,
236-
chunk_to_remove: &ChunkIdentifier,
230+
chunk_to_remove: ChunkIdentifier,
237231
) {
238232
let entry_nth_to_remove = chunks
239233
.iter()
240234
.enumerate()
241235
.find_map(|(nth, ChunkRow { room_id: room_id_candidate, chunk, .. })| {
242-
(room_id == room_id_candidate && chunk == chunk_to_remove).then_some(nth)
236+
(room_id == room_id_candidate && *chunk == chunk_to_remove).then_some(nth)
243237
})
244238
.expect("Remove an unknown chunk");
245239

@@ -294,7 +288,7 @@ mod tests {
294288

295289
relational_linked_chunk.apply_updates(
296290
room_id,
297-
&[
291+
vec![
298292
// 0
299293
Update::NewItemsChunk { previous: None, new: CId::new(0), next: None },
300294
// 1 after 0
@@ -351,7 +345,7 @@ mod tests {
351345

352346
relational_linked_chunk.apply_updates(
353347
room_id,
354-
&[
348+
vec![
355349
// 0
356350
Update::NewItemsChunk { previous: None, new: CId::new(0), next: None },
357351
// 1 after 0
@@ -408,7 +402,7 @@ mod tests {
408402

409403
relational_linked_chunk.apply_updates(
410404
room_id,
411-
&[
405+
vec![
412406
// 0
413407
Update::NewItemsChunk { previous: None, new: CId::new(0), next: None },
414408
// 1 after 0
@@ -454,7 +448,7 @@ mod tests {
454448

455449
relational_linked_chunk.apply_updates(
456450
room_id,
457-
&[
451+
vec![
458452
// new chunk (this is not mandatory for this test, but let's try to be realistic)
459453
Update::NewItemsChunk { previous: None, new: CId::new(0), next: None },
460454
// new items on 0
@@ -541,7 +535,7 @@ mod tests {
541535

542536
relational_linked_chunk.apply_updates(
543537
room_id,
544-
&[
538+
vec![
545539
// new chunk (this is not mandatory for this test, but let's try to be realistic)
546540
Update::NewItemsChunk { previous: None, new: CId::new(0), next: None },
547541
// new items on 0
@@ -596,7 +590,7 @@ mod tests {
596590

597591
relational_linked_chunk.apply_updates(
598592
room_id,
599-
&[
593+
vec![
600594
// new chunk
601595
Update::NewItemsChunk { previous: None, new: CId::new(0), next: None },
602596
// new chunk
@@ -670,7 +664,7 @@ mod tests {
670664
let mut relational_linked_chunk = RelationalLinkedChunk::<char, ()>::new();
671665

672666
relational_linked_chunk
673-
.apply_updates(room_id, &[Update::StartReattachItems, Update::EndReattachItems]);
667+
.apply_updates(room_id, vec![Update::StartReattachItems, Update::EndReattachItems]);
674668

675669
// Nothing happened.
676670
assert!(relational_linked_chunk.chunks.is_empty());

crates/matrix-sdk-sqlite/src/event_cache_store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl EventCacheStore for SqliteEventCacheStore {
186186
async fn handle_linked_chunk_updates(
187187
&self,
188188
_room_id: &RoomId,
189-
_updates: &[Update<Event, Gap>],
189+
_updates: Vec<Update<Event, Gap>>,
190190
) -> Result<(), Self::Error> {
191191
todo!()
192192
}

0 commit comments

Comments
 (0)