Skip to content

Commit 83a4bd1

Browse files
bors[bot]psarna
andauthored
Merge libsql#156
156: sqld: extract ffi bindings to a separate crate r=MarinPostma a=psarna That's done to avoid cyclic Cargo dependencies - bottomless depends on the FFI bindings, but sqld is going to depdend on bottomless soon. Co-authored-by: Piotr Sarna <[email protected]>
2 parents 43fc796 + b94eb95 commit 83a4bd1

File tree

13 files changed

+45
-21
lines changed

13 files changed

+45
-21
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ members = [
66
"libsql-client",
77
"sqlc",
88
"sqld",
9+
"sqld-libsql-bindings",
910
]
1011

1112
[patch.crates-io]

bottomless/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ aws-sdk-s3 = { version = "0.22.0" }
1616
bytes = "1"
1717
crc = "3.0.0"
1818
futures = { version = "0.3.25" }
19-
sqld = { path = "../sqld" }
19+
sqld-libsql-bindings = { version = "0", path = "../sqld-libsql-bindings" }
2020
tokio = { version = "1.22.0", features = ["full"] }
2121
tracing = "0.1.37"
2222
tracing-subscriber = "0.3.16"

bottomless/src/ffi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub use sqld::libsql::ffi::{
1+
pub use sqld_libsql_bindings::ffi::{
22
libsql_wal_methods, sqlite3, sqlite3_file, sqlite3_vfs, PageHdrIter, PgHdr, Wal, WalIndexHdr,
33
SQLITE_CANTOPEN, SQLITE_CHECKPOINT_TRUNCATE, SQLITE_IOERR_WRITE, SQLITE_OK,
44
};

sqld-libsql-bindings/Cargo.toml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "sqld-libsql-bindings"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
anyhow = "1.0.66"
10+
libsql-wasmtime-bindings = "0"
11+
mvfs = { git = "https://github.com/psarna/mvsqlite", branch = "mwal", optional = true }
12+
mwal = { git = "https://github.com/psarna/mvsqlite", branch = "mwal", optional = true }
13+
rusqlite = { git = "https://github.com/psarna/rusqlite", rev = "4df75df0b0f5ecea2714a8b21bf9f2a0fcfd5905", default-features = false, features = [
14+
"buildtime_bindgen",
15+
"bundled-libsql",
16+
"column_decltype"
17+
] }
18+
tracing = "0.1.37"
19+
20+
[features]
21+
mwal_backend = ["mvfs", "mwal"]
File renamed without changes.
File renamed without changes.

sqld/src/libsql/mod.rs renamed to sqld-libsql-bindings/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ pub use wblibsql::{
1212
use anyhow::ensure;
1313
use rusqlite::Connection;
1414

15-
use crate::libsql::{ffi::libsql_wal_methods_register, wal_hook::WalMethodsHook};
15+
use crate::{ffi::libsql_wal_methods_register, wal_hook::WalMethodsHook};
1616

1717
use self::{
1818
ffi::{libsql_wal_methods, libsql_wal_methods_find},
1919
wal_hook::WalHook,
2020
};
2121

22-
fn get_orig_wal_methods() -> anyhow::Result<*mut libsql_wal_methods> {
22+
pub fn get_orig_wal_methods() -> anyhow::Result<*mut libsql_wal_methods> {
2323
let orig: *mut libsql_wal_methods = unsafe { libsql_wal_methods_find(std::ptr::null()) };
2424
if orig.is_null() {
2525
anyhow::bail!("no underlying methods");
@@ -28,7 +28,7 @@ fn get_orig_wal_methods() -> anyhow::Result<*mut libsql_wal_methods> {
2828
Ok(orig)
2929
}
3030

31-
pub(crate) fn open_with_regular_wal(
31+
pub fn open_with_regular_wal(
3232
path: impl AsRef<std::path::Path>,
3333
flags: rusqlite::OpenFlags,
3434
wal_hook: impl WalHook + 'static,

sqld/src/libsql/mwal/mod.rs renamed to sqld-libsql-bindings/src/mwal/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
use std::sync::{Arc, Mutex};
44

5-
pub(crate) fn open_with_virtual_wal(
5+
pub use mwal::ffi;
6+
7+
pub fn open_with_virtual_wal(
68
path: impl AsRef<std::path::Path>,
79
flags: rusqlite::OpenFlags,
810
vwal_methods: Arc<Mutex<mwal::ffi::libsql_wal_methods>>,
File renamed without changes.

sqld/Cargo.toml

+2-5
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ hex = "0.4.3"
1919
hyper = { version = "0.14.23", features = ["http2"] }
2020
itertools = "0.10.5"
2121
jsonwebtoken = "8.2.0"
22-
libsql-wasmtime-bindings = "0"
23-
# Regular mvfs prevents users from enabling WAL mode
24-
mvfs = { git = "https://github.com/psarna/mvsqlite", branch = "mwal", optional = true }
25-
mwal = { git = "https://github.com/psarna/mvsqlite", branch = "mwal", optional = true }
2622
once_cell = "1.17.0"
2723
parking_lot = "0.12.1"
2824
pgwire = "0.7.0"
@@ -38,6 +34,7 @@ rusqlite = { git = "https://github.com/psarna/rusqlite", rev = "4df75df0b0f5ecea
3834
serde = { version = "1.0.149", features = ["derive"] }
3935
serde_json = "1.0.91"
4036
smallvec = "1.10.0"
37+
sqld-libsql-bindings = { version = "0", path = "../sqld-libsql-bindings" }
4138
sqlite3-parser = { git = "https://github.com/gwenn/lemon-rs.git", rev = "be6d0a96c4424fada2e068b51c25d1fdc9f983f4", default-features = false, features = [
4239
"YYNOERRORRECOVERY"
4340
] }
@@ -65,4 +62,4 @@ tonic-build = "0.8.4"
6562
vergen = "6"
6663

6764
[features]
68-
mwal_backend = ["mvfs", "mwal"]
65+
mwal_backend = ["sqld-libsql-bindings/mwal_backend"]

sqld/src/database/libsql.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ macro_rules! ok_or_exit {
133133
fn open_db(
134134
path: impl AsRef<Path> + Send + 'static,
135135
#[cfg(feature = "mwal_backend")] vwal_methods: Option<
136-
Arc<Mutex<mwal::ffi::libsql_wal_methods>>,
136+
Arc<Mutex<sqld_libsql_bindings::mwal::ffi::libsql_wal_methods>>,
137137
>,
138138
wal_hook: impl WalHook + Send + Clone + 'static,
139139
) -> anyhow::Result<rusqlite::Connection> {
@@ -198,7 +198,7 @@ impl LibSqlDb {
198198
pub fn new(
199199
path: impl AsRef<Path> + Send + 'static,
200200
#[cfg(feature = "mwal_backend")] vwal_methods: Option<
201-
Arc<Mutex<mwal::ffi::libsql_wal_methods>>,
201+
Arc<Mutex<sqld_libsql_bindings::mwal::ffi::libsql_wal_methods>>,
202202
>,
203203
wal_hook: impl WalHook + Send + Clone + 'static,
204204
) -> crate::Result<Self> {

sqld/src/database/write_proxy/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ pub struct WriteProxyDbFactory {
2828
write_proxy: ProxyClient<Channel>,
2929
db_path: PathBuf,
3030
#[cfg(feature = "mwal_backend")]
31-
vwal_methods: Option<Arc<std::sync::Mutex<mwal::ffi::libsql_wal_methods>>>,
31+
vwal_methods:
32+
Option<Arc<std::sync::Mutex<sqld_libsql_bindings::mwal::ffi::libsql_wal_methods>>>,
3233
/// abort handle: abort db update loop on drop
3334
_abort_handle: crossbeam::channel::Sender<()>,
3435
}
@@ -42,7 +43,7 @@ impl WriteProxyDbFactory {
4243
ca_cert_path: Option<PathBuf>,
4344
db_path: PathBuf,
4445
#[cfg(feature = "mwal_backend")] vwal_methods: Option<
45-
Arc<std::sync::Mutex<mwal::ffi::libsql_wal_methods>>,
46+
Arc<std::sync::Mutex<sqld_libsql_bindings::mwal::ffi::libsql_wal_methods>>,
4647
>,
4748
) -> anyhow::Result<Self> {
4849
let mut endpoint = Channel::from_shared(addr.to_string())?;
@@ -115,7 +116,7 @@ impl WriteProxyDatabase {
115116
write_proxy: ProxyClient<Channel>,
116117
path: PathBuf,
117118
#[cfg(feature = "mwal_backend")] vwal_methods: Option<
118-
Arc<std::sync::Mutex<mwal::ffi::libsql_wal_methods>>,
119+
Arc<std::sync::Mutex<sqld_libsql_bindings::mwal::ffi::libsql_wal_methods>>,
119120
>,
120121
) -> Result<Self> {
121122
let read_db = LibSqlDb::new(

sqld/src/lib.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ use crate::error::Error;
2222
use crate::postgres::service::PgConnectionFactory;
2323
use crate::server::Server;
2424

25+
pub use sqld_libsql_bindings as libsql;
26+
2527
mod database;
2628
mod error;
2729
mod http;
28-
pub mod libsql;
2930
mod postgres;
3031
mod query;
3132
mod query_analysis;
@@ -137,10 +138,11 @@ pub async fn run_server(config: Config) -> anyhow::Result<()> {
137138
}
138139

139140
#[cfg(feature = "mwal_backend")]
140-
let vwal_methods = config
141-
.mwal_addr
142-
.as_ref()
143-
.map(|_| Arc::new(Mutex::new(mwal::ffi::libsql_wal_methods::new())));
141+
let vwal_methods = config.mwal_addr.as_ref().map(|_| {
142+
Arc::new(Mutex::new(
143+
sqld_libsql_bindings::mwal::ffi::libsql_wal_methods::new(),
144+
))
145+
});
144146

145147
match config.writer_rpc_addr {
146148
Some(ref addr) => {

0 commit comments

Comments
 (0)