27
27
//! These types mimic the structure of the object stores and indices created in
28
28
//! [`crate::event_cache_store::migrations`].
29
29
30
+ use std:: sync:: LazyLock ;
31
+
30
32
use matrix_sdk_base:: linked_chunk:: ChunkIdentifier ;
31
33
use matrix_sdk_crypto:: CryptoStoreError ;
32
34
use ruma:: { events:: relation:: RelationType , EventId , OwnedEventId , RoomId } ;
@@ -60,6 +62,22 @@ const INDEXED_KEY_LOWER_CHARACTER: char = '\u{0000}';
60
62
/// [1]: https://en.wikipedia.org/wiki/Plane_(Unicode)#Basic_Multilingual_Plane
61
63
const INDEXED_KEY_UPPER_CHARACTER : char = '\u{FFFF}' ;
62
64
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
+
63
81
/// Representation of a range of keys of type `K`. This is loosely
64
82
/// correlated with [IDBKeyRange][1], with a few differences.
65
83
///
@@ -315,7 +333,7 @@ impl Indexed for Event {
315
333
serializer : & IndexeddbSerializer ,
316
334
) -> Result < Self :: IndexedType , Self :: Error > {
317
335
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) ;
319
337
let position = self
320
338
. position ( )
321
339
. map ( |position| IndexedEventPositionKey :: encode ( room_id, position, serializer) ) ;
@@ -348,9 +366,9 @@ impl Indexed for Event {
348
366
pub struct IndexedEventIdKey ( IndexedRoomId , IndexedEventId ) ;
349
367
350
368
impl IndexedKey < Event > for IndexedEventIdKey {
351
- type KeyComponents < ' a > = OwnedEventId ;
369
+ type KeyComponents < ' a > = & ' a EventId ;
352
370
353
- fn encode ( room_id : & RoomId , event_id : OwnedEventId , serializer : & IndexeddbSerializer ) -> Self {
371
+ fn encode ( room_id : & RoomId , event_id : & EventId , serializer : & IndexeddbSerializer ) -> Self {
354
372
let room_id = serializer. encode_key_as_string ( keys:: ROOMS , room_id) ;
355
373
let event_id = serializer. encode_key_as_string ( keys:: EVENTS , event_id) ;
356
374
Self ( room_id, event_id)
@@ -359,11 +377,11 @@ impl IndexedKey<Event> for IndexedEventIdKey {
359
377
360
378
impl IndexedKeyComponentBounds < Event > for IndexedEventIdKey {
361
379
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
363
381
}
364
382
365
383
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
367
385
}
368
386
}
369
387
0 commit comments