Skip to content

Commit 1c2efb0

Browse files
committed
refactor(indexeddb): add migrations and types for media retention metadata index
Signed-off-by: Michael Goldenberg <[email protected]>
1 parent e961507 commit 1c2efb0

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ pub mod v1 {
139139
pub const MEDIA_CONTENT_SIZE_KEY_PATH: &str = "content_size";
140140
pub const MEDIA_LAST_ACCESS: &str = "media_last_access";
141141
pub const MEDIA_LAST_ACCESS_KEY_PATH: &str = "last_access";
142+
pub const MEDIA_RETENTION_METADATA: &str = "media_retention_metadata";
143+
pub const MEDIA_RETENTION_METADATA_KEY_PATH: &str = "retention_metadata";
142144
}
143145

144146
/// Create all object stores and indices for v1 database
@@ -237,6 +239,8 @@ pub mod v1 {
237239
/// whether to ignore the [`MediaRetentionPolicy`][2]
238240
/// * Index - `last_access` - tracks the last time the associated media was
239241
/// accessed
242+
/// * Index - `retention_metadata` - tracks all retention metadata - i.e.,
243+
/// joins `content_size` and `last_access`
240244
///
241245
/// [1]: ruma::events::room::MediaSource
242246
/// [2]: matrix_sdk_base::event_cache::store::media::MediaRetentionPolicy
@@ -247,6 +251,10 @@ pub mod v1 {
247251
media.create_index(keys::MEDIA_SOURCE, &keys::MEDIA_SOURCE_KEY_PATH.into())?;
248252
media.create_index(keys::MEDIA_CONTENT_SIZE, &keys::MEDIA_CONTENT_SIZE_KEY_PATH.into())?;
249253
media.create_index(keys::MEDIA_LAST_ACCESS, &keys::MEDIA_LAST_ACCESS_KEY_PATH.into())?;
254+
media.create_index(
255+
keys::MEDIA_RETENTION_METADATA,
256+
&keys::MEDIA_RETENTION_METADATA_KEY_PATH.into(),
257+
)?;
250258
Ok(())
251259
}
252260
}

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

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,23 @@ static INDEXED_KEY_UPPER_EVENT_POSITION: LazyLock<Position> = LazyLock::new(|| P
154154
index: INDEXED_KEY_UPPER_EVENT_INDEX,
155155
});
156156

157+
/// An [`IndexedMediaContentSize`] set to it's minimal value - i.e., `0`.
158+
///
159+
/// This value is useful for constructing a key range over all keys which
160+
/// contain [`IndexedMediaContentSize`] values when used in conjunction with
161+
/// [`INDEXED_KEY_UPPER_MEDIA_CONTENT_SIZE`].
162+
const INDEXED_KEY_LOWER_MEDIA_CONTENT_SIZE: IndexedMediaContentSize = 0;
163+
164+
/// An [`IndexedMediaContentSize`] set to [`js_sys::Number::MAX_SAFE_INTEGER`].
165+
/// Note that this restricts the size of [`IndexedMedia::content`], which
166+
/// ultimately restricts the size of [`Media::content`].
167+
///
168+
/// This value is useful for constructing a key range over all keys which
169+
/// contain [`IndexedMediaContentSize`] values when used in conjunction with
170+
/// [`INDEXED_KEY_LOWER_MEDIA_CONTENT_SIZE`].
171+
const INDEXED_KEY_UPPER_MEDIA_CONTENT_SIZE: IndexedMediaContentSize =
172+
js_sys::Number::MAX_SAFE_INTEGER as usize;
173+
157174
/// The minimum possible [`Duration`].
158175
///
159176
/// This value is useful for constructing a key range over all keys which
@@ -1086,13 +1103,13 @@ impl<'a> IndexedPrefixKeyComponentBounds<'a, Media, IgnoreMediaRetentionPolicy>
10861103
fn lower_key_components_with_prefix(
10871104
prefix: IgnoreMediaRetentionPolicy,
10881105
) -> Self::KeyComponents<'a> {
1089-
(prefix, IndexedMediaContentSize::MIN)
1106+
(prefix, INDEXED_KEY_LOWER_MEDIA_CONTENT_SIZE)
10901107
}
10911108

