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