@@ -6,6 +6,7 @@ use crate::{base_rocks_secondary_index, rocks_table_new, CubeError};
66use chrono:: serde:: ts_seconds;
77use chrono:: { DateTime , Duration , Utc } ;
88use rocksdb:: WriteBatch ;
9+ use std:: cmp:: Ordering ;
910
1011use serde:: { Deserialize , Deserializer , Serialize } ;
1112
@@ -78,6 +79,22 @@ pub struct QueueItem {
7879
7980impl RocksEntity for QueueItem { }
8081
82+ impl Ord for QueueItem {
83+ fn cmp ( & self , other : & Self ) -> Ordering {
84+ if self . priority == other. priority {
85+ other. created . cmp ( & self . created )
86+ } else {
87+ self . priority . cmp ( & other. priority )
88+ }
89+ }
90+ }
91+
92+ impl PartialOrd for QueueItem {
93+ fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
94+ Some ( self . cmp ( other) )
95+ }
96+ }
97+
8198impl QueueItem {
8299 pub fn new ( path : String , value : String , status : QueueItemStatus , priority : i64 ) -> Self {
83100 let parts: Vec < & str > = path. rsplitn ( 2 , ":" ) . collect ( ) ;
@@ -328,3 +345,82 @@ impl RocksSecondaryIndex<QueueItem, QueueItemIndexKey> for QueueItemRocksIndex {
328345 * self as IndexId
329346 }
330347}
348+
349+ #[ cfg( test) ]
350+ mod tests {
351+ use super :: * ;
352+ use crate :: CubeError ;
353+ use itertools:: Itertools ;
354+
355+ #[ test]
356+ fn test_queue_item_sort ( ) -> Result < ( ) , CubeError > {
357+ let priority0_1 =
358+ QueueItem :: new ( "1" . to_string ( ) , "1" . to_string ( ) , QueueItemStatus :: Active , 0 ) ;
359+ let priority0_2 =
360+ QueueItem :: new ( "2" . to_string ( ) , "2" . to_string ( ) , QueueItemStatus :: Active , 0 ) ;
361+ let priority0_3 =
362+ QueueItem :: new ( "3" . to_string ( ) , "3" . to_string ( ) , QueueItemStatus :: Active , 0 ) ;
363+ let priority10_4 = QueueItem :: new (
364+ "4" . to_string ( ) ,
365+ "4" . to_string ( ) ,
366+ QueueItemStatus :: Active ,
367+ 10 ,
368+ ) ;
369+ let priority0_5 =
370+ QueueItem :: new ( "5" . to_string ( ) , "5" . to_string ( ) , QueueItemStatus :: Active , 0 ) ;
371+ let priority_n5_6 = QueueItem :: new (
372+ "6" . to_string ( ) ,
373+ "6" . to_string ( ) ,
374+ QueueItemStatus :: Active ,
375+ -5 ,
376+ ) ;
377+
378+ assert_eq ! (
379+ vec![
380+ priority0_1. clone( ) ,
381+ priority0_2. clone( ) ,
382+ priority0_3. clone( ) ,
383+ priority10_4. clone( ) ,
384+ priority_n5_6. clone( ) ,
385+ priority0_5. clone( )
386+ ]
387+ . into_iter( )
388+ . sorted_by( |a, b| b. cmp( & a) )
389+ . map( |item| item. get_key( ) . clone( ) )
390+ . collect:: <Vec <String >>( ) ,
391+ vec![
392+ "4" . to_string( ) ,
393+ "1" . to_string( ) ,
394+ "2" . to_string( ) ,
395+ "3" . to_string( ) ,
396+ "5" . to_string( ) ,
397+ "6" . to_string( )
398+ ]
399+ ) ;
400+
401+ assert_eq ! (
402+ vec![
403+ priority10_4,
404+ priority0_1,
405+ priority0_5,
406+ priority0_2,
407+ priority0_3,
408+ priority_n5_6
409+ ]
410+ . into_iter( )
411+ . sorted_by( |a, b| b. cmp( & a) )
412+ . map( |item| item. get_key( ) . clone( ) )
413+ . collect:: <Vec <String >>( ) ,
414+ vec![
415+ "4" . to_string( ) ,
416+ "1" . to_string( ) ,
417+ "2" . to_string( ) ,
418+ "3" . to_string( ) ,
419+ "5" . to_string( ) ,
420+ "6" . to_string( )
421+ ]
422+ ) ;
423+
424+ Ok ( ( ) )
425+ }
426+ }
0 commit comments