@@ -82,6 +82,22 @@ pub struct AssetEncoding {
82
82
}
83
83
84
84
impl AssetEncoding {
85
+ fn estimate_size ( & self ) -> usize {
86
+ let mut size = 0 ;
87
+ size += 8 ; // modified
88
+ size += self . total_length + self . content_chunks . len ( ) * 4 ;
89
+ size += 5 ; // total_length
90
+ size += 1 ; // certified
91
+ size += self . sha256 . len ( ) ;
92
+ size += 1 + self
93
+ . certificate_expression
94
+ . as_ref ( )
95
+ . map_or ( 0 , |ce| 2 + ce. expression . len ( ) + ce. expression_hash . len ( ) ) ;
96
+ size += 1 + self . response_hashes . as_ref ( ) . map_or ( 0 , |hashes| {
97
+ hashes. iter ( ) . fold ( 2 , |acc, ( _k, v) | acc + 2 + v. len ( ) )
98
+ } ) ;
99
+ size
100
+ }
85
101
fn asset_hash_path_v2 ( & self , path : & AssetPath , status_code : u16 ) -> Option < HashTreePath > {
86
102
self . certificate_expression . as_ref ( ) . and_then ( |ce| {
87
103
self . response_hashes . as_ref ( ) . and_then ( |hashes| {
@@ -206,6 +222,25 @@ pub struct Configuration {
206
222
pub max_bytes : Option < u64 > ,
207
223
}
208
224
225
+ impl Configuration {
226
+ fn estimate_size ( & self ) -> usize {
227
+ 1 + self
228
+ . max_batches
229
+ . as_ref ( )
230
+ . map_or ( 0 , |_| std:: mem:: size_of :: < u64 > ( ) )
231
+ + 1
232
+ + self
233
+ . max_chunks
234
+ . as_ref ( )
235
+ . map_or ( 0 , |_| std:: mem:: size_of :: < u64 > ( ) )
236
+ + 1
237
+ + self
238
+ . max_bytes
239
+ . as_ref ( )
240
+ . map_or ( 0 , |_| std:: mem:: size_of :: < u64 > ( ) )
241
+ }
242
+ }
243
+
209
244
#[ derive( Default ) ]
210
245
pub struct State {
211
246
assets : HashMap < AssetKey , Asset > ,
@@ -232,6 +267,16 @@ pub struct StableStatePermissions {
232
267
manage_permissions : BTreeSet < Principal > ,
233
268
}
234
269
270
+ impl StableStatePermissions {
271
+ fn estimate_size ( & self ) -> usize {
272
+ 8 + self . commit . len ( ) * std:: mem:: size_of :: < Principal > ( )
273
+ + 8
274
+ + self . prepare . len ( ) * std:: mem:: size_of :: < Principal > ( )
275
+ + 8
276
+ + self . manage_permissions . len ( ) * std:: mem:: size_of :: < Principal > ( )
277
+ }
278
+ }
279
+
235
280
#[ derive( Clone , Debug , CandidType , Deserialize ) ]
236
281
pub struct StableState {
237
282
authorized : Vec < Principal > , // ignored if permissions is Some(_)
@@ -242,7 +287,46 @@ pub struct StableState {
242
287
configuration : Option < Configuration > ,
243
288
}
244
289
290
+ impl StableState {
291
+ pub fn estimate_size ( & self ) -> usize {
292
+ let mut size = 0 ;
293
+ size += 2 + self . authorized . len ( ) * std:: mem:: size_of :: < Principal > ( ) ;
294
+ size += 1 + self . permissions . as_ref ( ) . map_or ( 0 , |p| p. estimate_size ( ) ) ;
295
+ size += self . stable_assets . iter ( ) . fold ( 2 , |acc, ( name, asset) | {
296
+ acc + 2 + name. len ( ) + asset. estimate_size ( )
297
+ } ) ;
298
+ size += 1 + self . next_batch_id . as_ref ( ) . map_or ( 0 , |_| 8 ) ;
299
+ size += 1 + self . configuration . as_ref ( ) . map_or ( 0 , |c| c. estimate_size ( ) ) ;
300
+ size
301
+ }
302
+ }
303
+
245
304
impl Asset {
305
+ fn estimate_size ( & self ) -> usize {
306
+ let mut size = 0 ;
307
+ size += 1 + self . content_type . len ( ) ;
308
+ size += self . encodings . iter ( ) . fold ( 1 , |acc, ( name, encoding) | {
309
+ acc + 1 + name. len ( ) + encoding. estimate_size ( )
310
+ } ) ;
311
+ size += 1 + self
312
+ . max_age
313
+ . as_ref ( )
314
+ . map_or ( 0 , |_| std:: mem:: size_of :: < u64 > ( ) ) ;
315
+ size += 1 + self . headers . as_ref ( ) . map_or ( 0 , |hm| {
316
+ hm. iter ( )
317
+ . fold ( 2 , |acc, ( k, v) | acc + 1 + k. len ( ) + 2 + v. len ( ) )
318
+ } ) ;
319
+ size += 1 + self
320
+ . is_aliased
321
+ . as_ref ( )
322
+ . map_or ( 0 , |_| std:: mem:: size_of :: < bool > ( ) ) ;
323
+ size += 1 + self
324
+ . allow_raw_access
325
+ . as_ref ( )
326
+ . map_or ( 0 , |_| std:: mem:: size_of :: < bool > ( ) ) ;
327
+ size
328
+ }
329
+
246
330
fn allow_raw_access ( & self ) -> bool {
247
331
self . allow_raw_access . unwrap_or ( true )
248
332
}
0 commit comments