File tree Expand file tree Collapse file tree 7 files changed +77
-18
lines changed
Expand file tree Collapse file tree 7 files changed +77
-18
lines changed Original file line number Diff line number Diff line change 22
33Spacedrive's sync system. Consumes types and helpers from ` sd-sync ` .
44
5- ## Using Sync
6-
75### Creating Records
86
97Prepare a sync id by creating or obtaining its value,
@@ -101,3 +99,34 @@ sync.write_ops(
10199 )
102100)
103101```
102+
103+ ### Setting Relation Fields
104+
105+ Setting relation fields requires providing the Sync ID of the relation.
106+ Setting the relation field's scalar fields instead will not properly sync then relation,
107+ usually because the scalar fields are local and disconnected from the Sync ID.
108+
109+ ``` rs
110+ let (sync_params , db_params ): (Vec <_ >, Vec <_ >) = [
111+ sync_db_entry! (
112+ prisma_sync :: object :: SyncId { pub_id : object_pub_id },
113+ file_path :: object
114+ )
115+ ]. into_iter (). unzip ();
116+
117+ sync . write_ops (
118+ db ,
119+ (
120+ sync . shared_update (
121+ prisma_sync :: file_path :: SyncId {
122+ pub_id : file_path_pub_id
123+ },
124+ sync_params
125+ ),
126+ db . file_path (). update (
127+ file_path :: id :: equals (file_path_id ),
128+ db_params
129+ )
130+ )
131+ )
132+ ```
Original file line number Diff line number Diff line change @@ -41,14 +41,17 @@ impl cloud_crdt_include::Data {
4141 Uuid :: from_slice ( & self . instance . pub_id ) . unwrap ( )
4242 }
4343
44- pub fn into_operation ( self ) -> CRDTOperation {
45- CRDTOperation {
46- instance : self . instance ( ) ,
47- timestamp : self . timestamp ( ) ,
48- record_id : rmp_serde:: from_slice ( & self . record_id ) . unwrap ( ) ,
49- model : self . model as u16 ,
50- data : serde_json:: from_slice ( & self . data ) . unwrap ( ) ,
51- }
44+ pub fn into_operation ( self ) -> ( i32 , CRDTOperation ) {
45+ (
46+ self . id ,
47+ CRDTOperation {
48+ instance : self . instance ( ) ,
49+ timestamp : self . timestamp ( ) ,
50+ record_id : rmp_serde:: from_slice ( & self . record_id ) . unwrap ( ) ,
51+ model : self . model as u16 ,
52+ data : serde_json:: from_slice ( & self . data ) . unwrap ( ) ,
53+ } ,
54+ )
5255 }
5356}
5457
Original file line number Diff line number Diff line change @@ -31,7 +31,7 @@ pub enum Request {
3131 timestamps : Vec < ( Uuid , NTP64 ) > ,
3232 tx : oneshot:: Sender < ( ) > ,
3333 } ,
34- Ingested ,
34+ // Ingested,
3535 FinishedIngesting ,
3636}
3737
@@ -129,6 +129,10 @@ impl Actor {
129129 }
130130 }
131131
132+ if let Some ( tx) = event. wait_tx {
133+ tx. send ( ( ) ) . ok ( ) ;
134+ }
135+
132136 match event. has_more {
133137 true => State :: RetrievingMessages ,
134138 false => {
@@ -421,7 +425,7 @@ impl Actor {
421425
422426 self . timestamps . write ( ) . await . insert ( instance, new_ts) ;
423427
424- self . io . req_tx . send ( Request :: Ingested ) . await . ok ( ) ;
428+ // self.io.req_tx.send(Request::Ingested).await.ok();
425429
426430 Ok ( ( ) )
427431 }
@@ -445,6 +449,7 @@ pub struct MessagesEvent {
445449 pub instance_id : Uuid ,
446450 pub messages : CompressedCRDTOperations ,
447451 pub has_more : bool ,
452+ pub wait_tx : Option < oneshot:: Sender < ( ) > > ,
448453}
449454
450455impl ActorTypes for Actor {
Original file line number Diff line number Diff line change @@ -246,7 +246,7 @@ impl Manager {
246246 pub async fn get_cloud_ops (
247247 & self ,
248248 args : GetOpsArgs ,
249- ) -> prisma_client_rust:: Result < Vec < CRDTOperation > > {
249+ ) -> prisma_client_rust:: Result < Vec < ( i32 , CRDTOperation ) > > {
250250 let db = & self . db ;
251251
252252 macro_rules! db_args {
Original file line number Diff line number Diff line change @@ -128,13 +128,14 @@ impl Instance {
128128 messages : CompressedCRDTOperations :: new ( messages) ,
129129 has_more : false ,
130130 instance_id : instance1. id ,
131+ wait_tx : None ,
131132 } ) )
132133 . await
133134 . unwrap ( ) ;
134135 }
135- ingest:: Request :: Ingested => {
136- instance2. sync . tx . send ( SyncMessage :: Ingested ) . ok ( ) ;
137- }
136+ // ingest::Request::Ingested => {
137+ // instance2.sync.tx.send(SyncMessage::Ingested).ok();
138+ // }
138139 ingest:: Request :: FinishedIngesting => { }
139140 }
140141 }
Original file line number Diff line number Diff line change 1+ use sd_prisma:: prisma:: cloud_crdt_operation;
12use sd_sync:: CompressedCRDTOperations ;
23use std:: sync:: {
34 atomic:: { AtomicBool , Ordering } ,
@@ -44,13 +45,15 @@ pub async fn run_actor(
4445 _ => continue ,
4546 } ;
4647
47- let ops = err_break ! (
48+ let ( ops_ids , ops) : ( Vec < _ > , Vec < _ > ) = err_break ! (
4849 sync. get_cloud_ops( GetOpsArgs {
4950 clocks: timestamps,
5051 count: OPS_PER_REQUEST ,
5152 } )
5253 . await
53- ) ;
54+ )
55+ . into_iter ( )
56+ . unzip ( ) ;
5457
5558 if ops. is_empty ( ) {
5659 break ;
@@ -63,16 +66,29 @@ pub async fn run_actor(
6366 ops. last( ) . map( |operation| operation. timestamp. as_u64( ) ) ,
6467 ) ;
6568
69+ let ( wait_tx, wait_rx) = tokio:: sync:: oneshot:: channel :: < ( ) > ( ) ;
70+
6671 err_break ! (
6772 sync. ingest
6873 . event_tx
6974 . send( sd_core_sync:: Event :: Messages ( MessagesEvent {
7075 instance_id: sync. instance,
7176 has_more: ops. len( ) == OPS_PER_REQUEST as usize ,
7277 messages: CompressedCRDTOperations :: new( ops) ,
78+ wait_tx: Some ( wait_tx)
7379 } ) )
7480 . await
7581 ) ;
82+
83+ err_break ! ( wait_rx. await ) ;
84+
85+ err_break ! (
86+ sync. db
87+ . cloud_crdt_operation( )
88+ . delete_many( vec![ cloud_crdt_operation:: id:: in_vec( ops_ids) ] )
89+ . exec( )
90+ . await
91+ ) ;
7692 }
7793 }
7894 }
Original file line number Diff line number Diff line change @@ -244,15 +244,20 @@ mod responder {
244244
245245 let rx:: Operations ( ops) = rx:: Operations :: from_stream ( stream) . await . unwrap ( ) ;
246246
247+ let ( wait_tx, wait_rx) = tokio:: sync:: oneshot:: channel :: < ( ) > ( ) ;
248+
247249 ingest
248250 . event_tx
249251 . send ( Event :: Messages ( MessagesEvent {
250252 instance_id : library. sync . instance ,
251253 has_more : ops. len ( ) == OPS_PER_REQUEST as usize ,
252254 messages : ops,
255+ wait_tx : Some ( wait_tx) ,
253256 } ) )
254257 . await
255258 . expect ( "TODO: Handle ingest channel closed, so we don't loose ops" ) ;
259+
260+ wait_rx. await . unwrap ( )
256261 }
257262
258263 debug ! ( "Sync responder done" ) ;
You can’t perform that action at this time.
0 commit comments