@@ -181,7 +181,10 @@ pub struct Item {
181181 pub i_data : String ,
182182}
183183
184- #[ table( accessor = stock) ]
184+ #[ table(
185+ accessor = stock,
186+ index( accessor = by_stock_key_quantity, btree( columns = [ stock_key, s_quantity] ) ) ,
187+ ) ]
185188#[ derive( Clone , Debug ) ]
186189pub struct Stock {
187190 #[ primary_key]
@@ -513,8 +516,7 @@ pub fn stock_level(
513516 let mut low_stock_count = 0u32 ;
514517 let _timer_count= LogStopwatch :: new ( "stock_level_count" ) ;
515518 for item_id in item_ids {
516- let stock = find_stock ( ctx, w_id, item_id) ?;
517- if stock. s_quantity < threshold {
519+ if find_low_stock ( ctx, w_id, item_id, threshold) . is_some ( ) {
518520 low_stock_count += 1 ;
519521 }
520522 }
@@ -792,6 +794,15 @@ fn find_customer_by_id(tx: &ReducerContext, w_id: WarehouseId, d_id: u8, c_id: u
792794 . ok_or_else ( || format ! ( "customer ({w_id}, {d_id}, {c_id}) not found" ) )
793795}
794796
797+ fn find_low_stock ( tx : & ReducerContext , w_id : WarehouseId , item_id : u32 , threshold : i32 ) -> Option < Stock > {
798+ let stock_key = pack_stock_key ( w_id, item_id) ;
799+ tx. db
800+ . stock ( )
801+ . by_stock_key_quantity ( )
802+ . filter ( ( stock_key, 0 ..threshold) )
803+ . next ( )
804+ }
805+
795806fn find_stock ( tx : & ReducerContext , w_id : WarehouseId , item_id : u32 ) -> Result < Stock , String > {
796807 let stock_key = pack_stock_key ( w_id, item_id) ;
797808 tx. db
0 commit comments