@@ -7,9 +7,10 @@ pub use builder::Builder;
77#[ cfg( feature = "core" ) ]
88pub use libsql_sys:: { Cipher , EncryptionConfig } ;
99
10- use std:: fmt;
11-
1210use crate :: { Connection , Result } ;
11+ use std:: fmt;
12+ use std:: sync:: atomic:: AtomicU64 ;
13+ use std:: sync:: Arc ;
1314
1415cfg_core ! {
1516 bitflags:: bitflags! {
@@ -76,6 +77,9 @@ impl fmt::Debug for DbType {
7677/// not do much work until the [`Database::connect`] fn is called.
7778pub struct Database {
7879 db_type : DbType ,
80+ /// The maximum replication index returned from a write performed using any connection created using this Database object.
81+ #[ allow( dead_code) ]
82+ max_write_replication_index : Arc < AtomicU64 > ,
7983}
8084
8185cfg_core ! {
@@ -87,6 +91,7 @@ cfg_core! {
8791
8892 Ok ( Database {
8993 db_type: DbType :: Memory { db } ,
94+ max_write_replication_index: Default :: default ( ) ,
9095 } )
9196 }
9297
@@ -105,6 +110,7 @@ cfg_core! {
105110 flags,
106111 encryption_config: None ,
107112 } ,
113+ max_write_replication_index: Default :: default ( ) ,
108114 } )
109115 }
110116 }
@@ -130,6 +136,7 @@ cfg_replication! {
130136
131137 Ok ( Database {
132138 db_type: DbType :: Sync { db, encryption_config } ,
139+ max_write_replication_index: Default :: default ( ) ,
133140 } )
134141 }
135142
@@ -191,6 +198,7 @@ cfg_replication! {
191198
192199 Ok ( Database {
193200 db_type: DbType :: Sync { db, encryption_config } ,
201+ max_write_replication_index: Default :: default ( ) ,
194202 } )
195203 }
196204
@@ -317,6 +325,7 @@ cfg_replication! {
317325
318326 Ok ( Database {
319327 db_type: DbType :: Sync { db, encryption_config } ,
328+ max_write_replication_index: Default :: default ( ) ,
320329 } )
321330 }
322331
@@ -331,6 +340,16 @@ cfg_replication! {
331340 }
332341 }
333342
343+ /// Sync database from remote until it gets to a given replication_index or further,
344+ /// and returns the committed frame_no after syncing, if applicable.
345+ pub async fn sync_until( & self , replication_index: FrameNo ) -> Result <crate :: replication:: Replicated > {
346+ if let DbType :: Sync { db, encryption_config: _ } = & self . db_type {
347+ db. sync_until( replication_index) . await
348+ } else {
349+ Err ( Error :: SyncNotSupported ( format!( "{:?}" , self . db_type) ) )
350+ }
351+ }
352+
334353 /// Apply a set of frames to the database and returns the committed frame_no after syncing, if
335354 /// applicable.
336355 pub async fn sync_frames( & self , frames: crate :: replication:: Frames ) -> Result <Option <FrameNo >> {
@@ -372,12 +391,25 @@ cfg_replication! {
372391 DbType :: Sync { db, .. } => {
373392 let path = db. path( ) . to_string( ) ;
374393 Ok ( Database {
375- db_type: DbType :: File { path, flags: OpenFlags :: default ( ) , encryption_config: None }
394+ db_type: DbType :: File { path, flags: OpenFlags :: default ( ) , encryption_config: None } ,
395+ max_write_replication_index: Default :: default ( ) ,
376396 } )
377397 }
378398 t => Err ( Error :: FreezeNotSupported ( format!( "{:?}" , t) ) )
379399 }
380400 }
401+
402+ /// Get the maximum replication index returned from a write performed using any connection created using this Database object.
403+ pub fn max_write_replication_index( & self ) -> Option <FrameNo > {
404+ let index = self
405+ . max_write_replication_index
406+ . load( std:: sync:: atomic:: Ordering :: SeqCst ) ;
407+ if index == 0 {
408+ None
409+ } else {
410+ Some ( index)
411+ }
412+ }
381413 }
382414}
383415
@@ -445,6 +477,7 @@ cfg_remote! {
445477 connector: crate :: util:: ConnectorService :: new( svc) ,
446478 version,
447479 } ,
480+ max_write_replication_index: Default :: default ( ) ,
448481 } )
449482 }
450483 }
@@ -552,7 +585,11 @@ impl Database {
552585
553586 let local = LibsqlConnection { conn } ;
554587 let writer = local. conn . new_connection_writer ( ) ;
555- let remote = crate :: replication:: RemoteConnection :: new ( local, writer) ;
588+ let remote = crate :: replication:: RemoteConnection :: new (
589+ local,
590+ writer,
591+ self . max_write_replication_index . clone ( ) ,
592+ ) ;
556593 let conn = std:: sync:: Arc :: new ( remote) ;
557594
558595 Ok ( Connection { conn } )
0 commit comments