@@ -8,9 +8,11 @@ pub use builder::Builder;
88pub use libsql_sys:: { Cipher , EncryptionConfig } ;
99
1010use crate :: { Connection , Result } ;
11- use std:: fmt;
12- use std:: sync:: atomic:: AtomicU64 ;
13- use std:: sync:: Arc ;
11+ use std:: {
12+ fmt,
13+ sync:: { atomic:: AtomicU64 , Arc } ,
14+ } ;
15+ use tokio:: sync:: Mutex ;
1416
1517cfg_core ! {
1618 bitflags:: bitflags! {
@@ -82,7 +84,14 @@ enum DbType {
8284 encryption_config : Option < EncryptionConfig > ,
8385 } ,
8486 #[ cfg( feature = "sync" ) ]
85- Offline { db : crate :: local:: Database } ,
87+ Offline {
88+ db : crate :: local:: Database ,
89+ remote_writes : bool ,
90+ read_your_writes : bool ,
91+ url : String ,
92+ auth_token : String ,
93+ connector : crate :: util:: ConnectorService ,
94+ } ,
8695 #[ cfg( feature = "remote" ) ]
8796 Remote {
8897 url : String ,
@@ -375,7 +384,7 @@ cfg_replication! {
375384 #[ cfg( feature = "replication" ) ]
376385 DbType :: Sync { db, encryption_config: _ } => db. sync( ) . await ,
377386 #[ cfg( feature = "sync" ) ]
378- DbType :: Offline { db } => db. sync_offline( ) . await ,
387+ DbType :: Offline { db, .. } => db. sync_offline( ) . await ,
379388 _ => Err ( Error :: SyncNotSupported ( format!( "{:?}" , self . db_type) ) ) ,
380389 }
381390 }
@@ -542,7 +551,7 @@ impl Database {
542551
543552 let conn = db. connect ( ) ?;
544553
545- let conn = std :: sync :: Arc :: new ( LibsqlConnection { conn } ) ;
554+ let conn = Arc :: new ( LibsqlConnection { conn } ) ;
546555
547556 Ok ( Connection { conn } )
548557 }
@@ -590,7 +599,7 @@ impl Database {
590599 }
591600 }
592601
593- let conn = std :: sync :: Arc :: new ( LibsqlConnection { conn } ) ;
602+ let conn = Arc :: new ( LibsqlConnection { conn } ) ;
594603
595604 Ok ( Connection { conn } )
596605 }
@@ -636,19 +645,47 @@ impl Database {
636645 writer,
637646 self . max_write_replication_index . clone ( ) ,
638647 ) ;
639- let conn = std :: sync :: Arc :: new ( remote) ;
648+ let conn = Arc :: new ( remote) ;
640649
641650 Ok ( Connection { conn } )
642651 }
643652
644653 #[ cfg( feature = "sync" ) ]
645- DbType :: Offline { db } => {
646- use crate :: local:: impls:: LibsqlConnection ;
647-
648- let conn = db. connect ( ) ?;
654+ DbType :: Offline {
655+ db,
656+ remote_writes,
657+ read_your_writes,
658+ url,
659+ auth_token,
660+ connector,
661+ } => {
662+ use crate :: {
663+ hrana:: { connection:: HttpConnection , hyper:: HttpSender } ,
664+ local:: impls:: LibsqlConnection ,
665+ replication:: connection:: State ,
666+ sync:: connection:: SyncedConnection ,
667+ } ;
649668
650- let conn = std:: sync:: Arc :: new ( LibsqlConnection { conn } ) ;
669+ let local = db. connect ( ) ?;
670+
671+ if * remote_writes {
672+ let synced = SyncedConnection {
673+ local,
674+ remote : HttpConnection :: new (
675+ url. clone ( ) ,
676+ auth_token. clone ( ) ,
677+ HttpSender :: new ( connector. clone ( ) , None ) ,
678+ ) ,
679+ read_your_writes : * read_your_writes,
680+ context : db. sync_ctx . clone ( ) . unwrap ( ) ,
681+ state : Arc :: new ( Mutex :: new ( State :: Init ) ) ,
682+ } ;
683+
684+ let conn = Arc :: new ( synced) ;
685+ return Ok ( Connection { conn } ) ;
686+ }
651687
688+ let conn = Arc :: new ( LibsqlConnection { conn : local } ) ;
652689 Ok ( Connection { conn } )
653690 }
654691
@@ -659,7 +696,7 @@ impl Database {
659696 connector,
660697 version,
661698 } => {
662- let conn = std :: sync :: Arc :: new (
699+ let conn = Arc :: new (
663700 crate :: hrana:: connection:: HttpConnection :: new_with_connector (
664701 url,
665702 auth_token,
0 commit comments