@@ -7,13 +7,13 @@ use tracing::{debug, info};
77use crate :: ids:: IdsFolloweesRecordV0 ;
88use crate :: {
99 ContentStoreRecordOwned , Database , DbResult , DbVersionTooHighSnafu , EventContentStateNew ,
10- IdSocialProfileRecord , IdsFolloweesRecord , LOG_TARGET , Latest , SocialPostRecord ,
11- WriteTransactionCtx , content_rc, content_store, db_version, events, events_by_time ,
12- events_content, events_content_missing, events_content_state, events_heads, events_missing ,
13- events_self, events_singletons, events_singletons_new, ids_followees , ids_followees_v0 ,
14- ids_followers , ids_full , ids_personas , ids_self , ids_unfollowed , social_posts ,
15- social_posts_by_time , social_posts_reactions , social_posts_replies , social_posts_v0 ,
16- social_profiles, social_profiles_v0,
10+ IdSocialProfileRecord , IdsDataUsageRecord , IdsFolloweesRecord , LOG_TARGET , Latest ,
11+ SocialPostRecord , WriteTransactionCtx , content_rc, content_store, db_version, events,
12+ events_by_time , events_content, events_content_missing, events_content_state, events_heads,
13+ events_missing , events_self, events_singletons, events_singletons_new, ids_data_usage ,
14+ ids_followees , ids_followees_v0 , ids_followers , ids_full , ids_personas , ids_self ,
15+ ids_unfollowed , social_posts , social_posts_by_time , social_posts_reactions ,
16+ social_posts_replies , social_posts_v0 , social_profiles, social_profiles_v0,
1717} ;
1818
1919impl Database {
@@ -26,6 +26,7 @@ impl Database {
2626 tx. open_table ( & ids_followees:: TABLE ) ?;
2727 tx. open_table ( & ids_unfollowed:: TABLE ) ?;
2828 tx. open_table ( & ids_personas:: TABLE ) ?;
29+ tx. open_table ( & ids_data_usage:: TABLE ) ?;
2930
3031 tx. open_table ( & events:: TABLE ) ?;
3132 tx. open_table ( & events_singletons:: TABLE ) ?;
@@ -50,7 +51,7 @@ impl Database {
5051 }
5152
5253 pub ( crate ) fn handle_db_ver_migrations ( dbtx : & WriteTransactionCtx ) -> DbResult < ( ) > {
53- const DB_VER : u64 = 4 ;
54+ const DB_VER : u64 = 5 ;
5455
5556 let mut table_db_ver = dbtx. open_table ( & db_version:: TABLE ) ?;
5657
@@ -76,6 +77,7 @@ impl Database {
7677 1 => Self :: migrate_v1 ( dbtx) ?,
7778 2 => Self :: migrate_v2 ( dbtx) ?,
7879 3 => Self :: migrate_v3 ( dbtx) ?,
80+ 4 => Self :: migrate_v4 ( dbtx) ?,
7981 DB_VER => { /* ensures we didn't forget to increment DB_VER */ }
8082 x => panic ! ( "Unexpected db ver: {x}" ) ,
8183 }
@@ -316,4 +318,60 @@ impl Database {
316318
317319 Ok ( ( ) )
318320 }
321+
322+ /// Migration v4: Calculate initial data usage per identity.
323+ ///
324+ /// Iterates all events to calculate:
325+ /// 1. metadata_size: count of events × EVENT_METADATA_SIZE (192 bytes)
326+ /// 2. content_size: sum of content_len for events in Available state
327+ pub ( crate ) fn migrate_v4 ( dbtx : & WriteTransactionCtx ) -> DbResult < ( ) > {
328+ use std:: collections:: HashMap ;
329+
330+ use rostra_core:: event:: EventExt as _;
331+
332+ /// Size of event metadata in bytes (Event struct + signature).
333+ /// See rostra_core::event::Event documentation.
334+ const EVENT_METADATA_SIZE : u64 = 192 ;
335+
336+ let events_table = dbtx. open_table ( & events:: TABLE ) ?;
337+ let state_table = dbtx. open_table ( & events_content_state:: TABLE ) ?;
338+ let mut usage_table = dbtx. open_table ( & ids_data_usage:: TABLE ) ?;
339+
340+ // Collect usage per author
341+ let mut usage_map: HashMap < rostra_core:: id:: RostraId , IdsDataUsageRecord > = HashMap :: new ( ) ;
342+
343+ for entry in events_table. range ( ..) ? {
344+ let ( event_id, record) = entry?;
345+ let event_id = event_id. value ( ) ;
346+ let record = record. value ( ) ;
347+ let author = record. author ( ) ;
348+
349+ let usage = usage_map. entry ( author) . or_default ( ) ;
350+
351+ // Every event contributes to metadata size
352+ usage. metadata_size += EVENT_METADATA_SIZE ;
353+
354+ // Content only counts if state is Available
355+ if let Some ( state) = state_table. get ( & event_id) ?. map ( |g| g. value ( ) ) {
356+ if matches ! ( state, EventContentStateNew :: Available ) {
357+ usage. content_size += u64:: from ( record. content_len ( ) ) ;
358+ }
359+ }
360+ }
361+
362+ // Write aggregated usage to table
363+ let mut count = 0u64 ;
364+ for ( author, usage) in usage_map {
365+ usage_table. insert ( & author, & usage) ?;
366+ count += 1 ;
367+ }
368+
369+ info ! (
370+ target: LOG_TARGET ,
371+ "Calculated data usage for {} identities" ,
372+ count
373+ ) ;
374+
375+ Ok ( ( ) )
376+ }
319377}
0 commit comments