sqlite_vfs_http/
lib.rs

1mod buffer;
2mod conn;
3mod utils;
4mod vfs;
5
6use buffer::LazyBuffer;
7use conn::Connection;
8use reqwest::Client;
9use sqlite_vfs::{register, RegisterError};
10use std::sync::{Arc, Once, RwLock};
11use utils::AtomicRuntime;
12use vfs::HttpVfs;
13
14const SQLITE_PAGE_SIZE: usize = 1024 * 4;
15pub const HTTP_VFS: &str = "http";
16
17pub struct HttpVfsRegister {
18    /// how many pages in block, default is 8MB, 2048 pages
19    block_size: usize,
20    /// default client
21    client: Option<Client>,
22    /// read the first few pages of each block without downloading the entire block
23    download_threshold: usize,
24    /// sqlite's page size is 4KB by default
25    page_size: usize,
26}
27
28impl HttpVfsRegister {
29    pub fn new() -> Self {
30        Self {
31            client: None,
32            block_size: SQLITE_PAGE_SIZE * 1024 * 2,
33            download_threshold: 0,
34            page_size: SQLITE_PAGE_SIZE,
35        }
36    }
37
38    pub fn with_block_size(self, page_num: usize) -> Self {
39        Self {
40            block_size: page_num * self.page_size,
41            ..self
42        }
43    }
44
45    pub fn with_client(self, client: Client) -> Self {
46        Self {
47            client: Some(client),
48            ..self
49        }
50    }
51
52    /// Set how many page read don't download full block
53    pub fn with_download_threshold(self, page_num: usize) -> Self {
54        Self {
55            download_threshold: page_num * self.page_size,
56            ..self
57        }
58    }
59
60    pub fn with_page_size(self, page_size: usize) -> Self {
61        Self { page_size, ..self }
62    }
63
64    pub fn register(self) -> Result<(), RegisterError> {
65        let vfs_instance = HttpVfs {
66            client: self.client,
67            block_size: self.block_size,
68            download_threshold: self.download_threshold,
69        };
70        register(HTTP_VFS, vfs_instance, true)
71    }
72}
73
74/// register http vfs, use `Once` internally to ensure only register once
75#[inline(always)]
76pub fn register_http_vfs() {
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)]
87pub 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    })
93}