11mod migration;
2+ mod partition;
23mod run;
34
45pub use migration:: * ;
6+ pub use partition:: * ;
57pub use run:: * ;
68
79use anyhow:: { anyhow, bail} ;
@@ -14,7 +16,7 @@ use sea_orm::{
1416 ConnectionTrait , DatabaseTransaction , DbErr , EntityTrait , ModelTrait , TransactionTrait ,
1517} ;
1618use sea_orm_migration:: { MigrationTrait , SchemaManager } ;
17- use std:: num:: NonZeroUsize ;
19+ use std:: num:: { NonZeroU64 , NonZeroUsize } ;
1820use trustify_common:: id:: Id ;
1921use trustify_entity:: { sbom, source_document} ;
2022use trustify_module_storage:: service:: { StorageBackend , StorageKey , dispatch:: DispatchBackend } ;
@@ -27,7 +29,7 @@ pub enum Sbom {
2729
2830#[ allow( async_fn_in_trait) ]
2931pub trait Document : Sized + Send + Sync {
30- type Model : Send ;
32+ type Model : Partitionable + Send ;
3133
3234 async fn all < C > ( tx : & C ) -> Result < Vec < Self :: Model > , DbErr >
3335 where
@@ -95,14 +97,22 @@ where
9597
9698#[ derive( Clone , Debug , PartialEq , Eq , clap:: Parser ) ]
9799pub struct Options {
100+ /// Number of concurrent documents being processes
98101 #[ arg( long, env = "MIGRATION_DATA_CONCURRENT" , default_value = "5" ) ]
99102 pub concurrent : NonZeroUsize ,
103+
104+ #[ arg( long, env = "MIGRATION_DATA_CURRENT_RUNNER" , default_value = "0" ) ]
105+ pub current : u64 ,
106+ #[ arg( long, env = "MIGRATION_DATA_TOTAL_RUNNER" , default_value = "1" ) ]
107+ pub total : NonZeroU64 ,
100108}
101109
102110impl Default for Options {
103111 fn default ( ) -> Self {
104112 Self {
105113 concurrent : unsafe { NonZeroUsize :: new_unchecked ( 5 ) } ,
114+ current : 0 ,
115+ total : unsafe { NonZeroU64 :: new_unchecked ( 1 ) } ,
106116 }
107117 }
108118}
@@ -128,30 +138,34 @@ impl<'c> DocumentProcessor for SchemaManager<'c> {
128138 where
129139 D : Document ,
130140 {
141+ let partition = Partition :: default ( ) ;
131142 let db = self . get_connection ( ) ;
132143
133144 let tx = db. begin ( ) . await ?;
134145 let all = D :: all ( & tx) . await ?;
135146 drop ( tx) ;
136147
137- stream:: iter ( all)
138- . map ( async |model| {
139- let tx = db. begin ( ) . await ?;
140-
141- let doc = D :: source ( & model, storage, & tx) . await . map_err ( |err| {
142- DbErr :: Migration ( format ! ( "Failed to load source document: {err}" ) )
143- } ) ?;
144- f. call ( doc, model, & tx) . await . map_err ( |err| {
145- DbErr :: Migration ( format ! ( "Failed to process document: {err}" ) )
146- } ) ?;
147-
148- tx. commit ( ) . await ?;
149-
150- Ok :: < _ , DbErr > ( ( ) )
151- } )
152- . buffer_unordered ( options. concurrent . into ( ) )
153- . try_collect :: < Vec < _ > > ( )
154- . await ?;
148+ stream:: iter (
149+ all. into_iter ( )
150+ . filter ( |model| partition. is_selected :: < D > ( & model) ) ,
151+ )
152+ . map ( async |model| {
153+ let tx = db. begin ( ) . await ?;
154+
155+ let doc = D :: source ( & model, storage, & tx) . await . map_err ( |err| {
156+ DbErr :: Migration ( format ! ( "Failed to load source document: {err}" ) )
157+ } ) ?;
158+ f. call ( doc, model, & tx)
159+ . await
160+ . map_err ( |err| DbErr :: Migration ( format ! ( "Failed to process document: {err}" ) ) ) ?;
161+
162+ tx. commit ( ) . await ?;
163+
164+ Ok :: < _ , DbErr > ( ( ) )
165+ } )
166+ . buffer_unordered ( options. concurrent . into ( ) )
167+ . try_collect :: < Vec < _ > > ( )
168+ . await ?;
155169
156170 Ok ( ( ) )
157171 }
0 commit comments