-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1652 from tursodatabase/libsql-wal-replicator
libsql wal replicator
- Loading branch information
Showing
27 changed files
with
695 additions
and
400 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
pub type Result<T, E = Error> = std::result::Result<T, E>; | ||
pub type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>; | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum Error { | ||
#[error("IO error: {0}")] | ||
Io(#[from] std::io::Error), | ||
#[error("SQLite error: {0}")] | ||
Sqlite(#[from] rusqlite::Error), | ||
#[error("A fatal error occured injecting frames")] | ||
FatalInjectError, | ||
#[error("A fatal error occured injecting frames: {0}")] | ||
FatalInjectError(BoxError), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use std::mem::size_of; | ||
|
||
use libsql_wal::io::StdIO; | ||
use libsql_wal::replication::injector::Injector; | ||
use libsql_wal::segment::Frame as WalFrame; | ||
use zerocopy::{AsBytes, FromZeroes}; | ||
|
||
use crate::frame::FrameNo; | ||
use crate::rpc::replication::Frame as RpcFrame; | ||
|
||
use super::error::{Error, Result}; | ||
|
||
pub struct LibsqlInjector { | ||
injector: Injector<StdIO>, | ||
} | ||
|
||
impl super::Injector for LibsqlInjector { | ||
async fn inject_frame(&mut self, frame: RpcFrame) -> Result<Option<FrameNo>> { | ||
// this is a bit annoying be we want to read the frame, and it has to be aligned, so we | ||
// must copy it... | ||
// FIXME: optimize this. | ||
let mut wal_frame = WalFrame::new_box_zeroed(); | ||
if frame.data.len() != size_of::<WalFrame>() { | ||
todo!("invalid frame"); | ||
} | ||
wal_frame.as_bytes_mut().copy_from_slice(&frame.data[..]); | ||
Ok(self | ||
.injector | ||
.insert_frame(wal_frame) | ||
.await | ||
.map_err(|e| Error::FatalInjectError(e.into()))?) | ||
} | ||
|
||
async fn rollback(&mut self) { | ||
self.injector.rollback(); | ||
} | ||
|
||
async fn flush(&mut self) -> Result<Option<FrameNo>> { | ||
self.injector | ||
.flush(None) | ||
.await | ||
.map_err(|e| Error::FatalInjectError(e.into()))?; | ||
Ok(None) | ||
} | ||
} |
Oops, something went wrong.