10921109
fn upper_key_components_with_prefix(
10931110
prefix: IgnoreMediaRetentionPolicy,
10941111
) -> Self::KeyComponents<'a> {
1095-
(prefix, IndexedMediaContentSize::MAX)
1112+
(prefix, INDEXED_KEY_UPPER_MEDIA_CONTENT_SIZE)
10961113
}
10971114
}
10981115

@@ -1146,3 +1163,57 @@ impl<'a> IndexedPrefixKeyComponentBounds<'a, Media, IgnoreMediaRetentionPolicy>
11461163
(prefix, INDEXED_KEY_UPPER_DURATION_SECONDS)
11471164
}
11481165
}
1166+
1167+
/// The value associated with the
1168+
/// [`retention_metadata`](IndexedMedia::retention_metadata) index of the
1169+
/// [`MEDIA`][1] object store, which is constructed from:
1170+
///
1171+
/// - The value of [`IgnoreMediaRetentionPolicy`]
1172+
/// - The last time the associated [`IndexedMedia`] was accessed (in seconds
1173+
/// since the Unix Epoch)
1174+
/// - The size in bytes of the associated [`IndexedMedia::content`]
1175+
///
1176+
/// [1]: crate::event_cache_store::migrations::v1::create_media_object_store
1177+
#[derive(Debug, Serialize, Deserialize)]
1178+
pub struct IndexedMediaRetentionMetadataKey(
1179+
#[serde(with = "ignore_media_retention_policy")] IgnoreMediaRetentionPolicy,
1180+
IndexedSecondsSinceUnixEpoch,
1181+
IndexedMediaContentSize,
1182+
);
1183+
1184+
impl IndexedKey<Media> for IndexedMediaRetentionMetadataKey {
1185+
type KeyComponents<'a> = (IgnoreMediaRetentionPolicy, Duration, IndexedMediaContentSize);
1186+
1187+
fn encode(
1188+
(ignore_policy, last_access, content_size): Self::KeyComponents<'_>,
1189+
_: &IndexeddbSerializer,
1190+
) -> Self {
1191+
Self(ignore_policy, last_access.as_secs(), content_size)
1192+
}
1193+
}
1194+
1195+
impl IndexedKeyComponentBounds<Media> for IndexedMediaRetentionMetadataKey {
1196+
fn lower_key_components() -> Self::KeyComponents<'static> {
1197+
Self::lower_key_components_with_prefix(IgnoreMediaRetentionPolicy::No)
1198+
}
1199+
1200+
fn upper_key_components() -> Self::KeyComponents<'static> {
1201+
Self::lower_key_components_with_prefix(IgnoreMediaRetentionPolicy::Yes)
1202+
}
1203+
}
1204+
1205+
impl<'a> IndexedPrefixKeyComponentBounds<'a, Media, IgnoreMediaRetentionPolicy>
1206+
for IndexedMediaRetentionMetadataKey
1207+
{
1208+
fn lower_key_components_with_prefix(
1209+
prefix: IgnoreMediaRetentionPolicy,
1210+
) -> Self::KeyComponents<'a> {
1211+
(prefix, INDEXED_KEY_LOWER_DURATION, INDEXED_KEY_LOWER_MEDIA_CONTENT_SIZE)
1212+
}
1213+
1214+
fn upper_key_components_with_prefix(
1215+
prefix: IgnoreMediaRetentionPolicy,
1216+
) -> Self::KeyComponents<'a> {
1217+
(prefix, INDEXED_KEY_UPPER_DURATION_SECONDS, INDEXED_KEY_UPPER_MEDIA_CONTENT_SIZE)
1218+
}
1219+
}

0 commit comments

Comments
 (0)