@@ -14,6 +14,7 @@ use tokio::task::spawn_blocking;
1414
1515pub const REVISION_WRITE_INTERVAL_IN_MILLIS : u64 = 600 ;
1616
17+ #[ derive( Clone ) ]
1718pub struct RevisionPersistenceConfiguration {
1819 merge_threshold : usize ,
1920}
@@ -24,14 +25,14 @@ impl RevisionPersistenceConfiguration {
2425 if merge_threshold > 1 {
2526 Self { merge_threshold }
2627 } else {
27- Self { merge_threshold : 2 }
28+ Self { merge_threshold : 100 }
2829 }
2930 }
3031}
3132
3233impl std:: default:: Default for RevisionPersistenceConfiguration {
3334 fn default ( ) -> Self {
34- Self { merge_threshold : 2 }
35+ Self { merge_threshold : 100 }
3536 }
3637}
3738
9394 pub ( crate ) async fn sync_revision ( & self , revision : & Revision ) -> FlowyResult < ( ) > {
9495 tracing:: Span :: current ( ) . record ( "rev_id" , & revision. rev_id ) ;
9596 self . add ( revision. clone ( ) , RevisionState :: Sync , false ) . await ?;
96- self . sync_seq . write ( ) . await . dry_push ( revision. rev_id ) ?;
97+ self . sync_seq . write ( ) . await . recv ( revision. rev_id ) ?;
9798 Ok ( ( ) )
9899 }
99100
@@ -105,13 +106,17 @@ where
105106 rev_compress : & Arc < dyn RevisionMergeable + ' a > ,
106107 ) -> FlowyResult < i64 > {
107108 let mut sync_seq = self . sync_seq . write ( ) . await ;
108- let step = sync_seq. step ;
109+ let compact_length = sync_seq. compact_length ;
109110
110- // Before the new_revision pushed into the sync_seq, we check if the current `step` of the
111- // sync_seq is less equal or greater than the merge threshold. If yes, it's need to merged
111+ // Before the new_revision is pushed into the sync_seq, we check if the current `step` of the
112+ // sync_seq is less equal to or greater than the merge threshold. If yes, it's needs to merged
112113 // with the new_revision into one revision.
113- if step >= self . configuration . merge_threshold - 1 {
114- let compact_seq = sync_seq. compact ( ) ;
114+ let mut compact_seq = VecDeque :: default ( ) ;
115+ // tracing::info!("{}", compact_seq)
116+ if compact_length >= self . configuration . merge_threshold - 1 {
117+ compact_seq. extend ( sync_seq. compact ( ) ) ;
118+ }
119+ if !compact_seq. is_empty ( ) {
115120 let range = RevisionRange {
116121 start : * compact_seq. front ( ) . unwrap ( ) ,
117122 end : * compact_seq. back ( ) . unwrap ( ) ,
@@ -127,15 +132,15 @@ where
127132 let merged_revision = rev_compress. merge_revisions ( & self . user_id , & self . object_id , revisions) ?;
128133 let rev_id = merged_revision. rev_id ;
129134 tracing:: Span :: current ( ) . record ( "rev_id" , & merged_revision. rev_id ) ;
130- let _ = sync_seq. dry_push ( merged_revision. rev_id ) ?;
135+ let _ = sync_seq. recv ( merged_revision. rev_id ) ?;
131136
132137 // replace the revisions in range with compact revision
133138 self . compact ( & range, merged_revision) . await ?;
134139 Ok ( rev_id)
135140 } else {
136141 tracing:: Span :: current ( ) . record ( "rev_id" , & new_revision. rev_id ) ;
137142 self . add ( new_revision. clone ( ) , RevisionState :: Sync , true ) . await ?;
138- sync_seq. push ( new_revision. rev_id ) ?;
143+ sync_seq. merge_recv ( new_revision. rev_id ) ?;
139144 Ok ( new_revision. rev_id )
140145 }
141146 }
@@ -163,6 +168,16 @@ where
163168 self . memory_cache . number_of_sync_records ( )
164169 }
165170
171+ pub ( crate ) fn number_of_records_in_disk ( & self ) -> usize {
172+ match self . disk_cache . read_revision_records ( & self . object_id , None ) {
173+ Ok ( records) => records. len ( ) ,
174+ Err ( e) => {
175+ tracing:: error!( "Read revision records failed: {:?}" , e) ;
176+ 0
177+ }
178+ }
179+ }
180+
166181 /// The cache gets reset while it conflicts with the remote revisions.
167182 #[ tracing:: instrument( level = "trace" , skip( self , revisions) , err) ]
168183 pub ( crate ) async fn reset ( & self , revisions : Vec < Revision > ) -> FlowyResult < ( ) > {
@@ -228,8 +243,8 @@ where
228243 }
229244 }
230245
231- pub fn batch_get ( & self , doc_id : & str ) -> FlowyResult < Vec < SyncRecord > > {
232- self . disk_cache . read_revision_records ( doc_id , None )
246+ pub fn load_all_records ( & self , object_id : & str ) -> FlowyResult < Vec < SyncRecord > > {
247+ self . disk_cache . read_revision_records ( object_id , None )
233248 }
234249
235250 // Read the revision which rev_id >= range.start && rev_id <= range.end
@@ -289,26 +304,31 @@ impl<C> RevisionMemoryCacheDelegate for Arc<dyn RevisionDiskCache<C, Error = Flo
289304#[ derive( Default ) ]
290305struct DeferSyncSequence {
291306 rev_ids : VecDeque < i64 > ,
292- start : Option < usize > ,
293- step : usize ,
307+ compact_index : Option < usize > ,
308+ compact_length : usize ,
294309}
295310
296311impl DeferSyncSequence {
297312 fn new ( ) -> Self {
298313 DeferSyncSequence :: default ( )
299314 }
300315
301- fn push ( & mut self , new_rev_id : i64 ) -> FlowyResult < ( ) > {
302- let _ = self . dry_push ( new_rev_id) ?;
316+ /// Pushes the new_rev_id to the end of the list and marks this new_rev_id is mergeable.
317+ ///
318+ /// When calling `compact` method, it will return a list of revision ids started from
319+ /// the `compact_start_pos`, and ends with the `compact_length`.
320+ fn merge_recv ( & mut self , new_rev_id : i64 ) -> FlowyResult < ( ) > {
321+ let _ = self . recv ( new_rev_id) ?;
303322
304- self . step += 1 ;
305- if self . start . is_none ( ) && !self . rev_ids . is_empty ( ) {
306- self . start = Some ( self . rev_ids . len ( ) - 1 ) ;
323+ self . compact_length += 1 ;
324+ if self . compact_index . is_none ( ) && !self . rev_ids . is_empty ( ) {
325+ self . compact_index = Some ( self . rev_ids . len ( ) - 1 ) ;
307326 }
308327 Ok ( ( ) )
309328 }
310329
311- fn dry_push ( & mut self , new_rev_id : i64 ) -> FlowyResult < ( ) > {
330+ /// Pushes the new_rev_id to the end of the list.
331+ fn recv ( & mut self , new_rev_id : i64 ) -> FlowyResult < ( ) > {
312332 // The last revision's rev_id must be greater than the new one.
313333 if let Some ( rev_id) = self . rev_ids . back ( ) {
314334 if * rev_id >= new_rev_id {
@@ -321,6 +341,7 @@ impl DeferSyncSequence {
321341 Ok ( ( ) )
322342 }
323343
344+ /// Removes the rev_id from the list
324345 fn ack ( & mut self , rev_id : & i64 ) -> FlowyResult < ( ) > {
325346 let cur_rev_id = self . rev_ids . front ( ) . cloned ( ) ;
326347 if let Some ( pop_rev_id) = cur_rev_id {
@@ -331,7 +352,20 @@ impl DeferSyncSequence {
331352 ) ;
332353 return Err ( FlowyError :: internal ( ) . context ( desc) ) ;
333354 }
334- let _ = self . rev_ids . pop_front ( ) ;
355+
356+ let mut compact_rev_id = None ;
357+ if let Some ( compact_index) = self . compact_index {
358+ compact_rev_id = self . rev_ids . get ( compact_index) . cloned ( ) ;
359+ }
360+
361+ let pop_rev_id = self . rev_ids . pop_front ( ) ;
362+ if let ( Some ( compact_rev_id) , Some ( pop_rev_id) ) = ( compact_rev_id, pop_rev_id) {
363+ if compact_rev_id <= pop_rev_id {
364+ if self . compact_length > 0 {
365+ self . compact_length -= 1 ;
366+ }
367+ }
368+ }
335369 }
336370 Ok ( ( ) )
337371 }
@@ -341,28 +375,22 @@ impl DeferSyncSequence {
341375 }
342376
343377 fn clear ( & mut self ) {
344- self . start = None ;
345- self . step = 0 ;
378+ self . compact_index = None ;
379+ self . compact_length = 0 ;
346380 self . rev_ids . clear ( ) ;
347381 }
348382
349383 // Compact the rev_ids into one except the current synchronizing rev_id.
350384 fn compact ( & mut self ) -> VecDeque < i64 > {
351- if self . start . is_none ( ) {
352- return VecDeque :: default ( ) ;
385+ let mut compact_seq = VecDeque :: with_capacity ( self . rev_ids . len ( ) ) ;
386+ if let Some ( start) = self . compact_index {
387+ if start < self . rev_ids . len ( ) {
388+ let seq = self . rev_ids . split_off ( start) ;
389+ compact_seq. extend ( seq) ;
390+ }
353391 }
354-
355- let start = self . start . unwrap ( ) ;
356- let compact_seq = self . rev_ids . split_off ( start) ;
357- self . start = None ;
358- self . step = 0 ;
392+ self . compact_index = None ;
393+ self . compact_length = 0 ;
359394 compact_seq
360-
361- // let mut new_seq = self.rev_ids.clone();
362- // let mut drained = new_seq.drain(1..).collect::<VecDeque<_>>();
363- //
364- // let start = drained.pop_front()?;
365- // let end = drained.pop_back().unwrap_or(start);
366- // Some((RevisionRange { start, end }, new_seq))
367395 }
368396}
0 commit comments