Skip to content

Commit a99df7e

Browse files
mgoldenbergHywan
authored andcommitted
refactor(indexeddb): add indexing trait impls for chunk
Signed-off-by: Michael Goldenberg <m@mgoldenberg.net>
1 parent 3e37f9d commit a99df7e

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ pub mod v1 {
109109

110110
pub mod keys {
111111
pub const CORE: &str = "core";
112+
pub const ROOMS: &str = "rooms";
112113
pub const LINKED_CHUNKS: &str = "linked_chunks";
113114
pub const LINKED_CHUNKS_KEY_PATH: &str = "id";
114115
pub const LINKED_CHUNKS_NEXT: &str = "linked_chunks_next";

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

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,19 @@
2727
//! These types mimic the structure of the object stores and indices created in
2828
//! [`crate::event_cache_store::migrations`].
2929
30+
use matrix_sdk_base::linked_chunk::ChunkIdentifier;
31+
use matrix_sdk_crypto::CryptoStoreError;
32+
use ruma::RoomId;
3033
use serde::{Deserialize, Serialize};
3134

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+
};
3343

3444
/// Represents the [`LINKED_CHUNKS`][1] object store.
3545
///
@@ -46,6 +56,38 @@ pub struct IndexedChunk {
4656
pub content: IndexedChunkContent,
4757
}
4858

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+
4991
/// The value associated with the [primary key](IndexedChunk::id) of the
5092
/// [`LINKED_CHUNKS`][1] object store, which is constructed from:
5193
///
@@ -56,6 +98,34 @@ pub struct IndexedChunk {
5698
#[derive(Debug, Serialize, Deserialize)]
5799
pub struct IndexedChunkIdKey(IndexedRoomId, IndexedChunkId);
58100

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+
59129
pub type IndexedRoomId = String;
60130
pub type IndexedChunkId = u64;
61131
pub type IndexedChunkContent = MaybeEncrypted;
@@ -84,6 +154,41 @@ pub enum IndexedNextChunkIdKey {
84154
Some(IndexedChunkIdKey),
85155
}
86156

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+
87192
/// Represents the [`EVENTS`][1] object store.
88193
///
89194
/// [1]: crate::event_cache_store::migrations::v1::create_events_object_store

0 commit comments

Comments
 (0)