Skip to content

Commit 65b9bd2

Browse files
mgoldenbergHywan
authored andcommitted
refactor(indexeddb): express IndexedKeyRange::All as IndexedKeyRange::Bound to loosen constraints on various functions
Signed-off-by: Michael Goldenberg <[email protected]>
1 parent 35505f9 commit 65b9bd2

File tree

3 files changed

+42
-33
lines changed

3 files changed

+42
-33
lines changed

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,14 @@ impl IndexeddbEventCacheStoreSerializer {
106106
) -> Result<IdbKeyRange, serde_wasm_bindgen::Error>
107107
where
108108
T: Indexed,
109-
K: IndexedKeyBounds<T> + Serialize,
109+
K: Serialize,
110110
{
111111
use serde_wasm_bindgen::to_value;
112112
Ok(match range.into() {
113113
IndexedKeyRange::Only(key) => IdbKeyRange::only(&to_value(&key)?)?,
114114
IndexedKeyRange::Bound(lower, upper) => {
115115
IdbKeyRange::bound(&to_value(&lower)?, &to_value(&upper)?)?
116116
}
117-
IndexedKeyRange::All => {
118-
let lower = to_value(&K::lower_key(room_id, &self.inner))?;
119-
let upper = to_value(&K::upper_key(room_id, &self.inner))?;
120-
IdbKeyRange::bound(&lower, &upper).expect("construct key range")
121-
}
122117
})
123118
}
124119

