@@ -42,9 +42,12 @@ use thiserror::Error;
42
42
use crate :: {
43
43
event_cache_store:: {
44
44
migrations:: current:: keys,
45
- serializer:: traits:: {
46
- Indexed , IndexedKey , IndexedKeyBounds , IndexedKeyComponentBounds ,
47
- IndexedPrefixKeyBounds , IndexedPrefixKeyComponentBounds ,
45
+ serializer:: {
46
+ foreign:: ignore_media_retention_policy,
47
+ traits:: {
48
+ Indexed , IndexedKey , IndexedKeyBounds , IndexedKeyComponentBounds ,
49
+ IndexedPrefixKeyBounds , IndexedPrefixKeyComponentBounds ,
50
+ } ,
48
51
} ,
49
52
types:: { Chunk , Event , Gap , Lease , Media , Position } ,
50
53
} ,
@@ -290,6 +293,10 @@ pub type IndexedMediaMetadata = MaybeEncrypted;
290
293
/// A (possibly) encrypted representation of [`Media::content`]
291
294
pub type IndexedMediaContent = Vec < u8 > ;
292
295
296
+ /// A representation of the size in bytes of the [`IndexedMediaContent`] which
297
+ /// is suitable for use in an IndexedDB key
298
+ pub type IndexedMediaContentSize = usize ;
299
+
293
300
/// Represents the [`LEASES`][1] object store.
294
301
///
295
302
/// [1]: crate::event_cache_store::migrations::v1::create_lease_object_store
@@ -900,6 +907,9 @@ impl IndexedKey<MediaRetentionPolicy> for IndexedCoreIdKey {
900
907
pub struct IndexedMedia {
901
908
/// The primary key of the object store
902
909
pub id : IndexedMediaIdKey ,
910
+ /// The size (in bytes) of the media content and whether to ignore the
911
+ /// [`MediaRetentionPolicy`]
912
+ pub content_size : IndexedMediaContentSizeKey ,
903
913
/// The (possibly) encrypted metadata - i.e., [`MediaMetadata`][1]
904
914
///
905
915
/// [1]: crate::event_cache_store::types::MediaMetadata
@@ -934,6 +944,10 @@ impl Indexed for Media {
934
944
& self . metadata . request_parameters ,
935
945
serializer,
936
946
) ,
947
+ content_size : IndexedMediaContentSizeKey :: encode (
948
+ ( self . metadata . ignore_policy , content. len ( ) ) ,
949
+ serializer,
950
+ ) ,
937
951
metadata : serializer. maybe_encrypt_value ( & self . metadata ) ?,
938
952
content,
939
953
} )
@@ -966,3 +980,66 @@ impl IndexedKey<Media> for IndexedMediaIdKey {
966
980
Self ( serializer. encode_key_as_string ( keys:: MEDIA , components. unique_key ( ) ) )
967
981
}
968
982
}
983
+
984
+ /// The value associated with the [`content_size`](IndexedMedia::content_size)
985
+ /// index of the [`MEDIA`][1] object store, which is constructed from:
986
+ ///
987
+ /// - The value of [`IgnoreMediaRetentionPolicy`]
988
+ /// - The size in bytes of the associated [`IndexedMedia::content`]
989
+ ///
990
+ /// [1]: crate::event_cache_store::migrations::v1::create_media_object_store
991
+ #[ derive( Debug , Serialize , Deserialize ) ]
992
+ pub struct IndexedMediaContentSizeKey (
993
+ #[ serde( with = "ignore_media_retention_policy" ) ] IgnoreMediaRetentionPolicy ,
994
+ IndexedMediaContentSize ,
995
+ ) ;
996
+
997
+ impl IndexedMediaContentSizeKey {
998
+ /// Returns whether the associated [`IndexedMedia`] record should ignore the
999
+ /// global [`MediaRetentionPolicy`]
1000
+ pub fn ignore_policy ( & self ) -> bool {
1001
+ self . 0 . is_yes ( )
1002
+ }
1003
+
1004
+ /// Returns the size in bytes of the associated [`IndexedMedia::content`]
1005
+ pub fn content_size ( & self ) -> usize {
1006
+ self . 1
1007
+ }
1008
+ }
1009
+
1010
+ impl IndexedKey < Media > for IndexedMediaContentSizeKey {
1011
+ type KeyComponents < ' a > = ( IgnoreMediaRetentionPolicy , IndexedMediaContentSize ) ;
1012
+
1013
+ fn encode (
1014
+ ( ignore_policy, content_size) : Self :: KeyComponents < ' _ > ,
1015
+ _: & IndexeddbSerializer ,
1016
+ ) -> Self {
1017
+ Self ( ignore_policy, content_size)
1018
+ }
1019
+ }
1020
+
1021
+ impl IndexedKeyComponentBounds < Media > for IndexedMediaContentSizeKey {
1022
+ fn lower_key_components ( ) -> Self :: KeyComponents < ' static > {
1023
+ Self :: lower_key_components_with_prefix ( IgnoreMediaRetentionPolicy :: No )
1024
+ }
1025
+
1026
+ fn upper_key_components ( ) -> Self :: KeyComponents < ' static > {
1027
+ Self :: lower_key_components_with_prefix ( IgnoreMediaRetentionPolicy :: Yes )
1028
+ }
1029
+ }
1030
+
1031
+ impl < ' a > IndexedPrefixKeyComponentBounds < ' a , Media , IgnoreMediaRetentionPolicy >
1032
+ for IndexedMediaContentSizeKey
1033
+ {
1034
+ fn lower_key_components_with_prefix (
1035
+ prefix : IgnoreMediaRetentionPolicy ,
1036
+ ) -> Self :: KeyComponents < ' a > {
1037
+ ( prefix, IndexedMediaContentSize :: MIN )
1038
+ }
1039
+
1040
+ fn upper_key_components_with_prefix (
1041
+ prefix : IgnoreMediaRetentionPolicy ,
1042
+ ) -> Self :: KeyComponents < ' a > {
1043
+ ( prefix, IndexedMediaContentSize :: MAX )
1044
+ }
1045
+ }
0 commit comments