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 matrix_sdk_base:: linked_chunk:: ChunkIdentifier ;
31
+ use matrix_sdk_crypto:: CryptoStoreError ;
32
+ use ruma:: RoomId ;
30
33
use serde:: { Deserialize , Serialize } ;
31
34
32
- use crate :: serializer:: MaybeEncrypted ;
35
+ use crate :: {
36
+ event_cache_store:: {
37
+ migrations:: current:: keys,
38
+ serializer:: traits:: { Indexed , IndexedKey , IndexedKeyBounds } ,
39
+ types:: Chunk ,
40
+ } ,
41
+ serializer:: { IndexeddbSerializer , MaybeEncrypted } ,
42
+ } ;
33
43
34
44
/// Represents the [`LINKED_CHUNKS`][1] object store.
35
45
///
@@ -46,6 +56,38 @@ pub struct IndexedChunk {
46
56
pub content : IndexedChunkContent ,
47
57
}
48
58
59
+ impl Indexed for Chunk {
60
+ type IndexedType = IndexedChunk ;
61
+ type Error = CryptoStoreError ;
62
+
63
+ fn to_indexed (
64
+ & self ,
65
+ room_id : & RoomId ,
66
+ serializer : & IndexeddbSerializer ,
67
+ ) -> Result < Self :: IndexedType , Self :: Error > {
68
+ Ok ( IndexedChunk {
69
+ id : <IndexedChunkIdKey as IndexedKey < Chunk > >:: encode (
70
+ room_id,
71
+ & ChunkIdentifier :: new ( self . identifier ) ,
72
+ serializer,
73
+ ) ,
74
+ next : IndexedNextChunkIdKey :: encode (
75
+ room_id,
76
+ & self . next . map ( ChunkIdentifier :: new) ,
77
+ serializer,
78
+ ) ,
79
+ content : serializer. maybe_encrypt_value ( self ) ?,
80
+ } )
81
+ }
82
+
83
+ fn from_indexed (
84
+ indexed : Self :: IndexedType ,
85
+ serializer : & IndexeddbSerializer ,
86
+ ) -> Result < Self , Self :: Error > {
87
+ serializer. maybe_decrypt_value ( indexed. content )
88
+ }
89
+ }
90
+
49
91
/// The value associated with the [primary key](IndexedChunk::id) of the
50
92
/// [`LINKED_CHUNKS`][1] object store, which is constructed from:
51
93
///
@@ -56,6 +98,34 @@ pub struct IndexedChunk {
56
98
#[ derive( Debug , Serialize , Deserialize ) ]
57
99
pub struct IndexedChunkIdKey ( IndexedRoomId , IndexedChunkId ) ;
58
100
101
+ impl IndexedKey < Chunk > for IndexedChunkIdKey {
102
+ type KeyComponents = ChunkIdentifier ;
103
+
104
+ fn encode (
105
+ room_id : & RoomId ,
106
+ chunk_id : & ChunkIdentifier ,
107
+ serializer : & IndexeddbSerializer ,
108
+ ) -> Self {
109
+ let room_id = serializer. encode_key_as_string ( keys:: ROOMS , room_id) ;
110
+ let chunk_id = chunk_id. index ( ) ;
111
+ Self ( room_id, chunk_id)
112
+ }
113
+ }
114
+
115
+ impl IndexedKeyBounds < Chunk > for IndexedChunkIdKey {
116
+ fn encode_lower ( room_id : & RoomId , serializer : & IndexeddbSerializer ) -> Self {
117
+ <Self as IndexedKey < Chunk > >:: encode ( room_id, & ChunkIdentifier :: new ( 0 ) , serializer)
118
+ }
119
+
120
+ fn encode_upper ( room_id : & RoomId , serializer : & IndexeddbSerializer ) -> Self {
121
+ <Self as IndexedKey < Chunk > >:: encode (
122
+ room_id,
123
+ & ChunkIdentifier :: new ( js_sys:: Number :: MAX_SAFE_INTEGER as u64 ) ,
124
+ serializer,
125
+ )
126
+ }
127
+ }
128
+
59
129
pub type IndexedRoomId = String ;
60
130
pub type IndexedChunkId = u64 ;
61
131
pub type IndexedChunkContent = MaybeEncrypted ;
@@ -84,6 +154,41 @@ pub enum IndexedNextChunkIdKey {
84
154
Some ( IndexedChunkIdKey ) ,
85
155
}
86
156
157
+ impl IndexedKey < Chunk > for IndexedNextChunkIdKey {
158
+ type KeyComponents = Option < ChunkIdentifier > ;
159
+
160
+ fn encode (
161
+ room_id : & RoomId ,
162
+ next_chunk_id : & Option < ChunkIdentifier > ,
163
+ serializer : & IndexeddbSerializer ,
164
+ ) -> Self {
165
+ next_chunk_id
166
+ . map ( |id| {
167
+ Self :: Some ( <IndexedChunkIdKey as IndexedKey < Chunk > >:: encode (
168
+ room_id, & id, serializer,
169
+ ) )
170
+ } )
171
+ . unwrap_or_else ( || {
172
+ let room_id = serializer. encode_key_as_string ( keys:: ROOMS , room_id) ;
173
+ Self :: none ( room_id)
174
+ } )
175
+ }
176
+ }
177
+
178
+ impl IndexedKeyBounds < Chunk > for IndexedNextChunkIdKey {
179
+ fn encode_lower ( room_id : & RoomId , serializer : & IndexeddbSerializer ) -> Self {
180
+ <Self as IndexedKey < Chunk > >:: encode ( room_id, & None , serializer)
181
+ }
182
+
183
+ fn encode_upper ( room_id : & RoomId , serializer : & IndexeddbSerializer ) -> Self {
184
+ <Self as IndexedKey < Chunk > >:: encode (
185
+ room_id,
186
+ & Some ( ChunkIdentifier :: new ( js_sys:: Number :: MAX_SAFE_INTEGER as u64 ) ) ,
187
+ serializer,
188
+ )
189
+ }
190
+ }
191
+
87
192
/// Represents the [`EVENTS`][1] object store.
88
193
///
89
194
/// [1]: crate::event_cache_store::migrations::v1::create_events_object_store
0 commit comments