Skip to content

Commit c6854a5

Browse files
mgoldenbergHywan
authored andcommitted
refactor(indexeddb): add support for indexing time-based locks in event cache
Signed-off-by: Michael Goldenberg <[email protected]>
1 parent fb56395 commit c6854a5

File tree

1 file changed

+73
-1
lines changed
  • crates/matrix-sdk-indexeddb/src/event_cache_store/serializer

1 file changed

+73
-1
lines changed

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

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use crate::{
4242
Indexed, IndexedKey, IndexedKeyBounds, IndexedKeyComponentBounds,
4343
IndexedPrefixKeyBounds, IndexedPrefixKeyComponentBounds,
4444
},
45-
types::{Chunk, Event, Gap, Position},
45+
types::{Chunk, Event, Gap, Lease, Position},
4646
},
4747
serializer::{IndexeddbSerializer, MaybeEncrypted},
4848
};
@@ -65,6 +65,14 @@ const INDEXED_KEY_LOWER_CHARACTER: char = '\u{0000}';
6565
/// [1]: https://en.wikipedia.org/wiki/Plane_(Unicode)#Basic_Multilingual_Plane
6666
const INDEXED_KEY_UPPER_CHARACTER: char = '\u{FFFF}';
6767

68+
/// Identical to [`INDEXED_KEY_LOWER_CHARACTER`] but represented as a [`String`]
69+
static INDEXED_KEY_LOWER_STRING: LazyLock<String> =
70+
LazyLock::new(|| String::from(INDEXED_KEY_LOWER_CHARACTER));
71+
72+
/// Identical to [`INDEXED_KEY_UPPER_CHARACTER`] but represented as a [`String`]
73+
static INDEXED_KEY_UPPER_STRING: LazyLock<String> =
74+
LazyLock::new(|| String::from(INDEXED_KEY_UPPER_CHARACTER));
75+
6876
/// A [`ChunkIdentifier`] constructed with `0`.
6977
///
7078
/// This value is useful for constructing a key range over all keys which
@@ -223,6 +231,70 @@ impl<K> From<K> for IndexedKeyRange<K> {
223231
}
224232
}
225233

234+
/// Represents the [`LEASES`][1] object store.
235+
///
236+
/// [1]: crate::event_cache_store::migrations::v1::create_lease_object_store
237+
#[derive(Debug, Serialize, Deserialize)]
238+
pub struct IndexedLease {
239+
/// The primary key of the object store.
240+
pub id: IndexedLeaseIdKey,
241+
/// The (possibly encrypted) content - i.e., a [`Lease`].
242+
pub content: IndexedLeaseContent,
243+
}
244+
245+
impl Indexed for Lease {
246+
type IndexedType = IndexedLease;
247+
248+
const OBJECT_STORE: &'static str = keys::LEASES;
249+
250+
type Error = CryptoStoreError;
251+
252+
fn to_indexed(
253+
&self,
254+
serializer: &IndexeddbSerializer,
255+
) -> Result<Self::IndexedType, Self::Error> {
256+
Ok(IndexedLease {
257+
id: IndexedLeaseIdKey::encode(&self.key, serializer),
258+
content: serializer.maybe_encrypt_value(self)?,
259+
})
260+
}
261+
262+
fn from_indexed(
263+
indexed: Self::IndexedType,
264+
serializer: &IndexeddbSerializer,
265+
) -> Result<Self, Self::Error> {
266+
serializer.maybe_decrypt_value(indexed.content)
267+
}
268+
}
269+
270+
/// The value associated with the [primary key](IndexedLease::id) of the
271+
/// [`LEASES`][1] object store, which is constructed from the value in
272+
/// [`Lease::key`]. This value may or may not be hashed depending on the
273+
/// provided [`IndexeddbSerializer`].
274+
///
275+
/// [1]: crate::event_cache_store::migrations::v1::create_linked_chunks_object_store
276+
pub type IndexedLeaseIdKey = String;
277+
278+
impl IndexedKey<Lease> for IndexedLeaseIdKey {
279+
type KeyComponents<'a> = &'a str;
280+
281+
fn encode(components: Self::KeyComponents<'_>, serializer: &IndexeddbSerializer) -> Self {
282+
serializer.encode_key_as_string(keys::LEASES, components)
283+
}
284+
}
285+
286+
impl IndexedKeyComponentBounds<Lease> for IndexedLeaseIdKey {
287+
fn lower_key_components() -> Self::KeyComponents<'static> {
288+
INDEXED_KEY_LOWER_STRING.as_str()
289+
}
290+
291+
fn upper_key_components() -> Self::KeyComponents<'static> {
292+
INDEXED_KEY_UPPER_STRING.as_str()
293+
}
294+
}
295+
296+
pub type IndexedLeaseContent = MaybeEncrypted;
297+
226298
/// Represents the [`LINKED_CHUNKS`][1] object store.
227299
///
228300
/// [1]: crate::event_cache_store::migrations::v1::create_linked_chunks_object_store

0 commit comments

Comments
 (0)