-
Notifications
You must be signed in to change notification settings - Fork 352
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 #209 from psarna/grpc_replication
replication: add gRPC-based replication
- Loading branch information
Showing
6 changed files
with
222 additions
and
65 deletions.
There are no files selected for viewing
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,57 +1,44 @@ | ||
use libsql::Database; | ||
use libsql_replication::{Frame, FrameHeader, Frames}; | ||
|
||
fn frame_data_offset(frame_no: u64) -> u64 { | ||
tracing::debug!( | ||
"WAL offset: {frame_no}->{}", | ||
32 + (frame_no - 1) * (24 + 4096) + 24 | ||
); | ||
32 + (frame_no - 1) * (24 + 4096) + 24 | ||
} | ||
|
||
fn test_frame(frame_no: u64) -> Frame { | ||
let header = FrameHeader { | ||
frame_no, | ||
checksum: 0xdeadc0de, | ||
page_no: frame_no as u32, | ||
size_after: frame_no as u32, | ||
}; | ||
|
||
let loaded = { | ||
use std::io::{Read, Seek}; | ||
let mut f = std::fs::File::open("tests/template.db-wal").unwrap(); | ||
f.seek(std::io::SeekFrom::Start(frame_data_offset(frame_no))) | ||
.unwrap(); | ||
let mut buf = vec![0; 4096]; | ||
f.read_exact(&mut buf).unwrap(); | ||
buf | ||
}; | ||
|
||
Frame::from_parts(&header, &loaded) | ||
} | ||
|
||
fn main() { | ||
#[tokio::main] | ||
async fn main() { | ||
tracing_subscriber::fmt::init(); | ||
|
||
std::fs::create_dir("data.libsql").ok(); | ||
std::fs::copy("tests/template.db", "data.libsql/data").unwrap(); | ||
|
||
let mut db = Database::with_replicator("data.libsql/data"); | ||
let db = Database::with_replicator("http://localhost:5001", "test.db") | ||
.await | ||
.unwrap(); | ||
let conn = db.connect().unwrap(); | ||
|
||
let sync_result = db.sync(Frames::Vec(vec![test_frame(1), test_frame(2)])); | ||
println!("sync result: {:?}", sync_result); | ||
let rows = conn | ||
.execute("SELECT * FROM sqlite_master", ()) | ||
.unwrap() | ||
.unwrap(); | ||
while let Ok(Some(row)) = rows.next() { | ||
println!( | ||
"| {:024} | {:024} | {:024} | {:024} |", | ||
row.get::<&str>(0).unwrap(), | ||
row.get::<&str>(1).unwrap(), | ||
row.get::<&str>(2).unwrap(), | ||
row.get::<&str>(3).unwrap(), | ||
); | ||
let db = std::sync::Arc::new(parking_lot::Mutex::new(db)); | ||
loop { | ||
if let Err(e) = tokio::task::spawn_blocking({ | ||
let db = db.clone(); | ||
move || db.lock().sync() | ||
}) | ||
.await | ||
{ | ||
println!("Error: {e}"); | ||
break; | ||
}; | ||
let response = conn.execute("SELECT * FROM sqlite_master", ()).unwrap(); | ||
let rows = match response { | ||
Some(rows) => rows, | ||
None => { | ||
println!("No rows"); | ||
continue; | ||
} | ||
}; | ||
while let Ok(Some(row)) = rows.next() { | ||
println!( | ||
"| {:024} | {:024} | {:024} | {:024} |", | ||
row.get::<&str>(0).unwrap(), | ||
row.get::<&str>(1).unwrap(), | ||
row.get::<&str>(2).unwrap(), | ||
row.get::<&str>(3).unwrap(), | ||
); | ||
} | ||
} | ||
} |
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