Skip to content

Commit ab9bfb2

Browse files
mgoldenbergHywan
authored andcommitted
refactor(indexeddb): add reusable const and static values to construct key component bounds
Signed-off-by: Michael Goldenberg <[email protected]>
1 parent de5f00f commit ab9bfb2

File tree

1 file changed

+64
-12
lines changed
  • crates/matrix-sdk-indexeddb/src/event_cache_store/serializer

1 file changed

+64
-12
lines changed

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

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,77 @@ const INDEXED_KEY_LOWER_CHARACTER: char = '\u{0000}';
6262
/// [1]: https://en.wikipedia.org/wiki/Plane_(Unicode)#Basic_Multilingual_Plane
6363
const INDEXED_KEY_UPPER_CHARACTER: char = '\u{FFFF}';
6464

65+
/// A [`ChunkIdentifier`] constructed with `0`.
66+
///
67+
/// This value is useful for constructing a key range over all keys which
68+
/// contain [`ChunkIdentifier`]s when used in conjunction with
69+
/// [`INDEXED_KEY_UPPER_CHUNK_IDENTIFIER`].
70+
static INDEXED_KEY_LOWER_CHUNK_IDENTIFIER: LazyLock<ChunkIdentifier> =
71+
LazyLock::new(|| ChunkIdentifier::new(0));
72+
73+
/// A [`ChunkIdentifier`] constructed with [`js_sys::Number::MAX_SAFE_INTEGER`].
74+
///
75+
/// This value is useful for constructing a key range over all keys which
76+
/// contain [`ChunkIdentifier`]s when used in conjunction with
77+
/// [`INDEXED_KEY_LOWER_CHUNK_IDENTIFIER`].
78+
static INDEXED_KEY_UPPER_CHUNK_IDENTIFIER: LazyLock<ChunkIdentifier> =
79+
LazyLock::new(|| ChunkIdentifier::new(js_sys::Number::MAX_SAFE_INTEGER as u64));
80+
6581
/// An [`OwnedEventId`] constructed with [`INDEXED_KEY_LOWER_CHARACTER`].
6682
///
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`].
83+
/// This value is useful for constructing a key range over all keys which
84+
/// contain [`EventId`]s when used in conjunction with
85+
/// [`INDEXED_KEY_UPPER_EVENT_ID`].
6986
static INDEXED_KEY_LOWER_EVENT_ID: LazyLock<OwnedEventId> = LazyLock::new(|| {
7087
OwnedEventId::try_from(format!("${INDEXED_KEY_LOWER_CHARACTER}")).expect("valid event id")
7188
});
7289

7390
/// An [`OwnedEventId`] constructed with [`INDEXED_KEY_UPPER_CHARACTER`].
7491
///
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`].
92+
/// This value is useful for constructing a key range over all keys which
93+
/// contain [`EventId`]s when used in conjunction with
94+
/// [`INDEXED_KEY_LOWER_EVENT_ID`].
7795
static INDEXED_KEY_UPPER_EVENT_ID: LazyLock<OwnedEventId> = LazyLock::new(|| {
7896
OwnedEventId::try_from(format!("${INDEXED_KEY_UPPER_CHARACTER}")).expect("valid event id")
7997
});
8098

99+
/// The lowest possible index that can be used to reference an [`Event`] inside
100+
/// a [`Chunk`] - i.e., `0`.
101+
///
102+
/// This value is useful for constructing a key range over all keys which
103+
/// contain [`Position`]s when used in conjunction with
104+
/// [`INDEXED_KEY_UPPER_EVENT_INDEX`].
105+
const INDEXED_KEY_LOWER_EVENT_INDEX: usize = 0;
106+
107+
/// The highest possible index that can be used to reference an [`Event`] inside
108+
/// a [`Chunk`] - i.e., [`js_sys::Number::MAX_SAFE_INTEGER`].
109+
///
110+
/// This value is useful for constructing a key range over all keys which
111+
/// contain [`Position`]s when used in conjunction with
112+
/// [`INDEXED_KEY_LOWER_EVENT_INDEX`].
113+
const INDEXED_KEY_UPPER_EVENT_INDEX: usize = js_sys::Number::MAX_SAFE_INTEGER as usize;
114+
115+
/// The lowest possible [`Position`] that can be used to reference an [`Event`].
116+
///
117+
/// This value is useful for constructing a key range over all keys which
118+
/// contain [`Position`]s when used in conjunction with
119+
/// [`INDEXED_KEY_UPPER_EVENT_INDEX`].
120+
static INDEXED_KEY_LOWER_EVENT_POSITION: LazyLock<Position> = LazyLock::new(|| Position {
121+
chunk_identifier: INDEXED_KEY_LOWER_CHUNK_IDENTIFIER.index(),
122+
index: INDEXED_KEY_LOWER_EVENT_INDEX,
123+
});
124+
125+
/// The highest possible [`Position`] that can be used to reference an
126+
/// [`Event`].
127+
///
128+
/// This value is useful for constructing a key range over all keys which
129+
/// contain [`Position`]s when used in conjunction with
130+
/// [`INDEXED_KEY_LOWER_EVENT_INDEX`].
131+
static INDEXED_KEY_UPPER_EVENT_POSITION: LazyLock<Position> = LazyLock::new(|| Position {
132+
chunk_identifier: INDEXED_KEY_UPPER_CHUNK_IDENTIFIER.index(),
133+
index: INDEXED_KEY_UPPER_EVENT_INDEX,
134+
});
135+
81136
/// Representation of a range of keys of type `K`. This is loosely
82137
/// correlated with [IDBKeyRange][1], with a few differences.
83138
///
@@ -221,11 +276,11 @@ impl IndexedKey<Chunk> for IndexedChunkIdKey {
221276

222277
impl IndexedKeyComponentBounds<Chunk> for IndexedChunkIdKey {
223278
fn lower_key_components() -> Self::KeyComponents<'static> {
224-
ChunkIdentifier::new(0)
279+
*INDEXED_KEY_LOWER_CHUNK_IDENTIFIER
225280
}
226281

227282
fn upper_key_components() -> Self::KeyComponents<'static> {
228-
ChunkIdentifier::new(js_sys::Number::MAX_SAFE_INTEGER as u64)
283+
*INDEXED_KEY_UPPER_CHUNK_IDENTIFIER
229284
}
230285
}
231286

@@ -292,7 +347,7 @@ impl IndexedKeyComponentBounds<Chunk> for IndexedNextChunkIdKey {
292347
}
293348

294349
fn upper_key_components() -> Self::KeyComponents<'static> {
295-
Some(ChunkIdentifier::new(js_sys::Number::MAX_SAFE_INTEGER as u64))
350+
Some(*INDEXED_KEY_UPPER_CHUNK_IDENTIFIER)
296351
}
297352
}
298353

@@ -411,14 +466,11 @@ impl IndexedKey<Event> for IndexedEventPositionKey {
411466

412467
impl IndexedKeyComponentBounds<Event> for IndexedEventPositionKey {
413468
fn lower_key_components() -> Self::KeyComponents<'static> {
414-
Position { chunk_identifier: 0, index: 0 }
469+
*INDEXED_KEY_LOWER_EVENT_POSITION
415470
}
416471

417472
fn upper_key_components() -> Self::KeyComponents<'static> {
418-
Position {
419-
chunk_identifier: js_sys::Number::MAX_SAFE_INTEGER as u64,
420-
index: js_sys::Number::MAX_SAFE_INTEGER as usize,
421-
}
473+
*INDEXED_KEY_UPPER_EVENT_POSITION
422474
}
423475
}
424476

0 commit comments

Comments
 (0)