@@ -133,7 +128,7 @@ impl IndexeddbEventCacheStoreSerializer {
133128
) -> Result<IdbKeyRange, serde_wasm_bindgen::Error>
134129
where
135130
T: Indexed,
136-
K: IndexedKeyComponentBounds<T> + Serialize,
131+
K: IndexedKey<T> + Serialize,
137132
{
138133
let range = match range.into() {
139134
IndexedKeyRange::Only(components) => {
@@ -144,11 +139,6 @@ impl IndexeddbEventCacheStoreSerializer {
144139
let upper = K::encode(room_id, upper, &self.inner);
145140
IndexedKeyRange::Bound(lower, upper)
146141
}
147-
IndexedKeyRange::All => {
148-
let lower = K::lower_key(room_id, &self.inner);
149-
let upper = K::upper_key(room_id, &self.inner);
150-
IndexedKeyRange::Bound(lower, upper)
151-
}
152142
};
153143
self.encode_key_range::<T, K>(room_id, range)
154144
}

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,10 @@ static INDEXED_KEY_UPPER_EVENT_POSITION: LazyLock<Position> = LazyLock::new(|| P
136136
/// Representation of a range of keys of type `K`. This is loosely
137137
/// correlated with [IDBKeyRange][1], with a few differences.
138138
///
139-
/// Firstly, this enum only provides a single way to express a bounded range
139+
/// Namely, this enum only provides a single way to express a bounded range
140140
/// which is always inclusive on both bounds. While all ranges can still be
141141
/// represented, [`IDBKeyRange`][1] provides more flexibility in this regard.
142142
///
143-
/// Secondly, this enum provides a way to express the range of all keys
144-
/// of type `K`.
145-
///
146143
/// [1]: https://developer.mozilla.org/en-US/docs/Web/API/IDBKeyRange
147144
#[derive(Debug, Copy, Clone)]
148145
pub enum IndexedKeyRange<K> {
@@ -160,8 +157,6 @@ pub enum IndexedKeyRange<K> {
160157
///
161158
/// [1]: https://developer.mozilla.org/en-US/docs/Web/API/IDBKeyRange/bound
162159
Bound(K, K),
163-
/// Represents an inclusive range of all keys of type `K`.
164-
All,
165160
}
166161

167162
impl<'a, C: 'a> IndexedKeyRange<C> {
@@ -184,11 +179,20 @@ impl<'a, C: 'a> IndexedKeyRange<C> {
184179
K::encode(room_id, lower, serializer),
185180
K::encode(room_id, upper, serializer),
186181
),
187-
Self::All => IndexedKeyRange::All,
188182
}
189183
}
190184
}
191185

186+
impl<K> IndexedKeyRange<K> {
187+
pub fn all<T>(room_id: &RoomId, serializer: &IndexeddbSerializer) -> IndexedKeyRange<K>
188+
where
189+
T: Indexed,
190+
K: IndexedKeyBounds<T>,
191+
{
192+
IndexedKeyRange::Bound(K::lower_key(room_id, serializer), K::upper_key(room_id, serializer))
193+
}
194+
}
195+
192196
impl<K> From<(K, K)> for IndexedKeyRange<K> {
193197
fn from(value: (K, K)) -> Self {
194198
Self::Bound(value.0, value.1)

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

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
111111
T: Indexed,
112112
T::IndexedType: DeserializeOwned,
113113
T::Error: AsyncErrorDeps,
114-
K: IndexedKeyBounds<T> + Serialize,
114+
K: IndexedKey<T> + Serialize,
115115
{
116116
let range = self.serializer.encode_key_range::<T, K>(room_id, range)?;
117117
let object_store = self.transaction.object_store(T::OBJECT_STORE)?;
@@ -141,7 +141,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
141141
T: Indexed + 'b,
142142
T::IndexedType: DeserializeOwned,
143143
T::Error: AsyncErrorDeps,
144-
K: IndexedKeyComponentBounds<T> + Serialize + 'b,
144+
K: IndexedKey<T> + Serialize + 'b,
145145
{
146146
let range: IndexedKeyRange<K> = range.into().encoded(room_id, self.serializer.inner());
147147
self.get_items_by_key::<T, K>(room_id, range).await
@@ -158,7 +158,11 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
158158
T::Error: AsyncErrorDeps,
159159
K: IndexedKeyBounds<T> + Serialize,
160160
{
161-
self.get_items_by_key::<T, K>(room_id, IndexedKeyRange::All).await
161+
self.get_items_by_key::<T, K>(
162+
room_id,
163+
IndexedKeyRange::all(room_id, self.serializer.inner()),
164+
)
165+
.await
162166
}
163167

164168
/// Query IndexedDB for items that match the given key in the given room. If
@@ -172,7 +176,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
172176
T: Indexed,
173177
T::IndexedType: DeserializeOwned,
174178
T::Error: AsyncErrorDeps,
175-
K: IndexedKeyBounds<T> + Serialize,
179+
K: IndexedKey<T> + Serialize,
176180
{
177181
let mut items = self.get_items_by_key::<T, K>(room_id, key).await?;
178182
if items.len() > 1 {
@@ -192,7 +196,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
192196
T: Indexed + 'b,
193197
T::IndexedType: DeserializeOwned,
194198
T::Error: AsyncErrorDeps,
195-
K: IndexedKeyComponentBounds<T> + Serialize + 'b,
199+
K: IndexedKey<T> + Serialize + 'b,
196200
{
197201
let mut items = self.get_items_by_key_components::<T, K>(room_id, components).await?;
198202
if items.len() > 1 {
@@ -212,7 +216,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
212216
T: Indexed,
213217
T::IndexedType: DeserializeOwned,
214218
T::Error: AsyncErrorDeps,
215-
K: IndexedKeyBounds<T> + Serialize,
219+
K: IndexedKey<T> + Serialize,
216220
{
217221
let range = self.serializer.encode_key_range::<T, K>(room_id, range)?;
218222
let object_store = self.transaction.object_store(T::OBJECT_STORE)?;
@@ -235,7 +239,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
235239
T: Indexed + 'b,
236240
T::IndexedType: DeserializeOwned,
237241
T::Error: AsyncErrorDeps,
238-
K: IndexedKeyBounds<T> + Serialize + 'b,
242+
K: IndexedKey<T> + Serialize + 'b,
239243
{
240244
let range: IndexedKeyRange<K> = range.into().encoded(room_id, self.serializer.inner());
241245
self.get_items_count_by_key::<T, K>(room_id, range).await
@@ -252,7 +256,11 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
252256
T::Error: AsyncErrorDeps,
253257
K: IndexedKeyBounds<T> + Serialize,
254258
{
255-
self.get_items_count_by_key::<T, K>(room_id, IndexedKeyRange::All).await
259+
self.get_items_count_by_key::<T, K>(
260+
room_id,
261+
IndexedKeyRange::all(room_id, self.serializer.inner()),
262+
)
263+
.await
256264
}
257265

258266
/// Query IndexedDB for the item with the maximum key in the given room.
@@ -264,9 +272,12 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
264272
T: Indexed,
265273
T::IndexedType: DeserializeOwned,
266274
T::Error: AsyncErrorDeps,
267-
K: IndexedKey<T> + IndexedKeyBounds<T> + Serialize,
275+
K: IndexedKeyBounds<T> + Serialize,
268276
{
269-
let range = self.serializer.encode_key_range::<T, K>(room_id, IndexedKeyRange::All)?;
277+
let range = self.serializer.encode_key_range::<T, K>(
278+
room_id,
279+
IndexedKeyRange::all(room_id, self.serializer.inner()),
280+
)?;
270281
let direction = IdbCursorDirection::Prev;
271282
let object_store = self.transaction.object_store(T::OBJECT_STORE)?;
272283
if let Some(index) = K::INDEX {
@@ -339,7 +350,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
339350
) -> Result<(), IndexeddbEventCacheStoreTransactionError>
340351
where
341352
T: Indexed,
342-
K: IndexedKeyBounds<T> + Serialize,
353+
K: IndexedKey<T> + Serialize,
343354
{
344355
let range = self.serializer.encode_key_range::<T, K>(room_id, range)?;
345356
let object_store = self.transaction.object_store(T::OBJECT_STORE)?;
@@ -366,7 +377,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
366377
) -> Result<(), IndexeddbEventCacheStoreTransactionError>
367378
where
368379
T: Indexed + 'b,
369-
K: IndexedKeyBounds<T> + Serialize + 'b,
380+
K: IndexedKey<T> + Serialize + 'b,
370381
{
371382
let range: IndexedKeyRange<K> = range.into().encoded(room_id, self.serializer.inner());
372383
self.delete_items_by_key::<T, K>(room_id, range).await
@@ -381,7 +392,11 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
381392
T: Indexed,
382393
K: IndexedKeyBounds<T> + Serialize,
383394
{
384-
self.delete_items_by_key::<T, K>(room_id, IndexedKeyRange::All).await
395+
self.delete_items_by_key::<T, K>(
396+
room_id,
397+
IndexedKeyRange::all(room_id, self.serializer.inner()),
398+
)
399+
.await
385400
}
386401

387402
/// Delete item that matches the given key components in the given room from
@@ -393,7 +408,7 @@ impl<'a> IndexeddbEventCacheStoreTransaction<'a> {
393408
) -> Result<(), IndexeddbEventCacheStoreTransactionError>
394409
where
395410
T: Indexed + 'b,
396-
K: IndexedKeyBounds<T> + Serialize + 'b,
411+
K: IndexedKey<T> + Serialize + 'b,
397412
{
398413
self.delete_items_by_key_components::<T, K>(room_id, key).await
399414
}

0 commit comments

Comments
 (0)