Skip to content

Commit e9dcdb7

Browse files
mgoldenbergHywan
authored andcommitted
refactor(indexeddb): add lifetime to IndexedKey::KeyComponents
Signed-off-by: Michael Goldenberg <[email protected]>
1 parent 0a3fe93 commit e9dcdb7

File tree

5 files changed

+102
-107
lines changed

5 files changed

+102
-107
lines changed

crates/matrix-sdk-indexeddb/src/event_cache_store/mod.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl_event_cache_store! {
195195
}
196196
Update::RemoveChunk(chunk_id) => {
197197
trace!("Removing chunk {chunk_id:?}");
198-
transaction.delete_chunk_by_id(room_id, &chunk_id).await?;
198+
transaction.delete_chunk_by_id(room_id, chunk_id).await?;
199199
}
200200
Update::PushItems { at, items } => {
201201
let chunk_identifier = at.chunk_identifier().index();
@@ -239,15 +239,15 @@ impl_event_cache_store! {
239239

240240
trace!(%room_id, "removing item @ {chunk_id}:{index}");
241241

242-
transaction.delete_event_by_position(room_id, &at.into()).await?;
242+
transaction.delete_event_by_position(room_id, at.into()).await?;
243243
}
244244
Update::DetachLastItems { at } => {
245245
let chunk_id = at.chunk_identifier().index();
246246
let index = at.index();
247247

248248
trace!(%room_id, "detaching last items @ {chunk_id}:{index}");
249249

250-
transaction.delete_events_by_chunk_from_index(room_id, &at.into()).await?;
250+
transaction.delete_events_by_chunk_from_index(room_id, at.into()).await?;
251251
}
252252
Update::StartReattachItems | Update::EndReattachItems => {
253253
// Nothing? See sqlite implementation
@@ -283,7 +283,7 @@ impl_event_cache_store! {
283283
let chunks = transaction.get_chunks_in_room(room_id).await?;
284284
for chunk in chunks {
285285
if let Some(raw_chunk) = transaction
286-
.load_chunk_by_id(room_id, &ChunkIdentifier::new(chunk.identifier))
286+
.load_chunk_by_id(room_id, ChunkIdentifier::new(chunk.identifier))
287287
.await?
288288
{
289289
raw_chunks.push(raw_chunk);
@@ -321,7 +321,7 @@ impl_event_cache_store! {
321321
let chunks = transaction.get_chunks_in_room(room_id).await?;
322322
for chunk in chunks {
323323
let chunk_id = ChunkIdentifier::new(chunk.identifier);
324-
let num_items = transaction.get_events_count_by_chunk(room_id, &chunk_id).await?;
324+
let num_items = transaction.get_events_count_by_chunk(room_id, chunk_id).await?;
325325
raw_chunks.push(ChunkMetadata {
326326
num_items,
327327
previous: chunk.previous.map(ChunkIdentifier::new),
@@ -355,7 +355,7 @@ impl_event_cache_store! {
355355
// Now that we know we have some chunks in the room, we query IndexedDB
356356
// for the last chunk in the room by getting the chunk which does not
357357
// have a next chunk.
358-
match transaction.get_chunk_by_next_chunk_id(room_id, &None).await {
358+
match transaction.get_chunk_by_next_chunk_id(room_id, None).await {
359359
Err(IndexeddbEventCacheStoreTransactionError::ItemIsNotUnique) => {
360360
// If there are multiple chunks that do not have a next chunk, that
361361
// means we have more than one last chunk, which means that we have
@@ -375,7 +375,7 @@ impl_event_cache_store! {
375375
Ok(Some(last_chunk)) => {
376376
let last_chunk_identifier = ChunkIdentifier::new(last_chunk.identifier);
377377
let last_raw_chunk = transaction
378-
.load_chunk_by_id(room_id, &last_chunk_identifier)
378+
.load_chunk_by_id(room_id, last_chunk_identifier)
379379
.await?
380380
.ok_or(IndexeddbEventCacheStoreError::UnableToLoadChunk)?;
381381
let max_chunk_id = transaction
@@ -404,10 +404,10 @@ impl_event_cache_store! {
404404
&[keys::LINKED_CHUNKS, keys::EVENTS, keys::GAPS],
405405
IdbTransactionMode::Readonly,
406406
)?;
407-
if let Some(chunk) = transaction.get_chunk_by_id(room_id, &before_chunk_identifier).await? {
407+
if let Some(chunk) = transaction.get_chunk_by_id(room_id, before_chunk_identifier).await? {
408408
if let Some(previous_identifier) = chunk.previous {
409409
let previous_identifier = ChunkIdentifier::new(previous_identifier);
410-
return Ok(transaction.load_chunk_by_id(room_id, &previous_identifier).await?);
410+
return Ok(transaction.load_chunk_by_id(room_id, previous_identifier).await?);
411411
}
412412
}
413413
Ok(None)
@@ -447,7 +447,7 @@ impl_event_cache_store! {
447447
let mut duplicated = Vec::new();
448448
for event_id in events {
449449
if let Some(types::Event::InBand(event)) =
450-
transaction.get_event_by_id(room_id, &event_id).await?
450+
transaction.get_event_by_id(room_id, event_id.clone()).await?
451451
{
452452
duplicated.push((event_id, event.position.into()));
453453
}
@@ -466,7 +466,7 @@ impl_event_cache_store! {
466466
let transaction =
467467
self.transaction(&[keys::EVENTS], IdbTransactionMode::Readonly)?;
468468
transaction
469-
.get_event_by_id(room_id, &event_id.to_owned())
469+
.get_event_by_id(room_id, event_id.to_owned())
470470
.await
471471
.map(|ok| ok.map(Into::into))
472472
.map_err(Into::into)
@@ -489,7 +489,7 @@ impl_event_cache_store! {
489489
Some(relation_types) if !relation_types.is_empty() => {
490490
for relation_type in relation_types {
491491
let relation = (event_id.to_owned(), relation_type.clone());
492-
let events = transaction.get_events_by_relation(room_id, &relation).await?;
492+
let events = transaction.get_events_by_relation(room_id, relation).await?;
493493
for event in events {
494494
let position = event.position().map(Into::into);
495495
related_events.push((event.into(), position));
@@ -498,7 +498,7 @@ impl_event_cache_store! {
498498
}
499499
_ => {
500500
for event in
501-
transaction.get_events_by_related_event(room_id, &event_id.to_owned()).await?
501+
transaction.get_events_by_related_event(room_id, event_id.to_owned()).await?
502502
{
503503
let position = event.position().map(Into::into);
504504
related_events.push((event.into(), position));
@@ -522,7 +522,7 @@ impl_event_cache_store! {
522522
};
523523
let transaction =
524524
self.transaction(&[keys::EVENTS], IdbTransactionMode::Readwrite)?;
525-
let event = match transaction.get_event_by_id(room_id, &event_id).await? {
525+
let event = match transaction.get_event_by_id(room_id, event_id).await? {
526526
Some(mut inner) => inner.with_content(event),
527527
None => types::Event::OutOfBand(OutOfBandEvent { content: event, position: () }),
528528
};

crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl IndexeddbEventCacheStoreSerializer {
7171
///
7272
/// Note that the particular key which is encoded is defined by the type
7373
/// `K`.
74-
pub fn encode_key<T, K>(&self, room_id: &RoomId, components: &K::KeyComponents) -> K
74+
pub fn encode_key<T, K>(&self, room_id: &RoomId, components: K::KeyComponents<'_>) -> K
7575
where
7676
T: Indexed,
7777
K: IndexedKey<T>,
@@ -86,7 +86,7 @@ impl IndexeddbEventCacheStoreSerializer {
8686
pub fn encode_key_as_value<T, K>(
8787
&self,
8888
room_id: &RoomId,
89-
components: &K::KeyComponents,
89+
components: K::KeyComponents<'_>,
9090
) -> Result<JsValue, serde_wasm_bindgen::Error>
9191
where
9292
T: Indexed,
@@ -129,12 +129,11 @@ impl IndexeddbEventCacheStoreSerializer {
129129
pub fn encode_key_component_range<'a, T, K>(
130130
&self,
131131
room_id: &RoomId,
132-
range: impl Into<IndexedKeyRange<&'a K::KeyComponents>>,
132+
range: impl Into<IndexedKeyRange<K::KeyComponents<'a>>>,
133133
) -> Result<IdbKeyRange, serde_wasm_bindgen::Error>
134134
where
135135
T: Indexed,
136136
K: IndexedKeyComponentBounds<T> + Serialize,
137-
K::KeyComponents: 'a,
138137
{
139138
let range = match range.into() {
140139
IndexedKeyRange::Only(components) => {

crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/traits.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub trait IndexedKey<T: Indexed> {
5454
const INDEX: Option<&'static str> = None;
5555

5656
/// Any extra data used to construct the key.
57-
type KeyComponents;
57+
type KeyComponents<'a>;
5858

5959
/// Encodes the key components into a type that can be used as a key in
6060
/// IndexedDB.
@@ -65,7 +65,7 @@ pub trait IndexedKey<T: Indexed> {
6565
/// encrypted before storage.
6666
fn encode(
6767
room_id: &RoomId,
68-
components: &Self::KeyComponents,
68+
components: Self::KeyComponents<'_>,
6969
serializer: &IndexeddbSerializer,
7070
) -> Self;
7171
}
@@ -104,12 +104,12 @@ where
104104
{
105105
/// Constructs the lower bound of the key.
106106
fn lower_key(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self {
107-
<Self as IndexedKey<T>>::encode(room_id, &Self::lower_key_components(), serializer)
107+
<Self as IndexedKey<T>>::encode(room_id, Self::lower_key_components(), serializer)
108108
}
109109

110110
/// Constructs the upper bound of the key.
111111
fn upper_key(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self {
112-
<Self as IndexedKey<T>>::encode(room_id, &Self::upper_key_components(), serializer)
112+
<Self as IndexedKey<T>>::encode(room_id, Self::upper_key_components(), serializer)
113113
}
114114
}
115115

@@ -123,8 +123,8 @@ where
123123
/// get a better overview of how these two interact.
124124
pub trait IndexedKeyComponentBounds<T: Indexed>: IndexedKeyBounds<T> {
125125
/// Constructs the lower bound of the key components.
126-
fn lower_key_components() -> Self::KeyComponents;
126+
fn lower_key_components() -> Self::KeyComponents<'static>;
127127

128128
/// Constructs the upper bound of the key components.
129-
fn upper_key_components() -> Self::KeyComponents;
129+
fn upper_key_components() -> Self::KeyComponents<'static>;
130130
}

0 commit comments

Comments
 (0)