1+ use crate :: services:: retry:: GetRowDataRetryAction ;
12use bytes:: Bytes ;
23use flowy_client_sync:: client_database:: { GridBlockRevisionChangeset , GridBlockRevisionPad } ;
34use flowy_client_sync:: make_operations_from_revisions;
@@ -8,11 +9,14 @@ use flowy_revision::{
89use flowy_sqlite:: ConnectionPool ;
910use grid_model:: { CellRevision , DatabaseBlockRevision , RowChangeset , RowRevision } ;
1011use lib_infra:: future:: FutureResult ;
12+ use lib_infra:: retry:: spawn_retry;
1113use lib_ot:: core:: EmptyAttributes ;
14+ use parking_lot:: RwLock ;
1215use revision_model:: Revision ;
1316use std:: borrow:: Cow ;
1417use std:: sync:: Arc ;
15- use tokio:: sync:: RwLock ;
18+ use std:: time:: Duration ;
19+ // use tokio::sync::RwLock;
1620
1721pub struct DatabaseBlockRevisionEditor {
1822 #[ allow( dead_code) ]
@@ -53,7 +57,7 @@ impl DatabaseBlockRevisionEditor {
5357 }
5458
5559 pub async fn duplicate_block ( & self , duplicated_block_id : & str ) -> DatabaseBlockRevision {
56- self . pad . read ( ) . await . duplicate_data ( duplicated_block_id) . await
60+ self . pad . read ( ) . duplicate_data ( duplicated_block_id)
5761 }
5862
5963 /// Create a row after the the with prev_row_id. If prev_row_id is None, the row will be appended to the list
@@ -108,28 +112,38 @@ impl DatabaseBlockRevisionEditor {
108112 }
109113
110114 pub async fn index_of_row ( & self , row_id : & str ) -> Option < usize > {
111- self . pad . read ( ) . await . index_of_row ( row_id)
115+ self . pad . read ( ) . index_of_row ( row_id)
112116 }
113117
114118 pub async fn number_of_rows ( & self ) -> i32 {
115- self . pad . read ( ) . await . rows . len ( ) as i32
119+ self . pad . read ( ) . rows . len ( ) as i32
116120 }
117121
118122 pub async fn get_row_rev ( & self , row_id : & str ) -> FlowyResult < Option < ( usize , Arc < RowRevision > ) > > {
119- if self . pad . try_read ( ) . is_err ( ) {
120- tracing :: error! ( "Required grid block read lock failed" ) ;
121- Ok ( None )
123+ let duration = Duration :: from_millis ( 300 ) ;
124+ if let Some ( pad ) = self . pad . try_read_for ( duration ) {
125+ Ok ( pad . get_row_rev ( row_id ) )
122126 } else {
123- let row_rev = self . pad . read ( ) . await . get_row_rev ( row_id) ;
124- Ok ( row_rev)
127+ tracing:: error!( "Required grid block read lock failed, retrying" ) ;
128+ let retry = GetRowDataRetryAction {
129+ row_id : row_id. to_owned ( ) ,
130+ pad : self . pad . clone ( ) ,
131+ } ;
132+ match spawn_retry ( 3 , 300 , retry) . await {
133+ Ok ( value) => Ok ( value) ,
134+ Err ( err) => {
135+ tracing:: error!( "Read row revision failed with: {}" , err) ;
136+ Ok ( None )
137+ }
138+ }
125139 }
126140 }
127141
128142 pub async fn get_row_revs < T > ( & self , row_ids : Option < Vec < Cow < ' _ , T > > > ) -> FlowyResult < Vec < Arc < RowRevision > > >
129143 where
130144 T : AsRef < str > + ToOwned + ?Sized ,
131145 {
132- let row_revs = self . pad . read ( ) . await . get_row_revs ( row_ids) ?;
146+ let row_revs = self . pad . read ( ) . get_row_revs ( row_ids) ?;
133147 Ok ( row_revs)
134148 }
135149
@@ -138,19 +152,19 @@ impl DatabaseBlockRevisionEditor {
138152 field_id : & str ,
139153 row_ids : Option < Vec < Cow < ' _ , String > > > ,
140154 ) -> FlowyResult < Vec < CellRevision > > {
141- let cell_revs = self . pad . read ( ) . await . get_cell_revs ( field_id, row_ids) ?;
155+ let cell_revs = self . pad . read ( ) . get_cell_revs ( field_id, row_ids) ?;
142156 Ok ( cell_revs)
143157 }
144158
145159 async fn modify < F > ( & self , f : F ) -> FlowyResult < ( ) >
146160 where
147161 F : for < ' a > FnOnce ( & ' a mut GridBlockRevisionPad ) -> FlowyResult < Option < GridBlockRevisionChangeset > > ,
148162 {
149- let mut write_guard = self . pad . write ( ) . await ;
150- match f ( & mut write_guard ) ? {
163+ let changeset = f ( & mut self . pad . write ( ) ) ? ;
164+ match changeset {
151165 None => { }
152- Some ( change ) => {
153- self . apply_change ( change ) . await ?;
166+ Some ( changeset ) => {
167+ self . apply_change ( changeset ) . await ?;
154168 }
155169 }
156170 Ok ( ( ) )
0 commit comments