Skip to content

Commit 75d04cf

Browse files
committed
libsql: Add Database::sync_until
Signed-off-by: Piotr Jastrzebski <piotr@chiselstrike.com>
1 parent 710ae01 commit 75d04cf

3 files changed

Lines changed: 35 additions & 2 deletions

File tree

libsql/src/database.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,16 @@ cfg_replication! {
340340
}
341341
}
342342

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+
343353
/// Apply a set of frames to the database and returns the committed frame_no after syncing, if
344354
/// applicable.
345355
pub async fn sync_frames(&self, frames: crate::replication::Frames) -> Result<Option<FrameNo>> {

libsql/src/local/database.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,29 @@ impl Database {
277277
Ok(self.sync_oneshot().await?)
278278
}
279279

280+
#[cfg(feature = "replication")]
281+
/// Sync with primary at least to a given replication index
282+
pub async fn sync_until(&self, replication_index: FrameNo) -> Result<crate::replication::Replicated> {
283+
if let Some(ctx) = &self.replication_ctx {
284+
let mut frame_no: Option<FrameNo> = ctx.replicator.committed_frame_no().await;
285+
let mut frames_synced: usize = 0;
286+
while frame_no.unwrap_or(0) < replication_index {
287+
let res = ctx.replicator.sync_oneshot().await?;
288+
frame_no = res.frame_no();
289+
frames_synced += res.frames_synced();
290+
}
291+
Ok(crate::replication::Replicated {
292+
frame_no,
293+
frames_synced,
294+
})
295+
} else {
296+
Err(crate::errors::Error::Misuse(
297+
"No replicator available. Use Database::with_replicator() to enable replication"
298+
.to_string(),
299+
))
300+
}
301+
}
302+
280303
#[cfg(feature = "replication")]
281304
pub async fn sync_frames(&self, frames: Frames) -> Result<Option<FrameNo>> {
282305
if let Some(ref ctx) = self.replication_ctx {

libsql/src/replication/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ pub(crate) mod remote_client;
3636

3737
#[derive(Debug)]
3838
pub struct Replicated {
39-
frame_no: Option<FrameNo>,
40-
frames_synced: usize,
39+
pub(crate) frame_no: Option<FrameNo>,
40+
pub(crate) frames_synced: usize,
4141
}
4242

4343
impl Replicated {

0 commit comments

Comments
 (0)