Skip to content

Commit 33c16b2

Browse files
mgoldenbergHywan
authored andcommitted
refactor(indexeddb): use references in IndexedEventIdKey::KeyComponents
Signed-off-by: Michael Goldenberg <[email protected]>
1 parent e9dcdb7 commit 33c16b2

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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.clone()).await?
450+
transaction.get_event_by_id(room_id, &event_id).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)
470470
.await
471471
.map(|ok| ok.map(Into::into))
472472
.map_err(Into::into)
@@ -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/types.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
//! These types mimic the structure of the object stores and indices created in
2828
//! [`crate::event_cache_store::migrations`].
2929
30+
use std::sync::LazyLock;
31+
3032
use matrix_sdk_base::linked_chunk::ChunkIdentifier;
3133
use matrix_sdk_crypto::CryptoStoreError;
3234
use ruma::{events::relation::RelationType, EventId, OwnedEventId, RoomId};
@@ -60,6 +62,22 @@ const INDEXED_KEY_LOWER_CHARACTER: char = '\u{0000}';
6062
/// [1]: https://en.wikipedia.org/wiki/Plane_(Unicode)#Basic_Multilingual_Plane
6163
const INDEXED_KEY_UPPER_CHARACTER: char = '\u{FFFF}';
6264

65+
/// An [`OwnedEventId`] constructed with [`INDEXED_KEY_LOWER_CHARACTER`].
66+
///
67+
/// This value is useful for constructing a key range over all [`Event`]s when
68+
/// used in conjunction with [`INDEXED_KEY_UPPER_EVENT_ID`].
69+
static INDEXED_KEY_LOWER_EVENT_ID: LazyLock<OwnedEventId> = LazyLock::new(|| {
70+
OwnedEventId::try_from(format!("${INDEXED_KEY_LOWER_CHARACTER}")).expect("valid event id")
71+
});
72+
73+
/// An [`OwnedEventId`] constructed with [`INDEXED_KEY_UPPER_CHARACTER`].
74+
///
75+
/// This value is useful for constructing a key range over all [`Event`]s when
76+
/// used in conjunction with [`INDEXED_KEY_LOWER_EVENT_ID`].
77+
static INDEXED_KEY_UPPER_EVENT_ID: LazyLock<OwnedEventId> = LazyLock::new(|| {
78+
OwnedEventId::try_from(format!("${INDEXED_KEY_UPPER_CHARACTER}")).expect("valid event id")
79+
});
80+
6381
/// Representation of a range of keys of type `K`. This is loosely
6482
/// correlated with [IDBKeyRange][1], with a few differences.
6583
///
@@ -315,7 +333,7 @@ impl Indexed for Event {
315333
serializer: &IndexeddbSerializer,
316334
) -> Result<Self::IndexedType, Self::Error> {
317335
let event_id = self.event_id().ok_or(Self::Error::NoEventId)?;
318-
let id = IndexedEventIdKey::encode(room_id, event_id, serializer);
336+
let id = IndexedEventIdKey::encode(room_id, &event_id, serializer);
319337
let position = self
320338
.position()
321339
.map(|position| IndexedEventPositionKey::encode(room_id, position, serializer));
@@ -348,9 +366,9 @@ impl Indexed for Event {
348366
pub struct IndexedEventIdKey(IndexedRoomId, IndexedEventId);
349367

350368
impl IndexedKey<Event> for IndexedEventIdKey {
351-
type KeyComponents<'a> = OwnedEventId;
369+
type KeyComponents<'a> = &'a EventId;
352370

353-
fn encode(room_id: &RoomId, event_id: OwnedEventId, serializer: &IndexeddbSerializer) -> Self {
371+
fn encode(room_id: &RoomId, event_id: &EventId, serializer: &IndexeddbSerializer) -> Self {
354372
let room_id = serializer.encode_key_as_string(keys::ROOMS, room_id);
355373
let event_id = serializer.encode_key_as_string(keys::EVENTS, event_id);
356374
Self(room_id, event_id)
@@ -359,11 +377,11 @@ impl IndexedKey<Event> for IndexedEventIdKey {
359377

360378
impl IndexedKeyComponentBounds<Event> for IndexedEventIdKey {
361379
fn lower_key_components() -> Self::KeyComponents<'static> {
362-
OwnedEventId::try_from(format!("${INDEXED_KEY_LOWER_CHARACTER}")).expect("valid event id")
380+
&*INDEXED_KEY_LOWER_EVENT_ID
363381
}
364382

365383
fn upper_key_components() -> Self::KeyComponents<'static> {
366-
OwnedEventId::try_from(format!("${INDEXED_KEY_UPPER_CHARACTER}")).expect("valid event id")
384+
&*INDEXED_KEY_UPPER_EVENT_ID
367385
}
368386
}
369387

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use matrix_sdk_base::{
1717
event_cache::{store::EventCacheStoreError, Event as RawEvent, Gap as RawGap},
1818
linked_chunk::{ChunkContent, ChunkIdentifier, RawChunk},
1919
};
20-
use ruma::{events::relation::RelationType, OwnedEventId, RoomId};
20+
use ruma::{events::relation::RelationType, EventId, OwnedEventId, RoomId};
2121
use serde::{
2222
de::{DeserializeOwned, Error},
2323
Serialize,
@@ -571,7 +571,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
571571
pub async fn get_event_by_id(
572572
&self,
573573
room_id: &RoomId,
574-
event_id: OwnedEventId,
574+
event_id: &EventId,
575575
) -> Result<Option<Event>, IndexeddbEventCacheStoreTransactionError> {
576576
let key = self.serializer.encode_key(room_id, event_id);
577577
self.get_item_by_key::<Event, IndexedEventIdKey>(room_id, key).await

0 commit comments

Comments
 (0)