@@ -261,6 +261,23 @@ impl RocksCacheStore {
261261 } )
262262 . await
263263 }
264+
265+ fn queue_count_by_prefix_and_status (
266+ db_ref : DbTableRef ,
267+ prefix : & Option < String > ,
268+ status : QueueItemStatus ,
269+ ) -> Result < u64 , CubeError > {
270+ let queue_schema = QueueItemRocksTable :: new ( db_ref. clone ( ) ) ;
271+ let index_key =
272+ QueueItemIndexKey :: ByPrefixAndStatus ( prefix. clone ( ) . unwrap_or ( "" . to_string ( ) ) , status) ;
273+ queue_schema. count_rows_by_index ( & index_key, & QueueItemRocksIndex :: ByPrefixAndStatus )
274+ }
275+ }
276+
277+ #[ derive( Clone , Serialize , Deserialize , Debug , Eq , PartialEq , Hash ) ]
278+ pub struct QueueAddResponse {
279+ pub added : bool ,
280+ pub pending : u64 ,
264281}
265282
266283#[ derive( Clone , Serialize , Deserialize , Debug , Eq , PartialEq , Hash ) ]
@@ -285,7 +302,7 @@ pub trait CacheStore: DIService + Send + Sync {
285302
286303 // queue
287304 async fn queue_all ( & self ) -> Result < Vec < IdRow < QueueItem > > , CubeError > ;
288- async fn queue_add ( & self , item : QueueItem ) -> Result < bool , CubeError > ;
305+ async fn queue_add ( & self , item : QueueItem ) -> Result < QueueAddResponse , CubeError > ;
289306 async fn queue_truncate ( & self ) -> Result < ( ) , CubeError > ;
290307 async fn queue_get ( & self , key : String ) -> Result < Option < IdRow < QueueItem > > , CubeError > ;
291308 async fn queue_to_cancel (
@@ -453,19 +470,32 @@ impl CacheStore for RocksCacheStore {
453470 . await
454471 }
455472
456- async fn queue_add ( & self , item : QueueItem ) -> Result < bool , CubeError > {
473+ async fn queue_add ( & self , item : QueueItem ) -> Result < QueueAddResponse , CubeError > {
457474 self . store
458475 . write_operation ( move |db_ref, batch_pipe| {
459476 let queue_schema = QueueItemRocksTable :: new ( db_ref. clone ( ) ) ;
460477 let index_key = QueueItemIndexKey :: ByPath ( item. get_path ( ) ) ;
461478 let id_row_opt = queue_schema
462479 . get_single_opt_row_by_index ( & index_key, & QueueItemRocksIndex :: ByPath ) ?;
463480
464- if id_row_opt. is_none ( ) {
481+ let pending = Self :: queue_count_by_prefix_and_status (
482+ db_ref,
483+ item. get_prefix ( ) ,
484+ QueueItemStatus :: Pending ,
485+ ) ?;
486+
487+ let added = if id_row_opt. is_none ( ) {
465488 queue_schema. insert ( item, batch_pipe) ?;
466- }
467489
468- Ok ( true )
490+ true
491+ } else {
492+ false
493+ } ;
494+
495+ Ok ( QueueAddResponse {
496+ added,
497+ pending : if added { pending + 1 } else { pending } ,
498+ } )
469499 } )
470500 . await
471501 }
@@ -629,26 +659,12 @@ impl CacheStore for RocksCacheStore {
629659
630660 if let Some ( id_row) = id_row_opt {
631661 if id_row. get_row ( ) . get_status ( ) == & QueueItemStatus :: Pending {
632- // TODO: Introduce count + Active index?
633- let index_key = QueueItemIndexKey :: ByPrefix (
634- if let Some ( prefix) = id_row. get_row ( ) . get_prefix ( ) {
635- prefix. clone ( )
636- } else {
637- "" . to_string ( )
638- } ,
639- ) ;
640- let in_queue = queue_schema
641- . get_rows_by_index ( & index_key, & QueueItemRocksIndex :: ByPrefix ) ?;
642-
643- let mut current_active = 0 ;
644-
645- for item in in_queue {
646- if item. get_row ( ) . get_status ( ) == & QueueItemStatus :: Active {
647- current_active += 1 ;
648- }
649- }
650-
651- if current_active >= allow_concurrency {
662+ let current_active = Self :: queue_count_by_prefix_and_status (
663+ db_ref,
664+ id_row. get_row ( ) . get_prefix ( ) ,
665+ QueueItemStatus :: Active ,
666+ ) ?;
667+ if current_active >= ( allow_concurrency as u64 ) {
652668 return Ok ( None ) ;
653669 }
654670
@@ -835,7 +851,7 @@ impl CacheStore for ClusterCacheStoreClient {
835851 panic ! ( "CacheStore cannot be used on the worker node! queue_all was used." )
836852 }
837853
838- async fn queue_add ( & self , _item : QueueItem ) -> Result < bool , CubeError > {
854+ async fn queue_add ( & self , _item : QueueItem ) -> Result < QueueAddResponse , CubeError > {
839855 panic ! ( "CacheStore cannot be used on the worker node! queue_add was used." )
840856 }
841857
0 commit comments