Skip to content

Commit 573ba92

Browse files
committed
refactor: prevent misuse of wal_insert_{begin,insert,end}
1 parent 5b8934e commit 573ba92

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

libsql/src/local/connection.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ impl Connection {
495495
Ok(buf)
496496
}
497497

498-
pub(crate) fn wal_insert_begin(&self) -> Result<()> {
498+
fn wal_insert_begin(&self) -> Result<()> {
499499
let rc = unsafe { libsql_sys::ffi::libsql_wal_insert_begin(self.handle()) };
500500
if rc != 0 {
501501
return Err(crate::errors::Error::SqliteFailure(
@@ -506,7 +506,7 @@ impl Connection {
506506
Ok(())
507507
}
508508

509-
pub(crate) fn wal_insert_end(&self) -> Result<()> {
509+
fn wal_insert_end(&self) -> Result<()> {
510510
let rc = unsafe { libsql_sys::ffi::libsql_wal_insert_end(self.handle()) };
511511
if rc != 0 {
512512
return Err(crate::errors::Error::SqliteFailure(
@@ -517,7 +517,7 @@ impl Connection {
517517
Ok(())
518518
}
519519

520-
pub(crate) fn wal_insert_frame(&self, frame: &[u8]) -> Result<()> {
520+
fn wal_insert_frame(&self, frame: &[u8]) -> Result<()> {
521521
let rc = unsafe {
522522
libsql_sys::ffi::libsql_wal_insert_frame(
523523
self.handle(),
@@ -534,6 +534,30 @@ impl Connection {
534534
}
535535
Ok(())
536536
}
537+
538+
pub(crate) fn wal_insert_handle(&self) -> Result<WalInsertHandle<'_>> {
539+
self.wal_insert_begin()?;
540+
Ok(WalInsertHandle { conn: self })
541+
}
542+
}
543+
544+
pub(crate) struct WalInsertHandle<'a> {
545+
conn: &'a Connection,
546+
}
547+
548+
impl WalInsertHandle<'_> {
549+
pub fn insert(&self, frame: &[u8]) -> Result<()> {
550+
self.conn.wal_insert_frame(frame)
551+
}
552+
}
553+
554+
impl Drop for WalInsertHandle<'_> {
555+
fn drop(&mut self) {
556+
if let Err(err) = self.conn.wal_insert_end() {
557+
tracing::error!("{:?}", err);
558+
Err(err).unwrap()
559+
}
560+
}
537561
}
538562

539563
impl fmt::Debug for Connection {

libsql/src/local/database.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -557,37 +557,29 @@ impl Database {
557557
) -> Result<crate::database::Replicated> {
558558
let generation = sync_ctx.generation();
559559
let mut frame_no = sync_ctx.durable_frame_num() + 1;
560-
conn.wal_insert_begin()?;
561560

562-
let mut err = None;
561+
let insert_handle = conn.wal_insert_handle()?;
563562

564563
loop {
565564
match sync_ctx.pull_one_frame(generation, frame_no).await {
566565
Ok(Some(frame)) => {
567-
conn.wal_insert_frame(&frame)?;
566+
insert_handle.insert(&frame)?;
568567
frame_no += 1;
569568
}
570569
Ok(None) => {
571-
break;
570+
sync_ctx.write_metadata().await?;
571+
return Ok(crate::database::Replicated {
572+
frame_no: None,
573+
frames_synced: 1,
574+
});
572575
}
573-
Err(e) => {
574-
tracing::debug!("pull_one_frame error: {:?}", e);
575-
err.replace(e);
576-
break;
576+
Err(err) => {
577+
tracing::debug!("pull_one_frame error: {:?}", err);
578+
sync_ctx.write_metadata().await?;
579+
return Err(err);
577580
}
578581
}
579582
}
580-
conn.wal_insert_end()?;
581-
sync_ctx.write_metadata().await?;
582-
583-
if let Some(err) = err {
584-
Err(err)
585-
} else {
586-
Ok(crate::database::Replicated {
587-
frame_no: None,
588-
frames_synced: 1,
589-
})
590-
}
591583
}
592584

593585
pub(crate) fn path(&self) -> &str {

0 commit comments

Comments
 (0)