Skip to content

Commit 9ea6cb5

Browse files
committed
feat: allow custom vfs during initialization
1 parent 8f32978 commit 9ea6cb5

File tree

5 files changed

+47
-17
lines changed

5 files changed

+47
-17
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sqlite-vfs-http"
3-
version = "0.1.3"
3+
version = "0.1.4"
44
edition = "2021"
55
authors = ["DarkSky <darksky2048@gmail.com>"]
66
license = "AGPL-3.0-only"

src/conn.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ impl Connection {
3939
None
4040
}
4141

42-
fn init_with_url(url: &str) -> Result<(AtomicRuntime, usize), Error> {
43-
let rt = AtomicRuntime::default();
42+
fn init_with_url(url: &str, client: Option<Client>) -> Result<(AtomicRuntime, usize), Error> {
43+
let rt = AtomicRuntime::new(client);
4444
match Self::get_length(rt.clone(), url.to_string()) {
4545
Some(size) if size != 0 => Ok((rt, size)),
4646
_ => {
@@ -53,8 +53,13 @@ impl Connection {
5353
}
5454
}
5555

56-
pub fn new(url: &str, block_size: usize, download_threshold: usize) -> Result<Self, Error> {
57-
let (rt, length) = Self::init_with_url(url)?;
56+
pub fn new(
57+
url: &str,
58+
client: Option<Client>,
59+
block_size: usize,
60+
download_threshold: usize,
61+
) -> Result<Self, Error> {
62+
let (rt, length) = Self::init_with_url(url, client)?;
5863
let buffer = LazyBuffer::new(
5964
length,
6065
block_size,

src/lib.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ mod vfs;
55

66
use buffer::LazyBuffer;
77
use conn::Connection;
8-
use sqlite_vfs::register;
8+
use reqwest::Client;
9+
use sqlite_vfs::{register, RegisterError};
910
use std::sync::{Arc, Once, RwLock};
1011
use utils::AtomicRuntime;
1112
use vfs::HttpVfs;
@@ -16,6 +17,8 @@ pub const HTTP_VFS: &str = "http";
1617
pub struct HttpVfsRegister {
1718
/// how many pages in block, default is 8MB, 2048 pages
1819
block_size: usize,
20+
/// default client
21+
client: Option<Client>,
1922
/// read the first few pages of each block without downloading the entire block
2023
download_threshold: usize,
2124
/// sqlite's page size is 4KB by default
@@ -25,6 +28,7 @@ pub struct HttpVfsRegister {
2528
impl HttpVfsRegister {
2629
pub fn new() -> Self {
2730
Self {
31+
client: None,
2832
block_size: SQLITE_PAGE_SIZE * 1024 * 2,
2933
download_threshold: 0,
3034
page_size: SQLITE_PAGE_SIZE,
@@ -38,6 +42,13 @@ impl HttpVfsRegister {
3842
}
3943
}
4044

45+
pub fn with_client(self, client: Client) -> Self {
46+
Self {
47+
client: Some(client),
48+
..self
49+
}
50+
}
51+
4152
/// Set how many page read don't download full block
4253
pub fn with_download_threshold(self, page_num: usize) -> Self {
4354
Self {
@@ -50,21 +61,33 @@ impl HttpVfsRegister {
5061
Self { page_size, ..self }
5162
}
5263

53-
pub fn register(self) {
54-
const ONCE: Once = Once::new();
55-
64+
pub fn register(self) -> Result<(), RegisterError> {
5665
let vfs_instance = HttpVfs {
66+
client: self.client,
5767
block_size: self.block_size,
5868
download_threshold: self.download_threshold,
5969
};
60-
61-
ONCE.call_once(|| {
62-
let _ = register(HTTP_VFS, vfs_instance, true);
63-
})
70+
register(HTTP_VFS, vfs_instance, true)
6471
}
6572
}
6673

74+
/// register http vfs, use `Once` internally to ensure only register once
6775
#[inline(always)]
6876
pub fn register_http_vfs() {
69-
HttpVfsRegister::new().register();
77+
const ONCE: Once = Once::new();
78+
79+
ONCE.call_once(|| {
80+
let _ = HttpVfsRegister::new().register();
81+
})
82+
}
83+
84+
/// register http vfs with custom client
85+
/// use `Once` internally to ensure only register once
86+
#[inline(always)]
87+
pub fn register_http_vfs_with_custom(cb: impl FnOnce(HttpVfsRegister) -> HttpVfsRegister) {
88+
const ONCE: Once = Once::new();
89+
90+
ONCE.call_once(|| {
91+
let _ = cb(HttpVfsRegister::new()).register();
92+
})
7093
}

src/utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ pub struct AtomicRuntime {
99
rt: Arc<RwLock<Option<Runtime>>>,
1010
}
1111

12-
impl Default for AtomicRuntime {
13-
fn default() -> Self {
12+
impl AtomicRuntime {
13+
pub fn new(client: Option<Client>) -> Self {
1414
Self {
15-
client: Client::new(),
15+
client: client.unwrap_or_else(|| Client::new()),
1616
rt: Arc::new(RwLock::new(Runtime::new().ok())),
1717
}
1818
}

src/vfs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::{
77
};
88

99
pub struct HttpVfs {
10+
pub(crate) client: Option<Client>,
1011
pub(crate) block_size: usize,
1112
pub(crate) download_threshold: usize,
1213
}
@@ -24,6 +25,7 @@ impl Vfs for HttpVfs {
2425

2526
Ok(Connection::new(
2627
db,
28+
self.client.clone(),
2729
self.block_size,
2830
self.download_threshold,
2931
)?)

0 commit comments

Comments
 (0)